MyraMath
tril_Pattern


Source: tests/sparse/tril.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.
20 #include <myramath/sparse/tril.h>
21 #include <myramath/dense/tril.h>
23 
24 // Reporting.
25 #include <tests/myratest.h>
26 
27 using namespace myra;
28 
29 namespace {
30 
31 template<class Number> void test(int I, int J, int N, typename ReflectPrecision<Number>::type tolerance)
32  {
33  myra::out() << typestring<Number>() << std::endl;
34  typedef typename ReflectPrecision<Number>::type Precision;
35  // Make random SparseMatrix A.
36  auto A = SparseMatrix<Number>::random(I,J,N);
37  // Compare sparse tril() against dense tril()
38  Precision error = frobenius( tril(A).make_Matrix() - tril(A.make_Matrix()) );
39  myra::out() << "|tril(A)(sparse) - tril(A)(dense)| = " << error << std::endl;
40  REQUIRE(error < tolerance);
41  }
42 
43 void test(int I, int J, int N)
44  {
45  auto A = Pattern::random(I,J,N);
46  auto L = tril(A);
47  bool ok = true;
48  // Check all nonzeros in lower triangle of A are present in L.
49  for (auto iterator = A.begin(); iterator != A.end(); ++iterator)
50  {
51  int i = iterator.i();
52  int j = iterator.j();
53  if (i >= j)
54  if (!L.test(i,j))
55  ok = false;
56  }
57  // Check that L contains no nonzeros in its upper triangle.
58  for (auto iterator = L.begin(); iterator != L.end(); ++iterator)
59  if (iterator.i() < iterator.j())
60  ok = false;
61  REQUIRE(ok);
62  }
63 
64 } // namespace
65 
66 ADD_TEST("tril","[sparse]")
67  {
68  test<NumberS>(20,30,150,1.0e-4f);
69  test<NumberD>(20,30,150,1.0e-12);
70  test<NumberC>(20,30,150,1.0e-4f);
71  test<NumberZ>(20,30,150,1.0e-12);
72 
73  test<NumberS>(30,20,150,1.0e-4f);
74  test<NumberD>(30,20,150,1.0e-12);
75  test<NumberC>(30,20,150,1.0e-4f);
76  test<NumberZ>(30,20,150,1.0e-12);
77  }
78 
79 ADD_TEST("tril_Pattern","[sparse]")
80  {
81  test(20,30,150);
82  test(30,20,150);
83  }
84 
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
Returns the lower triangle of a dense Matrix.
static Pattern random(int I, int J, int N)
Generates a random Pattern with size IxJ and (approximately) N nonzeros.
Definition: Pattern.cpp:300
General purpose dense matrix container, O(i*j) storage.
Range/Iterator types associated with Pattern.
Returns the lower triangle of a SparseMatrix.
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.
Range/Iterator types associated with SparseMatrix.


Results: [PASS]


Go back to Summary of /test programs.