MyraMath


Source: tests/sparse/SparseMatrix.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>
18 
19 // Algorithms.
22 
23 // Reporting.
24 #include <tests/myratest.h>
25 
26 using namespace myra;
27 
28 namespace {
29 
30 void test(int I, int J, int N)
31  {
32  // Make a random SparseMatrix.
33  auto A = SparseMatrix<double>::random(I,J,N);
34  double tolerance = 1.0e-12;
35  // Testing left()
36  int l = random_int(J);
37  double l_error = frobenius(A.left(l).make_Matrix()-A.make_Matrix().left(l));
38  myra::out() << " |A.left().make_Matrix() - A.make_Matrix().left()| = " << l_error << std::endl;
39  REQUIRE(l_error < tolerance);
40  // Testing right()
41  int r = random_int(J);
42  double r_error = frobenius(A.right(r).make_Matrix()-A.make_Matrix().right(r));
43  myra::out() << " |A.right().make_Matrix() - A.make_Matrix().right()| = " << r_error << std::endl;
44  REQUIRE(r_error < tolerance);
45  // Testing top()
46  int t = random_int(I);
47  double t_error = frobenius(A.top(t).make_Matrix()-A.make_Matrix().top(t));
48  myra::out() << " |A.top().make_Matrix() - A.make_Matrix().top()| = " << t_error << std::endl;
49  REQUIRE(t_error < tolerance);
50  // Testing bottom(1)
51  int b = random_int(I);
52  double b_error = frobenius(A.bottom(b).make_Matrix()-A.make_Matrix().bottom(b));
53  myra::out() << " |A.bottom().make_Matrix() - A.make_Matrix().bottom()| = " << b_error << std::endl;
54  REQUIRE(b_error < tolerance);
55  myra::out() << std::endl;
56  }
57 
58 } // namespace
59 
60 ADD_TEST("SparseMatrix","[sparse]")
61  {
62  // Small test.
63  test(5,5,10);
64  // Big test.
65  test(51,42,1000);
66  // Test when sparsely populated.
67  test(10,10,3);
68  // Test when densely populated.
69  test(12,13,100);
70  // Test when empty.
71  test(15,19,0);
72  }
73 
Interface class for representing subranges of dense Matrix&#39;s.
Routines for computing Frobenius norms of various algebraic containers.
Convenience type for building Pattern&#39;s, uses coordinate/couplet format. Note that PatternBuilder may...
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.
Simplistic random number functions.
Container for a diagonal matrix, O(n) storage. Used by SVD, row/column scaling, etc.
Range/Iterator types associated with SparseMatrix.


Results: [PASS]

|A.left().make_Matrix() - A.make_Matrix().left()| = 0
|A.right().make_Matrix() - A.make_Matrix().right()| = 0
|A.top().make_Matrix() - A.make_Matrix().top()| = 0
|A.bottom().make_Matrix() - A.make_Matrix().bottom()| = 0
|A.left().make_Matrix() - A.make_Matrix().left()| = 0
|A.right().make_Matrix() - A.make_Matrix().right()| = 0
|A.top().make_Matrix() - A.make_Matrix().top()| = 0
|A.bottom().make_Matrix() - A.make_Matrix().bottom()| = 0
|A.left().make_Matrix() - A.make_Matrix().left()| = 0
|A.right().make_Matrix() - A.make_Matrix().right()| = 0
|A.top().make_Matrix() - A.make_Matrix().top()| = 0
|A.bottom().make_Matrix() - A.make_Matrix().bottom()| = 0
|A.left().make_Matrix() - A.make_Matrix().left()| = 0
|A.right().make_Matrix() - A.make_Matrix().right()| = 0
|A.top().make_Matrix() - A.make_Matrix().top()| = 0
|A.bottom().make_Matrix() - A.make_Matrix().bottom()| = 0
|A.left().make_Matrix() - A.make_Matrix().left()| = 0
|A.right().make_Matrix() - A.make_Matrix().right()| = 0
|A.top().make_Matrix() - A.make_Matrix().top()| = 0
|A.bottom().make_Matrix() - A.make_Matrix().bottom()| = 0


Go back to Summary of /test programs.