| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [6d608d] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [fc3b67] | 8 | /*
 | 
|---|
| [fff54f] | 9 |  * MatrixContentUnitTest.cpp
 | 
|---|
| [fc3b67] | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Jan 8, 2010
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [fc3b67] | 20 | using namespace std;
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 24 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 25 | 
 | 
|---|
| [cec7a5] | 26 | #include <cmath>
 | 
|---|
 | 27 | #include <limits>
 | 
|---|
 | 28 | 
 | 
|---|
| [29cbe9] | 29 | // include headers that implement a archive in simple text format
 | 
|---|
 | 30 | #include <boost/archive/text_oarchive.hpp>
 | 
|---|
 | 31 | #include <boost/archive/text_iarchive.hpp>
 | 
|---|
 | 32 | 
 | 
|---|
| [fff54f] | 33 | #include "MatrixContentUnitTest.hpp"
 | 
|---|
| [0d4424] | 34 | 
 | 
|---|
| [bf4b9f] | 35 | #include "MatrixContent.hpp"
 | 
|---|
| [cec7a5] | 36 | #include "VectorContent.hpp"
 | 
|---|
| [fc3b67] | 37 | 
 | 
|---|
| [9b6b2f] | 38 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 39 | #include "UnitTestMain.hpp"
 | 
|---|
 | 40 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 41 | 
 | 
|---|
| [fc3b67] | 42 | /********************************************** Test classes **************************************/
 | 
|---|
 | 43 | 
 | 
|---|
| [cec7a5] | 44 | const std::string matrixA = "   9.962848439806617e-01   5.950413028139907e-01   3.172650036465889e-01\n\
 | 
|---|
 | 45 |    2.070939109924951e-01   8.365712117773689e-01   3.980567886073226e-01\n\
 | 
|---|
 | 46 |    1.021600573071414e-01   8.061359922326598e-02   8.493537275043391e-01\n\
 | 
|---|
 | 47 |    6.955361514014282e-01   6.873206387346102e-02   2.649165458481005e-01";
 | 
|---|
 | 48 | const std::string vectorS = "1.630290761001098e+00                    7.624807744374841e-01                    6.374093742147465e-01";
 | 
|---|
 | 49 | 
 | 
|---|
| [fc3b67] | 50 | // Registers the fixture into the 'registry'
 | 
|---|
| [0d4424] | 51 | CPPUNIT_TEST_SUITE_REGISTRATION( MatrixContentTest );
 | 
|---|
| [fc3b67] | 52 | 
 | 
|---|
 | 53 | 
 | 
|---|
| [0d4424] | 54 | void MatrixContentTest::setUp()
 | 
|---|
| [fc3b67] | 55 | {
 | 
|---|
| [0d4424] | 56 |   m = new MatrixContent(4,3);
 | 
|---|
| [fc3b67] | 57 | };
 | 
|---|
 | 58 | 
 | 
|---|
| [0d4424] | 59 | void MatrixContentTest::tearDown()
 | 
|---|
| [fc3b67] | 60 | {
 | 
|---|
 | 61 |   delete(m);
 | 
|---|
 | 62 | };
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 | /** Unit Test for accessing matrix elements.
 | 
|---|
 | 65 |  *
 | 
|---|
 | 66 |  */
 | 
|---|
| [0d4424] | 67 | void MatrixContentTest::AccessTest()
 | 
|---|
| [fc3b67] | 68 | {
 | 
|---|
 | 69 |   // check whether all elements are initially zero
 | 
|---|
 | 70 |   for (int i=0;i<4;i++)
 | 
|---|
 | 71 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 72 |       CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) );
 | 
|---|
| [fc3b67] | 73 | 
 | 
|---|
 | 74 |   // set
 | 
|---|
 | 75 |   for (int i=0;i<4;i++)
 | 
|---|
 | 76 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 77 |       m->set(i,j, i*3+j );
 | 
|---|
| [fc3b67] | 78 | 
 | 
|---|
 | 79 |   // and check
 | 
|---|
 | 80 |   double *ptr = NULL;
 | 
|---|
 | 81 |   for (int i=0;i<4;i++)
 | 
|---|
 | 82 |     for (int j=0;j<3;j++) {
 | 
|---|
| [0d4424] | 83 |       CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->at(i,j) );
 | 
|---|
| [fc3b67] | 84 |       ptr = m->Pointer(i,j);
 | 
|---|
 | 85 |       CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
 | 
|---|
 | 86 |     }
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |   // assignment
 | 
|---|
 | 89 |   for (int i=0;i<4;i++)
 | 
|---|
 | 90 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 91 |       m->set(i,j, i*3+j );
 | 
|---|
 | 92 |   MatrixContent *dest = new MatrixContent(4,3);
 | 
|---|
| [fc3b67] | 93 |   *dest = *m;
 | 
|---|
 | 94 |   for (int i=0;i<4;i++)
 | 
|---|
 | 95 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 96 |       CPPUNIT_ASSERT_EQUAL( dest->at(i,j), m->at(i,j) );
 | 
|---|
| [fc3b67] | 97 |   delete(dest);
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 |   // out of bounds
 | 
|---|
| [0d4424] | 100 |   //CPPUNIT_ASSERT_EQUAL(0., v->at(5,2) );
 | 
|---|
 | 101 |   //CPPUNIT_ASSERT_EQUAL(0., v->at(2,17) );
 | 
|---|
 | 102 |   //CPPUNIT_ASSERT_EQUAL(0., v->at(1024,140040) );
 | 
|---|
 | 103 |   //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,0) );
 | 
|---|
 | 104 |   //CPPUNIT_ASSERT_EQUAL(0., v->at(0,-1) );
 | 
|---|
 | 105 |   //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,-1) );
 | 
|---|
| [fc3b67] | 106 | };
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | /** Unit Test for initializating matrices.
 | 
|---|
 | 109 |  *
 | 
|---|
 | 110 |  */
 | 
|---|
| [0d4424] | 111 | void MatrixContentTest::InitializationTest()
 | 
|---|
| [fc3b67] | 112 | {
 | 
|---|
 | 113 |   // set zero
 | 
|---|
| [0d4424] | 114 |   m->setZero();
 | 
|---|
| [fc3b67] | 115 |   for (int i=0;i<4;i++)
 | 
|---|
 | 116 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 117 |       CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) );
 | 
|---|
| [fc3b67] | 118 | 
 | 
|---|
 | 119 |   // set all
 | 
|---|
| [0d4424] | 120 |   m->setValue(1.5);
 | 
|---|
| [fc3b67] | 121 |   for (int i=0;i<4;i++)
 | 
|---|
 | 122 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 123 |       CPPUNIT_ASSERT_EQUAL( 1.5, m->at(i,j) );
 | 
|---|
| [fc3b67] | 124 | 
 | 
|---|
 | 125 |   // set basis
 | 
|---|
| [0d4424] | 126 |   m->setIdentity();
 | 
|---|
| [fc3b67] | 127 |   for (int i=0;i<4;i++)
 | 
|---|
 | 128 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 129 |       CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) );
 | 
|---|
| [fc3b67] | 130 | 
 | 
|---|
 | 131 |   // set from array
 | 
|---|
 | 132 |   double array[] = { 1., 0., 0.,
 | 
|---|
 | 133 |                      0., 1., 0.,
 | 
|---|
 | 134 |                      0., 0., 1.,
 | 
|---|
 | 135 |                      0., 0., 0. };
 | 
|---|
| [0d4424] | 136 |   m->setFromDoubleArray(array);
 | 
|---|
| [fc3b67] | 137 |   for (int i=0;i<4;i++)
 | 
|---|
 | 138 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 139 |       CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) );
 | 
|---|
| [fc3b67] | 140 | 
 | 
|---|
 | 141 | };
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | /** Unit Test for copying matrices.
 | 
|---|
 | 144 |  *
 | 
|---|
 | 145 |  */
 | 
|---|
| [0d4424] | 146 | void MatrixContentTest::CopyTest()
 | 
|---|
| [fc3b67] | 147 | {
 | 
|---|
 | 148 |   // set basis
 | 
|---|
| [0d4424] | 149 |   MatrixContent *dest = NULL;
 | 
|---|
| [fc3b67] | 150 |   for (int i=0;i<4;i++) {
 | 
|---|
| [0d4424] | 151 |     m->setValue(i);
 | 
|---|
 | 152 |     dest = new MatrixContent(m);
 | 
|---|
| [fc3b67] | 153 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 154 |       CPPUNIT_ASSERT_EQUAL( m->at(i,j) , dest->at(i,j) );
 | 
|---|
| [fc3b67] | 155 | 
 | 
|---|
 | 156 |     delete(dest);
 | 
|---|
 | 157 |   }
 | 
|---|
 | 158 | };
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 | /** Unit Test for exchanging rows and columns.
 | 
|---|
 | 161 |  *
 | 
|---|
 | 162 |  */
 | 
|---|
| [0d4424] | 163 | void MatrixContentTest::ExchangeTest()
 | 
|---|
| [fc3b67] | 164 | {
 | 
|---|
 | 165 |   // set to 1,1,1,2, ...
 | 
|---|
 | 166 |   for (int i=0;i<4;i++)
 | 
|---|
 | 167 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 168 |       m->set(i,j, i+1 );
 | 
|---|
| [fc3b67] | 169 | 
 | 
|---|
 | 170 |   // swap such that nothing happens
 | 
|---|
 | 171 |   CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(1,2) );
 | 
|---|
 | 172 |   for (int i=0;i<4;i++)
 | 
|---|
 | 173 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 174 |       CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) );
 | 
|---|
| [fc3b67] | 175 | 
 | 
|---|
 | 176 |   // swap two rows
 | 
|---|
 | 177 |   CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
 | 
|---|
 | 178 |   for (int i=0;i<4;i++)
 | 
|---|
 | 179 |     for (int j=0;j<3;j++)
 | 
|---|
 | 180 |       switch (i) {
 | 
|---|
 | 181 |         case 0:
 | 
|---|
| [0d4424] | 182 |           CPPUNIT_ASSERT_EQUAL( 1., m->at(i,j) );
 | 
|---|
| [fc3b67] | 183 |           break;
 | 
|---|
 | 184 |         case 1:
 | 
|---|
| [0d4424] | 185 |           CPPUNIT_ASSERT_EQUAL( 3., m->at(i,j) );
 | 
|---|
| [fc3b67] | 186 |           break;
 | 
|---|
 | 187 |         case 2:
 | 
|---|
| [0d4424] | 188 |           CPPUNIT_ASSERT_EQUAL( 2., m->at(i,j) );
 | 
|---|
| [fc3b67] | 189 |           break;
 | 
|---|
 | 190 |         case 3:
 | 
|---|
| [0d4424] | 191 |           CPPUNIT_ASSERT_EQUAL( 4., m->at(i,j) );
 | 
|---|
| [fc3b67] | 192 |           break;
 | 
|---|
 | 193 |         default:
 | 
|---|
| [0d4424] | 194 |           CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) );
 | 
|---|
| [68c923] | 195 |           break;
 | 
|---|
| [fc3b67] | 196 |       }
 | 
|---|
 | 197 |   // check that op is reversable
 | 
|---|
 | 198 |   CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
 | 
|---|
 | 199 |   for (int i=0;i<4;i++)
 | 
|---|
 | 200 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 201 |       CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) );
 | 
|---|
| [fc3b67] | 202 | 
 | 
|---|
 | 203 |   // set to 1,2,3,1, ...
 | 
|---|
 | 204 |   for (int i=0;i<4;i++)
 | 
|---|
 | 205 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 206 |       m->set(i,j, j+1. );
 | 
|---|
| [fc3b67] | 207 | 
 | 
|---|
 | 208 |   // swap such that nothing happens
 | 
|---|
 | 209 |   CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(0,2) );
 | 
|---|
 | 210 |   for (int i=0;i<4;i++)
 | 
|---|
 | 211 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 212 |       CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) );
 | 
|---|
| [fc3b67] | 213 | 
 | 
|---|
 | 214 |   // swap two columns
 | 
|---|
 | 215 |   CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
 | 
|---|
 | 216 |   for (int i=0;i<4;i++)
 | 
|---|
 | 217 |     for (int j=0;j<3;j++)
 | 
|---|
 | 218 |       switch (j) {
 | 
|---|
 | 219 |         case 0:
 | 
|---|
| [0d4424] | 220 |           CPPUNIT_ASSERT_EQUAL( 3., m->at(i,j) );
 | 
|---|
| [fc3b67] | 221 |           break;
 | 
|---|
 | 222 |         case 1:
 | 
|---|
| [0d4424] | 223 |           CPPUNIT_ASSERT_EQUAL( 2., m->at(i,j) );
 | 
|---|
| [fc3b67] | 224 |           break;
 | 
|---|
 | 225 |         case 2:
 | 
|---|
| [0d4424] | 226 |           CPPUNIT_ASSERT_EQUAL( 1., m->at(i,j) );
 | 
|---|
| [fc3b67] | 227 |           break;
 | 
|---|
 | 228 |         default:
 | 
|---|
| [0d4424] | 229 |           CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) );
 | 
|---|
| [68c923] | 230 |           break;
 | 
|---|
| [fc3b67] | 231 |       }
 | 
|---|
 | 232 |   // check that op is reversable
 | 
|---|
 | 233 |   CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
 | 
|---|
 | 234 |   for (int i=0;i<4;i++)
 | 
|---|
 | 235 |     for (int j=0;j<3;j++)
 | 
|---|
| [0d4424] | 236 |       CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) );
 | 
|---|
| [fc3b67] | 237 | 
 | 
|---|
 | 238 | 
 | 
|---|
 | 239 |   // set to 1,2,3,4, ...
 | 
|---|
| [0d4424] | 240 |   MatrixContent *n =  new MatrixContent(3,4);
 | 
|---|
| [fc3b67] | 241 |   for (int i=0;i<4;i++)
 | 
|---|
| [0d4424] | 242 |     for (int j=0;j<3;j++) {
 | 
|---|
 | 243 |       m->set(i,j, 3*i+j+1 );
 | 
|---|
 | 244 |       n->set(j,i, 3*i+j+1 );
 | 
|---|
 | 245 |     }
 | 
|---|
| [fc3b67] | 246 |   // transpose
 | 
|---|
| [26108c] | 247 |   MatrixContent res = (*m).transposed();
 | 
|---|
| [0d4424] | 248 |   CPPUNIT_ASSERT( *n == res );
 | 
|---|
| [fc3b67] | 249 |   // second transpose
 | 
|---|
| [26108c] | 250 |   MatrixContent res2 = res.transposed();
 | 
|---|
| [0d4424] | 251 |   CPPUNIT_ASSERT( *m == res2 );
 | 
|---|
| [bbf1bd] | 252 |   delete n;
 | 
|---|
| [fc3b67] | 253 | };
 | 
|---|
 | 254 | 
 | 
|---|
 | 255 | /** Unit Test for matrix properties.
 | 
|---|
 | 256 |  *
 | 
|---|
 | 257 |  */
 | 
|---|
| [0d4424] | 258 | void MatrixContentTest::PropertiesTest()
 | 
|---|
| [fc3b67] | 259 | {
 | 
|---|
 | 260 |   // is zero
 | 
|---|
| [0d4424] | 261 |   m->setZero();
 | 
|---|
| [fc3b67] | 262 |   CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
 | 
|---|
 | 263 |   CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
 | 
|---|
 | 264 |   CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
 | 
|---|
 | 265 |   CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
 | 
|---|
 | 266 | 
 | 
|---|
 | 267 |   // is positive
 | 
|---|
| [0d4424] | 268 |   m->setValue(0.5);
 | 
|---|
| [fc3b67] | 269 |   CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
 | 
|---|
 | 270 |   CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
 | 
|---|
 | 271 |   CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
 | 
|---|
 | 272 |   CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
 | 
|---|
 | 273 | 
 | 
|---|
 | 274 |   // is negative
 | 
|---|
| [0d4424] | 275 |   m->setValue(-0.1);
 | 
|---|
| [fc3b67] | 276 |   CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
 | 
|---|
 | 277 |   CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
 | 
|---|
 | 278 |   CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
 | 
|---|
 | 279 |   CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
 | 
|---|
 | 280 | 
 | 
|---|
 | 281 |   // is positive definite
 | 
|---|
 | 282 |   CPPUNIT_ASSERT_EQUAL( false, m->IsPositiveDefinite() );
 | 
|---|
 | 283 | };
 | 
|---|
| [6f68d6] | 284 | 
 | 
|---|
 | 285 | 
 | 
|---|
 | 286 | /** Unit test for reading from and writing matrix to stream
 | 
|---|
 | 287 |  *
 | 
|---|
 | 288 |  */
 | 
|---|
 | 289 | void MatrixContentTest::ReadWriteTest()
 | 
|---|
 | 290 | {
 | 
|---|
 | 291 |   // set up matrix
 | 
|---|
 | 292 |   for (int i=0;i<4;i++)
 | 
|---|
 | 293 |     for (int j=0;j<3;j++)
 | 
|---|
 | 294 |       m->set(i,j, i*3+j );
 | 
|---|
 | 295 | 
 | 
|---|
 | 296 |   // write to stream
 | 
|---|
 | 297 |   std::stringstream matrixstream;
 | 
|---|
 | 298 |   m->write(matrixstream);
 | 
|---|
 | 299 | 
 | 
|---|
 | 300 |   // parse in dimensions and check
 | 
|---|
 | 301 |   std::pair<size_t, size_t> matrixdimensions = MatrixContent::preparseMatrixDimensions(matrixstream);
 | 
|---|
 | 302 |   CPPUNIT_ASSERT_EQUAL( (size_t)4, matrixdimensions.first );
 | 
|---|
 | 303 |   CPPUNIT_ASSERT_EQUAL( (size_t)3, matrixdimensions.second );
 | 
|---|
 | 304 |   // parse in matrix and check
 | 
|---|
 | 305 |   MatrixContent* n = new MatrixContent(4,3, matrixstream);
 | 
|---|
 | 306 |   CPPUNIT_ASSERT_EQUAL( *m, *n );
 | 
|---|
 | 307 | 
 | 
|---|
 | 308 |   // free matrix
 | 
|---|
 | 309 |   delete n;
 | 
|---|
 | 310 | }
 | 
|---|
| [cec7a5] | 311 | 
 | 
|---|
 | 312 | /** Unit test for MatrixContent::performSingularValueDecomposition().
 | 
|---|
 | 313 |  *
 | 
|---|
 | 314 |  */
 | 
|---|
 | 315 | void MatrixContentTest::SVDTest()
 | 
|---|
 | 316 | {
 | 
|---|
 | 317 |   // setup "A", U,S,V
 | 
|---|
 | 318 |   std::stringstream Astream(matrixA);
 | 
|---|
 | 319 |   std::stringstream Sstream(vectorS);
 | 
|---|
 | 320 |   MatrixContent A(m->getRows(), m->getColumns(), Astream);
 | 
|---|
 | 321 |   VectorContent S_expected(m->getColumns(), Sstream);
 | 
|---|
 | 322 |   *m = A;
 | 
|---|
 | 323 | 
 | 
|---|
 | 324 |   // check SVD
 | 
|---|
 | 325 |   VectorContent S(m->getColumns());
 | 
|---|
 | 326 |   MatrixContent V(m->getColumns(), m->getColumns());
 | 
|---|
 | 327 |   A.performSingularValueDecomposition(V,S);
 | 
|---|
 | 328 |   MatrixContent S_diag(m->getColumns(), m->getColumns());
 | 
|---|
 | 329 |   for (size_t index = 0; index < m->getColumns(); ++index)
 | 
|---|
 | 330 |     S_diag.set(index, index, S[index]);
 | 
|---|
 | 331 |   CPPUNIT_ASSERT_EQUAL(*m,A*S_diag*(V.transpose()));
 | 
|---|
 | 332 |   for (size_t index = 0; index < m->getColumns(); ++index) {
 | 
|---|
 | 333 |     //CPPUNIT_ASSERT_EQUAL(S_expected[index], S[index]);
 | 
|---|
 | 334 |     CPPUNIT_ASSERT(fabs(S_expected[index] - S[index]) < numeric_limits<double>::epsilon()*10.);
 | 
|---|
 | 335 |   }
 | 
|---|
 | 336 | 
 | 
|---|
 | 337 |   // set up right-hand side
 | 
|---|
 | 338 |   VectorContent b(4);
 | 
|---|
 | 339 |   b[0] = 1.;
 | 
|---|
 | 340 |   b[1] = 0.;
 | 
|---|
 | 341 |   b[2] = 0.;
 | 
|---|
 | 342 |   b[3] = 0.;
 | 
|---|
 | 343 | 
 | 
|---|
 | 344 |   // SVD
 | 
|---|
 | 345 |   VectorContent x_expected(3);
 | 
|---|
 | 346 |   x_expected[0] = -0.00209169;
 | 
|---|
 | 347 |   x_expected[1] = -0.325399;
 | 
|---|
 | 348 |   x_expected[2] = 0.628004;
 | 
|---|
 | 349 |   const VectorContent x_result(A.solveOverdeterminedLinearEquation(V,S,b));
 | 
|---|
 | 350 | 
 | 
|---|
 | 351 |   // check result
 | 
|---|
 | 352 |   for (size_t index = 0; index < m->getColumns(); ++index) {
 | 
|---|
 | 353 |     CPPUNIT_ASSERT(fabs(x_expected[index] - x_result[index]) < 1.e-6);
 | 
|---|
 | 354 |   }
 | 
|---|
 | 355 | }
 | 
|---|
| [29cbe9] | 356 | 
 | 
|---|
 | 357 | /** Unit test for serialization
 | 
|---|
 | 358 |  *
 | 
|---|
 | 359 |  */
 | 
|---|
 | 360 | void MatrixContentTest::SerializationTest()
 | 
|---|
 | 361 | {
 | 
|---|
 | 362 |   m->setZero();
 | 
|---|
 | 363 |   int count=0;
 | 
|---|
 | 364 |   for (size_t x = 0; x < 4 ; ++x)
 | 
|---|
 | 365 |     for (size_t y = 0; y < 3 ; ++y)
 | 
|---|
 | 366 |       m->at(x,y) = count++;
 | 
|---|
 | 367 |   // write element to stream
 | 
|---|
 | 368 |   std::stringstream stream;
 | 
|---|
 | 369 |   boost::archive::text_oarchive oa(stream);
 | 
|---|
 | 370 |   oa << m;
 | 
|---|
 | 371 | 
 | 
|---|
 | 372 |   //std::cout << "Contents of archive is " << stream.str() << std::endl;
 | 
|---|
 | 373 | 
 | 
|---|
 | 374 |   // create and open an archive for input
 | 
|---|
 | 375 |   boost::archive::text_iarchive ia(stream);
 | 
|---|
 | 376 |   // read class state from archive
 | 
|---|
 | 377 |   MatrixContent *newm;
 | 
|---|
 | 378 | 
 | 
|---|
 | 379 |   ia >> newm;
 | 
|---|
 | 380 | 
 | 
|---|
 | 381 |   CPPUNIT_ASSERT (*m == *newm);
 | 
|---|
 | 382 | }
 | 
|---|