MyraMath


Source: tests/sparse/SparseMatrixRange.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.
12 #include <myramath/dense/Matrix.h>
16 
17 // Algorithms.
18 #include <myramath/sparse/gemm.h>
19 #include <myramath/dense/gemm.h>
21 
22 // Reporting.
23 #include <tests/myratest.h>
24 
25 using namespace myra;
26 
27 namespace {
28 
29 template<class Number> void test(int I, int J, int N, typename ReflectPrecision<Number>::type tolerance)
30  {
31  typedef typename ReflectPrecision<Number>::type Precision;
32  // Generate random A (sparse) and B (dense).
33  auto A = SparseMatrix<Number>::random(I,J,N);
34  auto B = Matrix<Number>::random(J,10);
35  // Multiply only using the lower right corner, DenseMatrix (reference)
36  // [ ~ ~ ] * [ ~ ]
37  // [ ~ A22] [ B2 ]
38  Matrix<Number> C1 = gemm(A.make_Matrix().window(I/2,I,J/2,J), 'N', B.rows(J/2,J), 'N');
39  // Repeat the multiply with a CSparseMatrixRange.
40  Matrix<Number> C2 = gemm(A.window(I/2,I,J/2,J), 'N', B.rows(J/2,J), 'N');
41  // Repeat the multiply with a SparseMatrix, constructed from the range.
42  SparseMatrix<Number> A2( A.window(I/2,I,J/2,J) );
43  Matrix<Number> C3 = gemm(A2, 'N', B.rows(J/2,J), 'N');
44  // Check error C2 - C1.
45  Precision error2 = frobenius(C2-C1);
46  myra::out() << " |A.window()*B [sparse-dense]| = " << error2 << std::endl;
47  REQUIRE(error2 < tolerance);
48  // Check error C3 - C1.
49  Precision error3 = frobenius(C3-C1);
50  myra::out() << " |A*B.window() [sparse-dense]| = " << error3 << std::endl;
51  REQUIRE(error3 < tolerance);
52  }
53 
54 } // namespace
55 
56 ADD_TEST("SparseMatrixRange","[sparse]")
57  {
58  myra::out() << typestring<NumberS>() << std::endl;
59  test<NumberS> (100,100,1000,1.0e-4f);
60  test<NumberS > (10,1,3,1.0e-4f);
61  test<NumberS > (1,10,3,1.0e-4f);
62  test<NumberS > (0,0,0,1.0e-4f);
63  myra::out() << typestring<NumberD>() << std::endl;
64  test<NumberD> (100,100,1000,1.0e-9);
65  test<NumberD> (10,1,3,1.0e-9);
66  test<NumberD> (1,10,3,1.0e-9);
67  test<NumberD> (0,0,0,1.0e-9);
68  myra::out() << typestring<NumberC>() << std::endl;
69  test<NumberC> (100,100,1000,1.0e-4f);
70  test<NumberC> (10,1,3,1.0e-4f);
71  test<NumberC> (1,10,3,1.0e-4f);
72  test<NumberC> (0,0,0,1.0e-4f);
73  myra::out() << typestring<NumberZ>() << std::endl;
74  test<NumberZ> (100,100,1000,1.0e-9);
75  test<NumberZ> (10,1,3,1.0e-9);
76  test<NumberZ> (1,10,3,1.0e-9);
77  test<NumberZ> (0,0,0,1.0e-9);
78  }
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
Variety of routines for mixed dense*sparse or dense*sparse matrix multiplies. The dense*dense case is...
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
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
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
Stores an IxJ matrix A in compressed sparse column format.
Definition: bothcat.h:23
Variety of routines all for dense Matrix*Matrix multiplies. Delegates to the BLAS.
Range/Iterator types associated with SparseMatrix.


Results: [PASS]

float
|A.window()*B [sparse-dense]| = 8.40485e-07
|A*B.window() [sparse-dense]| = 8.40485e-07
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
double
|A.window()*B [sparse-dense]| = 1.86056e-15
|A*B.window() [sparse-dense]| = 1.86056e-15
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
|A.window()*B [sparse-dense]| = 3.46945e-17
|A*B.window() [sparse-dense]| = 3.46945e-17
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
std::complex<float>
|A.window()*B [sparse-dense]| = 2.57639e-06
|A*B.window() [sparse-dense]| = 2.57639e-06
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
std::complex<double>
|A.window()*B [sparse-dense]| = 4.8191e-15
|A*B.window() [sparse-dense]| = 4.8191e-15
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0
|A.window()*B [sparse-dense]| = 2.77556e-16
|A*B.window() [sparse-dense]| = 2.77556e-16
|A.window()*B [sparse-dense]| = 0
|A*B.window() [sparse-dense]| = 0


Go back to Summary of /test programs.