MOAB: Mesh Oriented datABase  (version 5.4.1)
Matrix3DTest.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Sandia Corporation and Argonne National
00005     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
00006     with Sandia Corporation, the U.S. Government retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     [email protected], [email protected], [email protected],
00024     [email protected], [email protected], [email protected]
00025 
00026   ***************************************************************** */
00027 //
00028 //    AUTHOR: Thomas Leurent <[email protected]>
00029 //       ORG: Argonne National Laboratory
00030 //    E-MAIL: [email protected]
00031 //
00032 // ORIG-DATE: 12-Nov-02 at 18:05:56
00033 //  LAST-MOD: 15-Apr-04 at 14:57:05 by Thomas Leurent
00034 //
00035 // DESCRIPTION:
00036 // ============
00037 /*! \file Matrix3DTest.cpp
00038 
00039 Unit testing of various functions in the Matrix3D class.
00040 
00041 */
00042 // DESCRIP-END.
00043 //
00044 
00045 #include "Vector3D.hpp"
00046 #include "Matrix3D.hpp"
00047 
00048 #include <cmath>
00049 
00050 #include "cppunit/extensions/HelperMacros.h"
00051 
00052 using namespace MBMesquite;
00053 using std::cerr;
00054 using std::cout;
00055 using std::endl;
00056 
00057 class Matrix3DTest : public CppUnit::TestFixture
00058 {
00059 
00060   private:
00061     CPPUNIT_TEST_SUITE( Matrix3DTest );
00062     CPPUNIT_TEST( test_equal );
00063     CPPUNIT_TEST( test_plus );
00064     CPPUNIT_TEST( test_minus );
00065     CPPUNIT_TEST( test_Frobenius_2 );
00066     CPPUNIT_TEST( test_transpose );
00067     CPPUNIT_TEST( test_plus_equal );
00068     CPPUNIT_TEST( test_times_equal_scalar );
00069     CPPUNIT_TEST( test_times_scalar );
00070     CPPUNIT_TEST( test_plus_transpose );
00071     CPPUNIT_TEST( test_plus_transpose_equal );
00072     CPPUNIT_TEST( test_outer_product );
00073     CPPUNIT_TEST( test_fill_lower_triangle );
00074     CPPUNIT_TEST( test_times );
00075     CPPUNIT_TEST( test_mult_element );
00076     CPPUNIT_TEST( test_times_vector );
00077     CPPUNIT_TEST( test_vector_times );
00078     CPPUNIT_TEST( test_det );
00079     CPPUNIT_TEST( test_B_times_invA );
00080     CPPUNIT_TEST_SUITE_END();
00081 
00082   private:
00083     Vector3D e1, e2, e3;
00084     Matrix3D mIdentity;
00085     Matrix3D mMat1;
00086     Matrix3D mMat1sym;
00087     Matrix3D mMat2;
00088     Matrix3D mMat2trans;
00089     Matrix3D mMat1plus2;
00090     Matrix3D mMat1plus2trans;
00091     Matrix3D mMat1times2;
00092     Matrix3D mMat1times2inv;
00093     double tolEps;
00094 
00095   public:
00096     void setUp()
00097     {
00098         // sets up the unit vectors
00099         e1.set( 1, 0, 0 );
00100         e2.set( 0, 1, 0 );
00101         e3.set( 0, 0, 1 );
00102 
00103         mIdentity = " 1    0    0 "
00104                     " 0    1    0 "
00105                     " 0    0    1";
00106 
00107         mMat1 = " 1    4.2  2 "
00108                 " 5.2  3    4 "
00109                 " 1    7    0.4";
00110 
00111         mMat1sym = " 1    4.2  2 "
00112                    " 4.2  3    4 "
00113                    " 2    4    0.4";
00114 
00115         mMat2 = " 2    4    5 "
00116                 " 2    1    3 "
00117                 " 0    7    8 ";
00118 
00119         mMat2trans = " 2    2    0 "
00120                      " 4    1    7 "
00121                      " 5    3    8 ";
00122 
00123         mMat1plus2 = " 3    8.2   7 "
00124                      " 7.2  4     7 "
00125                      " 1    14    8.4";
00126 
00127         mMat1plus2trans = " 3    6.2   2 "
00128                           " 9.2  4     11 "
00129                           " 6    10    8.4";
00130 
00131         mMat1times2 = " 10.4 22.2  33.6 "
00132                       " 16.4 51.8  67.0 "
00133                       " 16.0 13.8  29.2 ";
00134 
00135         mMat1times2inv = " 2.519141   0.088216  -0.977863 "
00136                          " 1.009487   0.304594  -0.593375 "
00137                          " 5.838881  -0.699068  -2.203728 ";
00138 
00139         tolEps = 1e-12;
00140     }
00141 
00142     void tearDown() {}
00143 
00144   public:
00145     Matrix3DTest() {}
00146 
00147     void test_equal()
00148     {
00149         CPPUNIT_ASSERT( mMat1 == mMat1 );
00150         CPPUNIT_ASSERT( mMat1 != mMat2 );
00151     }
00152 
00153     void test_plus()
00154     {
00155         Matrix3D sum;
00156         sum = mMat1 + mMat2;
00157         CPPUNIT_ASSERT( sum == mMat1plus2 );
00158     }
00159 
00160     void test_minus()
00161     {
00162         Matrix3D res;
00163         res = mMat1 - mIdentity;
00164         Matrix3D correct( " 0    4.2  2 "
00165                           " 5.2  2    4 "
00166                           " 1    7    -0.6 " );
00167         CPPUNIT_ASSERT( res == correct );
00168     }
00169 
00170     void test_Frobenius_2()
00171     {
00172         double fro = Frobenius_2( mMat1 );
00173         CPPUNIT_ASSERT_DOUBLES_EQUAL( 124.84, fro, tolEps );
00174     }
00175 
00176     void test_transpose()
00177     {
00178         Matrix3D trans = transpose( mMat2 );
00179         CPPUNIT_ASSERT( trans == mMat2trans );
00180     }
00181 
00182     void test_plus_equal()
00183     {
00184         mMat1 += mMat2;
00185         CPPUNIT_ASSERT( mMat1 == mMat1plus2 );
00186     }
00187 
00188     void test_times_equal_scalar()
00189     {
00190         mMat2 *= 3;
00191         Matrix3D correct( " 6   12   15 "
00192                           " 6    3    9 "
00193                           " 0   21   24 " );
00194         CPPUNIT_ASSERT( mMat2 == correct );
00195     }
00196     void test_times_scalar()
00197     {
00198         Matrix3D tmp = mMat2 * 3;
00199         Matrix3D correct( " 6   12   15 "
00200                           " 6    3    9 "
00201                           " 0   21   24 " );
00202         CPPUNIT_ASSERT( tmp == correct );
00203         tmp[0][0] = 0;
00204         tmp       = 3 * mMat2;
00205         CPPUNIT_ASSERT( tmp == correct );
00206     }
00207     void test_plus_transpose()
00208     {
00209         Matrix3D plus_trans = plus_transpose( mMat1, mMat2 );
00210         CPPUNIT_ASSERT( plus_trans == mMat1plus2trans );
00211     }
00212 
00213     void test_plus_transpose_equal()
00214     {
00215         mMat1.plus_transpose_equal( mMat2 );
00216         CPPUNIT_ASSERT( mMat1 == mMat1plus2trans );
00217     }
00218 
00219     void test_outer_product()
00220     {
00221         Matrix3D mat;
00222         Vector3D vec1( 2, 7, 3 );
00223         Vector3D vec2( 5, 8, 9 );
00224         mat.outer_product( vec1, vec2 );
00225         Matrix3D correct( " 10    16    18 "
00226                           " 35    56    63 "
00227                           " 15    24    27 " );
00228 
00229         CPPUNIT_ASSERT( mat == correct );
00230     }
00231 
00232     void test_fill_lower_triangle()
00233     {
00234         mMat1.fill_lower_triangle();
00235         CPPUNIT_ASSERT( mMat1 == mMat1sym );
00236     }
00237 
00238     void test_times()
00239     {
00240         Matrix3D mult = mMat1 * mMat2;
00241         CPPUNIT_ASSERT( mult == mMat1times2 );
00242 
00243         mult = mMat1 * mIdentity;
00244         CPPUNIT_ASSERT( mult == mMat1 );
00245     }
00246 
00247     void test_mult_element()
00248     {
00249         Matrix3D mat = mult_element( mMat1, mIdentity );
00250         Matrix3D correct( " 1 0 0 "
00251                           " 0 3 0 "
00252                           " 0 0 0.4" );
00253         CPPUNIT_ASSERT( mat == correct );
00254     }
00255 
00256     void test_times_vector()
00257     {
00258         Vector3D vec = mMat1 * e1;
00259         Vector3D correct( 1, 5.2, 1 );
00260         CPPUNIT_ASSERT( vec == correct );
00261 
00262         Vector3D vec_2( 3., 2., 5. );
00263         Vector3D vec_12 = mMat1 * vec_2;
00264         correct.set( 21.4, 41.6, 19. );
00265         CPPUNIT_ASSERT( vec_12 == correct );
00266     }
00267 
00268     void test_vector_times()
00269     {
00270         Vector3D vec = e1 * mMat1;
00271         Vector3D correct( 1, 4.2, 2 );
00272         CPPUNIT_ASSERT( vec == correct );
00273 
00274         Vector3D vec2( 2.1, 3, 8 );
00275         vec = vec2 * mMat1;
00276         correct.set( 25.7, 73.82, 19.4 );
00277         int loop_i = 0;
00278         for( loop_i = 0; loop_i < 3; ++loop_i )
00279         {
00280             CPPUNIT_ASSERT_DOUBLES_EQUAL( vec[loop_i], correct[loop_i], tolEps );
00281         }
00282     }
00283 
00284     void test_det()
00285     {
00286         double d = det( mMat1 );
00287         CPPUNIT_ASSERT_DOUBLES_EQUAL( 48.064, d, tolEps );
00288     }
00289 
00290     void test_B_times_invA()
00291     {
00292         Matrix3D orig1( mMat1 );
00293         timesInvA( mMat2, mMat1 );
00294 
00295         // Checks mMat1 is unchanged
00296         CPPUNIT_ASSERT( mMat1 == orig1 );
00297 
00298         // Checks mMat2 now contains the correct result
00299         for( int i = 0; i < 3; ++i )
00300             for( int j = 0; j < 3; ++j )
00301                 CPPUNIT_ASSERT_DOUBLES_EQUAL( mMat2[i][j], mMat1times2inv[i][j], 0.0001 );
00302     }
00303 };
00304 
00305 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Matrix3DTest, "Matrix3DTest" );
00306 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Matrix3DTest, "Unit" );
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines