25 #ifndef WTENSORFUNCTIONS_TEST_H
26 #define WTENSORFUNCTIONS_TEST_H
32 #include <cxxtest/TestSuite.h>
34 #include "../../WAssert.h"
35 #include "../../WLimits.h"
36 #include "../../WStringUtils.h"
37 #include "../WTensorFunctions.h"
53 t( 0, 0 ) = 0.000179516;
54 t( 0, 1 ) = 2.09569e-05;
55 t( 0, 2 ) = 2.76557e-06;
56 t( 1, 1 ) = 0.000170189;
57 t( 1, 2 ) = -5.52619e-07;
58 t( 2, 2 ) = 0.00015239;
60 jacobiEigenvector3D( t, &sys );
62 TS_ASSERT_DELTA( sys[0].first, 1.5062467240725114e-04, 1e-9 );
63 TS_ASSERT_DELTA( sys[1].first, 1.5507354000104679e-04, 1e-9 );
64 TS_ASSERT_DELTA( sys[2].first, 1.9639678759170208e-04, 1e-9 );
82 jacobiEigenvector3D( t, &sys );
84 TS_ASSERT_DELTA( sys[0].first, 1.0, 1e-6 );
85 TS_ASSERT_DELTA( sys[1].first, 2.0, 1e-6 );
86 TS_ASSERT_DELTA( sys[2].first, 3.0, 1e-6 );
88 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
89 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
90 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
91 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
92 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
93 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
100 jacobiEigenvector3D( t, &sys );
102 TS_ASSERT_DELTA( sys[0].first, -3.0, 1e-6 );
103 TS_ASSERT_DELTA( sys[1].first, 1.0, 1e-6 );
104 TS_ASSERT_DELTA( sys[2].first, 2.0, 1e-6 );
105 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
106 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
107 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
108 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
109 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
110 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
117 jacobiEigenvector3D( t, &sys );
119 TS_ASSERT_DELTA( sys[0].first, 1.0, 1e-6 );
120 TS_ASSERT_DELTA( sys[1].first, 2.0, 1e-6 );
121 TS_ASSERT_DELTA( sys[2].first, 2.0, 1e-6 );
122 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
123 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
124 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
125 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
126 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
127 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
134 jacobiEigenvector3D( t, &sys );
136 TS_ASSERT_DELTA( sys[0].first, -1.0, 1e-6 );
137 TS_ASSERT_DELTA( sys[1].first, -1.0, 1e-6 );
138 TS_ASSERT_DELTA( sys[2].first, -1.0, 1e-6 );
139 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
140 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
141 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
142 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
143 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
144 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
151 jacobiEigenvector3D( t, &sys );
153 TS_ASSERT_DELTA( sys[0].first, 0.0, 1e-6 );
154 TS_ASSERT_DELTA( sys[1].first, 1.0, 1e-6 );
155 TS_ASSERT_DELTA( sys[2].first, 1.0, 1e-6 );
156 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
157 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
158 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
159 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
160 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
161 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
168 jacobiEigenvector3D( t, &sys );
170 TS_ASSERT_DELTA( sys[0].first, 0.0, 1e-6 );
171 TS_ASSERT_DELTA( sys[1].first, 0.0, 1e-6 );
172 TS_ASSERT_DELTA( sys[2].first, 0.0, 1e-6 );
173 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
174 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
175 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
176 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
177 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
178 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
182 t( 0, 0 ) = 2.000001;
184 t( 2, 2 ) = 1.999998;
186 jacobiEigenvector3D( t, &sys );
188 TS_ASSERT_DELTA( sys[0].first, 0.0, 1e-6 );
189 TS_ASSERT_DELTA( sys[1].first, 1.999998, 1e-6 );
190 TS_ASSERT_DELTA( sys[2].first, 2.000001, 1e-6 );
191 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
192 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
193 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
194 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
195 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
196 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
200 t( 0, 0 ) = 3.824572321236e30;
204 jacobiEigenvector3D( t, &sys );
206 TS_ASSERT_DELTA( sys[0].first, 1.0, 1e-6 );
207 TS_ASSERT_DELTA( sys[1].first, 2.0, 1e-6 );
208 TS_ASSERT_DELTA( sys[2].first, 3.824572321236e30, 1e-6 );
209 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
210 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
211 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
212 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
213 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
214 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
218 t( 0, 0 ) = 3.824572321236e-30;
222 jacobiEigenvector3D( t, &sys );
224 TS_ASSERT_DELTA( sys[0].first, 3.824572321236e-30, 1e-6 );
225 TS_ASSERT_DELTA( sys[1].first, 1.0, 1e-6 );
226 TS_ASSERT_DELTA( sys[2].first, 2.0, 1e-6 );
227 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
228 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
229 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
230 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
231 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
232 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
244 jacobiEigenvector3D( t, &sys );
246 TS_ASSERT_DELTA( sys[0].first, 1.0, 1e-6 );
247 TS_ASSERT_DELTA( sys[1].first, 2.0, 1e-6 );
248 TS_ASSERT_DELTA( sys[2].first, 3.0, 1e-6 );
249 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
250 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
251 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
252 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
253 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
254 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
267 jacobiEigenvector3D( t, &sys );
269 TS_ASSERT_DELTA( sys[0].first, 1.0, 1e-6 );
270 TS_ASSERT_DELTA( sys[1].first, 2.0, 1e-6 );
271 TS_ASSERT_DELTA( sys[2].first, 3.0, 1e-6 );
272 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
273 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
274 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
275 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
276 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
277 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
290 jacobiEigenvector3D( t, &sys );
292 TS_ASSERT_DELTA( sys[0].first, 2.0, 1e-6 );
293 TS_ASSERT_DELTA( sys[1].first, 2.0, 1e-6 );
294 TS_ASSERT_DELTA( sys[2].first, 2.0, 1e-6 );
295 TS_ASSERT_DELTA( dot( sys[0].second, sys[1].second ), 0.0, 1e-9 );
296 TS_ASSERT_DELTA( dot( sys[1].second, sys[2].second ), 0.0, 1e-9 );
297 TS_ASSERT_DELTA( dot( sys[2].second, sys[0].second ), 0.0, 1e-9 );
298 TS_ASSERT_DELTA( length( sys[0].second ), 1.0, 1e-9 );
299 TS_ASSERT_DELTA( length( sys[1].second ), 1.0, 1e-9 );
300 TS_ASSERT_DELTA( length( sys[2].second ), 1.0, 1e-9 );
312 std::vector< double > d( 3 );
321 d = getEigenvaluesCardano( t );
323 TS_ASSERT_DELTA( d[ 0 ], 3.0, 1e-6 );
324 TS_ASSERT_DELTA( d[ 1 ], 2.0, 1e-6 );
325 TS_ASSERT_DELTA( d[ 2 ], 1.0, 1e-6 );
332 d = getEigenvaluesCardano( t );
334 TS_ASSERT_DELTA( d[ 0 ], 2.0, 1e-6 );
335 TS_ASSERT_DELTA( d[ 1 ], 1.0, 1e-6 );
336 TS_ASSERT_DELTA( d[ 2 ], -3.0, 1e-6 );
343 d = getEigenvaluesCardano( t );
345 TS_ASSERT_DELTA( d[ 0 ], 2.0, 1e-6 );
346 TS_ASSERT_DELTA( d[ 1 ], 2.0, 1e-6 );
347 TS_ASSERT_DELTA( d[ 2 ], 1.0, 1e-6 );
354 d = getEigenvaluesCardano( t );
356 TS_ASSERT_DELTA( d[ 0 ], -1.0, 1e-6 );
357 TS_ASSERT_DELTA( d[ 1 ], -1.0, 1e-6 );
358 TS_ASSERT_DELTA( d[ 2 ], -1.0, 1e-6 );
365 d = getEigenvaluesCardano( t );
367 TS_ASSERT_DELTA( d[ 0 ], 1.0, 1e-6 );
368 TS_ASSERT_DELTA( d[ 1 ], 1.0, 1e-6 );
369 TS_ASSERT_DELTA( d[ 2 ], 0.0, 1e-6 );
376 d = getEigenvaluesCardano( t );
378 TS_ASSERT_DELTA( d[ 0 ], 0.0, 1e-6 );
379 TS_ASSERT_DELTA( d[ 1 ], 0.0, 1e-6 );
380 TS_ASSERT_DELTA( d[ 2 ], 0.0, 1e-6 );
384 t( 0, 0 ) = 2.000001;
386 t( 2, 2 ) = 1.999998;
388 d = getEigenvaluesCardano( t );
390 TS_ASSERT_DELTA( d[ 0 ], 2.000001, 1e-6 );
391 TS_ASSERT_DELTA( d[ 1 ], 1.999998, 1e-6 );
392 TS_ASSERT_DELTA( d[ 2 ], 0.0, 1e-6 );
396 t( 0, 0 ) = 3.824572321236e10;
400 d = getEigenvaluesCardano( t );
402 TS_ASSERT_DELTA( d[ 0 ], 3.824572321236e10, 1e-6 );
403 TS_ASSERT_DELTA( d[ 1 ], 2.0, 1e-6 );
404 TS_ASSERT_DELTA( d[ 2 ], 1.0, 1e-6 );
408 t( 0, 0 ) = 3.824572321236e-30;
412 d = getEigenvaluesCardano( t );
414 TS_ASSERT_DELTA( d[ 0 ], 2.0, 1e-6 );
415 TS_ASSERT_DELTA( d[ 1 ], 1.0, 1e-6 );
416 TS_ASSERT_DELTA( d[ 2 ], 3.824572321236e-30, 1e-6 );
428 d = getEigenvaluesCardano( t );
430 TS_ASSERT_DELTA( d[ 0 ], 3.0, 1e-6 );
431 TS_ASSERT_DELTA( d[ 1 ], 2.0, 1e-6 );
432 TS_ASSERT_DELTA( d[ 2 ], 1.0, 1e-6 );
461 d = getEigenvaluesCardano( t );
463 TS_ASSERT_DELTA( d[ 0 ], 2.0, 1e-6 );
464 TS_ASSERT_DELTA( d[ 1 ], 2.0, 1e-6 );
465 TS_ASSERT_DELTA( d[ 2 ], 2.0, 1e-6 );
484 TS_ASSERT_DELTA( s( 0, 0 ), tens( 0, 0 ), 1e-4 );
485 TS_ASSERT_DELTA( s( 0, 1 ), tens( 0, 1 ), 1e-4 );
486 TS_ASSERT_DELTA( s( 0, 2 ), tens( 0, 2 ), 1e-4 );
487 TS_ASSERT_DELTA( s( 1, 1 ), tens( 1, 1 ), 1e-4 );
488 TS_ASSERT_DELTA( s( 1, 2 ), tens( 1, 2 ), 1e-4 );
489 TS_ASSERT_DELTA( s( 2, 2 ), tens( 2, 2 ), 1e-4 );
505 template< std::
size_t dim,
typename Data_T >
511 WAssert( i != j,
"" );
513 double s = sin( angle );
514 double c = cos( angle );
516 for( std::size_t k = 0; k < dim; ++k )
518 if( k == i || k == j )
530 std::swap( t( i, j ), t( j, i ) );
534 res( 0, 0 ) = r( 0, 0 );
535 res( 1, 0 ) = r( 1, 0 );
536 res( 2, 0 ) = r( 2, 0 );
537 res( 1, 1 ) = r( 1, 1 );
538 res( 2, 1 ) = r( 2, 1 );
539 res( 2, 2 ) = r( 2, 2 );
551 template< std::
size_t dim,
typename Data_T >
555 for( std::size_t i = 0; i < dim; ++i )
557 for( std::size_t j = 0; j < dim; ++j )
559 t( j, i ) = sys[i].second[j];
560 r( j, i ) = sys[i].first * sys[i].second[j];
565 for( std::size_t i = 0; i < dim; ++i )
567 for( std::size_t j = 0; j < dim; ++j )
569 TS_ASSERT_DELTA( t( i, j ), r( i, j ), 1e-15 );
629 t( 0, 0, 0, 0 ) = 2.5476;
630 t( 1, 1, 1, 1 ) = 3.5476;
631 t( 2, 2, 2, 2 ) = 4.5476;
632 t( 0, 0, 0, 1 ) = 5.5476;
633 t( 0, 0, 0, 2 ) = 6.5476;
634 t( 1, 1, 1, 0 ) = 7.5476;
635 t( 1, 1, 1, 2 ) = 8.5476;
636 t( 2, 2, 2, 0 ) = 9.5476;
637 t( 2, 2, 2, 1 ) = 10.5476;
638 t( 0, 0, 1, 2 ) = 11.5476;
639 t( 1, 1, 0, 2 ) = 12.5476;
640 t( 2, 2, 0, 1 ) = 13.5476;
641 t( 0, 0, 1, 1 ) = 14.5476;
642 t( 0, 0, 2, 2 ) = 15.5476;
643 t( 1, 1, 2, 2 ) = 16.5476;
646 std::vector< WVector3d > gradients;
647 gradients.push_back(
WVector3d( 1.0, 0.0, 0.0 ) );
648 gradients.push_back(
WVector3d( 0.0, 1.0, 0.0 ) );
649 gradients.push_back( normalize(
WVector3d( 1.0, 1.0, 0.0 ) ) );
650 gradients.push_back( normalize(
WVector3d( 0.3, 0.4, 0.5 ) ) );
651 gradients.push_back( normalize(
WVector3d( -7.0, 3.0, -1.0 ) ) );
653 for(
int k = 0; k < 5; ++k )
655 double res =
calcTens( t, gradients[ k ] );
656 TS_ASSERT_DELTA( res, evaluateSphericalFunction( t, gradients[ k ] ), 0.001 );
666 one( 0, 0 ) =
one( 1, 1 ) =
one( 2, 2 ) = 1.0;
748 for(
int a = 0; a < 3; ++a )
750 for(
int b = 0; b < 3; ++b )
752 for(
int c = 0; c < 3; ++c )
754 for(
int d = 0; d < 3; ++d )
756 res += v[ a ] * v[ b ] * v[ c ] * v[ d ] * t( a, b, c, d );
Test class for some tensor functions.
WTensorSym< 2, dim, Data_T > similarity_rotate_givens(WTensorSym< 2, dim, Data_T > const &m, std::size_t i, std::size_t j, double angle)
A helper function performing a similarity transform using a givens rotation.
void testJacobiEigenvectors()
Test the jacobi eigenvector calculation.
void testCardanoEigenvalues()
Test the cardano eigenvalue calculation.
void testSpecialSymMatrixEigenvalueTestCaseNumericalStability(void)
The eigenvalue of the symmetrical matrix: 0.000179516, 2.09569e-05, 2.76557e-06, 0....
void compare_results(WTensorSym< 2, dim, Data_T > const &m, RealEigenSystem const &sys)
Test if the given vectors are eigenvectors to the given eigenvalues of a symmetric matrix.
void testLogAndExp()
Test if tensor log and exp functions behave correctly.
Test class for all tensor operators.
WTensor< 2, 3, int > zero
a test tensor
WTensor< 2, 3, int > res1
a test tensor
WTensor< 2, 3, int > rdm1
a test tensor
WTensor< 2, 3, int > res2
a test tensor
WTensorSym< 2, 3, int > szero
a test tensor
double calcTens(WTensorSym< 4, 3, double > const &t, WVector3d const &v)
A helper function that implements the simple approach to tensor evaluation.
WTensorSym< 2, 3, int > srdm1
a test tensor
WTensor< 2, 3, int > res4
a test tensor
WTensor< 2, 3, int > rdm2
a test tensor
void testMultiplyTensorsOperator()
Test order 2 tensor multiplication.
WTensor< 2, 3, int > one
a test tensor
void testEvaluateSphericalFunction()
The optimizations for symmetric tensors should not corrupt the result.
void setUp()
Initialize a lot of tensors.
WTensor< 2, 3, int > res3
a test tensor
WTensorSym< 2, 3, int > sone
a test tensor
WTensorSym< 2, 3, int > srdm2
a test tensor
Implements a symmetric tensor that has the same number of components in every direction.
Implements a tensor that has the same number of components in every direction.