matrixwrapper_test.cpp
Go to the documentation of this file.
00001 // Copyright (C) 2007 Wim Meeussen <wim.meeussen@mech.kuleuven.be>
00002 //                    Tinne De Laet<first DOT last AT mech.kuleuven.be>
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 //
00018 
00019 
00020 #include "matrixwrapper_test.hpp"
00021 #include "approxEqual.hpp"
00022 
00023 
00024 // Registers the fixture into the 'registry'
00025 CPPUNIT_TEST_SUITE_REGISTRATION( MatrixwrapperTest );
00026 
00027 using namespace MatrixWrapper;
00028 
00029 
00030 void
00031 MatrixwrapperTest::setUp()
00032 {
00033 }
00034 
00035 
00036 void
00037 MatrixwrapperTest::tearDown()
00038 {
00039 }
00040 
00041 void
00042 MatrixwrapperTest::testMatrixwrapperValue()
00043 {
00044   double epsilon = 0.00001;
00045 
00046   unsigned int one = 1;
00047   unsigned int r = 4;
00048   unsigned int c = 3;
00049 
00050   // reference numbers
00051   vector< vector<double> > REF;
00052   for (unsigned int i=0; i<r+c; i++){
00053     vector<double> row;
00054     for (unsigned int j=0; j<r+c; j++)
00055       row.push_back( (r+c)*i+j );
00056     REF.push_back(row);
00057   }
00058 
00059   // TEST DIMENSIONS
00060   //
00061   // TEST DIMENSIONS  MATRIX
00062   Matrix Am(r,c);
00063   // rows()
00064   CPPUNIT_ASSERT_EQUAL(Am.rows(), r);
00065   // columns()
00066   CPPUNIT_ASSERT_EQUAL(Am.columns(), c);
00067   // TEST DIMENSIONS SYMMETRICMATRIX
00068   SymmetricMatrix As(r);
00069   // rows()
00070   CPPUNIT_ASSERT_EQUAL(As.rows(), r);
00071   // columns()
00072   CPPUNIT_ASSERT_EQUAL(As.columns(), r);
00073   // TEST DIMENSIONS COLUMNVECTOR
00074   ColumnVector Ac(r);
00075   // rows()
00076   CPPUNIT_ASSERT_EQUAL(Ac.rows(), r);
00077   // columns()
00078   CPPUNIT_ASSERT_EQUAL(Ac.columns(), one);
00079   // TEST DIMENSIONS ROWVECTOR
00080   RowVector Ar(c);
00081   // rows()
00082   CPPUNIT_ASSERT_EQUAL(Ar.rows(), one);
00083   // columns()
00084   CPPUNIT_ASSERT_EQUAL(Ar.columns(), c);
00085 
00086   // test operator = double
00087   double v = 3.5;
00088   Am = v;  As = v;  Ac = v;  Ar = v;
00089   // MATRIX
00090   for (unsigned int i=0; i<r; i++)
00091     for (unsigned int j=0; j<c; j++)
00092       CPPUNIT_ASSERT_EQUAL(Am(i+1,j+1), v);
00093   // SYMMETRICMATRIX
00094   for (unsigned int i=0; i<r; i++)
00095     for (unsigned int j=0; j<r; j++)
00096       CPPUNIT_ASSERT_EQUAL(As(i+1,j+1), v);
00097   // COLUMNVECTOR
00098   for (unsigned int i=0; i<r; i++)
00099     CPPUNIT_ASSERT_EQUAL(Ac(i+1), v);
00100   // ROWVECTOR
00101   for (unsigned int i=0; i<c; i++)
00102     CPPUNIT_ASSERT_EQUAL(Ar(i+1), v);
00103 
00104   // test operator ()
00105   // MATRIX
00106   Matrix Bm(r,c);
00107   for (unsigned int i=0; i<r; i++){
00108     for (unsigned int j=0; j<c; j++){
00109       Bm(i+1,j+1) = REF[i][j];
00110       CPPUNIT_ASSERT_EQUAL(Bm(i+1,j+1), REF[i][j]);
00111     }
00112   }
00113 
00114   // SYMMETRICMATRIX
00115   SymmetricMatrix Bs(r);
00116   for (unsigned int i=0; i<r; i++){ // fill in upper triangle
00117     for (unsigned int j=0; j<=i; j++){
00118       Bs(j+1,i+1) = REF[i][j];
00119       CPPUNIT_ASSERT_EQUAL(Bs(i+1,j+1), REF[i][j]);
00120       CPPUNIT_ASSERT_EQUAL(Bs(j+1,i+1), REF[i][j]);
00121     }
00122   }
00123   for (unsigned int i=0; i<r; i++){   // fill in lower triangle
00124     for (unsigned int j=0; j<=i; j++){
00125       Bs(i+1,j+1) = REF[i][j];
00126       CPPUNIT_ASSERT_EQUAL(Bs(i+1,j+1), REF[i][j]);
00127       CPPUNIT_ASSERT_EQUAL(Bs(j+1,i+1), REF[i][j]);
00128     }
00129   }
00130   // COLUMNVECTOR
00131   ColumnVector Bc(r);
00132   for (unsigned int i=0; i<r; i++){
00133     Bc(i+1) = REF[0][i];
00134     CPPUNIT_ASSERT_EQUAL(Bc(i+1), REF[0][i]);
00135   }
00136   // ROWVECTOR
00137   RowVector Br(c);
00138   for (unsigned int i=0; i<c; i++){
00139     Br(i+1) = REF[0][i];
00140     CPPUNIT_ASSERT_EQUAL(Br(i+1), REF[0][i]);
00141   }
00142 
00143   // test operator =
00144   // MATRIX
00145   Am = Bm;
00146   for (unsigned int i=0; i<r; i++){
00147     for (unsigned int j=0; j<c; j++){
00148       CPPUNIT_ASSERT_EQUAL(Am(i+1,j+1), REF[i][j]);
00149       CPPUNIT_ASSERT_EQUAL(Bm(i+1,j+1), REF[i][j]);
00150     }
00151   }
00152   // SYMMETRICMATRIX
00153   As = Bs;
00154   for (unsigned int i=0; i<r; i++){
00155     for (unsigned int j=0; j<=i; j++){
00156       CPPUNIT_ASSERT_EQUAL(As(i+1,j+1), REF[i][j]);
00157       CPPUNIT_ASSERT_EQUAL(Bs(i+1,j+1), REF[i][j]);
00158       CPPUNIT_ASSERT_EQUAL(As(j+1,i+1), REF[i][j]);
00159       CPPUNIT_ASSERT_EQUAL(Bs(j+1,i+1), REF[i][j]);
00160     }
00161   }
00162   // COLUMNVECTOR
00163   Ac = Bc;
00164   for (unsigned int i=0; i<r; i++){
00165     CPPUNIT_ASSERT_EQUAL(Ac(i+1), REF[0][i]);
00166     CPPUNIT_ASSERT_EQUAL(Bc(i+1), REF[0][i]);
00167   }
00168   // ROWVECTOR
00169   Ar = Br;
00170   for (unsigned int i=0; i<c; i++){
00171     CPPUNIT_ASSERT_EQUAL(Ar(i+1), REF[0][i]);
00172     CPPUNIT_ASSERT_EQUAL(Br(i+1), REF[0][i]);
00173   }
00174 
00175 
00176 
00177   // test resize
00178   // MATRIX
00179   Matrix Km(r+2,c+2); Km = v;
00180   Matrix Km_resize(r,c); Km_resize = v;
00181   CPPUNIT_ASSERT_EQUAL(Km.rows(), r+2);
00182   CPPUNIT_ASSERT_EQUAL(Km.columns(), c+2);
00183   Km.resize(r,c);
00184   CPPUNIT_ASSERT_EQUAL(Km.rows(), r);
00185   CPPUNIT_ASSERT_EQUAL(Km.columns(), c);
00186   CPPUNIT_ASSERT_EQUAL(Km, Km_resize);
00187   // SYMMETRICMATRIX
00188   SymmetricMatrix Ks(r+2); Ks = v;
00189   SymmetricMatrix Ks_resize(r); Ks_resize = v;
00190   CPPUNIT_ASSERT_EQUAL(Ks.rows(), r+2);
00191   CPPUNIT_ASSERT_EQUAL(Ks.columns(), r+2);
00192 //  Ks.resize(r);
00193 //  CPPUNIT_ASSERT_EQUAL(Ks.rows(), r);
00194 //  CPPUNIT_ASSERT_EQUAL(Ks.columns(), r);
00195 //  CPPUNIT_ASSERT_EQUAL(Ks, Ks_resize);
00196   // COLUMNVECTOR
00197   ColumnVector Kc(r+2); Kc = v;
00198   ColumnVector Kc_resize(r); Kc_resize = v;
00199   CPPUNIT_ASSERT_EQUAL(Kc.rows(), r+2);
00200   Kc.resize(r);
00201   CPPUNIT_ASSERT_EQUAL(Kc.rows(), r);
00202   CPPUNIT_ASSERT_EQUAL(Kc, Kc_resize);
00203   // ROWVECTOR
00204   RowVector Kr(c+2); Kr = v;
00205   RowVector Kr_resize(c); Kr_resize = v;
00206   CPPUNIT_ASSERT_EQUAL(Kr.columns(), c+2);
00207   Kr.resize(c);
00208   CPPUNIT_ASSERT_EQUAL(Kr.columns(), c);
00209   CPPUNIT_ASSERT_EQUAL(Kr, Kr_resize);
00210 
00211   // test operator ==
00212   // MATRIX
00213   Matrix Bm_eq; Bm_eq = Bm;
00214   CPPUNIT_ASSERT_EQUAL(Bm_eq == Bm, true);
00215   Bm(1,1) = Bm(1,1) + v;
00216   CPPUNIT_ASSERT_EQUAL(Bm == Bm_eq, false);
00217   Matrix Bm_res(Bm.rows(), Bm.columns()+1);
00218   CPPUNIT_ASSERT_EQUAL(Bm == Bm_res, false);
00219   Bm_res = Bm;
00220   CPPUNIT_ASSERT_EQUAL(Bm == Bm_res, true);
00221   // SYMMETRICMATRIX
00222   SymmetricMatrix Bs_eq; Bs_eq = Bs;
00223   CPPUNIT_ASSERT_EQUAL(Bs_eq == Bs, true);
00224   Bs(1,1) = Bs(1,1) + v;
00225   CPPUNIT_ASSERT_EQUAL(Bs == Bs_eq, false);
00226   SymmetricMatrix Bs_res(Bs.columns()+1);
00227   CPPUNIT_ASSERT_EQUAL(Bs == Bs_res, false);
00228   Bs_res = Bs;
00229   CPPUNIT_ASSERT_EQUAL(Bs == Bs_res, true);
00230   // COLUMNVECTOR
00231   ColumnVector Bc_eq; Bc_eq = Bc;
00232   CPPUNIT_ASSERT_EQUAL(Bc_eq == Bc, true);
00233   Bc(1) = Bc(1) + v;
00234   CPPUNIT_ASSERT_EQUAL(Bc == Bc_eq, false);
00235   ColumnVector Bc_res(Bc.rows()+1);
00236   CPPUNIT_ASSERT_EQUAL(Bc == Bc_res, false);
00237   Bc_res = Bc;
00238   CPPUNIT_ASSERT_EQUAL(Bc == Bc_res, true);
00239   // ROWVECTOR
00240   RowVector Br_eq; Br_eq = Br;
00241   CPPUNIT_ASSERT_EQUAL(Br_eq == Br, true);
00242   Br(1) = Br(1) + v;
00243   CPPUNIT_ASSERT_EQUAL(Br == Br_eq, false);
00244   RowVector Br_res(Br.columns()+1);
00245   CPPUNIT_ASSERT_EQUAL(Br == Br_res, false);
00246   Br_res = Br;
00247   CPPUNIT_ASSERT_EQUAL(Br == Br_res, true);
00248 
00249 
00250   // test transpose
00251   // MATRIX
00252   Matrix Am_trans = Am.transpose();
00253   CPPUNIT_ASSERT_EQUAL(Am.rows(),  Am_trans.columns());
00254   CPPUNIT_ASSERT_EQUAL(Am.columns(),  Am_trans.rows());
00255   for (unsigned int i=0; i<r; i++){
00256     for (unsigned int j=0; j<c; j++){
00257       CPPUNIT_ASSERT_EQUAL(Am(i+1,j+1),  REF[i][j]);
00258       CPPUNIT_ASSERT_EQUAL(Am_trans(j+1,i+1), REF[i][j]);
00259     }
00260   }
00261   // SYMMETRICMATRIX
00262   SymmetricMatrix As_trans = As.transpose();
00263   CPPUNIT_ASSERT_EQUAL(As.rows(),  As_trans.columns());
00264   CPPUNIT_ASSERT_EQUAL(As.columns(),  As_trans.rows());
00265   for (unsigned int i=0; i<r; i++){
00266     for (unsigned int j=0; j<=i; j++){
00267       CPPUNIT_ASSERT_EQUAL(As(i+1,j+1),  REF[i][j]);
00268       CPPUNIT_ASSERT_EQUAL(As_trans(j+1,i+1), REF[i][j]);
00269       CPPUNIT_ASSERT_EQUAL(As(j+1,i+1),  REF[i][j]);
00270       CPPUNIT_ASSERT_EQUAL(As_trans(i+1,j+1), REF[i][j]);
00271     }
00272   }
00273   // COLUMNVECTOR
00274   RowVector Ac_trans = Ac.transpose();
00275   CPPUNIT_ASSERT_EQUAL(Ac.rows(),  Ac_trans.columns());
00276   CPPUNIT_ASSERT_EQUAL(Ac.columns(),  Ac_trans.rows());
00277   for (unsigned int i=0; i<r; i++){
00278     CPPUNIT_ASSERT_EQUAL(Ac(i+1),  REF[0][i]);
00279     CPPUNIT_ASSERT_EQUAL(Ac_trans(i+1), REF[0][i]);
00280   }
00281   // ROWVECTOR
00282   ColumnVector Ar_trans = Ar.transpose();
00283   CPPUNIT_ASSERT_EQUAL(Ar.rows(),  Ar_trans.columns());
00284   CPPUNIT_ASSERT_EQUAL(Ar.columns(),  Ar_trans.rows());
00285   for (unsigned int i=0; i<c; i++){
00286     CPPUNIT_ASSERT_EQUAL(Ar(i+1),  REF[0][i]);
00287     CPPUNIT_ASSERT_EQUAL(Ar_trans(i+1), REF[0][i]);
00288   }
00289 
00290   // test sub matrix
00291   // MATRIX
00292   Matrix Am_sub = Am.sub(2,r,2,c);
00293   CPPUNIT_ASSERT_EQUAL(Am_sub.rows(), r-1);
00294   CPPUNIT_ASSERT_EQUAL(Am_sub.columns(), c-1);
00295   for (unsigned int i=0; i<c-1; i++){
00296     for (unsigned int j=0; j<c-1; j++){
00297       CPPUNIT_ASSERT_EQUAL(Am_sub(i+1,j+1), Am(i+2,j+2));
00298       CPPUNIT_ASSERT_EQUAL(Am_sub(i+1,j+1), REF[i+1][j+1]);
00299     }
00300   }
00301   // SYMMETRICMATRIX
00302   Matrix As_sub = As.sub(2,c,2,c);
00303   CPPUNIT_ASSERT_EQUAL(As_sub.rows(), c-1);
00304   CPPUNIT_ASSERT_EQUAL(As_sub.columns(), c-1);
00305   for (unsigned int i=0; i<c-1; i++){
00306     for (unsigned int j=0; j<=i; j++){
00307       CPPUNIT_ASSERT_EQUAL(As_sub(i+1,j+1), As(i+2,j+2));
00308       CPPUNIT_ASSERT_EQUAL(As_sub(i+1,j+1), REF[i+1][j+1]);
00309       CPPUNIT_ASSERT_EQUAL(As_sub(j+1,i+1), As(i+2,j+2));
00310       CPPUNIT_ASSERT_EQUAL(As_sub(j+1,i+1), REF[i+1][j+1]);
00311     }
00312   }
00313   // COLUMNVECTOR
00314   ColumnVector Ac_sub = Ac.sub(2,c);
00315   CPPUNIT_ASSERT_EQUAL(Ac_sub.rows(), c-1);
00316   CPPUNIT_ASSERT_EQUAL(Ac_sub.columns(), one);
00317   for (unsigned int i=0; i<c-1; i++){
00318     CPPUNIT_ASSERT_EQUAL(Ac_sub(i+1), Ac(i+2));
00319     CPPUNIT_ASSERT_EQUAL(Ac_sub(i+1), REF[0][i+1]);
00320   }
00321   // ROWVECTOR
00322   RowVector Ar_sub = Ar.sub(2,r-1);
00323   CPPUNIT_ASSERT_EQUAL(Ar_sub.rows(), one);
00324   CPPUNIT_ASSERT_EQUAL(Ar_sub.columns(), r-2);
00325   for (unsigned int i=0; i<r-2; i++){
00326     CPPUNIT_ASSERT_EQUAL(Ar_sub(i+1), Ar(i+2));
00327     CPPUNIT_ASSERT_EQUAL(Ar_sub(i+1), REF[0][i+1]);
00328   }
00329 
00330   // test operator *
00331   // MATRIX * MATRIX
00332   Matrix Cm = Am * Am_trans;
00333   Matrix Cm_check(r,c);
00334   for (unsigned int i=0; i<r; i++){
00335     for (unsigned int j=0; j<c; j++){
00336       // test if original elements were maintained
00337       CPPUNIT_ASSERT_EQUAL(Am(i+1,j+1),  REF[i][j]);
00338       CPPUNIT_ASSERT_EQUAL(Am_trans(j+1,i+1), REF[i][j]);
00339       // test correct multiplication
00340       double sum = 0.0;
00341       for (unsigned int t=0; t<c; t++){
00342           sum += Am(i+1,t+1) * Am_trans(t+1,j+1);
00343       }
00344       Cm_check(i+1,j+1) = sum;
00345       CPPUNIT_ASSERT_EQUAL(Cm(i+1,j+1),  Cm_check(i+1,j+1));
00346     }
00347   }
00348 
00349   // MATRIX * DOUBLE
00350   Cm = Am * v;
00351   for (unsigned int i=0; i<r; i++){
00352     for (unsigned int j=0; j<c; j++){
00353       // test if original elements were maintained
00354       CPPUNIT_ASSERT_EQUAL(Am(i+1,j+1),  REF[i][j]);
00355       CPPUNIT_ASSERT_EQUAL(Am_trans(j+1,i+1), REF[i][j]);
00356       // test correct multiplication
00357       CPPUNIT_ASSERT_EQUAL(Cm(i+1,j+1),  REF[i][j] * v);
00358     }
00359   }
00360 
00361   // SYMMETRICMATRIX * SYMMETRICMATRIX
00362   Matrix Cs_check(r,r);
00363   Matrix Cs = As * As_trans;
00364   for (unsigned int i=0; i<r; i++){
00365     for (unsigned int j=0; j<=i; j++){
00366       // test if original elements were maintained
00367       CPPUNIT_ASSERT_EQUAL(As(i+1,j+1),  REF[i][j]);
00368       CPPUNIT_ASSERT_EQUAL(As_trans(j+1,i+1), REF[i][j]);
00369       CPPUNIT_ASSERT_EQUAL(As(j+1,i+1),  REF[i][j]);
00370       CPPUNIT_ASSERT_EQUAL(As_trans(i+1,j+1), REF[i][j]);
00371       // test correct multiplication
00372       double sum = 0.0;
00373       for (unsigned int t=0; t<r; t++){
00374           sum += As(i+1,t+1) * As_trans(t+1,j+1);
00375       }
00376       Cs_check(i+1,j+1) = sum;
00377       CPPUNIT_ASSERT_EQUAL(Cs(i+1,j+1),  Cs_check(i+1,j+1));
00378     }
00379   }
00380 
00381   // SYMMETRICMATRIX * DOUBLE
00382   Cs = As * v;
00383   for (unsigned int i=0; i<r; i++){
00384     for (unsigned int j=0; j<=i; j++){
00385       // test if original elements were maintained
00386       CPPUNIT_ASSERT_EQUAL(As(i+1,j+1),  REF[i][j]);
00387       CPPUNIT_ASSERT_EQUAL(As(j+1,i+1),  REF[i][j]);
00388       // test correct multiplication
00389       CPPUNIT_ASSERT_EQUAL(Cs(i+1,j+1),  REF[i][j] * v);
00390       CPPUNIT_ASSERT_EQUAL(Cs(j+1,i+1),  REF[i][j] * v);
00391     }
00392   }
00393 
00394   // MATRIX * SYMMETRICMATRIX
00395   // TODO: not implemented?
00396 
00397   // SYMMETRICMATRIX * MATRIX
00398   Matrix Csm_check(r,c);
00399   Matrix Csm = As * Am;
00400   for (unsigned int i=0; i<r; i++){
00401     for (unsigned int j=0; j<c; j++){
00402       // test if original elements were maintained
00403       if (j<=i){
00404          CPPUNIT_ASSERT_EQUAL(As(i+1,j+1),  REF[i][j]);
00405          CPPUNIT_ASSERT_EQUAL(As(j+1,i+1),  REF[i][j]);
00406       }
00407       CPPUNIT_ASSERT_EQUAL(Am(i+1,j+1), REF[i][j]);
00408       // test correct multiplication
00409       double sum = 0.0;
00410       for (unsigned int t=0; t<r; t++){
00411           sum += As(i+1,t+1) * Am(t+1,j+1);
00412       }
00413       Csm_check(i+1,j+1) = sum;
00414       CPPUNIT_ASSERT_EQUAL(Csm(i+1,j+1),  Csm_check(i+1,j+1));
00415     }
00416   }
00417 
00418   // COLUMNVECTOR * DOUBLE
00419   ColumnVector Cc = Ac * v;
00420   for (unsigned int i=0; i<r; i++){
00421       // test if original elements were maintained
00422       CPPUNIT_ASSERT_EQUAL(Ac(i+1),  REF[0][i]);
00423       // test correct multiplication
00424       CPPUNIT_ASSERT_EQUAL(Cc(i+1),  REF[0][i] * v);
00425   }
00426 
00427   // ROWVECTOR * DOUBLE
00428   RowVector Cr = Ar * v;
00429   for (unsigned int i=0; i<c; i++){
00430       // test if original elements were maintained
00431       CPPUNIT_ASSERT_EQUAL(Ar(i+1),  REF[0][i]);
00432       // test correct multiplication
00433       CPPUNIT_ASSERT_EQUAL(Cr(i+1),  REF[0][i] * v);
00434   }
00435 
00436   // COLUMNVECTOR * ROWVECTOR
00437   Matrix Ccr = Ac * Ar;
00438   Matrix Ccr_check(r,c);
00439   for (unsigned int j=0; j<c; j++)  CPPUNIT_ASSERT_EQUAL(Ar(j+1), REF[0][j]);
00440   for (unsigned int i=0; i<r; i++){
00441      // test if original elements were maintained
00442      CPPUNIT_ASSERT_EQUAL(Ac(i+1),  REF[0][i]);
00443     for (unsigned int j=0; j<c; j++){
00444       // test correct multiplication
00445       Ccr_check(i+1,j+1) = Ac(i+1) * Ar(j+1);
00446       CPPUNIT_ASSERT_EQUAL(Ccr(i+1,j+1),  Ccr_check(i+1,j+1));
00447     }
00448   }
00449 
00450   // ROWVECTOR * COLUMNVECTOR
00451   double rc = Ac_trans * Ac;
00452   double rc_check;
00453   // test if original elements were maintained
00454   for (unsigned int j=0; j<c; j++)  CPPUNIT_ASSERT_EQUAL(Ac_trans(j+1), REF[0][j]);
00455   for (unsigned int i=0; i<r; i++)  CPPUNIT_ASSERT_EQUAL(Ac(i+1),  REF[0][i]);
00456   // test correct multiplication
00457   double sum = 0.0;
00458   for (unsigned int t=0; t<r; t++){
00459       sum += Ac_trans(t+1) * Ac(t+1);
00460   }
00461   rc_check = sum;
00462   CPPUNIT_ASSERT_EQUAL(rc,  rc_check);
00463 
00464   // ROWVECTOR * MATRIX
00465   // TODO: only implemented for lti
00466   //RowVector Cr2= Ar * Am;
00467   //Matrix Cr2_check(r);
00468   //for (unsigned int j=0; j<c; j++){
00469   //  // test if original elements were maintained
00470   //  CPPUNIT_ASSERT_EQUAL(Ar(j+1),  REF[0][j]);
00471   //  for (unsigned int i=0; i<r; i++){
00472   //    // test if original elements were maintained
00473   //    CPPUNIT_ASSERT_EQUAL(Am(i+1,j+1),  REF[i][j]);
00474   //  }
00475   //}
00476   //for (unsigned int i=0; i<r; i++){
00477   //  // test correct multiplication
00478   //  double sum = 0.0;
00479   //  for (unsigned int t=0; t<c; t++){
00480   //      sum += Ar(t+1) * Am(t+1,j+1);
00481   //  }
00482   //  Cr2_check(i+1) = sum;
00483   //  CPPUNIT_ASSERT_EQUAL(Cr2(i+1),  Cr2_check(i+1));
00484   //}
00485 
00486   // MATRIX * COLUMNVECTOR
00487   ColumnVector Cc2= Am_trans * Ac;
00488   ColumnVector Cc2_check(c);
00489   for (unsigned int j=0; j<r; j++){
00490     // test if original elements were maintained
00491     CPPUNIT_ASSERT_EQUAL(Ac(j+1),  REF[0][j]);
00492     for (unsigned int i=0; i<c; i++){
00493       // test if original elements were maintained
00494       CPPUNIT_ASSERT_EQUAL(Am_trans(i+1,j+1),  REF[j][i]);
00495     }
00496   }
00497   for (unsigned int i=0; i<c; i++){
00498     // test correct multiplication
00499     double sum = 0.0;
00500     for (unsigned int t=0; t<r; t++){
00501         sum += Am_trans(i+1,t+1) * Ac(t+1);
00502     }
00503     Cc2_check(i+1) = sum;
00504     CPPUNIT_ASSERT_EQUAL(Cc2(i+1),  Cc2_check(i+1));
00505   }
00506 
00507 
00508   // test operator /
00509   Cm = Am / v;
00510   for (unsigned int i=0; i<r; i++){
00511     for (unsigned int j=0; j<c; j++){
00512     //  CPPUNIT_ASSERT_EQUAL(Cm(i+1,j+1),  REF[i][j] / v);
00513       CPPUNIT_ASSERT_EQUAL(approxEqual(Cm(i+1,j+1),  REF[i][j] / v,epsilon),true);
00514     }
00515   }
00516   Cs = As / v;
00517   for (unsigned int i=0; i<r; i++){
00518     for (unsigned int j=0; j<=i; j++){
00519       CPPUNIT_ASSERT_EQUAL(Cs(i+1,j+1),  REF[i][j] / v);
00520       CPPUNIT_ASSERT_EQUAL(Cs(j+1,i+1),  REF[i][j] / v);
00521     }
00522   }
00523   Cc = Ac / v;
00524   for (unsigned int i=0; i<r; i++)
00525     CPPUNIT_ASSERT_EQUAL(Cc(i+1),  REF[0][i] / v);
00526   Cr = Ar / v;
00527   for (unsigned int i=0; i<c; i++)
00528     CPPUNIT_ASSERT_EQUAL(Cr(i+1),  REF[0][i] / v);
00529 
00530   // test inverse
00531   Matrix Rm(c,c);
00532   Rm(1,1) = 3;   Rm(1,2) = 3;    Rm(1,3) = 3;
00533   Rm(2,1) = 5;   Rm(2,2) = 2;    Rm(2,3) = 9;
00534   Rm(3,1) = 9;   Rm(3,2) = 7;    Rm(3,3) = 0;
00535   Matrix Rm_inv = Rm.inverse();
00536   Matrix Im(c,c); Im = 0;
00537   for (unsigned int i=0; i<c; i++)
00538     Im(i+1,i+1) = 1;
00539   Matrix Im_test = Rm * Rm_inv;
00540   CPPUNIT_ASSERT_EQUAL(approxEqual(Im_test, Im, epsilon),true);
00541 
00542   Matrix Rs(c,c);
00543   Rs(1,1) = 3;   Rs(1,2) = 5;    Rs(1,3) = 3;
00544   Rs(2,1) = 5;   Rs(2,2) = 2;    Rs(2,3) = 7;
00545   Rs(3,1) = 3;   Rs(3,2) = 7;    Rs(3,3) = 0;
00546   Matrix Rs_inv = Rs.inverse();
00547   Matrix Is(c,c); Is = 0;
00548   for (unsigned int i=0; i<c; i++)
00549     Is(i+1,i+1) = 1;
00550   Matrix Is_test = Rs * Rs_inv;
00551   CPPUNIT_ASSERT_EQUAL(approxEqual(Is_test, Is, epsilon),true);
00552 
00553   // test determinant
00554   CPPUNIT_ASSERT_EQUAL(approxEqual(Rm.determinant(), 105, epsilon),true);
00555   CPPUNIT_ASSERT_EQUAL(approxEqual(Rs.determinant(), 45, epsilon),true);
00556 
00557   // test cholesky
00558   SymmetricMatrix Ps(c);
00559   Ps(1,1) = 3;   Ps(1,2) = 2;    Ps(1,3) = 1;
00560   Ps(2,1) = 2;   Ps(2,2) = 2;    Ps(2,3) = 1;
00561   Ps(3,1) = 1;   Ps(3,2) = 1;    Ps(3,3) = 1;
00562   Matrix CHs;
00563   Matrix CHs_check(c,c);
00564   CHs_check(1,1) = 1.73205;   CHs_check(1,2) = 0.00000;    CHs_check(1,3) = 0.00000;
00565   CHs_check(2,1) = 1.15470;   CHs_check(2,2) = 0.81650;    CHs_check(2,3) = 0.00000;
00566   CHs_check(3,1) = 0.57735;   CHs_check(3,2) = 0.40825;    CHs_check(3,3) = 0.70711;
00567   Ps.cholesky_semidefinite(CHs);
00568   CPPUNIT_ASSERT_EQUAL(approxEqual(CHs, CHs_check, epsilon),true);
00569 
00570   // test operator - -=
00571   Matrix Rm_bak; Rm_bak = Rm;
00572   Matrix Dm(c,c); Dm = v;
00573   Matrix Em = Rm - Dm;
00574   Matrix Fm = Rm - v;
00575   Matrix Gm(Rm);
00576   Gm -= Dm;
00577   Matrix Hm; Hm = Rm;
00578   Hm -= v;
00579   CPPUNIT_ASSERT_EQUAL(Rm, Rm_bak);
00580   CPPUNIT_ASSERT_EQUAL( Em, Fm);
00581   CPPUNIT_ASSERT_EQUAL( Fm, Gm);
00582   CPPUNIT_ASSERT_EQUAL( Gm, Hm);
00583   for (unsigned int i=0; i<c; i++)
00584     for (unsigned int j=0; j<c; j++)
00585       CPPUNIT_ASSERT_EQUAL( Dm(i+1,j+1), v);
00586 
00587   // test pseudoinverse
00588   Matrix Bm_bak; Bm_bak = Bm;
00589   Matrix Bm_pinv = Bm.pseudoinverse();
00590   CPPUNIT_ASSERT_EQUAL(Bm, Bm_bak);
00591   Im_test = Bm_pinv * Bm;
00592   CPPUNIT_ASSERT_EQUAL(approxEqual(Im_test, Im, epsilon),true);
00593 
00594   // test svd
00595   int rows = 4;
00596   int cols = 3;
00597   Matrix A_svd(rows,cols);
00598   Matrix W_svd(cols,cols);
00599   Matrix U_svd,V_svd;
00600   W_svd = 0.0;
00601 
00602   ColumnVector w_svd;
00603   A_svd(1,1)=1; A_svd(2,2)=2; A_svd(3,3)=3;
00604   A_svd(1,2)=-0.5; A_svd(1,3)=-0.8;
00605   A_svd(2,1)=-1.5; A_svd(2,3)=-2.8;
00606   A_svd(3,1)=2.5;  A_svd(3,2)=0.8;
00607   A_svd(4,1)=0.5;  A_svd(4,2)=1.8; A_svd(4,3)=1.6 ;
00608 
00609   A_svd.SVD(w_svd,U_svd,V_svd);
00610   for (int i=1; i<=A_svd.columns() ; i++)  W_svd(i,i) = w_svd(i);
00611   CPPUNIT_ASSERT_EQUAL(approxEqual(A_svd, U_svd * W_svd * V_svd.transpose(), epsilon),true);
00612 
00613   int rows2 = 3;
00614   int cols2 = 4;
00615   Matrix A2_svd(rows2,cols2);
00616   Matrix W2_svd(cols2,cols2);
00617   Matrix U2_svd,V2_svd;
00618   W2_svd = 0.0;
00619 
00620   ColumnVector w2_svd;
00621   A2_svd(1,1)=1; A2_svd(2,2)=2; A2_svd(3,3)=3; //A(4,4)=4;
00622   A2_svd(1,2)=-0.5; A2_svd(1,3)=-0.8; A2_svd(1,4)=-0.1 ;
00623   A2_svd(2,1)=-1.5; A2_svd(2,3)=-2.8; A2_svd(2,4)=3.1 ;
00624   A2_svd(3,1)=2.5;  A2_svd(3,2)=-0.8; A2_svd(3,4)=1.1 ;
00625 
00626   A2_svd.SVD(w2_svd,U2_svd,V2_svd);
00627   for (int i=1; i<=A2_svd.columns() ; i++)  W2_svd(i,i) = w2_svd(i);
00628   CPPUNIT_ASSERT_EQUAL(approxEqual(A2_svd, U2_svd * W2_svd * V2_svd.transpose(), epsilon),true);
00629 
00630   // TEST SPECIAL CASES
00631   // Inverse for 1x1 Matrix
00632   Matrix M1(1,1);
00633   M1(1,1)= 1.4;
00634   Matrix M1_inv = M1.inverse();
00635   Matrix I1(1,1);
00636   I1(1,1)= 1.0;
00637   CPPUNIT_ASSERT_EQUAL(M1_inv * M1, I1);
00638   // Inverse for 2x2 Matrix
00639   Matrix M2(2,2);
00640   M2(1,1)= 1.4;
00641   M2(2,2)= 0.4;
00642   M2(1,2)= 2.1;
00643   M2(2,1)= -0.8;
00644   Matrix M2_inv = M2.inverse();
00645   Matrix I2(2,2);
00646   I2=0.0;
00647   I2(1,1)= 1.0;
00648   I2(2,2)= 1.0;
00649   CPPUNIT_ASSERT_EQUAL(approxEqual(M2_inv * M2, I2,epsilon),true);
00650   // Determinant for 1x1 Matrix
00651   CPPUNIT_ASSERT_EQUAL(M1.determinant(), M1(1,1));
00652   // Determinant for 2x2 Matrix
00653   CPPUNIT_ASSERT_EQUAL(M2.determinant(), M2(1,1)*M2(2,2)-M2(1,2)*M2(2,1));
00654   // Inverse for 1x1 SymmetricMatrix
00655   SymmetricMatrix SM1(1);
00656   SM1(1,1)= 1.4;
00657   SymmetricMatrix SM1_inv = SM1.inverse();
00658   CPPUNIT_ASSERT_EQUAL(Matrix(SM1_inv * SM1), I1);
00659   // Inverse for 2x2 Matrix
00660   SymmetricMatrix SM2(2);
00661   SM2(1,1)= 1.4;
00662   SM2(2,2)= 0.4;
00663   SM2(1,2)= 2.1;
00664   SM2(2,1)= -0.8;
00665   SymmetricMatrix SM2_inv = SM2.inverse();
00666   CPPUNIT_ASSERT_EQUAL(approxEqual(Matrix(SM2_inv * SM2), I2,epsilon),true);
00667   // Determinant for 1x1 Matrix
00668   CPPUNIT_ASSERT_EQUAL(SM1.determinant(), SM1(1,1));
00669   // Determinant for 2x2 Matrix
00670   CPPUNIT_ASSERT_EQUAL(SM2.determinant(), SM2(1,1)*SM2(2,2)-SM2(1,2)*SM2(2,1));
00671 
00672   Matrix M3(3,3);
00673   M3(1,1)=1;
00674   M3(1,2)=2;
00675   M3(1,3)=3;
00676   M3(2,1)=4;
00677   M3(2,2)=5;
00678   M3(2,3)=6;
00679   M3(3,1)=7;
00680   M3(3,2)=8;
00681   M3(3,3)=9;
00682   // test rowCopy()
00683   RowVector rcopy1 = M3.rowCopy(1);
00684   RowVector rcopy1test(3);
00685   rcopy1test(1) = M3(1,1);
00686   rcopy1test(2) = M3(1,2);
00687   rcopy1test(3) = M3(1,3);
00688   CPPUNIT_ASSERT_EQUAL(rcopy1,rcopy1test);
00689   RowVector rcopy2 = M3.rowCopy(2);
00690   RowVector rcopy2test(3);
00691   rcopy2test(1) = M3(2,1);
00692   rcopy2test(2) = M3(2,2);
00693   rcopy2test(3) = M3(2,3);
00694   CPPUNIT_ASSERT_EQUAL(rcopy2,rcopy2test);
00695   RowVector rcopy3 = M3.rowCopy(3);
00696   RowVector rcopy3test(3);
00697   rcopy3test(1) = M3(3,1);
00698   rcopy3test(2) = M3(3,2);
00699   rcopy3test(3) = M3(3,3);
00700   CPPUNIT_ASSERT_EQUAL(rcopy3,rcopy3test);
00701 
00702   // test columnCopy()
00703   ColumnVector copy1 = M3.columnCopy(1);
00704   ColumnVector copy1test(3);
00705   copy1test(1) = M3(1,1);
00706   copy1test(2) = M3(2,1);
00707   copy1test(3) = M3(3,1);
00708   CPPUNIT_ASSERT_EQUAL(copy1,copy1test);
00709   ColumnVector copy2 = M3.columnCopy(2);
00710   ColumnVector copy2test(3);
00711   copy2test(1) = M3(1,2);
00712   copy2test(2) = M3(2,2);
00713   copy2test(3) = M3(3,2);
00714   CPPUNIT_ASSERT_EQUAL(copy2,copy2test);
00715   ColumnVector copy3 = M3.columnCopy(3);
00716   ColumnVector copy3test(3);
00717   copy3test(1) = M3(1,3);
00718   copy3test(2) = M3(2,3);
00719   copy3test(3) = M3(3,3);
00720   CPPUNIT_ASSERT_EQUAL(copy3,copy3test);
00721 }
00722 
00723 
00724 


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Thu Feb 11 2016 22:31:56