Commit fdc07bd7 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[MERGE]

parents 036ad863 0cdc95f4
......@@ -156,6 +156,21 @@ T factorial( T i )
return res;
}
/**
* Checks if two values are equal within a given delta.
*
* \tparam The floating point type.
* \param a The first value.
* \param b The second value.
* \param delta The tolerance parameter.
* \return True if both values are equal within the delta, otherwise false.
*/
template< typename T >
T areEqual( T a, T b, T delta = T( 0 ) )
{
return ( std::fabs( a - b ) <= delta );
}
template< typename T > inline int signum( const T& value )
{
if( value < 0 )
......
......@@ -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 )
{
......
......@@ -152,7 +152,7 @@ double WSymmetricSphericalHarmonic::calcGFA( std::vector< WUnitSphereCoordinates
double d = 0.0;
double gfa = 0.0;
double mean = 0.0;
double v[ 15 ];
std::vector< double > v( orientations.size() );
for( std::size_t i = 0; i < orientations.size(); ++i )
{
......@@ -513,6 +513,8 @@ WMatrix< double > WSymmetricSphericalHarmonic::calcSHToTensorSymMatrix( std::siz
WMatrix< double > p = pseudoInverse( TEMat );
WAssert( ( TEMat*p ).isIdentity( 1e-3 ), "Test of inverse matrix failed. Are the given orientations linear independent?" );
return p * calcBaseMatrix( ori2, order );
}
......
......@@ -265,6 +265,18 @@ public:
TS_ASSERT_EQUALS( factorial( 7 ), 5040 );
TS_ASSERT_EQUALS( factorial( 8 ), 40320 );
}
/**
* Test the areEqual function.
*/
void testAreEqual( void )
{
TS_ASSERT_EQUALS( areEqual( 1.0, 1.0 ), true );
TS_ASSERT_EQUALS( areEqual( 1.0, 1.0, 1e-3 ), true );
TS_ASSERT_EQUALS( areEqual( 1.0, 1.0+1e-3, 1e-3 ), true );
TS_ASSERT_EQUALS( areEqual( 1.0, 1.0+2e-3, 1e-3 ), false );
TS_ASSERT_EQUALS( areEqual( 1.0, 1.0+1e-3 ), false );
}
};
#endif // WMATH_TEST_H
......@@ -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