25 #ifndef WMATRIXFIXED_TEST_H
26 #define WMATRIXFIXED_TEST_H
28 #include <cxxtest/TestSuite.h>
31 #include <boost/array.hpp>
34 #include "../WMatrixFixed.h"
35 #include "../WVectorFixed.h"
57 TS_ASSERT_THROWS_NOTHING( WMD32 matrix() );
58 TS_ASSERT_THROWS_NOTHING( WMF32 matrix() );
59 TS_ASSERT_THROWS_NOTHING( WMD11 matrix() );
60 TS_ASSERT_THROWS_NOTHING( WMF11 matrix() );
61 TS_ASSERT_THROWS_NOTHING( WMD44 matrix() );
62 TS_ASSERT_THROWS_NOTHING( WMF44 matrix() );
63 TS_ASSERT_THROWS_NOTHING( WMI32 matrix() );
64 TS_ASSERT_THROWS_NOTHING( WMS32 matrix() );
74 TS_ASSERT_THROWS_NOTHING( WMD32 matrix2( matrix ) );
78 TS_ASSERT_THROWS_NOTHING( WMD11 scalar2( scalar ) );
82 TS_ASSERT_THROWS_NOTHING( WMD13 vector2( vector ) );
97 for( std::size_t i = 0; i < 3; ++i )
99 for( std::size_t j = 0; j < 3; ++j )
101 TS_ASSERT_EQUALS( mat( i, j ), mat2( i, j ) );
111 const size_t nbRows = 3, nbCols = 2;
113 TS_ASSERT_EQUALS( matrix.
getRows(), nbRows );
114 TS_ASSERT_EQUALS( matrix.
getColumns(), nbCols );
133 for(
int i = 0; i < 9; ++i )
160 for( std::size_t i = 0; i < 3; ++i )
162 for( std::size_t j = 0; j < 4; ++j )
164 TS_ASSERT_EQUALS( matrix( i, j ), matrix.
at( i, j ) );
193 rowVector( 0, 0 ) = matrix( 0, 0 );
194 rowVector( 1, 0 ) = matrix( 0, 1 );
195 rowVector( 2, 0 ) = matrix( 0, 2 );
197 TS_ASSERT_EQUALS( matrix.
getRowVector( 0 )( 0, 0 ), rowVector( 0, 0 ) );
198 TS_ASSERT_EQUALS( matrix.
getRowVector( 0 )( 0, 1 ), rowVector( 1, 0 ) );
199 TS_ASSERT_EQUALS( matrix.
getRowVector( 0 )( 0, 2 ), rowVector( 2, 0 ) );
219 colVector( 0, 0 ) = matrix( 0, 1 );
220 colVector( 1, 0 ) = matrix( 1, 1 );
221 colVector( 2, 0 ) = matrix( 2, 1 );
234 rowVector( 0, 0 ) = 1;
235 rowVector( 1, 0 ) = 2;
236 rowVector( 2, 0 ) = 3;
241 TS_ASSERT_EQUALS( matrix( 0, 0 ), rowVector( 0, 0 ) );
242 TS_ASSERT_EQUALS( matrix( 0, 1 ), rowVector( 1, 0 ) );
243 TS_ASSERT_EQUALS( matrix( 0, 2 ), rowVector( 2, 0 ) );
252 colVector( 0, 0 ) = 2;
253 colVector( 1, 0 ) = 5;
254 colVector( 2, 0 ) = 8;
259 TS_ASSERT_EQUALS( matrix( 0, 1 ), colVector( 0, 0 ) );
260 TS_ASSERT_EQUALS( matrix( 1, 1 ), colVector( 1, 0 ) );
261 TS_ASSERT_EQUALS( matrix( 2, 1 ), colVector( 2, 0 ) );
270 TS_ASSERT_EQUALS( WMD13::zero()( 0, 0 ), 0.0 );
271 TS_ASSERT_EQUALS( WMD13::zero()( 0, 1 ), 0.0 );
272 TS_ASSERT_EQUALS( WMD13::zero()( 0, 2 ), 0.0 );
275 TS_ASSERT_EQUALS( WMU32::zero()( 0, 0 ), 0 );
276 TS_ASSERT_EQUALS( WMU32::zero()( 0, 1 ), 0 );
277 TS_ASSERT_EQUALS( WMU32::zero()( 1, 0 ), 0 );
278 TS_ASSERT_EQUALS( WMU32::zero()( 1, 1 ), 0 );
290 for(
int i = 0; i < 4; i++ )
292 for(
int j = 0; j < 5; ++j )
296 TS_ASSERT_EQUALS( WMU45::identity()( i, j ), 1 );
300 TS_ASSERT_EQUALS( WMU45::identity()( i, j ), 0 );
306 for(
int i = 0; i < 5; i++ )
308 for(
int j = 0; j < 4; ++j )
312 TS_ASSERT_EQUALS( WMU54::identity()( i, j ), 1 );
316 TS_ASSERT_EQUALS( WMU54::identity()( i, j ), 0 );
331 for( std::size_t i = 0; i < 3; ++i )
333 for( std::size_t j = 0; j < 3; ++j )
335 TS_ASSERT_EQUALS(
m_matrix( i, j ), matrix2( i, j ) );
344 TS_ASSERT_EQUALS( matrix2( 0, 0 ), 1 );
345 TS_ASSERT_EQUALS( matrix2( 0, 1 ), 0.0 );
346 TS_ASSERT_EQUALS( matrix2( 0, 2 ), 3 );
347 TS_ASSERT_EQUALS( matrix2( 1, 0 ), 4000 );
348 TS_ASSERT_EQUALS( matrix2( 1, 1 ), 5 );
349 TS_ASSERT_EQUALS( matrix2( 1, 2 ), -5343 );
350 TS_ASSERT_EQUALS( matrix2( 2, 0 ), 1 );
351 TS_ASSERT_EQUALS( matrix2( 2, 1 ), 0 );
352 TS_ASSERT_EQUALS( matrix2( 2, 2 ), 0 );
359 template<
typename ValueT,
size_t Rows,
size_t Cols >
374 return m_values[ row + col * Rows ];
387 const ValueT&
operator()(
size_t row,
size_t col )
const throw()
389 return m_values[ row + col * Rows ];
401 template <
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
404 for(
size_t row = 0; row < Rows; ++row )
406 for(
size_t col = 0; col < Cols; ++col )
408 ( row, col ) = rhs( row, col );
426 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 0 ], 1.52234 );
427 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 1 ], 4e3 );
428 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 2 ], 1 );
429 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 3 ], -0.4534 );
430 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 4 ], 5.666 );
431 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 5 ], 0 );
432 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 6 ], 3.0 );
433 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 7 ], -5343.959 );
434 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 8 ], 0.1 );
436 for( std::size_t i = 0; i < 3; ++i )
438 for( std::size_t j = 0; j < 3; ++j )
440 TS_ASSERT_EQUALS( matrix( i, j ),
m_matrix( i, j ) );
454 TS_ASSERT_EQUALS(
m_matrix( 0, 0 ), 1.52234 );
455 TS_ASSERT_EQUALS(
m_matrix( 0, 1 ), -0.4534 );
456 TS_ASSERT_EQUALS(
m_matrix( 0, 2 ), 3.0 );
457 TS_ASSERT_EQUALS(
m_matrix( 1, 0 ), 4e3 );
458 TS_ASSERT_EQUALS(
m_matrix( 1, 1 ), 5.666 );
459 TS_ASSERT_EQUALS(
m_matrix( 1, 2 ), -5343.959 );
460 TS_ASSERT_EQUALS(
m_matrix( 2, 0 ), 1 );
461 TS_ASSERT_EQUALS(
m_matrix( 2, 1 ), 0 );
462 TS_ASSERT_EQUALS(
m_matrix( 2, 2 ), 0.1 );
471 Eigen::Matrix< double, 3, 3 > emat =
m_matrix;
472 for( std::size_t i = 0; i < 3; ++i )
474 for( std::size_t j = 0; j < 3; ++j )
476 TS_ASSERT_EQUALS( emat( i, j ),
m_matrix( i, j ) );
481 for( std::size_t i = 0; i < 3; ++i )
483 for( std::size_t j = 0; j < 3; ++j )
485 TS_ASSERT_EQUALS( matrix2( i, j ),
m_matrix( i, j ) );
502 TS_ASSERT( mi( 0, 0 ) == 0 );
503 TS_ASSERT( mi( 1, 0 ) == 1 );
504 TS_ASSERT( mi( 0, 1 ) == 2 );
505 TS_ASSERT( mi( 1, 1 ) == 3 );
539 TS_ASSERT_EQUALS( WMI44::identity() * vec, vec );
540 TS_ASSERT_EQUALS( WMI44::zero() * vec, WMI41::zero() );
544 TS_ASSERT_EQUALS( res[ 0 ], 0 );
545 TS_ASSERT_EQUALS( res[ 1 ], -3 );
546 TS_ASSERT_EQUALS( res[ 2 ], 4 );
553 TS_ASSERT_EQUALS( WMI44::zero() * WMI44::zero(), WMI44::zero() );
554 TS_ASSERT_EQUALS( WMI44::zero() * WMI44::identity(), WMI44::zero() );
555 TS_ASSERT_EQUALS( WMI44::identity() * WMI44::zero(), WMI44::zero() );
556 TS_ASSERT_EQUALS( WMI44::identity() * WMI44::identity(), WMI44::identity() );
558 TS_ASSERT_EQUALS( matrix * WMI44::identity(), matrix );
559 TS_ASSERT_EQUALS( matrix * WMI44::zero(), WMI34::zero() );
563 mat( 0, 0 ) = mat( 2, 2 ) = 1;
565 mat( 0, 1 ) = mat( 1, 0 ) = -2;
566 mat( 0, 2 ) = mat( 2, 0 ) = 3;
567 mat( 1, 2 ) = mat( 2, 1 ) = 2;
569 WMI34 res = mat * matrix;
570 TS_ASSERT_EQUALS( res( 0, 0 ), -3 );
571 TS_ASSERT_EQUALS( res( 1, 2 ), -4 );
572 TS_ASSERT_EQUALS( res( 2, 0 ), 7 );
573 TS_ASSERT_EQUALS( res( 2, 3 ), 11 );
574 TS_ASSERT_EQUALS( res( 1, 3 ), 10 );
578 TS_ASSERT_EQUALS( mat( 0, 0 ), 14 );
579 TS_ASSERT_EQUALS( mat( 2, 2 ), 14 );
580 TS_ASSERT_EQUALS( mat( 0, 1 ), 4 );
581 TS_ASSERT_EQUALS( mat( 2, 1 ), -4 );
582 TS_ASSERT_EQUALS( mat( 1, 2 ), -4 );
593 for(
int i = 0; i < 3; i++ )
595 for(
int j = 0; j < 3; ++j )
597 TS_ASSERT_EQUALS( mat( i, j ), 2 *
m_matrix( i, j ) );
602 for(
int i = 0; i < 3; i++ )
604 for(
int j = 0; j < 3; ++j )
606 TS_ASSERT_EQUALS( mat( i, j ), 4 *
m_matrix( i, j ) );
632 TS_ASSERT_EQUALS( mat, matrix * 2 );
633 TS_ASSERT_EQUALS( mat - matrix, matrix );
662 TS_ASSERT_EQUALS( i, 24 );
663 TS_ASSERT_EQUALS( i, j( 0, 0 ) );
676 TS_ASSERT_DELTA( length( vec ), 5.0, 1e-10 );
677 TS_ASSERT_DELTA( length( transpose( vec ) ), 5.0, 1e-10 );
683 TS_ASSERT_DELTA( length( vec ), sqrt( 3.0 ), 1e-10 );
684 TS_ASSERT_DELTA( length( transpose( vec ) ), sqrt( 3.0 ), 1e-10 );
702 TS_ASSERT_DELTA( distance( vec1, vec2 ), 5.0, 1e-10 );
703 TS_ASSERT_DELTA( distance( transpose( vec1 ), transpose( vec2 ) ), 5.0, 1e-10 );
713 TS_ASSERT_DELTA( distance( vec1, vec2 ), sqrt( 10.0 ), 1e-10 );
714 TS_ASSERT_DELTA( distance( transpose( vec1 ), transpose( vec2 ) ), sqrt( 10.0 ), 1e-10 );
728 TS_ASSERT_EQUALS( normalize( vec )[ 0 ], 1.0 );
729 TS_ASSERT_DELTA( length( normalize( vec ) ), 1.0, 1e-10 );
735 TS_ASSERT_DELTA( length( normalize( vec ) ), 1.0, 1e-10 );
745 for(
int i = 0; i < 3; i++ )
747 for(
int j = 0; j < 3; ++j )
764 WMF11 mat2 = invert( mat );
765 TS_ASSERT_EQUALS( mat2( 0, 0 ), 0.5f );
776 TS_ASSERT( ( matrix !=
m_matrix ) ==
false );
781 TS_ASSERT( ( matrix ==
m_matrix ) ==
false );
786 TS_ASSERT( ( matrix ==
m_matrix ) ==
false );
791 TS_ASSERT( ( matrix ==
m_matrix ) ==
false );
801 for( std::size_t i = 0; i < 3; ++i )
803 for( std::size_t j = 0; j < 3; ++j )
805 TS_ASSERT_EQUALS( mat.
at( j, i ),
m_matrix.
at( i, j ) );
825 for( std::size_t i = 0; i < 3; ++i )
827 for( std::size_t j = 0; j < 4; ++j )
829 TS_ASSERT_EQUALS( mat.
at( j, i ), matrix.
at( i, j ) );
860 TS_ASSERT_EQUALS( s.str(),
"1;2;3;10;4;5;6;11;7;8;9;12;" );
865 TS_ASSERT_EQUALS( matrix, matrix2 );
889 #endif // WMATRIXFIXED_TEST_H
boost::array< ValueT, Rows *Cols > m_values
The value array. Stored column-major.
void testMatrixTimesScalar()
Matrix-scalar multiplication.
void testMatrixInverse()
Test matrix inversion.
void testSetColVector()
Check if setColumnVector() sets the matrix contents correctly.
size_t getRows() const
The number of rows.
void testConversion()
Test conversion between several matrix types.
size_t getColumns() const
The number of columns.
A data store with the specified dimensions and type.
void testSetRowVector()
Check if setRowVector() sets the matrix contents correctly.
void testGetColVector()
Check if getColumnVector() returns the correct contents.
void testAccessOperator()
Tests the access operator for the standard storage type.
void testGetRowVector()
Check if getRowVector() returns the correct contents.
void testAssignmentDifferentStorage()
Assignment from matrices with different storage types should work correctly.
void testZero()
The zero function should return a matrix that contains only zeros.
void testEqualityOperators()
Test for equality comparison of two matrices.
void testLength()
Test vector length.
Indicates invalid element access of a container.
const ValueT & operator()(size_t row, size_t col) const
Returns a const reference to the component of an row and column in order to provide access to the com...
void testAt()
Check if at() correctly checks for out of bounds indices and returns the same values as operator ()...
void testNormalize()
Test vector normalization.
void testTranspose()
Test transpose method.
void testInstantiation(void)
Instantiation should throw nothing.
void testSelfAssignment()
Test self-assignment.
A fixed size matrix class.
void testDistance()
Test vector distance.
void testGetNbRowsAndCols(void)
Number of rows and columns should be returned correctly.
void setColumnVector(size_t index, const WMatrixFixed< RHSValueT, Rows, 1, RHSValueStoreT > &vec)
Set a column to a specific vector.
void testMatrixAdditionAndSubtraction()
Matrix addition and subtraction.
ValueT m_values[Rows *Cols]
The value array.
A class used for a test with different data storage, we use column major order.
static MatrixType identity()
Returns an identity matrix.
WMatrixFixed< double, 3, 3 > m_matrix
A matrix used for a lot of tests.
ValueT & at(size_t row, size_t col)
Returns a reference to the component of an row and column in order to provide access to the component...
WMatrixFixed< ValueT, Cols, 1, ValueStoreT > getRowVector(size_t index) const
Get a vector containing a specific row.
ValueStore< ValueT, Rows, Cols > & operator=(RHSValueStoreT< RHSValueT, Rows, Cols > const &rhs)
Replaces the values in this array.
void testStreamOperators()
Test stream operators.
ValueT & operator()(size_t row, size_t col)
Returns a reference to the component of an row and column in order to provide access to the component...
void testIdentity()
Tests the identity function.
void testDot()
Test the dot product.
void setRowVector(size_t index, const WMatrixFixed< RHSValueT, Rows, 1, RHSValueStoreT > &vec)
Set a row to a specific vector.
void setUp()
Set up a matrix used for a lot of tests.
void testAssignmentMatchingOrDifferentType()
Assignment from matrices with matching or different integral types should work correctly.
void testMatrixMultiplication()
Test matrix multiplication.
ValueStoreType m_values
The value array.
void testEigen3Coversion()
Matrices should be converted to eigen3 matrices correctly.
void testCopyInstantiation(void)
Instantiation with copy constructor should throw nothing.
WMatrixFixed< ValueT, Rows, 1 > getColumnVector(size_t index) const
Get a vector containing a specific column.