MOAB: Mesh Oriented datABase
(version 5.4.1)
|
00001 /* ***************************************************************** 00002 MESQUITE -- The Mesh Quality Improvement Toolkit 00003 00004 Copyright 2007 Sandia National Laboratories. Developed at the 00005 University of Wisconsin--Madison under SNL contract number 00006 624796. The U.S. Government and the University of Wisconsin 00007 retain certain rights to 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 (2008) kraftche@cae.wisc.edu 00024 00025 ***************************************************************** */ 00026 00027 #include "SymMatrix3D.hpp" 00028 00029 #include <cmath> 00030 00031 #include "UnitUtil.hpp" 00032 00033 using namespace MBMesquite; 00034 00035 class SymMatrix3DTest : public CppUnit::TestFixture 00036 { 00037 00038 private: 00039 CPPUNIT_TEST_SUITE( SymMatrix3DTest ); 00040 CPPUNIT_TEST( test_init_diag ); 00041 CPPUNIT_TEST( test_indices ); 00042 CPPUNIT_TEST( test_plus_eq ); 00043 CPPUNIT_TEST( test_minus_eq ); 00044 CPPUNIT_TEST( test_times_eq ); 00045 CPPUNIT_TEST( test_divide_eq ); 00046 CPPUNIT_TEST( test_plus ); 00047 CPPUNIT_TEST( test_minus ); 00048 CPPUNIT_TEST( test_plus_nonsym ); 00049 CPPUNIT_TEST( test_minus_nonsym ); 00050 CPPUNIT_TEST( test_multiply ); 00051 CPPUNIT_TEST( test_scalar_multiply ); 00052 CPPUNIT_TEST( test_divide ); 00053 CPPUNIT_TEST( test_vector_multiply ); 00054 CPPUNIT_TEST( test_nonsym_multiply ); 00055 CPPUNIT_TEST( test_determinant ); 00056 CPPUNIT_TEST( test_inverse ); 00057 CPPUNIT_TEST_SUITE_END(); 00058 00059 public: 00060 void test_init_diag(); 00061 void test_indices(); 00062 void test_plus_eq(); 00063 void test_minus_eq(); 00064 void test_times_eq(); 00065 void test_divide_eq(); 00066 void test_plus(); 00067 void test_minus(); 00068 void test_plus_nonsym(); 00069 void test_minus_nonsym(); 00070 void test_multiply(); 00071 void test_scalar_multiply(); 00072 void test_divide(); 00073 void test_vector_multiply(); 00074 void test_nonsym_multiply(); 00075 void test_determinant(); 00076 void test_inverse(); 00077 }; 00078 00079 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SymMatrix3DTest, "SymMatrix3DTest" ); 00080 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SymMatrix3DTest, "Unit" ); 00081 00082 void SymMatrix3DTest::test_init_diag() 00083 { 00084 const SymMatrix3D A( 2.0 ); 00085 00086 CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0, A[SymMatrix3D::T00], DBL_EPSILON ); 00087 CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A[SymMatrix3D::T01], DBL_EPSILON ); 00088 CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A[SymMatrix3D::T02], DBL_EPSILON ); 00089 00090 CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A[SymMatrix3D::T10], DBL_EPSILON ); 00091 CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0, A[SymMatrix3D::T11], DBL_EPSILON ); 00092 CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A[SymMatrix3D::T12], DBL_EPSILON ); 00093 00094 CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A[SymMatrix3D::T20], DBL_EPSILON ); 00095 CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, A[SymMatrix3D::T21], DBL_EPSILON ); 00096 CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0, A[SymMatrix3D::T22], DBL_EPSILON ); 00097 } 00098 00099 void SymMatrix3DTest::test_indices() 00100 { 00101 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00102 00103 CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, A( 0, 0 ), DBL_EPSILON ); 00104 CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0, A( 0, 1 ), DBL_EPSILON ); 00105 CPPUNIT_ASSERT_DOUBLES_EQUAL( 3.0, A( 0, 2 ), DBL_EPSILON ); 00106 00107 CPPUNIT_ASSERT_DOUBLES_EQUAL( 2.0, A( 1, 0 ), DBL_EPSILON ); 00108 CPPUNIT_ASSERT_DOUBLES_EQUAL( 4.0, A( 1, 1 ), DBL_EPSILON ); 00109 CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0, A( 1, 2 ), DBL_EPSILON ); 00110 00111 CPPUNIT_ASSERT_DOUBLES_EQUAL( 3.0, A( 2, 0 ), DBL_EPSILON ); 00112 CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.0, A( 2, 1 ), DBL_EPSILON ); 00113 CPPUNIT_ASSERT_DOUBLES_EQUAL( 6.0, A( 2, 2 ), DBL_EPSILON ); 00114 } 00115 00116 void SymMatrix3DTest::test_plus_eq() 00117 { 00118 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00119 const SymMatrix3D B( 101, 102, 103, 104, 105, 106 ); 00120 SymMatrix3D C( A ); 00121 C += B; 00122 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0] + B[0], C[0], DBL_EPSILON ); 00123 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1] + B[1], C[1], DBL_EPSILON ); 00124 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2] + B[2], C[2], DBL_EPSILON ); 00125 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[3] + B[3], C[3], DBL_EPSILON ); 00126 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[4] + B[4], C[4], DBL_EPSILON ); 00127 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[5] + B[5], C[5], DBL_EPSILON ); 00128 } 00129 00130 void SymMatrix3DTest::test_minus_eq() 00131 { 00132 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00133 const SymMatrix3D B( 101, 102, 103, 104, 105, 106 ); 00134 SymMatrix3D C( A ); 00135 C -= B; 00136 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0] - B[0], C[0], DBL_EPSILON ); 00137 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1] - B[1], C[1], DBL_EPSILON ); 00138 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2] - B[2], C[2], DBL_EPSILON ); 00139 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[3] - B[3], C[3], DBL_EPSILON ); 00140 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[4] - B[4], C[4], DBL_EPSILON ); 00141 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[5] - B[5], C[5], DBL_EPSILON ); 00142 } 00143 00144 void SymMatrix3DTest::test_times_eq() 00145 { 00146 const SymMatrix3D A( 0.5, 2, 3, 4, 5, 6 ); 00147 const double s = 0.25; 00148 SymMatrix3D C( A ); 00149 C *= s; 00150 CPPUNIT_ASSERT_DOUBLES_EQUAL( s * A[0], C[0], DBL_EPSILON ); 00151 CPPUNIT_ASSERT_DOUBLES_EQUAL( s * A[1], C[1], DBL_EPSILON ); 00152 CPPUNIT_ASSERT_DOUBLES_EQUAL( s * A[2], C[2], DBL_EPSILON ); 00153 CPPUNIT_ASSERT_DOUBLES_EQUAL( s * A[3], C[3], DBL_EPSILON ); 00154 CPPUNIT_ASSERT_DOUBLES_EQUAL( s * A[4], C[4], DBL_EPSILON ); 00155 CPPUNIT_ASSERT_DOUBLES_EQUAL( s * A[5], C[5], DBL_EPSILON ); 00156 } 00157 00158 void SymMatrix3DTest::test_divide_eq() 00159 { 00160 const SymMatrix3D A( 0.5, 2, 3, 4, 5, 6 ); 00161 const double s = 0.25; 00162 SymMatrix3D C( A ); 00163 C /= s; 00164 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0] / s, C[0], DBL_EPSILON ); 00165 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1] / s, C[1], DBL_EPSILON ); 00166 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2] / s, C[2], DBL_EPSILON ); 00167 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[3] / s, C[3], DBL_EPSILON ); 00168 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[4] / s, C[4], DBL_EPSILON ); 00169 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[5] / s, C[5], DBL_EPSILON ); 00170 } 00171 00172 void SymMatrix3DTest::test_plus() 00173 { 00174 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00175 const SymMatrix3D B( 101, 102, 103, 104, 105, 106 ); 00176 const SymMatrix3D C( A + B ); 00177 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0] + B[0], C[0], DBL_EPSILON ); 00178 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1] + B[1], C[1], DBL_EPSILON ); 00179 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2] + B[2], C[2], DBL_EPSILON ); 00180 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[3] + B[3], C[3], DBL_EPSILON ); 00181 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[4] + B[4], C[4], DBL_EPSILON ); 00182 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[5] + B[5], C[5], DBL_EPSILON ); 00183 } 00184 00185 void SymMatrix3DTest::test_minus() 00186 { 00187 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00188 const SymMatrix3D B( 101, 102, 103, 104, 105, 106 ); 00189 const SymMatrix3D C( A - B ); 00190 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0] - B[0], C[0], DBL_EPSILON ); 00191 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1] - B[1], C[1], DBL_EPSILON ); 00192 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2] - B[2], C[2], DBL_EPSILON ); 00193 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[3] - B[3], C[3], DBL_EPSILON ); 00194 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[4] - B[4], C[4], DBL_EPSILON ); 00195 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[5] - B[5], C[5], DBL_EPSILON ); 00196 } 00197 00198 void SymMatrix3DTest::test_plus_nonsym() 00199 { 00200 const SymMatrix3D A( 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625 ); 00201 const Matrix3D B( 1, 2, 3, 4, 5, 6, 7, 8, 9 ); 00202 const Matrix3D C( A + B ); 00203 const Matrix3D D( B + A ); 00204 const Matrix3D E( A( 0, 0 ) + B( 0, 0 ), A( 0, 1 ) + B( 0, 1 ), A( 0, 2 ) + B( 0, 2 ), A( 1, 0 ) + B( 1, 0 ), 00205 A( 1, 1 ) + B( 1, 1 ), A( 1, 2 ) + B( 1, 2 ), A( 2, 0 ) + B( 2, 0 ), A( 2, 1 ) + B( 2, 1 ), 00206 A( 2, 2 ) + B( 2, 2 ) ); 00207 CPPUNIT_ASSERT_MATRICES_EQUAL( E, C, DBL_EPSILON ); 00208 CPPUNIT_ASSERT_MATRICES_EQUAL( E, D, DBL_EPSILON ); 00209 } 00210 00211 void SymMatrix3DTest::test_minus_nonsym() 00212 { 00213 const SymMatrix3D A( 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625 ); 00214 const Matrix3D B( 1, 2, 3, 4, 5, 6, 7, 8, 9 ); 00215 00216 const Matrix3D C( A - B ); 00217 const Matrix3D E( A( 0, 0 ) - B( 0, 0 ), A( 0, 1 ) - B( 0, 1 ), A( 0, 2 ) - B( 0, 2 ), A( 1, 0 ) - B( 1, 0 ), 00218 A( 1, 1 ) - B( 1, 1 ), A( 1, 2 ) - B( 1, 2 ), A( 2, 0 ) - B( 2, 0 ), A( 2, 1 ) - B( 2, 1 ), 00219 A( 2, 2 ) - B( 2, 2 ) ); 00220 CPPUNIT_ASSERT_MATRICES_EQUAL( E, C, DBL_EPSILON ); 00221 00222 const Matrix3D D( B - A ); 00223 const Matrix3D F( B( 0, 0 ) - A( 0, 0 ), B( 0, 1 ) - A( 0, 1 ), B( 0, 2 ) - A( 0, 2 ), B( 1, 0 ) - A( 1, 0 ), 00224 B( 1, 1 ) - A( 1, 1 ), B( 1, 2 ) - A( 1, 2 ), B( 2, 0 ) - A( 2, 0 ), B( 2, 1 ) - A( 2, 1 ), 00225 B( 2, 2 ) - A( 2, 2 ) ); 00226 CPPUNIT_ASSERT_MATRICES_EQUAL( F, D, DBL_EPSILON ); 00227 } 00228 00229 void SymMatrix3DTest::test_multiply() 00230 { 00231 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00232 const SymMatrix3D B( 101, 102, 103, 104, 105, 106 ); 00233 const Matrix3D C( A ), D( B ); 00234 CPPUNIT_ASSERT_MATRICES_EQUAL( C * D, A * B, DBL_EPSILON ); 00235 CPPUNIT_ASSERT_MATRICES_EQUAL( D * C, B * A, DBL_EPSILON ); 00236 } 00237 00238 void SymMatrix3DTest::test_scalar_multiply() 00239 { 00240 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00241 const double s = 0.25; 00242 const SymMatrix3D B( A * s ), C( s * A ); 00243 00244 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0] * s, B[0], DBL_EPSILON ); 00245 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1] * s, B[1], DBL_EPSILON ); 00246 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2] * s, B[2], DBL_EPSILON ); 00247 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[3] * s, B[3], DBL_EPSILON ); 00248 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[4] * s, B[4], DBL_EPSILON ); 00249 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[5] * s, B[5], DBL_EPSILON ); 00250 00251 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0] * s, C[0], DBL_EPSILON ); 00252 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1] * s, C[1], DBL_EPSILON ); 00253 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2] * s, C[2], DBL_EPSILON ); 00254 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[3] * s, C[3], DBL_EPSILON ); 00255 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[4] * s, C[4], DBL_EPSILON ); 00256 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[5] * s, C[5], DBL_EPSILON ); 00257 } 00258 00259 void SymMatrix3DTest::test_divide() 00260 { 00261 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00262 const double s = 0.25; 00263 const SymMatrix3D B( A / s ); 00264 00265 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0] / s, B[0], DBL_EPSILON ); 00266 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1] / s, B[1], DBL_EPSILON ); 00267 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2] / s, B[2], DBL_EPSILON ); 00268 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[3] / s, B[3], DBL_EPSILON ); 00269 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[4] / s, B[4], DBL_EPSILON ); 00270 CPPUNIT_ASSERT_DOUBLES_EQUAL( A[5] / s, B[5], DBL_EPSILON ); 00271 } 00272 00273 void SymMatrix3DTest::test_vector_multiply() 00274 { 00275 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00276 const Vector3D v( 101, 102, 103 ); 00277 const Matrix3D B( A ); 00278 CPPUNIT_ASSERT_VECTORS_EQUAL( B * v, A * v, DBL_EPSILON ); 00279 CPPUNIT_ASSERT_VECTORS_EQUAL( v * B, v * A, DBL_EPSILON ); 00280 } 00281 00282 void SymMatrix3DTest::test_nonsym_multiply() 00283 { 00284 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00285 const Matrix3D B( 101, 102, 103, 104, 105, 106, 107, 108, 109 ); 00286 const Matrix3D C( A ); 00287 CPPUNIT_ASSERT_MATRICES_EQUAL( C * B, A * B, DBL_EPSILON ); 00288 CPPUNIT_ASSERT_MATRICES_EQUAL( B * C, B * A, DBL_EPSILON ); 00289 } 00290 00291 void SymMatrix3DTest::test_determinant() 00292 { 00293 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00294 const Matrix3D B( A ); 00295 CPPUNIT_ASSERT_DOUBLES_EQUAL( det( B ), det( A ), DBL_EPSILON ); 00296 } 00297 00298 void SymMatrix3DTest::test_inverse() 00299 { 00300 const SymMatrix3D A( 1, 2, 3, 4, 5, 6 ); 00301 const Matrix3D B( A * inverse( A ) ); 00302 const Matrix3D I( 1, 0, 0, 0, 1, 0, 0, 0, 1 ); 00303 CPPUNIT_ASSERT_MATRICES_EQUAL( I, B, 1e-8 ); 00304 }