MyraMath
dCholeskySolver


Source: tests/dense/RCholeskySolver.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.
13 #include <myramath/dense/Matrix.h>
16 
17 // Algorithms.
18 #include <myramath/dense/gemm.h>
19 #include <myramath/dense/syrk.h>
20 #include <myramath/dense/tril.h>
22 
23 // Solver class under test.
25 
26 // Serialization testing.
28 
29 // Reporting.
30 #include <tests/myratest.h>
31 
32 using namespace myra;
33 
34 namespace {
35 
36 template<class Precision> void test(int I, int J, Precision tolerance)
37  {
38  myra::out() << typestring<Precision>() << std::endl;
39  // Construct random symmetric positive definite matrix, A = G'*G
40  auto G = Matrix<Precision>::random(I,I);
41  Matrix<Precision> A = gemm(G,'T',G) + 10*Matrix<Precision>::identity(I);
42  // Restrict A to lower triangle, generate cholesky solver for it.
44  typedef RCholeskySolver<Precision> Solver;
45  Solver solver(L);
46  // Solve A*X=B
47  {
48  auto B = Matrix<Precision>::random(I,J);
49  auto X = B;
50  solver.solve(X,'L','N');
51  Precision error = frobenius(A*X-B)/frobenius(B);
52  myra::out() << " |A*X-B|/|B| = " << error << std::endl;
53  REQUIRE(error < tolerance);
54  }
55  // Solve X*A=B
56  {
57  auto B = Matrix<Precision>::random(J,I);
58  auto X = B;
59  solver.solve(X,'R','N');
60  Precision error = frobenius(X*A-B)/frobenius(B);
61  myra::out() << " |X*A-B|/|B| = " << error << std::endl;
62  REQUIRE(error < tolerance);
63  }
64  // Roundtrip solver to disk, then solve A*X=B again.
65  {
66  VectorStream inout;
67  solver.write(inout);
68  Solver saved(inout);
69  auto B = Matrix<Precision>::random(I,J);
70  auto X = B;
71  saved.solve(X,'L','N');
72  Precision error = frobenius(A*X-B)/frobenius(B);
73  myra::out() << " |inv(A)*B-X| (saved) = " << error << std::endl;
74  REQUIRE(error < tolerance);
75  }
76  };
77 
78 } // namespace
79 
80 ADD_TEST("sCholeskySolver","[dense][solver]")
81  { test<float>(25,14,1.0e-2f); }
82 
83 ADD_TEST("dCholeskySolver","[dense][solver]")
84  { test<double>(25,14,1.0e-10); }
85 
Interface class for representing subranges of dense Matrix&#39;s.
Routines for computing Frobenius norms of various algebraic containers.
static Matrix< Number > random(int I, int J)
Generates a random Matrix of specified size.
Definition: Matrix.cpp:353
Wraps a std::vector<char>, presents it as both an InputStream and OutputStream. Useful for hygienic u...
Definition: VectorStream.h:22
Definition: syntax.dox:1
Specialized container for a lower triangular matrix, O(N^2/2) storage. Used by symmetry exploiting ma...
Returns the lower triangle of a dense Matrix.
Various utility functions/classes related to scalar Number types.
static Matrix< Number > identity(int IJ)
Generates an identity Matrix of specified size.
Definition: Matrix.cpp:349
A stream that serialize/deserializes to std::vector<char> buffer.
General purpose dense matrix container, O(i*j) storage.
A solver suitable for a real symmetric positive definite Matrix.
Factors A into L*L&#39;, presents solve methods.
Definition: RCholeskySolver.h:29
Routines for symmetric rank-k updates, a specialized form of Matrix*Matrix multiplication.
Variety of routines all for dense Matrix*Matrix multiplies. Delegates to the BLAS.


Results: [PASS]

double
|A*X-B|/|B| = 4.00191e-16
|X*A-B|/|B| = 3.44249e-16
|inv(A)*B-X| (saved) = 3.43516e-16


Go back to Summary of /test programs.