MyraMath
compose_action2


Source: tests/iterative/compose2.cpp

1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
11 // Containers for A,P and and inv(A)
13 #include <myramath/dense/Matrix.h>
15 
16 // Container for x/b
17 #include <myramath/dense/Vector.h>
19 
20 // For examining eigenvalues of inv(A)*(A+P*P')
21 #include <myramath/dense/geevd.h>
25 #include <myramath/dense/expr.h>
28 
29 // An underlying direct solver for A, wrapped by make_SolveAction.
31 
32 // Iterative solve/action stuff.
39 
40 // Reporting.
42 #include <tests/myratest.h>
43 
44 using namespace myra;
45 using namespace myra_stlprint;
46 
47 namespace {
48 
49 // Tests Action composition using gmres()
50 template<class Number> void test(int N, int K, typename ReflectPrecision<Number>::type tolerance)
51  {
52  typedef typename ReflectPrecision<Number>::type Precision;
53  myra::out() << typestring<Number>() << std::endl;
54  // Form random A and a direct solver (lu) for it.
55  auto A = Matrix<Number>::random(N,N);
56  LUSolver<Number> solver(A.add_const());
57  // Form forward action (A+PP', a rank-K perturbation of A).
58  auto P = Matrix<Number>::random(N,K);
59  auto forward = make_GemmAction(A) + make_GemmAction(P)*make_GemmAction(P,'T');
60  auto preconditioner = make_SolveAction(solver);
61  // Examine eigenvalues of preconditioner*forward, should be mostly 1's with K non-1's
62  /*
63  auto DRL = geevd( (preconditioner*forward).make_Matrix() );
64  myra::out() << "|lambda| = " << abs(expr1(DRL.D)) << std::endl;
65  */
66  // Solve forward*x = b, using gmres(preconditioner,forward)
67  auto b = Vector<Number>::random(N);
68  auto x = Vector<Number>::zeros(N);
69  auto result = gmres(preconditioner,forward,b,x,tolerance);
70  // Check residual is small.
71  Precision residual = euclidean(forward*x-b) / euclidean(b);
72  myra::out() << "residual = " << residual << std::endl;
73  // Since (preconditioner*action) is a rank-K perturbation of identity, gmres() should converge in O(K) iterations.
74  myra::out() << "history = " << result.history << std::endl;
75  // Perform assertions.
76  REQUIRE(residual < tolerance*100.0);
77  REQUIRE(result.history.size() <= K+5);
78  }
79 
80 } // namespace
81 
82 ADD_TEST("compose_action2","[iterative]")
83  {
84  // Test gmres() on a nonsymmetric composition.
85  test<NumberD>(100,5,1.0e-8);
86  test<NumberZ>(100,5,1.0e-8);
87  }
88 
Routines for computing euclidean norm of a Vector/VectorRange, or normalizing a Vector/VectorRange to...
Interface class for representing subranges of DiagonalMatrix&#39;s.
Interface class for representing subranges of dense Matrix&#39;s.
Interface class for representing subranges of dense Vector&#39;s.
Applies the "Action" of a linear operator, b := A*x, used in iterative solution algorithms.
static Matrix< Number > random(int I, int J)
Generates a random Matrix of specified size.
Definition: Matrix.cpp:353
Linear system solution via gmres (for invertible action A)
Computes all eigenpairs of general Matrix.
static Vector< Number > random(int N)
Generates a random Vector of specified size.
Definition: Vector.cpp:276
Definition: syntax.dox:1
Overloads expr() for Matrix<Number>, LowerMatrix<Number>, Vector<Number> and DiagonalMatrix<Number> ...
Definition: stlprint.h:32
Action< typename ReflectNumber< Solver >::type > make_SolveAction(const Solver &solver, char op='N')
Free function for making SolveAction&#39;s.
Definition: SolveAction.h:67
Routines for printing the contents of various std::container&#39;s to a std::ostream using operator <<...
Various utility functions/classes related to scalar Number types.
Composes two Action&#39;s A and B, yielding an Action that applies (A+B)*X.
static Vector< Number > zeros(int N)
Generates a zeros Vector of specified size.
Definition: Vector.cpp:280
Composes two Action&#39;s A and B, yielding an Action that cascades A*(B*X)
General purpose dense matrix container, O(i*j) storage.
Container for either a column vector or row vector (depends upon the usage context) ...
Reflects Precision trait for a Number, scalar Number types should specialize it.
Definition: Number.h:33
General purpose linear solver, no symmetry/definiteness assumptions upon A (just square) ...
Function overloads (sin, exp, etc) for Expression&#39;s.
Adapts a class with a .solve() method into an Action.
An Action for multiplying by a dense Matrix or SparseMatrix using gemm().
Factors a square matrix A into L*U, presents solve methods.
Definition: LUSolver.h:30
Container for a diagonal matrix, O(n) storage. Used by SVD, row/column scaling, etc.


Results: [PASS]

double
residual = 8.17273e-14
history = [ 47.2166 7.17701 2.92443 0.855566 0.196902 0.196818 7.43497e-14 ] (7)
std::complex<double>
residual = 1.96979e-13
history = [ 35.5167 1.26435 1.11192 1.02587 0.637327 0.545325 1.1242e-13 ] (7)


Go back to Summary of /test programs.