testSparseMatrix.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <numeric>
3 
4 #include <utl/SparseMatrix.h>
5 
6 #include <tst/Verify.h>
7 #include <cppunit/extensions/HelperMacros.h>
8 
9 using namespace std;
10 using namespace utl;
11 using namespace tst;
12 
13 #define ASSERT_CLOSE(x, y) CPPUNIT_ASSERT(Verify<CloseTo>(x, y))
14 #define ASSERT_CLOSE_EPS(x, y, eps) CPPUNIT_ASSERT(Verify<CloseTo>(x, y, eps))
15 #define ASSERT_EQUAL(x, y) CPPUNIT_ASSERT(Verify<Equal>(x, y))
16 #define ASSERT_NOT_EQUAL(x, y) CPPUNIT_ASSERT(Verify<Not<Equal> >(x, y))
17 #define ASSERT_ITERATOR_EQUAL(it, e, r, c, v) \
18  CPPUNIT_ASSERT(it != e); \
19  ASSERT_EQUAL(it.GetRow(), r); \
20  ASSERT_EQUAL(it.GetColumn(), c); \
21  ASSERT_EQUAL(it(), v)
22 
23 
33 class TestSparseMatrix : public CppUnit::TestFixture {
34 
35  CPPUNIT_TEST_SUITE(TestSparseMatrix);
36  CPPUNIT_TEST(TestAccess);
37  CPPUNIT_TEST(TestIterators);
38  CPPUNIT_TEST(TestOperators);
39  CPPUNIT_TEST(TestMultiplication);
40  CPPUNIT_TEST(TestNonSquareMultiplication);
41  CPPUNIT_TEST(TestNonSquareMultiplication2);
42  CPPUNIT_TEST(TestTranspose);
43  CPPUNIT_TEST_SUITE_END();
44 
45 public:
46  void
48  {
49  // assume 10 x 10
50  const int rows = 5;
51  const int cols = 5;
53 
54  m(0, 0) = 1;
55  m(2, 4) = 2;
56  m(4, 4) = 3;
57  m(1, 3) = 4;
58  m(3, 1) = 5;
59 
60  stringstream ss;
61 
62  OutputSparse(ss, m, rows, cols);
63  const string test1 =
64  "1 . . . .\n"
65  ". . . 4 .\n"
66  ". . . . 2\n"
67  ". 5 . . .\n"
68  ". . . . 3\n";
69  ASSERT_EQUAL(ss.str(), test1);
70 
71  for (int i = 0; i < rows; ++i)
72  for (int j = 0; j < cols; ++j)
73  m[i][j];
74 
75  ss.str("");
76  OutputSparse(ss, m, rows, cols);
77  ASSERT_EQUAL(ss.str(), test1);
78 
79  for (int i = 0; i < rows; ++i)
80  for (int j = 0; j < cols; ++j)
81  m(i, j);
82 
83  ss.str("");
84  OutputSparse(ss, m, rows, cols);
85  const string test2 =
86  "1 0 0 0 0\n"
87  "0 0 0 4 0\n"
88  "0 0 0 0 2\n"
89  "0 5 0 0 0\n"
90  "0 0 0 0 3\n";
91  ASSERT_EQUAL(ss.str(), test2);
92 
93  m.Reclaim();
94 
95  ss.str("");
96  OutputSparse(ss, m, rows, cols);
97  ASSERT_EQUAL(ss.str(), test1);
98  }
99 
100  void
102  {
103  // assume 10 x 20
105 
106  m(1, 1) = 1;
107  m(5, 7) = 2;
108  m(7, 15) = 3;
109  m(9, 19) = 4;
110  m(0, 0) = 5;
111  m(9, 0) = 6;
112 
114  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 0, 0, 5);
115  ++it;
116  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 1, 1, 1);
117  ++it;
118  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 5, 7, 2);
119  ++it;
120  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 7, 15, 3);
121  ++it;
122  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 9, 0, 6);
123  ++it;
124  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 9, 19, 4);
125  ++it;
126  CPPUNIT_ASSERT(it == m.SparseEnd());
127 
128  it = m.Find(0, 0);
129  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 0, 0, 5);
130 
131  it = m.Find(7, 15);
132  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 7, 15, 3);
133 
134  it = m.Find(9, 19);
135  ASSERT_ITERATOR_EQUAL(it, m.SparseEnd(), 9, 19, 4);
136  }
137 
138  void
140  {
142 
143  m(0, 0) = 1;
144  m(1, 1) = 2;
145  m(1, 4) = 3;
146  m(2, 0) = 4;
147  m(2, 4) = 5;
148 
150 
151  m *= 2;
152 
154  it != m.SparseEnd(); ++it)
155  ASSERT_EQUAL(it(), 2 * mm[it.GetRow()][it.GetColumn()]);
156 
157  m += mm;
158 
160  it != m.SparseEnd(); ++it)
161  ASSERT_EQUAL(it(), 3 * mm[it.GetRow()][it.GetColumn()]);
162 
163  m -= mm;
164 
166  it != m.SparseEnd(); ++it)
167  ASSERT_EQUAL(it(), 2 * mm[it.GetRow()][it.GetColumn()]);
168 
169  m -= m;
170 
171  m.Reclaim();
172 
174  }
175 
176  void
178  {
179  // 4 x 4
181 
182  a(0, 0) = 1; a(0, 2) = 2;
183  a(1, 1) = 3; a(1, 3) = 4;
184  a(2, 0) = 5; a(2, 2) = 6;
185  a(3, 1) = 7; a(3, 3) = 8;
186 
188 
189  b(0, 1) = 1; b(0, 3) = -2;
190  b(1, 0) = 4; b(1, 2) = -8;
191  b(2, 1) = 16; b(2, 3) = -32;
192  b(3, 0) = 64; b(3, 2) = -128;
193 
194  SparseMatrix<double> r = a * b;
195 
197 
198  c(0, 1) = 33; c(0, 3) = -66;
199  c(1, 0) = 268; c(1, 2) = -536;
200  c(2, 1) = 101; c(2, 3) = -202;
201  c(3, 0) = 540; c(3, 2) = -1080;
202 
203  CPPUNIT_ASSERT(r == c);
204  }
205 
206  void
208  {
209  // 2 x 10
211 
212  a(0, 0) = 1; a(0, 9) = 1;
213  a(1, 0) = 1; a(1, 9) = 1;
214 
215  // 10 x 2
217  b(0, 0) = 1; b(0, 1) = 1;
218  b(9, 0) = 1; b(9, 1) = 1;
219 
220  // 2 x 2
221  SparseMatrix<float> c = a * b;
222 
224 
225  t(0, 0) = 2; t(0, 1) = 2;
226  t(1, 0) = 2; t(1, 1) = 2;
227 
228  ASSERT_EQUAL(t, c);
229 
230  SparseMatrix<double> td(t);
231 
232  CPPUNIT_ASSERT(td == c);
233  }
234 
235  void
237  {
238  const int n = 50;
239  const int m = 25;
240 
243 
244  for (int i = 0; i < n; ++i)
245  for (int j = 0; j < m; ++j) {
246  a(i, j) = i + j;
247  b(j, i) = -2 * i + j;
248  }
249 
250  const SparseMatrix<> c = a * b;
251 
252  for (int i = 0; i < n; ++i)
253  for (int j = 0; j < n; ++j)
254  ASSERT_EQUAL(c[i][j],
255  double(-2*m*i*j+(i-2*j)*(m-1)*m/2+(m-1)*m*(2*m-1)/6));
256  }
257 
258  void
260  {
261  // 3 x 10
263 
264  a(0, 0) = 1; a(0, 2) = 2; a(0, 9) = 1;
265  a(1, 1) = 3; a(1, 3) = 4;
266  a(2, 0) = 5;
267 
268  SparseMatrix<double> at(a);
269  at.Transpose();
270 
272 
273  t(0, 0) = 1; t(2, 0) = 2; t(9, 0) = 1;
274  t(1, 1) = 3; t(3, 1) = 4;
275  t(0, 2) = 5;
276 
277  CPPUNIT_ASSERT(t == at);
278 
279  at.Transpose();
280 
281  CPPUNIT_ASSERT(a == at);
282  }
283 
284 };
285 
286 
#define ASSERT_ITERATOR_EQUAL(it, e, r, c, v)
Stream & OutputSparse(Stream &s, const SparseMatrix< T > &m, const typename SparseMatrix< T >::IndexType rows, const typename SparseMatrix< T >::IndexType columns, const char separator, const SparseType &sparse)
Definition: SparseMatrix.h:346
constexpr double mm
Definition: AugerUnits.h:113
void TestNonSquareMultiplication2()
unsigned int GetNonEmptySize() const
Definition: SparseMatrix.h:123
void Transpose()
in-situ transpose
Definition: SparseMatrix.h:263
void TestNonSquareMultiplication()
Sparse container class for matrix data.
Definition: SparseMatrix.h:36
Iterator SparseEnd()
Definition: SparseMatrix.h:219
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
#define U
#define ASSERT_EQUAL(x, y)
Iterator Find(const IndexType row, const IndexType column)
Definition: SparseMatrix.h:223
constexpr double m
Definition: AugerUnits.h:121
Iterator SparseBegin()
Definition: SparseMatrix.h:215
unsigned int Reclaim()
Definition: SparseMatrix.h:163

, generated on Tue Sep 26 2023.