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