25 #ifndef WMATRIXFIXED_TEST_H
26 #define WMATRIXFIXED_TEST_H
28 #include <cxxtest/TestSuite.h>
30 #include <boost/array.hpp>
32 #include "../WMatrixFixed.h"
33 #include "../WVectorFixed.h"
55 TS_ASSERT_THROWS_NOTHING( WMD32 matrix );
56 TS_ASSERT_THROWS_NOTHING( WMF32 matrix );
57 TS_ASSERT_THROWS_NOTHING( WMD11 matrix );
58 TS_ASSERT_THROWS_NOTHING( WMF11 matrix );
59 TS_ASSERT_THROWS_NOTHING( WMD44 matrix );
60 TS_ASSERT_THROWS_NOTHING( WMF44 matrix );
61 TS_ASSERT_THROWS_NOTHING( WMI32 matrix );
62 TS_ASSERT_THROWS_NOTHING( WMS32 matrix );
72 TS_ASSERT_THROWS_NOTHING( WMD32 matrix2( matrix ) );
76 TS_ASSERT_THROWS_NOTHING( WMD11 scalar2( scalar ) );
80 TS_ASSERT_THROWS_NOTHING( WMD13 vector2( vector ) );
95 for( std::size_t i = 0; i < 3; ++i )
97 for( std::size_t j = 0; j < 3; ++j )
99 TS_ASSERT_EQUALS( mat( i, j ), mat2( i, j ) );
109 const size_t nbRows = 3, nbCols = 2;
111 TS_ASSERT_EQUALS( matrix.
getRows(), nbRows );
112 TS_ASSERT_EQUALS( matrix.
getColumns(), nbCols );
131 for(
int i = 0; i < 9; ++i )
133 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ i ], i + 1 );
158 for( std::size_t i = 0; i < 3; ++i )
160 for( std::size_t j = 0; j < 4; ++j )
162 TS_ASSERT_EQUALS( matrix( i, j ), matrix.
at( i, j ) );
191 rowVector( 0, 0 ) = matrix( 0, 0 );
192 rowVector( 1, 0 ) = matrix( 0, 1 );
193 rowVector( 2, 0 ) = matrix( 0, 2 );
195 TS_ASSERT_EQUALS( matrix.
getRowVector( 0 )( 0, 0 ), rowVector( 0, 0 ) );
196 TS_ASSERT_EQUALS( matrix.
getRowVector( 0 )( 0, 1 ), rowVector( 1, 0 ) );
197 TS_ASSERT_EQUALS( matrix.
getRowVector( 0 )( 0, 2 ), rowVector( 2, 0 ) );
217 colVector( 0, 0 ) = matrix( 0, 1 );
218 colVector( 1, 0 ) = matrix( 1, 1 );
219 colVector( 2, 0 ) = matrix( 2, 1 );
232 rowVector( 0, 0 ) = 1;
233 rowVector( 1, 0 ) = 2;
234 rowVector( 2, 0 ) = 3;
239 TS_ASSERT_EQUALS( matrix( 0, 0 ), rowVector( 0, 0 ) );
240 TS_ASSERT_EQUALS( matrix( 0, 1 ), rowVector( 1, 0 ) );
241 TS_ASSERT_EQUALS( matrix( 0, 2 ), rowVector( 2, 0 ) );
250 colVector( 0, 0 ) = 2;
251 colVector( 1, 0 ) = 5;
252 colVector( 2, 0 ) = 8;
257 TS_ASSERT_EQUALS( matrix( 0, 1 ), colVector( 0, 0 ) );
258 TS_ASSERT_EQUALS( matrix( 1, 1 ), colVector( 1, 0 ) );
259 TS_ASSERT_EQUALS( matrix( 2, 1 ), colVector( 2, 0 ) );
268 TS_ASSERT_EQUALS( WMD13::zero()( 0, 0 ), 0.0 );
269 TS_ASSERT_EQUALS( WMD13::zero()( 0, 1 ), 0.0 );
270 TS_ASSERT_EQUALS( WMD13::zero()( 0, 2 ), 0.0 );
273 TS_ASSERT_EQUALS( WMU32::zero()( 0, 0 ), 0 );
274 TS_ASSERT_EQUALS( WMU32::zero()( 0, 1 ), 0 );
275 TS_ASSERT_EQUALS( WMU32::zero()( 1, 0 ), 0 );
276 TS_ASSERT_EQUALS( WMU32::zero()( 1, 1 ), 0 );
288 for(
int i = 0; i < 4; i++ )
290 for(
int j = 0; j < 5; ++j )
294 TS_ASSERT_EQUALS( WMU45::identity()( i, j ), 1 );
298 TS_ASSERT_EQUALS( WMU45::identity()( i, j ), 0 );
304 for(
int i = 0; i < 5; i++ )
306 for(
int j = 0; j < 4; ++j )
310 TS_ASSERT_EQUALS( WMU54::identity()( i, j ), 1 );
314 TS_ASSERT_EQUALS( WMU54::identity()( i, j ), 0 );
329 for( std::size_t i = 0; i < 3; ++i )
331 for( std::size_t j = 0; j < 3; ++j )
333 TS_ASSERT_EQUALS(
m_matrix( i, j ), matrix2( i, j ) );
342 TS_ASSERT_EQUALS( matrix2( 0, 0 ), 1 );
343 TS_ASSERT_EQUALS( matrix2( 0, 1 ), 0.0 );
344 TS_ASSERT_EQUALS( matrix2( 0, 2 ), 3 );
345 TS_ASSERT_EQUALS( matrix2( 1, 0 ), 4000 );
346 TS_ASSERT_EQUALS( matrix2( 1, 1 ), 5 );
347 TS_ASSERT_EQUALS( matrix2( 1, 2 ), -5343 );
348 TS_ASSERT_EQUALS( matrix2( 2, 0 ), 1 );
349 TS_ASSERT_EQUALS( matrix2( 2, 1 ), 0 );
350 TS_ASSERT_EQUALS( matrix2( 2, 2 ), 0 );
357 template<
typename ValueT,
size_t Rows,
size_t Cols >
372 return m_values[ row + col * Rows ];
385 const ValueT&
operator()(
size_t row,
size_t col )
const throw()
387 return m_values[ row + col * Rows ];
399 template <
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
402 for(
size_t row = 0; row < Rows; ++row )
404 for(
size_t col = 0; col < Cols; ++col )
406 ( row, col ) = rhs( row, col );
424 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 0 ], 1.52234 );
425 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 1 ], 4e3 );
426 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 2 ], 1 );
427 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 3 ], -0.4534 );
428 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 4 ], 5.666 );
429 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 5 ], 0 );
430 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 6 ], 3.0 );
431 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 7 ], -5343.959 );
432 TS_ASSERT_EQUALS( matrix.
m_values.m_values[ 8 ], 0.1 );
434 for( std::size_t i = 0; i < 3; ++i )
436 for( std::size_t j = 0; j < 3; ++j )
438 TS_ASSERT_EQUALS( matrix( i, j ),
m_matrix( i, j ) );
452 TS_ASSERT_EQUALS(
m_matrix( 0, 0 ), 1.52234 );
453 TS_ASSERT_EQUALS(
m_matrix( 0, 1 ), -0.4534 );
454 TS_ASSERT_EQUALS(
m_matrix( 0, 2 ), 3.0 );
455 TS_ASSERT_EQUALS(
m_matrix( 1, 0 ), 4e3 );
456 TS_ASSERT_EQUALS(
m_matrix( 1, 1 ), 5.666 );
457 TS_ASSERT_EQUALS(
m_matrix( 1, 2 ), -5343.959 );
458 TS_ASSERT_EQUALS(
m_matrix( 2, 0 ), 1 );
459 TS_ASSERT_EQUALS(
m_matrix( 2, 1 ), 0 );
460 TS_ASSERT_EQUALS(
m_matrix( 2, 2 ), 0.1 );
469 Eigen::Matrix< double, 3, 3 > emat =
m_matrix;
470 for( std::size_t i = 0; i < 3; ++i )
472 for( std::size_t j = 0; j < 3; ++j )
474 TS_ASSERT_EQUALS( emat( i, j ),
m_matrix( i, j ) );
479 for( std::size_t i = 0; i < 3; ++i )
481 for( std::size_t j = 0; j < 3; ++j )
483 TS_ASSERT_EQUALS( matrix2( i, j ),
m_matrix( i, j ) );
500 TS_ASSERT( mi( 0, 0 ) == 0 );
501 TS_ASSERT( mi( 1, 0 ) == 1 );
502 TS_ASSERT( mi( 0, 1 ) == 2 );
503 TS_ASSERT( mi( 1, 1 ) == 3 );
537 TS_ASSERT_EQUALS( WMI44::identity() * vec, vec );
538 TS_ASSERT_EQUALS( WMI44::zero() * vec, WMI41::zero() );
542 TS_ASSERT_EQUALS( res[ 0 ], 0 );
543 TS_ASSERT_EQUALS( res[ 1 ], -3 );
544 TS_ASSERT_EQUALS( res[ 2 ], 4 );
551 TS_ASSERT_EQUALS( WMI44::zero() * WMI44::zero(), WMI44::zero() );
552 TS_ASSERT_EQUALS( WMI44::zero() * WMI44::identity(), WMI44::zero() );
553 TS_ASSERT_EQUALS( WMI44::identity() * WMI44::zero(), WMI44::zero() );
554 TS_ASSERT_EQUALS( WMI44::identity() * WMI44::identity(), WMI44::identity() );
556 TS_ASSERT_EQUALS( matrix * WMI44::identity(), matrix );
557 TS_ASSERT_EQUALS( matrix * WMI44::zero(), WMI34::zero() );
561 mat( 0, 0 ) = mat( 2, 2 ) = 1;
563 mat( 0, 1 ) = mat( 1, 0 ) = -2;
564 mat( 0, 2 ) = mat( 2, 0 ) = 3;
565 mat( 1, 2 ) = mat( 2, 1 ) = 2;
567 WMI34 res = mat * matrix;
568 TS_ASSERT_EQUALS( res( 0, 0 ), -3 );
569 TS_ASSERT_EQUALS( res( 1, 2 ), -4 );
570 TS_ASSERT_EQUALS( res( 2, 0 ), 7 );
571 TS_ASSERT_EQUALS( res( 2, 3 ), 11 );
572 TS_ASSERT_EQUALS( res( 1, 3 ), 10 );
576 TS_ASSERT_EQUALS( mat( 0, 0 ), 14 );
577 TS_ASSERT_EQUALS( mat( 2, 2 ), 14 );
578 TS_ASSERT_EQUALS( mat( 0, 1 ), 4 );
579 TS_ASSERT_EQUALS( mat( 2, 1 ), -4 );
580 TS_ASSERT_EQUALS( mat( 1, 2 ), -4 );
591 for(
int i = 0; i < 3; i++ )
593 for(
int j = 0; j < 3; ++j )
595 TS_ASSERT_EQUALS( mat( i, j ), 2 *
m_matrix( i, j ) );
600 for(
int i = 0; i < 3; i++ )
602 for(
int j = 0; j < 3; ++j )
604 TS_ASSERT_EQUALS( mat( i, j ), 4 *
m_matrix( i, j ) );
630 TS_ASSERT_EQUALS( mat, matrix * 2 );
631 TS_ASSERT_EQUALS( mat - matrix, matrix );
660 TS_ASSERT_EQUALS( i, 24 );
661 TS_ASSERT_EQUALS( i, j( 0, 0 ) );
674 TS_ASSERT_DELTA( length( vec ), 5.0, 1e-10 );
675 TS_ASSERT_DELTA( length( transpose( vec ) ), 5.0, 1e-10 );
681 TS_ASSERT_DELTA( length( vec ), sqrt( 3.0 ), 1e-10 );
682 TS_ASSERT_DELTA( length( transpose( vec ) ), sqrt( 3.0 ), 1e-10 );
700 TS_ASSERT_DELTA( distance( vec1, vec2 ), 5.0, 1e-10 );
701 TS_ASSERT_DELTA( distance( transpose( vec1 ), transpose( vec2 ) ), 5.0, 1e-10 );
711 TS_ASSERT_DELTA( distance( vec1, vec2 ), sqrt( 10.0 ), 1e-10 );
712 TS_ASSERT_DELTA( distance( transpose( vec1 ), transpose( vec2 ) ), sqrt( 10.0 ), 1e-10 );
726 TS_ASSERT_EQUALS( normalize( vec )[ 0 ], 1.0 );
727 TS_ASSERT_DELTA( length( normalize( vec ) ), 1.0, 1e-10 );
733 TS_ASSERT_DELTA( length( normalize( vec ) ), 1.0, 1e-10 );
743 for(
int i = 0; i < 3; i++ )
745 for(
int j = 0; j < 3; ++j )
762 WMF11 mat2 = invert( mat );
763 TS_ASSERT_EQUALS( mat2( 0, 0 ), 0.5f );
774 TS_ASSERT( ( matrix !=
m_matrix ) ==
false );
779 TS_ASSERT( ( matrix ==
m_matrix ) ==
false );
784 TS_ASSERT( ( matrix ==
m_matrix ) ==
false );
789 TS_ASSERT( ( matrix ==
m_matrix ) ==
false );
799 for( std::size_t i = 0; i < 3; ++i )
801 for( std::size_t j = 0; j < 3; ++j )
803 TS_ASSERT_EQUALS( mat.
at( j, i ),
m_matrix.
at( i, j ) );
823 for( std::size_t i = 0; i < 3; ++i )
825 for( std::size_t j = 0; j < 4; ++j )
827 TS_ASSERT_EQUALS( mat.
at( j, i ), matrix.
at( i, j ) );
858 TS_ASSERT_EQUALS( s.str(),
"1;2;3;10;4;5;6;11;7;8;9;12;" );
863 TS_ASSERT_EQUALS( matrix, matrix2 );
A data store with the specified dimensions and type.
A class used for a test with different data storage, we use column major order.
boost::array< ValueT, Rows *Cols > m_values
The value array. Stored column-major.
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...
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...
ValueStore< ValueT, Rows, Cols > & operator=(RHSValueStoreT< RHSValueT, Rows, Cols > const &rhs)
Replaces the values in this array.
void testMatrixMultiplication()
Test matrix multiplication.
void testMatrixInverse()
Test matrix inversion.
void testTranspose()
Test transpose method.
void testMatrixAdditionAndSubtraction()
Matrix addition and subtraction.
void testDistance()
Test vector distance.
void testSetColVector()
Check if setColumnVector() sets the matrix contents correctly.
void testDot()
Test the dot product.
void testAssignmentMatchingOrDifferentType()
Assignment from matrices with matching or different integral types should work correctly.
void testGetColVector()
Check if getColumnVector() returns the correct contents.
void setUp()
Set up a matrix used for a lot of tests.
void testAssignmentDifferentStorage()
Assignment from matrices with different storage types should work correctly.
void testGetRowVector()
Check if getRowVector() returns the correct contents.
void testSetRowVector()
Check if setRowVector() sets the matrix contents correctly.
void testGetNbRowsAndCols(void)
Number of rows and columns should be returned correctly.
void testEqualityOperators()
Test for equality comparison of two matrices.
void testNormalize()
Test vector normalization.
void testStreamOperators()
Test stream operators.
void testInstantiation(void)
Instantiation should throw nothing.
void testZero()
The zero function should return a matrix that contains only zeros.
void testAccessOperator()
Tests the access operator for the standard storage type.
void testAt()
Check if at() correctly checks for out of bounds indices and returns the same values as operator ().
WMatrixFixed< double, 3, 3 > m_matrix
A matrix used for a lot of tests.
void testMatrixTimesScalar()
Matrix-scalar multiplication.
void testLength()
Test vector length.
void testIdentity()
Tests the identity function.
void testEigen3Coversion()
Matrices should be converted to eigen3 matrices correctly.
void testCopyInstantiation(void)
Instantiation with copy constructor should throw nothing.
void testSelfAssignment()
Test self-assignment.
void testConversion()
Test conversion between several matrix types.
A fixed size matrix class.
WMatrixFixed< ValueT, Rows, 1 > getColumnVector(size_t index) const
Get a vector containing a specific column.
static MatrixType identity()
Returns an identity matrix.
size_t getColumns() const
The number of columns.
ValueStoreType m_values
The value array.
void setColumnVector(size_t index, const WMatrixFixed< RHSValueT, Rows, 1, RHSValueStoreT > &vec)
Set a column to a specific vector.
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...
size_t getRows() const
The number of rows.
void setRowVector(size_t index, const WMatrixFixed< RHSValueT, Rows, 1, RHSValueStoreT > &vec)
Set a row to a specific vector.
WMatrixFixed< ValueT, Cols, 1, ValueStoreT > getRowVector(size_t index) const
Get a vector containing a specific row.
Indicates invalid element access of a container.