testSparseMatrixVectorOp.cc
Go to the documentation of this file.
1 
11 #include <iostream>
12 
13 #include <utl/SparseVector.h>
14 #include <utl/SparseMatrix.h>
15 
16 #include <tst/Verify.h>
17 #include <cppunit/extensions/HelperMacros.h>
18 
19 using namespace std;
20 using namespace utl;
21 using namespace tst;
22 
23 #define ASSERT_CLOSE(x, y) CPPUNIT_ASSERT(Verify<CloseTo>(x, y))
24 #define ASSERT_CLOSE_EPS(x, y, eps) CPPUNIT_ASSERT(Verify<CloseTo>(x, y, eps))
25 #define ASSERT_EQUAL(x, y) CPPUNIT_ASSERT(Verify<Equal>(x, y))
26 #define ASSERT_ITERATOR_EQUAL(it, e, i, v) \
27  CPPUNIT_ASSERT(it != e); \
28  ASSERT_EQUAL(it.GetIndex(), i); \
29  ASSERT_EQUAL(it(), v)
30 
31 
35 class TestSparseMatrixVectorOp : public CppUnit::TestFixture {
36 
37  CPPUNIT_TEST_SUITE(TestSparseMatrixVectorOp);
38  CPPUNIT_TEST(TestMultiply);
39  CPPUNIT_TEST(TestMultiply2);
40  CPPUNIT_TEST_SUITE_END();
41 
42 public:
43  void
45  {
47  for (int i = 0; i < 10; ++i)
48  for (int j = 0; j < 5; ++j) {
49  const int s = i + j + 1;
50  if (s%2)
51  m(i, j) = s;
52  }
53 
55  a(0) = 1; a(2) = -1; a(4) = 1;
56 
57  SparseVector<> b = m * a;
58 
59  int i = 0;
61  it != b.SparseEnd(); ++it, ++i) {
62  ASSERT_ITERATOR_EQUAL(it, b.SparseEnd(), 2*i, 2*i + 3.);
63  }
64  }
65 
66  void
68  {
69  const int n = 100;
70  const int m = 50;
71 
73 
74  for (int i = 0; i < n; ++i)
75  for (int j = 0; j < m; ++j)
76  a(i, j) = int(i) - 3*int(j);
77 
79 
80  for (int i = 0; i < m; ++i)
81  x(i) = i;
82 
83  const SparseVector<> y = a * x;
84 
85  for (int i = 0; i < n; ++i)
86  ASSERT_EQUAL(y[i],
87  double((i-(2*m-1))*(m-1)*m/2));
88  }
89 
90 };
91 
92 
94 
95 
96 // Configure (x)emacs for this file ...
97 // Local Variables:
98 // mode: c++
99 // End:
Sparse container class for vectorial data.
Definition: SparseVector.h:36
#define ASSERT_EQUAL(x, y)
Sparse container class for matrix data.
Definition: SparseMatrix.h:36
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
constexpr double s
Definition: AugerUnits.h:163
Iterator SparseEnd()
Definition: SparseVector.h:182
Iterator SparseBegin()
Definition: SparseVector.h:178
#define ASSERT_ITERATOR_EQUAL(it, e, i, v)
constexpr double m
Definition: AugerUnits.h:121

, generated on Tue Sep 26 2023.