Commit 30469a63 authored by Stefan Philips's avatar Stefan Philips
Browse files

[ADD] isIdentity and isSquare functions to WMatrix

parent 9f5d40dc
......@@ -195,6 +195,19 @@ public:
}
}
/**
* Returns true if the matrix is a square matrix.
* \return true for square matrixes, otherwise false.
*/
bool isSquare() const;
/**
* Returns true if the matrix is a identity matrix.
* \param delta - tolerance parameter when checking the values.
* \return true for identity matrixes, otherwise false.
*/
bool isIdentity( T delta = T( 0.0 ) ) const;
protected:
private:
size_t m_nbCols; //!< Number of columns of the matrix. The number of rows will be computed by (size/m_nbCols).
......@@ -454,6 +467,33 @@ template< typename T > WVector3d WMatrix< T >::operator*( const WVector3d& rhs )
return result;
}
template< typename T > bool WMatrix< T >::isSquare() const
{
return getNbRows() == getNbCols();
}
template< typename T > bool WMatrix< T >::isIdentity( T delta ) const
{
if( !isSquare() )
{
return false;
}
for( size_t row = 0; row < getNbRows(); row++ )
{
for( size_t col = 0; col < getNbCols(); col++ )
{
T val = ( *this )( row, col );
T expected = ( row == col ? T( 1.0 ) : T( 0.0 ) );
if( std::fabs( val - expected ) > delta )
{
return false;
}
}
}
return true;
}
template< typename T >
inline std::ostream& operator<<( std::ostream& os, const WMatrix< T >& m )
{
......
......@@ -247,6 +247,28 @@ public:
for( size_t col = 0; col < nbCols; col++ )
TS_ASSERT_EQUALS( matrixTransposed( col, row ), ( row+1 )*10 + col + 1 );
}
/**
* Test isIdentity method of WMatrix
*/
void testIsIdentity( void )
{
WMatrix< double > a( 3, 2 );
a.makeIdentity();
TS_ASSERT_EQUALS( a.isIdentity(), false );
WMatrix< double > b( 3, 3 );
b.makeIdentity();
TS_ASSERT_EQUALS( b.isIdentity(), true );
b( 0, 0 ) += 1e-3;
TS_ASSERT_EQUALS( b.isIdentity(), false );
TS_ASSERT_EQUALS( b.isIdentity( 1e-3 ), true );
b( 0, 1 ) += 2e-3;
TS_ASSERT_EQUALS( b.isIdentity( 1e-3 ), false );
TS_ASSERT_EQUALS( b.isIdentity( 2e-3 ), true );
}
};
#endif // WMATRIX_TEST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment