MyraMath
sparse_io


Source: tests/sparse/io.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 // Container types.
19 
20 // Algorithms.
22 
23 // Serialization backend.
25 
26 // Reporting.
27 #include <tests/myratest.h>
28 
29 using namespace myra;
30 
31 namespace {
32 
33 // Tests serialization roundtrip for SparseMatrixRange -> file -> SparseMatrix
34 template<class Number> void test1()
35  {
36  myra::out() << typestring<Number>() << std::endl;
37  // Make random A.
38  auto A = SparseMatrix<Number>::random(20,20,100);
39  // Serialize a window of it, read back into B.
40  VectorStream inout;
41  SparseMatrixRange<Number> A_range = A.window(4,15,6,11);
42  A_range.write(inout);
43  SparseMatrix<Number> B(inout);
44  // Verify A_range == B.
45  typename ReflectPrecision<Number>::type error = frobenius(A_range-B.range().add_const());
46  myra::out() << " |A(4:15,6:11)-B| = " << error << std::endl;
47  REQUIRE(error == 0);
48  }
49 
50 // Tests serialization roundtrip for SparseMatrixBuilder -> file -> SparseMatrix
51 template<class Number> void test2()
52  {
53  myra::out() << typestring<Number>() << std::endl;
54  // Save random SparseMatrixBuilder A.
55  auto A = SparseMatrixBuilder<Number>::random(20,20,100);
56  VectorStream inout;
57  A.write(inout);
58  SparseMatrix<Number> B(inout);
59  // Verify A == B
60  typename ReflectPrecision<Number>::type error = frobenius(A.make_SparseMatrix()-B);
61  myra::out() << " |A-B| = " << error << std::endl;
62  REQUIRE(error == 0);
63  }
64 
65 
66 // Tests serialization roundtrip for PatternBuilder -> file -> Pattern
67 void test3()
68  {
69  // Save random PatternBuilder A.
70  auto A = PatternBuilder::random(20,20,100);
71  VectorStream inout;
72  A.write(inout);
73  // Verify A == B
74  Pattern B(inout);
75  REQUIRE(A.make_Pattern() == B);
76  }
77 
78 } // namespace
79 
80 ADD_TEST("sparse_io","[sparse]")
81  {
82  // Test SparseMatrixRange/SparseMatrix..
83  test1<NumberS>();
84  test1<NumberD>();
85  test1<NumberC>();
86  test1<NumberZ>();
87  // Test SparseMatrixBuilder/SparseMatrix..
88  test2<NumberS>();
89  test2<NumberD>();
90  test2<NumberC>();
91  test2<NumberZ>();
92  // Test PatternBuilder/Pattern
93  test3();
94  }
void write(OutputStream &out) const
Writes to OutputStream. Layout compatible with SparseMatrix&#39;s InputStream constructor.
Definition: SparseMatrixRange.cpp:59
static SparseMatrixBuilder< Number > random(int I, int J, int N)
Generates a random SparseMatrix with size IxJ and (approximately) N nonzeros.
Definition: SparseMatrixBuilder.cpp:208
Represents a mutable SparseMatrixRange.
Definition: conjugate.h:21
static PatternBuilder random(std::pair< int, int > IJ, int N)
Generates a random PatternBuilder with size IxJ and (approximately) N nonzeros.
Definition: PatternBuilder.cpp:210
Convenience type for building Pattern&#39;s, uses coordinate/couplet format. Note that PatternBuilder may...
CSparseMatrixRange< Number > add_const() const
Adds "const" (C) qualifier, explicit conversion to CSparseMatrixRange<Number>
Definition: SparseMatrixRange.cpp:149
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
Wraps a std::vector<char>, presents it as both an InputStream and OutputStream. Useful for hygienic u...
Definition: VectorStream.h:22
General purpose compressed-sparse-column (CSC) container.
Definition: syntax.dox:1
Various utility functions/classes related to scalar Number types.
A stream that serialize/deserializes to std::vector<char> buffer.
Range/Iterator types associated with Pattern.
Holds the nonzero pattern of a sparse matrix.
Definition: Pattern.h:55
Reflects Precision trait for a Number, scalar Number types should specialize it.
Definition: Number.h:33
Container class for a sparse nonzero pattern, used in reordering/symbolic analysis.
Returns frobenius norm of a SparseMatrix.
Convenience type for building SparseMatrix&#39;s, uses coordinate/triplet format. Note that SparseMatrixB...
Stores an IxJ matrix A in compressed sparse column format.
Definition: bothcat.h:23
Range/Iterator types associated with SparseMatrix.


Results: [PASS]

float
|A(4:15,6:11)-B| = 0
double
|A(4:15,6:11)-B| = 0
std::complex<float>
|A(4:15,6:11)-B| = 0
std::complex<double>
|A(4:15,6:11)-B| = 0
float
|A-B| = 0
double
|A-B| = 0
std::complex<float>
|A-B| = 0
std::complex<double>
|A-B| = 0


Go back to Summary of /test programs.