1 | /*
|
---|
2 | * gslmatrixunittest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jan 8, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | #include <cppunit/CompilerOutputter.h>
|
---|
11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
13 |
|
---|
14 | #include "gslmatrixsymmetricunittest.hpp"
|
---|
15 |
|
---|
16 | #ifdef HAVE_TESTRUNNER
|
---|
17 | #include "UnitTestMain.hpp"
|
---|
18 | #endif /*HAVE_TESTRUNNER*/
|
---|
19 |
|
---|
20 | /********************************************** Test classes **************************************/
|
---|
21 |
|
---|
22 | // Registers the fixture into the 'registry'
|
---|
23 | CPPUNIT_TEST_SUITE_REGISTRATION( GSLMatrixSymmetricTest );
|
---|
24 |
|
---|
25 |
|
---|
26 | void GSLMatrixSymmetricTest::setUp()
|
---|
27 | {
|
---|
28 | m = new GSLMatrix(3,3);
|
---|
29 | };
|
---|
30 |
|
---|
31 | void GSLMatrixSymmetricTest::tearDown()
|
---|
32 | {
|
---|
33 | delete(m);
|
---|
34 | };
|
---|
35 |
|
---|
36 | /** Unit Test for accessing matrix elements.
|
---|
37 | *
|
---|
38 | */
|
---|
39 | void GSLMatrixSymmetricTest::AccessTest()
|
---|
40 | {
|
---|
41 | // check whether all elements are initially zero
|
---|
42 | for (int i=0;i<3;i++)
|
---|
43 | for (int j=0;j<3;j++)
|
---|
44 | CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
|
---|
45 |
|
---|
46 | // set
|
---|
47 | for (int i=0;i<3;i++)
|
---|
48 | for (int j=0;j<3;j++)
|
---|
49 | m->Set(i,j, i*3+j );
|
---|
50 |
|
---|
51 | // and check
|
---|
52 | double *ptr = NULL;
|
---|
53 | for (int i=0;i<3;i++)
|
---|
54 | for (int j=0;j<3;j++) {
|
---|
55 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->Get(i,j) );
|
---|
56 | ptr = m->Pointer(i,j);
|
---|
57 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
|
---|
58 | }
|
---|
59 |
|
---|
60 | // assignment
|
---|
61 | for (int i=0;i<3;i++)
|
---|
62 | for (int j=0;j<3;j++)
|
---|
63 | m->Set(i,j, i*3+j );
|
---|
64 | GSLMatrix *dest = new GSLMatrix(3,3);
|
---|
65 | *dest = *m;
|
---|
66 | for (int i=0;i<3;i++)
|
---|
67 | for (int j=0;j<3;j++)
|
---|
68 | CPPUNIT_ASSERT_EQUAL( dest->Get(i,j), m->Get(i,j) );
|
---|
69 | delete(dest);
|
---|
70 |
|
---|
71 | // out of bounds
|
---|
72 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(4,2) );
|
---|
73 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(2,17) );
|
---|
74 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(1024,140040) );
|
---|
75 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,0) );
|
---|
76 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(0,-1) );
|
---|
77 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,-1) );
|
---|
78 | };
|
---|
79 |
|
---|
80 | /** Unit Test for initializating matrices.
|
---|
81 | *
|
---|
82 | */
|
---|
83 | void GSLMatrixSymmetricTest::InitializationTest()
|
---|
84 | {
|
---|
85 | // set zero
|
---|
86 | m->SetZero();
|
---|
87 | for (int i=0;i<3;i++)
|
---|
88 | for (int j=0;j<3;j++)
|
---|
89 | CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
|
---|
90 |
|
---|
91 | // set all
|
---|
92 | m->SetAll(1.5);
|
---|
93 | for (int i=0;i<3;i++)
|
---|
94 | for (int j=0;j<3;j++)
|
---|
95 | CPPUNIT_ASSERT_EQUAL( 1.5, m->Get(i,j) );
|
---|
96 |
|
---|
97 | // set basis
|
---|
98 | m->SetIdentity();
|
---|
99 | for (int i=0;i<3;i++)
|
---|
100 | for (int j=0;j<3;j++)
|
---|
101 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
|
---|
102 |
|
---|
103 | // set from array
|
---|
104 | double array[] = { 1., 0., 0.,
|
---|
105 | 0., 1., 0.,
|
---|
106 | 0., 0., 1. };
|
---|
107 | m->SetFromDoubleArray(array);
|
---|
108 | for (int i=0;i<3;i++)
|
---|
109 | for (int j=0;j<3;j++)
|
---|
110 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
|
---|
111 |
|
---|
112 | };
|
---|
113 |
|
---|
114 | /** Unit Test for copying matrices.
|
---|
115 | *
|
---|
116 | */
|
---|
117 | void GSLMatrixSymmetricTest::CopyTest()
|
---|
118 | {
|
---|
119 | // set basis
|
---|
120 | GSLMatrix *dest = NULL;
|
---|
121 | for (int i=0;i<3;i++) {
|
---|
122 | m->SetAll(i);
|
---|
123 | dest = new GSLMatrix(m);
|
---|
124 | for (int j=0;j<3;j++)
|
---|
125 | CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) );
|
---|
126 |
|
---|
127 | delete(dest);
|
---|
128 | }
|
---|
129 | };
|
---|
130 |
|
---|
131 | /** Unit Test for exchanging rows and columns.
|
---|
132 | *
|
---|
133 | */
|
---|
134 | void GSLMatrixSymmetricTest::ExchangeTest()
|
---|
135 | {
|
---|
136 | // set to 1,1,1,2, ...
|
---|
137 | for (int i=0;i<3;i++)
|
---|
138 | for (int j=0;j<3;j++)
|
---|
139 | m->Set(i,j, i+1 );
|
---|
140 |
|
---|
141 | // swap such that nothing happens
|
---|
142 | m->SwapColumns(1,2);
|
---|
143 | for (int i=0;i<3;i++)
|
---|
144 | for (int j=0;j<3;j++)
|
---|
145 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
|
---|
146 |
|
---|
147 | // swap two rows
|
---|
148 | m->SwapRows(1,2);
|
---|
149 | for (int i=0;i<3;i++)
|
---|
150 | for (int j=0;j<3;j++)
|
---|
151 | switch (j) {
|
---|
152 | case 0:
|
---|
153 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(j,i) );
|
---|
154 | break;
|
---|
155 | case 1:
|
---|
156 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(j,i) );
|
---|
157 | break;
|
---|
158 | case 2:
|
---|
159 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(j,i) );
|
---|
160 | break;
|
---|
161 | default:
|
---|
162 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
|
---|
163 | }
|
---|
164 | // check that op is reversable
|
---|
165 | m->SwapRows(1,2);
|
---|
166 | for (int i=0;i<3;i++)
|
---|
167 | for (int j=0;j<3;j++)
|
---|
168 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
|
---|
169 |
|
---|
170 | // set to 1,2,3,1, ...
|
---|
171 | for (int i=0;i<3;i++)
|
---|
172 | for (int j=0;j<3;j++)
|
---|
173 | m->Set(i,j, j+1. );
|
---|
174 |
|
---|
175 | // swap such that nothing happens
|
---|
176 | m->SwapRows(0,2);
|
---|
177 | for (int i=0;i<3;i++)
|
---|
178 | for (int j=0;j<3;j++)
|
---|
179 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
|
---|
180 |
|
---|
181 | // swap two columns
|
---|
182 | m->SwapColumns(0,2);
|
---|
183 | for (int i=0;i<3;i++)
|
---|
184 | for (int j=0;j<3;j++)
|
---|
185 | switch (j) {
|
---|
186 | case 0:
|
---|
187 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
|
---|
188 | break;
|
---|
189 | case 1:
|
---|
190 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
|
---|
191 | break;
|
---|
192 | case 2:
|
---|
193 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
|
---|
194 | break;
|
---|
195 | default:
|
---|
196 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
|
---|
197 | }
|
---|
198 | // check that op is reversable
|
---|
199 | m->SwapColumns(0,2);
|
---|
200 | for (int i=0;i<3;i++)
|
---|
201 | for (int j=0;j<3;j++)
|
---|
202 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
|
---|
203 |
|
---|
204 |
|
---|
205 | // set to 1,2,3,4, ...
|
---|
206 | for (int i=0;i<3;i++)
|
---|
207 | for (int j=0;j<3;j++)
|
---|
208 | m->Set(i,j, 3*i+j+1 );
|
---|
209 | // transpose
|
---|
210 | m->Transpose();
|
---|
211 | for (int i=0;i<3;i++)
|
---|
212 | for (int j=0;j<3;j++)
|
---|
213 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) );
|
---|
214 | // second transpose
|
---|
215 | m->Transpose();
|
---|
216 | for (int i=0;i<3;i++)
|
---|
217 | for (int j=0;j<3;j++)
|
---|
218 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) );
|
---|
219 | };
|
---|
220 |
|
---|
221 | /** Unit Test for matrix properties.
|
---|
222 | *
|
---|
223 | */
|
---|
224 | void GSLMatrixSymmetricTest::PropertiesTest()
|
---|
225 | {
|
---|
226 | // is zero
|
---|
227 | m->SetZero();
|
---|
228 | CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
|
---|
229 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
|
---|
230 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
|
---|
231 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
|
---|
232 |
|
---|
233 | // is positive
|
---|
234 | m->SetAll(0.5);
|
---|
235 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
|
---|
236 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
|
---|
237 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
|
---|
238 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
|
---|
239 |
|
---|
240 | // is negative
|
---|
241 | m->SetAll(-0.1);
|
---|
242 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
|
---|
243 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
|
---|
244 | CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
|
---|
245 | CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
|
---|
246 |
|
---|
247 | // is positive definite
|
---|
248 | double array[] = { 1., 0., 0.,
|
---|
249 | 0., 1., 1.,
|
---|
250 | 0., 0., 1. };
|
---|
251 | m->SetFromDoubleArray(array);
|
---|
252 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositiveDefinite() );
|
---|
253 |
|
---|
254 | //determinant
|
---|
255 | m->SetIdentity();
|
---|
256 | CPPUNIT_ASSERT_EQUAL( 1., m->Determinant() );
|
---|
257 |
|
---|
258 | m->SetZero();
|
---|
259 | CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
|
---|
260 |
|
---|
261 | m->Set( 0, 0, 1.);
|
---|
262 | m->Set( 1, 1, 1.);
|
---|
263 | m->Set( 2, 1, 1.);
|
---|
264 | CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
|
---|
265 |
|
---|
266 | double array2[] = { 2., 0., 1.,
|
---|
267 | -3., 1., 1.,
|
---|
268 | 1., 5.5, 1. };
|
---|
269 | m->SetFromDoubleArray(array2);
|
---|
270 | CPPUNIT_ASSERT_EQUAL( -26.5, m->Determinant() );
|
---|
271 | };
|
---|