MyraMath
inverse_DiagonalMatrix


Source: tests/dense/inverse.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>
19 
20 // Algorithms.
21 #include <myramath/dense/gemm.h>
22 #include <myramath/dense/tril.h>
23 #include <myramath/dense/inverse.h>
25 
26 // Reporting.
27 #include <tests/myratest.h>
28 
29 using namespace myra;
30 
31 namespace {
32 
33 // Generates DiagonalMatrix A, computes inv(A), checks A*inv(A)=I
34 template<class Number> void test_diagonal(int N, typename ReflectPrecision<Number>::type tolerance)
35  {
36  myra::out() << typestring<Number>() << std::endl;
37  typedef typename ReflectPrecision<Number>::type Precision;
38  // Construct random matrix and compute its inverse.
40  DiagonalMatrix<Number> inv_A = inverse(A);
41  // Check A*inv_A = I
42  Precision error = frobenius(A*inv_A-DiagonalMatrix<Number>::identity(N));
43  myra::out() << " |A*inv(A)-I| = " << error << std::endl;
44  REQUIRE(error < tolerance);
45  }
46 
47 // Generates LowerMatrix A, computes inv(A), checks A*inv(A)=I
48 template<class Number> void test_lower(int N, typename ReflectPrecision<Number>::type tolerance)
49  {
50  myra::out() << typestring<Number>() << std::endl;
51  typedef typename ReflectPrecision<Number>::type Precision;
52  // Construct random matrix and compute its inverse.
53  auto A = LowerMatrix<Number>::random(N);
54  // Make A well-conditioned by putting 1's on diagonal.
55  for (int n = 0; n < N; ++n)
56  A(n,n) = Number(1);
57  LowerMatrix<Number> inv_A = inverse(A);
58  // Check A*inv_A = I. Note that there's no LowerMatrix*LowerMatrix kernel,
59  // so have to promote both to explicit square Matrix's to multiply.
60  Precision error = frobenius(A.make_Matrix()*inv_A.make_Matrix()-Matrix<Number>::identity(N));
61  myra::out() << " |A*inv(A)-I| = " << error << std::endl;
62  REQUIRE(error < tolerance);
63  }
64 
65 // Generates square Matrix A, computes inv(A), checks A*inv(A)=I
66 template<class Number> void test_square(int N, typename ReflectPrecision<Number>::type tolerance)
67  {
68  myra::out() << typestring<Number>() << std::endl;
69  typedef typename ReflectPrecision<Number>::type Precision;
70  // Construct random matrix and compute its inverse.
71  auto A = Matrix<Number>::random(N,N);
72  Matrix<Number> inv_A = inverse(A);
73  // Check A*inv_A = I
74  Precision error = frobenius(A*inv_A-Matrix<Number>::identity(N));
75  myra::out() << " |A*inv(A)-I| = " << error << std::endl;
76  REQUIRE(error < tolerance);
77  }
78 
79 } // namespace
80 
81 ADD_TEST("inverse_DiagonalMatrix","[dense][lapack]")
82  {
83  myra::out() << "Testing explicit inverse routines (DiagonalMatrix)" << std::endl;
84  test_diagonal<float >(15,1.0e-4f);
85  test_diagonal<double>(16,1.0e-9);
86  test_diagonal<std::complex<float > >(17,1.0e-4f);
87  test_diagonal<std::complex<double> >(18,1.0e-9);
88  myra::out() << std::endl;
89  }
90 
91 ADD_TEST("inverse_LowerMatrix","[dense][lapack]")
92  {
93  myra::out() << "Testing explicit inverse routines (LowerMatrix)" << std::endl;
94  test_lower<float >(15,1.0e-4f);
95  test_lower<double>(16,1.0e-9);
96  test_lower<std::complex<float > >(17,1.0e-4f);
97  test_lower<std::complex<double> >(18,1.0e-9);
98  myra::out() << std::endl;
99  }
100 
101 ADD_TEST("inverse_Matrix","[dense][lapack]")
102  {
103  myra::out() << "Testing explicit inverse routines (square Matrix)" << std::endl;
104  test_square<float >(15,1.0e-4f);
105  test_square<double>(16,1.0e-9);
106  test_square<std::complex<float > >(17,1.0e-4f);
107  test_square<std::complex<double> >(18,1.0e-9);
108  myra::out() << std::endl;
109  }
Interface class for representing subranges of DiagonalMatrix&#39;s.
Tabulates the values of a square NxN diagonal matrix. Allows random access, but only on the diagonal...
Definition: conjugate.h:23
Interface class for representing subranges of dense Matrix&#39;s.
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Matrix< Number > make_Matrix(char op='N') const
Accumulates *this onto the lower triangle of a Matrix<Number>
Definition: LowerMatrix.cpp:152
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
Range construct for a lower triangular matrix stored in rectangular packed format.
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
static DiagonalMatrix< Number > random(int N)
Generates a random DiagonalMatrix of specified size.
Definition: DiagonalMatrix.cpp:217
General purpose dense matrix container, O(i*j) storage.
Reflects Precision trait for a Number, scalar Number types should specialize it.
Definition: Number.h:33
Overwrites a LowerMatrix, DiagonalMatrix, or square Matrix with its own inverse. Or, returns it as a copy.
Stores a lower triangular matrix in rectangular packed format.
Definition: conjugate.h:22
Container for a diagonal matrix, O(n) storage. Used by SVD, row/column scaling, etc.
Variety of routines all for dense Matrix*Matrix multiplies. Delegates to the BLAS.
static LowerMatrix< Number > random(int N)
Generates a random LowerMatrix of specified size.
Definition: LowerMatrix.cpp:249


Results: [PASS]

Testing explicit inverse routines (DiagonalMatrix)
float
|A*inv(A)-I| = 8.42937e-08
double
|A*inv(A)-I| = 1.11022e-16
std::complex<float>
|A*inv(A)-I| = 1.42927e-07
std::complex<double>
|A*inv(A)-I| = 2.83137e-16


Go back to Summary of /test programs.