MyraMath
compose_action1


Source: tests/iterative/compose1.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 M
13 #include <myramath/dense/Matrix.h>
17 #include <myramath/dense/syrk.h>
18 #include <myramath/dense/trsm.h>
19 #include <myramath/dense/potrf.h>
20 
21 // Container for x/b
22 #include <myramath/dense/Vector.h>
24 
25 // For examining eigenvalues of L\(A+Q*Q')/L'
26 #include <myramath/dense/syev.h>
30 
31 // Iterative solve/action stuff.
32 #include <myramath/iterative/pcg.h>
42 
43 // Reporting.
45 #include <tests/myratest.h>
46 
47 using namespace myra;
48 using namespace myra_stlprint;
49 
50 namespace {
51 
52 // Tests Action composition using pcg(). A little awkward, because pcg() only handles symmetric Action's
53 template<class Precision> void test(int N, int K, Precision tolerance)
54  {
55  myra::out() << typestring<Precision>() << std::endl;
56  // Form symmetric positive definite LowerMatrix A.
57  auto G = Matrix<Precision>::random(N,N);
58  auto A = syrk(G);
59  // Compute cholesky triangle L of A.
60  auto L = A;
61  potrf_inplace(L);
62  // Form forward action (A+PP', a rank-K perturbation of A), then precondition it from both sides.
63  auto P = Matrix<Precision>::random(N,K);
64  auto action1 = make_SymmAction(A) + make_GemmAction(P)*make_GemmAction(P,'T');
65  auto action2 = make_TrsmAction(L) * action1 * make_TrsmAction(L,'T');
66  // Examine eigenvalues of action2, should be mostly 1's with K non-1's
67  auto XD = syev(action2.make_Matrix());
68  myra::out() << "lambda = " << XD.second << std::endl;
69  // Make random b and zero x.
70  auto b = Vector<Precision>::random(N);
71  auto x = Vector<Precision>::zeros(N);
72  // Solve action1*x=b, by using pcg(identity,action2) and a bit of trsm() pre/post processing.
73  auto b2 = b;
74  auto eye = make_IdentityAction<Precision>(N);
75  trsm_inplace('L','N',L,b2.column());
76  // auto result = pcg(eye,action2,b2,x,tolerance); // doesn't converge as expected .. too many iterations?
77  auto result = gmres(eye,action2,b2,x,tolerance); // converges as expected
78  trsm_inplace('L','T',L,x.column());
79  // Check residual is small.
80  Precision residual = euclidean(action1*x-b) / euclidean(b);
81  myra::out() << "residual = " << residual << std::endl;
82  REQUIRE(residual < tolerance*10.0);
83  // Since action2 is a rank-K perturbation of identity, pcg() should converge in O(K) iterations.
84  myra::out() << "history = " << result.history << std::endl;
85  REQUIRE(result.history.size() <= K+5);
86  }
87 
88 } // namespace
89 
90 ADD_TEST("compose_action1","[iterative]")
91  {
92  // Test pcg() on a symmetric composition.
93  // test<NumberS>(100,5,1.0e-5);
94  test<NumberD>(100,10,1.0e-10);
95  }
96 
Routines for computing euclidean norm of a Vector/VectorRange, or normalizing a Vector/VectorRange to...
Cholesky factorization routines for positive definite matrices.
Interface class for representing subranges of DiagonalMatrix&#39;s.
Computes all eigenpairs of real symmetric Matrix.
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
An Action for solving by a triangular Matrix, LowerMatrix or SparseMatrix using trsm().
Linear system solution via gmres (for invertible action A)
Range construct for a lower triangular matrix stored in rectangular packed format.
static Vector< Number > random(int N)
Generates a random Vector of specified size.
Definition: Vector.cpp:276
Definition: syntax.dox:1
An Action for multiplying by a symmetric dense Matrix. LowerMatrix or SparseMatrix using symm() ...
An Action that is just the identity operator.
Definition: stlprint.h:32
Routines for backsolving by a triangular Matrix or LowerMatrix.
Specialized container for a lower triangular matrix, O(N^2/2) storage. Used by symmetry exploiting ma...
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
Linear system solution via conjugate gradients (symmetric positive definite action A) ...
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) ...
CMatrixRange< Number > column() const
Returns an Nx1 column-shaped MatrixRange over all of *this.
Definition: Vector.cpp:152
Adapts a class with a .solve() method into an Action.
An Action for multiplying by a dense Matrix or SparseMatrix using gemm().
Container for a diagonal matrix, O(n) storage. Used by SVD, row/column scaling, etc.
Routines for symmetric rank-k updates, a specialized form of Matrix*Matrix multiplication.


Results: [PASS]

double
lambda = size 100 DiagonalMatrix of double: [ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4.10069 5.40528 6.64531 9.39695 13.5439 19.6899 28.7806 91.8219 208.946 2805.82 ]
residual = 6.51755e-14
history = [ 6.10651 0.774494 0.50434 0.418633 0.392986 0.325529 0.224299 0.12668 0.0437848 0.00566803 0.00120244 2.54744e-14 ] (12)


Go back to Summary of /test programs.