MyraMath
sparse_hemm


Source: tests/sparse/hemm.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.
19 #include <myramath/dense/hemm.h>
20 #include <myramath/sparse/hemm.h>
22 
23 // Reporting.
24 #include <tests/myratest.h>
25 
26 using namespace myra;
27 
28 namespace {
29 
30 // Tests hemm('L'eft)
31 template<class Number> void test1(int IJ, int N, int K, typename ReflectPrecision<Number>::type tolerance)
32  {
33  myra::out() << "test1 " << typestring<Number>() << std::endl;
34  typedef typename ReflectPrecision<Number>::type Precision;
35  auto A = SparseMatrix<Number>::random(IJ,IJ,N);
36  // Diagonal must be pure real.
37  A.transform_diagonal([](const Number& a){return realpart(a);});
38  auto X = Matrix<Number>::random(IJ,K);
39  auto B1 = hemm('L','U',A,X);
40  auto B2 = hemm('L','U',A.make_Matrix(),X);
41  Precision B_error = frobenius(B1-B2);
42  auto C1 = hemm('L','L',A,X);
43  auto C2 = hemm('L','L',A.make_Matrix(),X);
44  Precision C_error = frobenius(C1-C2);
45  myra::out() << " error in hemm('L','U') = " << B_error << std::endl;
46  myra::out() << " error in hemm('L','L') = " << C_error << std::endl;
47  REQUIRE(B_error < tolerance);
48  REQUIRE(C_error < tolerance);
49  }
50 
51 // Tests hemm('R'ight)
52 template<class Number> void test2(int IJ, int N, int K, typename ReflectPrecision<Number>::type tolerance)
53  {
54  myra::out() << "test2 " << typestring<Number>() << std::endl;
55  typedef typename ReflectPrecision<Number>::type Precision;
56  auto A = SparseMatrix<Number>::random(IJ,IJ,N);
57  // Diagonal must be pure real.
58  A.transform_diagonal([](const Number& a){return realpart(a);});
59  auto X = Matrix<Number>::random(K,IJ);
60  auto B1 = hemm('R','U',A,X);
61  auto B2 = hemm('R','U',A.make_Matrix(),X);
62  Precision B_error = frobenius(B1-B2);
63  auto C1 = hemm('R','L',A,X);
64  auto C2 = hemm('R','L',A.make_Matrix(),X);
65  Precision C_error = frobenius(C1-C2);
66  myra::out() << " error in hemm('R','U') = " << B_error << std::endl;
67  myra::out() << " error in hemm('R','L') = " << C_error << std::endl;
68  REQUIRE(B_error < tolerance);
69  REQUIRE(C_error < tolerance);
70  }
71 
72 } // namespace
73 
74 ADD_TEST("sparse_hemm","[sparse]")
75  {
76  // Test hemm('L',...)
77  test1<NumberS>(15,40,8,1.0e-4f);
78  test1<NumberD>(15,40,8,1.0e-9);
79  test1<NumberC>(15,40,8,1.0e-4f);
80  test1<NumberZ>(15,40,8,1.0e-9);
81  // Test hemm('R',...)
82  test2<NumberS>(15,40,8,1.0e-4f);
83  test2<NumberD>(15,40,8,1.0e-9);
84  test2<NumberC>(15,40,8,1.0e-4f);
85  test2<NumberZ>(15,40,8,1.0e-9);
86  }
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
Routines for hermitian Matrix * dense Matrix multiplication.
static SparseMatrix< Number > random(int I, int J, int N)
Generates a random SparseMatrix with size IxJ and (approximately) N nonzeros.
Definition: SparseMatrix.cpp:493
General purpose compressed-sparse-column (CSC) container.
Definition: syntax.dox:1
Various utility functions/classes related to scalar Number types.
Routines for hermitian SparseMatrix * dense Matrix multiplication.
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
Range/Iterator types associated with SparseMatrix.


Results: [PASS]

test1 float
error in hemm('L','U') = 1.2495e-07
error in hemm('L','L') = 3.51724e-07
test1 double
error in hemm('L','U') = 2.83817e-16
error in hemm('L','L') = 3.6817e-16
test1 std::complex<float>
error in hemm('L','U') = 5.19281e-07
error in hemm('L','L') = 5.62858e-07
test1 std::complex<double>
error in hemm('L','U') = 1.06808e-15
error in hemm('L','L') = 1.1866e-15
test2 float
error in hemm('R','U') = 3.08221e-07
error in hemm('R','L') = 2.27604e-07
test2 double
error in hemm('R','U') = 3.39066e-16
error in hemm('R','L') = 4.11565e-16
test2 std::complex<float>
error in hemm('R','U') = 0
error in hemm('R','L') = 5.02576e-07
test2 std::complex<double>
error in hemm('R','U') = 0
error in hemm('R','L') = 0


Go back to Summary of /test programs.