MyraMath
cMatrix22


Source: tests/dense/Matrix22.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 #include <myramath/dense/gemm.h>
18 
19 // Reporting.
20 #include <tests/myratest.h>
21 
22 using namespace myra;
23 
24 namespace {
25 
26 // Form random A, then its inverse Z, and verify A*Z=I
27 template<class Number> void test1(typename ReflectPrecision<Number>::type tolerance)
28  {
29  typedef typename ReflectPrecision<Number>::type Precision;
30  auto A = Matrix22<Number>::random();
31  auto Z = inverse(A);
32  auto I = Matrix22<Number>::identity();
33  Precision error = frobenius(A*Z-I) / frobenius(A);
34  myra::out() << "|A*Z-I|/|A| = " << error << std::endl;
35  REQUIRE( error < tolerance );
36  }
37 
38 // Compare gemm_inplace(Matrix22, Matrix) to reference gemm()
39 template<class Number> void test2(typename ReflectPrecision<Number>::type tolerance)
40  {
41  typedef typename ReflectPrecision<Number>::type Precision;
42  auto A = Matrix22<Number>::random();
43  auto B0 = Matrix<Number>::random(2,25);
44  auto B1 = gemm(A,B0);
45  gemm_inplace(A,B0);
46  Precision error = frobenius(B0-B1)/frobenius(B1);
47  myra::out() << "|B0-B1|/|B1| = " << error << std::endl;
48  REQUIRE( error < tolerance );
49  }
50 
51 // Compare gemm_inplace(Matrix, Matrix22) to reference gemm()
52 template<class Number> void test3(typename ReflectPrecision<Number>::type tolerance)
53  {
54  typedef typename ReflectPrecision<Number>::type Precision;
55  auto B = Matrix22<Number>::random();
56  auto A0 = Matrix<Number>::random(25,2);
57  auto A1 = gemm(A0,B);
58  gemm_inplace(A0,B);
59  Precision error = frobenius(A0-A1)/frobenius(A1);
60  myra::out() << "|A0-A1|/|A1| = " << error << std::endl;
61  REQUIRE( error < tolerance );
62  }
63 
64 } // namespace
65 
66 ADD_TEST("sMatrix22","[dense][lapack]")
67  {
68  test1<NumberS>(1.0e-5f);
69  test2<NumberS>(1.0e-5f);
70  test3<NumberS>(1.0e-5f);
71  }
72 
73 ADD_TEST("dMatrix22","[dense][lapack]")
74  {
75  test1<NumberD>(1.0e-10);
76  test2<NumberD>(1.0e-10);
77  test3<NumberD>(1.0e-10);
78  }
79 
80 ADD_TEST("cMatrix22","[dense][lapack]")
81  {
82  test1<NumberS>(1.0e-5f);
83  test2<NumberS>(1.0e-5f);
84  test3<NumberS>(1.0e-5f);
85  }
86 
87 ADD_TEST("zMatrix22","[dense][lapack]")
88  {
89  test1<NumberZ>(1.0e-10);
90  test2<NumberZ>(1.0e-10);
91  test3<NumberZ>(1.0e-10);
92  }
93 
static Matrix22< Number > random()
Generates a random Matrix22.
Definition: Matrix22.cpp:150
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
Definition: syntax.dox:1
Matrix type with fixed size of 2x2, algorithms that operate upon them.
static Matrix22< Number > identity()
Generates an identity Matrix22.
Definition: Matrix22.cpp:137
Various utility functions/classes related to scalar Number types.
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
Variety of routines all for dense Matrix*Matrix multiplies. Delegates to the BLAS.


Results: [PASS]

|A*Z-I|/|A| = 5.2169e-08
|B0-B1|/|B1| = 2.50384e-08
|A0-A1|/|A1| = 3.02244e-08


Go back to Summary of /test programs.