MyraMath
multiply


Source: tests/sparse/multiply.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.
15 #include <myramath/dense/Matrix.h>
17 
18 // Algorithms.
20 #include <myramath/dense/gemm.h>
22 
23 // Reporting.
24 #include <tests/myratest.h>
25 
26 using namespace myra;
27 
28 namespace {
29 
30 // Tests multiply(A) = A'A
31 template<class Number> void test1(int Ai, int Aj, int An, typename ReflectPrecision<Number>::type tolerance)
32  {
33  myra::out() << typestring<Number>() << std::endl;
34  typedef typename ReflectPrecision<Number>::type Precision;
35  auto A = SparseMatrix<Number>::random(Ai,Aj,An);
36  auto C1 = multiply(A);
37  auto C2 = gemm(A.make_Matrix(),'H',A.make_Matrix());
38  Precision error = frobenius(C1.make_Matrix()-C2)/frobenius(C2);
39  myra::out() << "|A'A(sparse) - A'A(dense)| = " << error << std::endl;
40  myra::out() << C1.pattern() << std::endl;
41  REQUIRE(error < tolerance);
42  }
43 
44 // Tests multiply(A,B) = A'B
45 template<class Number> void test2(int Ai, int Aj, int An, int Bi, int Bj, int Bn, typename ReflectPrecision<Number>::type tolerance)
46  {
47  myra::out() << typestring<Number>() << std::endl;
48  typedef typename ReflectPrecision<Number>::type Precision;
49  auto A = SparseMatrix<Number>::random(Ai,Aj,An);
50  auto B = SparseMatrix<Number>::random(Bi,Bj,Bn);
51  auto C1 = multiply(A,B);
52  auto C2 = gemm(A.make_Matrix(),'H',B.make_Matrix());
53  Precision error = frobenius(C1.make_Matrix()-C2)/frobenius(C2);
54  myra::out() << "|A'B(sparse) - A'B(dense)| = " << error << std::endl;
55  myra::out() << C1.pattern() << std::endl;
56  REQUIRE(error < tolerance);
57  }
58 
59 } // namespace
60 
61 ADD_TEST("multiply","[sparse]")
62  {
63  // Test multiply(A)
64  int Ai = 20;
65  int Aj = 10;
66  int An = 35;
67  test1<NumberS>(Ai,Aj,An,1.0e-4f);
68  test1<NumberD>(Ai,Aj,An,1.0e-10);
69  test1<NumberC>(Ai,Aj,An,1.0e-4f);
70  test1<NumberZ>(Ai,Aj,An,1.0e-10);
71  // Test multiply(A,B)
72  int Bi = Ai;
73  int Bj = 15;
74  int Bn = 25;
75  test2<NumberS>(Ai,Aj,An,Bi,Bj,Bn,1.0e-4f);
76  test2<NumberD>(Ai,Aj,An,Bi,Bj,Bn,1.0e-10);
77  test2<NumberC>(Ai,Aj,An,Bi,Bj,Bn,1.0e-4f);
78  test2<NumberZ>(Ai,Aj,An,Bi,Bj,Bn,1.0e-10);
79  }
Routines to multiply "fat inner products" of SparseMatrix&#39;s, C = A&#39;A and C = A&#39;B. ...
Interface class for representing subranges of dense Matrix&#39;s.
Routines for computing Frobenius norms of various algebraic containers.
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.
Range/Iterator types associated with Pattern.
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.
Range/Iterator types associated with SparseMatrix.


Results: [PASS]

float
|A'A(sparse) - A'A(dense)| = 4.35266e-08
size 10 by 10 Pattern:
[ x - x x - x - x x - ]
[ - x - - - x - - - - ]
[ x - x x x x x - x - ]
[ x - x x - - x - - - ]
[ - - x - x - - - x - ]
[ x x x - - x - - x - ]
[ - - x x - - x - - - ]
[ x - - - - - - x - - ]
[ x - x - x x - - x x ]
[ - - - - - - - - x x ]
double
|A'A(sparse) - A'A(dense)| = 0
size 10 by 10 Pattern:
[ x - - - - - x - - - ]
[ - x - x x x - x x x ]
[ - - - - - - - - - - ]
[ - x - x - x - - - x ]
[ - x - - x - - x - x ]
[ - x - x - x - - - - ]
[ x - - - - - x x - - ]
[ - x - - x - x x x x ]
[ - x - - - - - x x x ]
[ - x - x x - - x x x ]
std::complex<float>
|A'A(sparse) - A'A(dense)| = 5.35833e-08
size 10 by 10 Pattern:
[ x - - x - - - x - - ]
[ - x - - - - x - - x ]
[ - - x - - - - - x - ]
[ x - - x x - x x - x ]
[ - - - x x x x - x x ]
[ - - - - x x - - - - ]
[ - x - x x - x - x x ]
[ x - - x - - - x x x ]
[ - - x - x - x x x - ]
[ - x - x x - x x - x ]
std::complex<double>
|A'A(sparse) - A'A(dense)| = 9.69401e-17
size 10 by 10 Pattern:
[ x - - x - - - x - - ]
[ - x x - x x x - x x ]
[ - x x - x - x - - - ]
[ x - - x x - x x - - ]
[ - x x x x - x - - - ]
[ - x - - - x - - - - ]
[ - x x x x - x - x x ]
[ x - - x - - - x - x ]
[ - x - - - - x - x - ]
[ - x - - - - x x - x ]
float
|A'B(sparse) - A'B(dense)| = 6.48248e-09
size 10 by 15 Pattern:
[ - x - x - - - - - - - - - - x ]
[ - - - - - - x - - - - - - - - ]
[ - - - - - x - - - x x - - - x ]
[ x - - x - - x - - - - x - x x ]
[ - - - - - x - - - - - x - - x ]
[ - - - x - - x x x x x - - x - ]
[ - - - - - x - - - x x - - x x ]
[ x x - x - - x - - x x - - x x ]
[ - - - x - - - - - - - - - - - ]
[ - - - - - - - - - - - x - - - ]
double
|A'B(sparse) - A'B(dense)| = 0
size 10 by 15 Pattern:
[ x x - - x - x - x x x x x x x ]
[ x - - x - - - - - - - - - - - ]
[ - - - x - - - - - - - - - - - ]
[ - x x - x - - - - - - - - - x ]
[ - x x x x - x - - - - x - x x ]
[ - x - - x - - - - - - - - - x ]
[ x x - - x - - - - - - - - x x ]
[ x x - - - - - - x x x x x - - ]
[ - x x x - - x - - - x - - x - ]
[ - - - - - - - - - - - - - - - ]
std::complex<float>
|A'B(sparse) - A'B(dense)| = 2.07961e-08
size 10 by 15 Pattern:
[ x - - - - - - - - - - - - - - ]
[ x - - - - - - x - - x - x - - ]
[ - - - - - - - x - - x - x - x ]
[ - - - - - - - x - - x - - - - ]
[ - - x - - - - x - - - - - - - ]
[ x - - - - - - x - - x - - - - ]
[ - - x - - - - - - x - x x - x ]
[ - - - - - - - - - - - - - - - ]
[ - - - - - - - - - - - x - - - ]
[ - - - - - - - x - - x - x - x ]
std::complex<double>
|A'B(sparse) - A'B(dense)| = 2.64554e-17
size 10 by 15 Pattern:
[ - - - x - - - x - - x - x - - ]
[ - - x - x - - - - x x - - - - ]
[ - x - - - x - x - - x - x x - ]
[ - - x - x x - - - - x - - - - ]
[ - x - - - - - - - x x - - x - ]
[ - x x - x - - x - x x - x x - ]
[ - x - x - x - - - - - x - - - ]
[ - - - - - x - - - x - - - - - ]
[ - x - - - x - - - x x - - x - ]
[ - - - - - - - - - - - - - - - ]


Go back to Summary of /test programs.