MyraMath
IdentityAction


Source: tests/iterative/IdentityAction.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>
15 
16 // Algorithms.
19 
20 // Iterative solve/action stuff.
23 
24 // Reporting.
25 #include <tests/myratest.h>
26 
27 using namespace myra;
28 
29 namespace {
30 
31 template<class Number> void test(int I, int J, typename ReflectPrecision<Number>::type tolerance)
32  {
33  myra::out() << typestring<Number>() << std::endl;
34  typedef typename ReflectPrecision<Number>::type Precision;
35  // Make an identity_Action of specified size.
36  Action<Number> action = make_IdentityAction<Number>(I);
37  // Check action.make_Matrix()
38  {
39  Precision error = frobenius(action.make_Matrix()-Matrix<Number>::identity(I));
40  myra::out() << " |I(action) - I(matrix)| = " << error << std::endl;
41  REQUIRE(error < tolerance);
42  }
43  // Check action.multiply(X,B)
44  {
45  auto X = Matrix<Number>::random(I,J);
46  auto B = Matrix<Number>::zeros(I,J);
47  action.multiply(X,B);
48  Precision error = frobenius(X-B);
49  myra::out() << " |I*X - X| = " << error << std::endl;
50  REQUIRE(error < tolerance);
51  }
52  // Check action.multiply(X,B,alpha,beta)
53  {
54  auto X = Matrix<Number>::random(I,J);
55  auto B = Matrix<Number>::random(I,J);
56  auto alpha = random<Number>();
57  auto beta = random<Number>();
58  auto C = alpha*X + beta*B;
59  action.multiply(X,B,alpha,beta);
60  Precision error = frobenius(B-C);
61  myra::out() << " | (alpha*I*X+beta*B) - (alpha*X+beta*B) | = " << error << std::endl;
62  REQUIRE(error < tolerance);
63  }
64  }
65 
66 } // namespace
67 
68 ADD_TEST("IdentityAction","[iterative]")
69  {
70  int I = 10;
71  int J = 5;
72  test<NumberS>(I,J,1.0e-4f);
73  test<NumberD>(I,J,1.0e-12);
74  test<NumberC>(I,J,1.0e-4f);
75  test<NumberZ>(I,J,1.0e-12);
76  }
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
Definition: syntax.dox:1
An Action that is just the identity operator.
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
General purpose dense matrix container, O(i*j) storage.
Applies the "Action" of a linear operator, b := A*x.
Definition: Action.h:29
Reflects Precision trait for a Number, scalar Number types should specialize it.
Definition: Number.h:33
Matrix< Number > make_Matrix() const
Tabulates *this into a dense Matrix.
Definition: Action.cpp:71
Simplistic random number functions.
void multiply(const CMatrixRange< Number > &X, const MatrixRange< Number > &B, Number alpha, Number beta) const
Assigns B = alpha*A*X + beta*B.
Definition: Action.cpp:55


Results: [PASS]

float
|I(action) - I(matrix)| = 0
|I*X - X| = 0
| (alpha*I*X+beta*B) - (alpha*X+beta*B) | = 0
double
|I(action) - I(matrix)| = 0
|I*X - X| = 0
| (alpha*I*X+beta*B) - (alpha*X+beta*B) | = 0
std::complex<float>
|I(action) - I(matrix)| = 0
|I*X - X| = 0
| (alpha*I*X+beta*B) - (alpha*X+beta*B) | = 0
std::complex<double>
|I(action) - I(matrix)| = 0
|I*X - X| = 0
| (alpha*I*X+beta*B) - (alpha*X+beta*B) | = 0


Go back to Summary of /test programs.