MyraMath
dense_io2


Source: tests/dense/io2.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 
10 // Container types.
12 #include <myramath/dense/Matrix.h>
14 #include <myramath/dense/Vector.h>
18 
19 // Algorithms.
21 
22 // Serialization backend.
24 
25 // Reporting.
26 #include <tests/myratest.h>
27 
28 using namespace myra;
29 
30 namespace {
31 
32 // Tests serialization roundtrip for MatrixRange -> file -> Matrix
33 template<class Number> void test1()
34  {
35  myra::out() << typestring<Number>() << std::endl;
36  // Make random A.
37  auto A = Matrix<Number>::random(5,5);
38  // Serialize a window of it, read back into B.
39  VectorStream inout;
40  MatrixRange<Number> A_range = A.bottom(3).right(2);
41  A_range.write(inout);
42  Matrix<Number> B(inout);
43  // Verify A_range == B.
44  typename ReflectPrecision<Number>::type error = frobenius(A_range-B);
45  myra::out() << " |A(2:4,3:4)-B| = " << error << std::endl;
46  REQUIRE(error == 0);
47  }
48 
49 // Tests serialization roundtrip for VectorRange -> file -> Vector
50 template<class Number> void test2()
51  {
52  myra::out() << typestring<Number>() << std::endl;
53  // Matrix random a.
54  auto a = Vector<Number>::random(5);
55  // Serialize a window of it, read back into B.
56  VectorStream inout;
57  VectorRange<Number> a_range = a.window(1,3);
58  a_range.write(inout);
59  Vector<Number> b(inout);
60  // Verify a_range == b.
61  typename ReflectPrecision<Number>::type error = frobenius(a_range-b);
62  myra::out() << " |a(1:3)-b| = " << error << std::endl;
63  REQUIRE(error == 0);
64  }
65 
66 // Tests serialization roundtrip for LowerMatrixRange -> file -> LowerMatrix
67 template<class Number> void test3()
68  {
69  myra::out() << typestring<Number>() << std::endl;
70  // Matrix random A.
71  auto A = LowerMatrix<Number>::random(5);
72  // Serialize a window of it, read back into B.
73  VectorStream inout;
74  LowerMatrixRange<Number> A_range = A.range();
75  A_range.write(inout);
76  LowerMatrix<Number> B(inout);
77  // Verify a_range == B.
78  typename ReflectPrecision<Number>::type error = frobenius(A_range-B);
79  myra::out() << " |a(:,:)-b| = " << error << std::endl;
80  REQUIRE(error == 0);
81  }
82 
83 } // namespace
84 
85 ADD_TEST("dense_io2","[dense]")
86  {
87  // Test MatrixRange/Matrix..
88  test1<NumberS>();
89  test1<NumberD>();
90  test1<NumberC>();
91  test1<NumberZ>();
92  // Test VectorRange/Vector..
93  test2<NumberS>();
94  test2<NumberD>();
95  test2<NumberC>();
96  test2<NumberZ>();
97  // Test LowerMatrixRange/LowerMatrix..
98  test3<NumberS>();
99  test3<NumberD>();
100  test3<NumberC>();
101  test3<NumberZ>();
102  }
void write(OutputStream &out) const
Writes to OutputStream. Layout compatible with Vector&#39;s InputStream constructor.
Definition: VectorRange.cpp:42
Interface class for representing subranges of dense Matrix&#39;s.
void write(OutputStream &out) const
Writes to OutputStream. Layout compatible with LowerMatrix&#39;s InputStream constructor.
Definition: LowerMatrixRange.cpp:52
Represents a mutable LowerMatrixRange.
Definition: conjugate.h:28
Interface class for representing subranges of dense Vector&#39;s.
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Represents a mutable VectorRange.
Definition: axpy.h:21
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
Wraps a std::vector<char>, presents it as both an InputStream and OutputStream. Useful for hygienic u...
Definition: VectorStream.h:22
Range construct for a lower triangular matrix stored in rectangular packed format.
static Vector< Number > random(int N)
Generates a random Vector of specified size.
Definition: Vector.cpp:276
Definition: syntax.dox:1
Specialized container for a lower triangular matrix, O(N^2/2) storage. Used by symmetry exploiting ma...
VectorRange< Number > window(int n0, int n1) const
Returns this(n0:n1)
Definition: VectorRange.cpp:73
Various utility functions/classes related to scalar Number types.
Represents a mutable MatrixRange.
Definition: conjugate.h:26
void write(OutputStream &out) const
Writes to OutputStream. Layout compatible with Matrix&#39;s InputStream constructor.
Definition: MatrixRange.cpp:54
A stream that serialize/deserializes to std::vector<char> buffer.
General purpose dense matrix container, O(i*j) storage.
Tabulates a vector of length N, allows random access.
Definition: conjugate.h:21
Container for either a column vector or row vector (depends upon the usage context) ...
Reflects Precision trait for a Number, scalar Number types should specialize it.
Definition: Number.h:33
MatrixRange< Number > bottom(int i) const
Returns the i bottommost rows, this(I-i:I,:)
Definition: MatrixRange.cpp:186
Stores a lower triangular matrix in rectangular packed format.
Definition: conjugate.h:22
static LowerMatrix< Number > random(int N)
Generates a random LowerMatrix of specified size.
Definition: LowerMatrix.cpp:249


Results: [PASS]

float
|A(2:4,3:4)-B| = 0
double
|A(2:4,3:4)-B| = 0
std::complex<float>
|A(2:4,3:4)-B| = 0
std::complex<double>
|A(2:4,3:4)-B| = 0
float
|a(1:3)-b| = 0
double
|a(1:3)-b| = 0
std::complex<float>
|a(1:3)-b| = 0
std::complex<double>
|a(1:3)-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.