MyraMath
DimmAction


Source: tests/iterative/DimmAction.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>
17 
18 // Algorithms.
20 #include <myramath/dense/dimm.h>
24 
25 // Reporting.
26 #include <tests/myratest.h>
28 
29 using namespace myra;
30 using namespace myra_stlprint;
31 
32 namespace {
33 
34 // Test make_DimmAction()
35 template<class Number> void test(int IJ, int K, typename ReflectPrecision<Number>::type tolerance)
36  {
37  myra::out() << typestring<Number>() << std::endl;
38  typedef typename ReflectPrecision<Number>::type Precision;
39  // Make a random DiagonalMatrix A, wrap it up into a DimmAction
40  auto A = DiagonalMatrix<Number>::random(IJ);
41  auto action = make_DimmAction(A);
42  // Check action.make_Matrix()
43  {
44  Precision error = frobenius(action.make_Matrix()-A.make_Matrix());
45  myra::out() << " |A(action) - A(dense)| = " << error << std::endl;
46  REQUIRE(error < tolerance);
47  }
48  // Check action.multiply(X,B)
49  {
50  auto X = Matrix<Number>::random(IJ,K);
51  auto B = Matrix<Number>::zeros(IJ,K);
52  action.multiply(X,B);
53  Precision error = frobenius(A*X-B);
54  myra::out() << " |A*X - B| = " << error << std::endl;
55  REQUIRE(error < tolerance);
56  }
57  // Check action.multiply(X,B,alpha,beta)
58  {
59  auto X = Matrix<Number>::random(IJ,K);
60  auto B = Matrix<Number>::random(IJ,K);
61  auto alpha = random<Number>();
62  auto beta = random<Number>();
63  auto C = alpha*A*X + beta*B;
64  action.multiply(X,B,alpha,beta);
65  Precision error = frobenius(B-C);
66  myra::out() << " | (alpha*A*X+beta*B)[action] - (alpha*A*X+beta*B)[dense] | = " << error << std::endl;
67  REQUIRE(error < tolerance);
68  }
69  }
70 
71 } // namespace
72 
73 ADD_TEST("DimmAction","[iterative]")
74  {
75  // Test make_DimmAction()
76  int IJ = 10;
77  int K = 7;
78  test<NumberS>(IJ,K,1.0e-4f);
79  test<NumberD>(IJ,K,1.0e-12);
80  test<NumberC>(IJ,K,1.0e-4f);
81  test<NumberZ>(IJ,K,1.0e-12);
82  }
Interface class for representing subranges of DiagonalMatrix&#39;s.
Interface class for representing subranges of dense Matrix&#39;s.
Applies the "Action" of a linear operator, b := A*x, used in iterative solution algorithms.
static Matrix< Number > zeros(int I, int J)
Generates a zeros Matrix of specified size.
Definition: Matrix.cpp:357
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
Routines for multiplying by a DiagonalMatrix.
Definition: syntax.dox:1
Definition: stlprint.h:32
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.
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
Simplistic random number functions.
An Action for multiplying by a DiagonalMatrix using dimm()
Container for a diagonal matrix, O(n) storage. Used by SVD, row/column scaling, etc.


Results: [PASS]

float
|A(action) - A(dense)| = 0
|A*X - B| = 0
| (alpha*A*X+beta*B)[action] - (alpha*A*X+beta*B)[dense] | = 4.90162e-08
double
|A(action) - A(dense)| = 0
|A*X - B| = 0
| (alpha*A*X+beta*B)[action] - (alpha*A*X+beta*B)[dense] | = 3.64275e-16
std::complex<float>
|A(action) - A(dense)| = 0
|A*X - B| = 0
| (alpha*A*X+beta*B)[action] - (alpha*A*X+beta*B)[dense] | = 3.78002e-07
std::complex<double>
|A(action) - A(dense)| = 0
|A*X - B| = 0
| (alpha*A*X+beta*B)[action] - (alpha*A*X+beta*B)[dense] | = 8.31973e-16


Go back to Summary of /test programs.