[5630bd] | 1 | /*
|
---|
| 2 | * MatrixUnittest.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jul 7, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[5630bd] | 13 | #include <cppunit/CompilerOutputter.h>
|
---|
| 14 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 15 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 16 |
|
---|
| 17 | #include "MatrixUnittest.hpp"
|
---|
[57f243] | 18 | #include "LinearAlgebra/Matrix.hpp"
|
---|
| 19 | #include "LinearAlgebra/Vector.hpp"
|
---|
[5630bd] | 20 | #include "Exceptions/NotInvertibleException.hpp"
|
---|
| 21 |
|
---|
| 22 | #ifdef HAVE_TESTRUNNER
|
---|
| 23 | #include "UnitTestMain.hpp"
|
---|
| 24 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 25 |
|
---|
| 26 | // Registers the fixture into the 'registry'
|
---|
| 27 | CPPUNIT_TEST_SUITE_REGISTRATION( MatrixUnittest );
|
---|
| 28 |
|
---|
| 29 | void MatrixUnittest::setUp(){
|
---|
| 30 | zero = new Matrix();
|
---|
| 31 | one = new Matrix();
|
---|
| 32 | for(int i =NDIM;i--;){
|
---|
| 33 | one->at(i,i)=1.;
|
---|
| 34 | }
|
---|
| 35 | full=new Matrix();
|
---|
| 36 | for(int i=NDIM;i--;){
|
---|
| 37 | for(int j=NDIM;j--;){
|
---|
| 38 | full->at(i,j)=1.;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | diagonal = new Matrix();
|
---|
| 42 | for(int i=NDIM;i--;){
|
---|
| 43 | diagonal->at(i,i)=i+1.;
|
---|
| 44 | }
|
---|
| 45 | perm1 = new Matrix();
|
---|
| 46 | perm1->column(0) = e1;
|
---|
| 47 | perm1->column(1) = e3;
|
---|
| 48 | perm1->column(2) = e2;
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | perm2 = new Matrix();
|
---|
| 52 | perm2->column(0) = e2;
|
---|
| 53 | perm2->column(1) = e1;
|
---|
| 54 | perm2->column(2) = e3;
|
---|
| 55 |
|
---|
| 56 | perm3 = new Matrix();
|
---|
| 57 | perm3->column(0) = e2;
|
---|
| 58 | perm3->column(1) = e3;
|
---|
| 59 | perm3->column(2) = e1;
|
---|
| 60 |
|
---|
| 61 | perm4 = new Matrix();
|
---|
| 62 | perm4->column(0) = e3;
|
---|
| 63 | perm4->column(1) = e2;
|
---|
| 64 | perm4->column(2) = e1;
|
---|
| 65 |
|
---|
| 66 | perm5 = new Matrix();
|
---|
| 67 | perm5->column(0) = e3;
|
---|
| 68 | perm5->column(1) = e1;
|
---|
| 69 | perm5->column(2) = e2;
|
---|
| 70 |
|
---|
| 71 | }
|
---|
| 72 | void MatrixUnittest::tearDown(){
|
---|
| 73 | delete zero;
|
---|
| 74 | delete one;
|
---|
| 75 | delete full;
|
---|
| 76 | delete diagonal;
|
---|
| 77 | delete perm1;
|
---|
| 78 | delete perm2;
|
---|
| 79 | delete perm3;
|
---|
| 80 | delete perm4;
|
---|
| 81 | delete perm5;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void MatrixUnittest::AccessTest(){
|
---|
| 85 | Matrix mat;
|
---|
| 86 | for(int i=NDIM;i--;){
|
---|
| 87 | for(int j=NDIM;j--;){
|
---|
| 88 | CPPUNIT_ASSERT_EQUAL(mat.at(i,j),0.);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | int k=1;
|
---|
| 92 | for(int i=NDIM;i--;){
|
---|
| 93 | for(int j=NDIM;j--;){
|
---|
| 94 | mat.at(i,j)=k++;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | k=1;
|
---|
| 98 | for(int i=NDIM;i--;){
|
---|
| 99 | for(int j=NDIM;j--;){
|
---|
| 100 | CPPUNIT_ASSERT_EQUAL(mat.at(i,j),(double)k);
|
---|
| 101 | ++k;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void MatrixUnittest::VectorTest(){
|
---|
| 107 | Matrix mat;
|
---|
| 108 | for(int i=NDIM;i--;){
|
---|
| 109 | CPPUNIT_ASSERT_EQUAL(mat.row(i),zeroVec);
|
---|
| 110 | CPPUNIT_ASSERT_EQUAL(mat.column(i),zeroVec);
|
---|
| 111 | }
|
---|
| 112 | CPPUNIT_ASSERT_EQUAL(mat.diagonal(),zeroVec);
|
---|
| 113 |
|
---|
| 114 | mat.one();
|
---|
| 115 | CPPUNIT_ASSERT_EQUAL(mat.row(0),e1);
|
---|
| 116 | CPPUNIT_ASSERT_EQUAL(mat.row(1),e2);
|
---|
| 117 | CPPUNIT_ASSERT_EQUAL(mat.row(2),e3);
|
---|
| 118 | CPPUNIT_ASSERT_EQUAL(mat.column(0),e1);
|
---|
| 119 | CPPUNIT_ASSERT_EQUAL(mat.column(1),e2);
|
---|
| 120 | CPPUNIT_ASSERT_EQUAL(mat.column(2),e3);
|
---|
| 121 |
|
---|
| 122 | Vector t1=Vector(1.,1.,1.);
|
---|
| 123 | Vector t2=Vector(2.,2.,2.);
|
---|
| 124 | Vector t3=Vector(3.,3.,3.);
|
---|
| 125 | Vector t4=Vector(1.,2.,3.);
|
---|
| 126 |
|
---|
| 127 | mat.row(0)=t1;
|
---|
| 128 | mat.row(1)=t2;
|
---|
| 129 | mat.row(2)=t3;
|
---|
| 130 | CPPUNIT_ASSERT_EQUAL(mat.row(0),t1);
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL(mat.row(1),t2);
|
---|
| 132 | CPPUNIT_ASSERT_EQUAL(mat.row(2),t3);
|
---|
| 133 | CPPUNIT_ASSERT_EQUAL(mat.column(0),t4);
|
---|
| 134 | CPPUNIT_ASSERT_EQUAL(mat.column(1),t4);
|
---|
| 135 | CPPUNIT_ASSERT_EQUAL(mat.column(2),t4);
|
---|
| 136 | CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
|
---|
| 137 | for(int i=NDIM;i--;){
|
---|
| 138 | for(int j=NDIM;j--;){
|
---|
| 139 | CPPUNIT_ASSERT_EQUAL(mat.at(i,j),i+1.);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | mat.column(0)=t1;
|
---|
| 144 | mat.column(1)=t2;
|
---|
| 145 | mat.column(2)=t3;
|
---|
| 146 | CPPUNIT_ASSERT_EQUAL(mat.column(0),t1);
|
---|
| 147 | CPPUNIT_ASSERT_EQUAL(mat.column(1),t2);
|
---|
| 148 | CPPUNIT_ASSERT_EQUAL(mat.column(2),t3);
|
---|
| 149 | CPPUNIT_ASSERT_EQUAL(mat.row(0),t4);
|
---|
| 150 | CPPUNIT_ASSERT_EQUAL(mat.row(1),t4);
|
---|
| 151 | CPPUNIT_ASSERT_EQUAL(mat.row(2),t4);
|
---|
| 152 | CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
|
---|
| 153 | for(int i=NDIM;i--;){
|
---|
| 154 | for(int j=NDIM;j--;){
|
---|
| 155 | CPPUNIT_ASSERT_EQUAL(mat.at(i,j),j+1.);
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | void MatrixUnittest::OperationTest(){
|
---|
| 161 | Matrix res;
|
---|
| 162 |
|
---|
| 163 | res =(*zero) *(*zero);
|
---|
| 164 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 165 | res =(*zero) *(*one);
|
---|
| 166 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 167 | res =(*zero) *(*full);
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 169 | res =(*zero) *(*diagonal);
|
---|
| 170 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 171 | res =(*zero) *(*perm1);
|
---|
| 172 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 173 | res =(*zero) *(*perm2);
|
---|
| 174 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 175 | res =(*zero) *(*perm3);
|
---|
| 176 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 177 | res =(*zero) *(*perm4);
|
---|
| 178 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 179 | res =(*zero) *(*perm5);
|
---|
| 180 | CPPUNIT_ASSERT_EQUAL(res,*zero);
|
---|
| 181 |
|
---|
| 182 | res =(*one)*(*one);
|
---|
| 183 | CPPUNIT_ASSERT_EQUAL(res,*one);
|
---|
| 184 | res =(*one)*(*full);
|
---|
| 185 | CPPUNIT_ASSERT_EQUAL(res,*full);
|
---|
| 186 | res =(*one)*(*diagonal);
|
---|
| 187 | CPPUNIT_ASSERT_EQUAL(res,*diagonal);
|
---|
| 188 | res =(*one)*(*perm1);
|
---|
| 189 | CPPUNIT_ASSERT_EQUAL(res,*perm1);
|
---|
| 190 | res =(*one)*(*perm2);
|
---|
| 191 | CPPUNIT_ASSERT_EQUAL(res,*perm2);
|
---|
| 192 | res =(*one)*(*perm3);
|
---|
| 193 | CPPUNIT_ASSERT_EQUAL(res,*perm3);
|
---|
| 194 | res =(*one)*(*perm4);
|
---|
| 195 | CPPUNIT_ASSERT_EQUAL(res,*perm4);
|
---|
| 196 | res =(*one)*(*perm5);
|
---|
| 197 | CPPUNIT_ASSERT_EQUAL(res,*perm5);
|
---|
| 198 |
|
---|
| 199 | res = (*full)*(*perm1);
|
---|
| 200 | CPPUNIT_ASSERT_EQUAL(res,*full);
|
---|
| 201 | res = (*full)*(*perm2);
|
---|
| 202 | CPPUNIT_ASSERT_EQUAL(res,*full);
|
---|
| 203 | res = (*full)*(*perm3);
|
---|
| 204 | CPPUNIT_ASSERT_EQUAL(res,*full);
|
---|
| 205 | res = (*full)*(*perm4);
|
---|
| 206 | CPPUNIT_ASSERT_EQUAL(res,*full);
|
---|
| 207 | res = (*full)*(*perm5);
|
---|
| 208 | CPPUNIT_ASSERT_EQUAL(res,*full);
|
---|
| 209 |
|
---|
| 210 | res = (*diagonal)*(*perm1);
|
---|
| 211 | CPPUNIT_ASSERT_EQUAL(res.column(0),e1);
|
---|
| 212 | CPPUNIT_ASSERT_EQUAL(res.column(1),3*e3);
|
---|
| 213 | CPPUNIT_ASSERT_EQUAL(res.column(2),2*e2);
|
---|
| 214 | res = (*diagonal)*(*perm2);
|
---|
| 215 | CPPUNIT_ASSERT_EQUAL(res.column(0),2*e2);
|
---|
| 216 | CPPUNIT_ASSERT_EQUAL(res.column(1),e1);
|
---|
| 217 | CPPUNIT_ASSERT_EQUAL(res.column(2),3*e3);
|
---|
| 218 | res = (*diagonal)*(*perm3);
|
---|
| 219 | CPPUNIT_ASSERT_EQUAL(res.column(0),2*e2);
|
---|
| 220 | CPPUNIT_ASSERT_EQUAL(res.column(1),3*e3);
|
---|
| 221 | CPPUNIT_ASSERT_EQUAL(res.column(2),e1);
|
---|
| 222 | res = (*diagonal)*(*perm4);
|
---|
| 223 | CPPUNIT_ASSERT_EQUAL(res.column(0),3*e3);
|
---|
| 224 | CPPUNIT_ASSERT_EQUAL(res.column(1),2*e2);
|
---|
| 225 | CPPUNIT_ASSERT_EQUAL(res.column(2),e1);
|
---|
| 226 | res = (*diagonal)*(*perm5);
|
---|
| 227 | CPPUNIT_ASSERT_EQUAL(res.column(0),3*e3);
|
---|
| 228 | CPPUNIT_ASSERT_EQUAL(res.column(1),e1);
|
---|
| 229 | CPPUNIT_ASSERT_EQUAL(res.column(2),2*e2);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 | void MatrixUnittest::InvertTest(){
|
---|
| 234 | CPPUNIT_ASSERT_THROW(zero->invert(),NotInvertibleException);
|
---|
| 235 | CPPUNIT_ASSERT_THROW(full->invert(),NotInvertibleException);
|
---|
| 236 |
|
---|
| 237 | Matrix res;
|
---|
| 238 | res = (*one)*one->invert();
|
---|
| 239 | CPPUNIT_ASSERT_EQUAL(res,*one);
|
---|
| 240 | res = (*diagonal)*diagonal->invert();
|
---|
| 241 | CPPUNIT_ASSERT_EQUAL(res,*one);
|
---|
| 242 | res = (*perm1)*perm1->invert();
|
---|
| 243 | CPPUNIT_ASSERT_EQUAL(res,*one);
|
---|
| 244 | res = (*perm2)*perm2->invert();
|
---|
| 245 | CPPUNIT_ASSERT_EQUAL(res,*one);
|
---|
| 246 | res = (*perm3)*perm3->invert();
|
---|
| 247 | CPPUNIT_ASSERT_EQUAL(res,*one);
|
---|
| 248 | res = (*perm4)*perm4->invert();
|
---|
| 249 | CPPUNIT_ASSERT_EQUAL(res,*one);
|
---|
| 250 | res = (*perm5)*perm5->invert();
|
---|
| 251 | CPPUNIT_ASSERT_EQUAL(res,*one);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 | void MatrixUnittest::DeterminantTest(){
|
---|
| 256 | CPPUNIT_ASSERT_EQUAL(zero->determinant(),0.);
|
---|
| 257 | CPPUNIT_ASSERT_EQUAL(one->determinant(),1.);
|
---|
| 258 | CPPUNIT_ASSERT_EQUAL(diagonal->determinant(),6.);
|
---|
| 259 | CPPUNIT_ASSERT_EQUAL(full->determinant(),0.);
|
---|
| 260 | CPPUNIT_ASSERT_EQUAL(perm1->determinant(),-1.);
|
---|
| 261 | CPPUNIT_ASSERT_EQUAL(perm2->determinant(),-1.);
|
---|
| 262 | CPPUNIT_ASSERT_EQUAL(perm3->determinant(),1.);
|
---|
| 263 | CPPUNIT_ASSERT_EQUAL(perm4->determinant(),-1.);
|
---|
| 264 | CPPUNIT_ASSERT_EQUAL(perm5->determinant(),1.);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | void MatrixUnittest::VecMultTest(){
|
---|
| 268 | CPPUNIT_ASSERT_EQUAL((*zero)*e1,zeroVec);
|
---|
| 269 | CPPUNIT_ASSERT_EQUAL((*zero)*e2,zeroVec);
|
---|
| 270 | CPPUNIT_ASSERT_EQUAL((*zero)*e3,zeroVec);
|
---|
| 271 | CPPUNIT_ASSERT_EQUAL((*zero)*zeroVec,zeroVec);
|
---|
| 272 |
|
---|
| 273 | CPPUNIT_ASSERT_EQUAL((*one)*e1,e1);
|
---|
| 274 | CPPUNIT_ASSERT_EQUAL((*one)*e2,e2);
|
---|
| 275 | CPPUNIT_ASSERT_EQUAL((*one)*e3,e3);
|
---|
| 276 | CPPUNIT_ASSERT_EQUAL((*one)*zeroVec,zeroVec);
|
---|
| 277 |
|
---|
| 278 | CPPUNIT_ASSERT_EQUAL((*diagonal)*e1,e1);
|
---|
| 279 | CPPUNIT_ASSERT_EQUAL((*diagonal)*e2,2*e2);
|
---|
| 280 | CPPUNIT_ASSERT_EQUAL((*diagonal)*e3,3*e3);
|
---|
| 281 | CPPUNIT_ASSERT_EQUAL((*diagonal)*zeroVec,zeroVec);
|
---|
| 282 |
|
---|
| 283 | CPPUNIT_ASSERT_EQUAL((*perm1)*e1,e1);
|
---|
| 284 | CPPUNIT_ASSERT_EQUAL((*perm1)*e2,e3);
|
---|
| 285 | CPPUNIT_ASSERT_EQUAL((*perm1)*e3,e2);
|
---|
| 286 | CPPUNIT_ASSERT_EQUAL((*perm1)*zeroVec,zeroVec);
|
---|
| 287 |
|
---|
| 288 | CPPUNIT_ASSERT_EQUAL((*perm2)*e1,e2);
|
---|
| 289 | CPPUNIT_ASSERT_EQUAL((*perm2)*e2,e1);
|
---|
| 290 | CPPUNIT_ASSERT_EQUAL((*perm2)*e3,e3);
|
---|
| 291 | CPPUNIT_ASSERT_EQUAL((*perm2)*zeroVec,zeroVec);
|
---|
| 292 |
|
---|
| 293 | CPPUNIT_ASSERT_EQUAL((*perm3)*e1,e2);
|
---|
| 294 | CPPUNIT_ASSERT_EQUAL((*perm3)*e2,e3);
|
---|
| 295 | CPPUNIT_ASSERT_EQUAL((*perm3)*e3,e1);
|
---|
| 296 | CPPUNIT_ASSERT_EQUAL((*perm3)*zeroVec,zeroVec);
|
---|
| 297 |
|
---|
| 298 | CPPUNIT_ASSERT_EQUAL((*perm4)*e1,e3);
|
---|
| 299 | CPPUNIT_ASSERT_EQUAL((*perm4)*e2,e2);
|
---|
| 300 | CPPUNIT_ASSERT_EQUAL((*perm4)*e3,e1);
|
---|
| 301 | CPPUNIT_ASSERT_EQUAL((*perm4)*zeroVec,zeroVec);
|
---|
| 302 |
|
---|
| 303 | CPPUNIT_ASSERT_EQUAL((*perm5)*e1,e3);
|
---|
| 304 | CPPUNIT_ASSERT_EQUAL((*perm5)*e2,e1);
|
---|
| 305 | CPPUNIT_ASSERT_EQUAL((*perm5)*e3,e2);
|
---|
| 306 | CPPUNIT_ASSERT_EQUAL((*perm5)*zeroVec,zeroVec);
|
---|
| 307 |
|
---|
| 308 | Vector t = Vector(1.,2.,3.);
|
---|
| 309 | CPPUNIT_ASSERT_EQUAL((*perm1)*t,Vector(1,3,2));
|
---|
| 310 | CPPUNIT_ASSERT_EQUAL((*perm2)*t,Vector(2,1,3));
|
---|
| 311 | CPPUNIT_ASSERT_EQUAL((*perm3)*t,Vector(3,1,2));
|
---|
| 312 | CPPUNIT_ASSERT_EQUAL((*perm4)*t,Vector(3,2,1));
|
---|
| 313 | CPPUNIT_ASSERT_EQUAL((*perm5)*t,Vector(2,3,1));
|
---|
| 314 | }
|
---|