Commit 5c071db4 authored by schurade's avatar schurade
Browse files

[MERGE] with f797bcfbcac464b7aa797c56b44cdaec40973a02

parents 4e24a114 f9b9d0f9
......@@ -6,3 +6,5 @@ math@informatik.uni-leipzig.de Mathias_Goldau
schurade Ralph_Schurade
ebaum Sebastian_Eichelbaum
eichelbaum@informatik.uni-leipzig.de Sebastian_Eichelbaum
cornimueller Cornelius_Mueller
hlawitschka@ucdavis.edu Mario_Hlawitschka
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WMATRIX_H
#define WMATRIX_H
#include "WValue.hpp"
namespace wmath
{
/**
* Matrix template class with variable number of rows and columns.
*/
template< typename T > class WMatrix : WValue< T >
{
public:
/**
* Produces a matrix with the given number of components.
* The components will be set to zero if T is a type representing numbers.
*/
explicit WMatrix( size_t nbRows, size_t nbCols )
: WValue< T >( nbRows * nbCols )
{
m_nbCols = nbCols;
}
/**
* Produces a matrix as copy of the one given as parameter.
*/
WMatrix( const WMatrix& newMatrix )
: WValue< T >( newMatrix )
{
m_nbCols = newMatrix.m_nbCols;
}
/**
* Get number of rows.
*/
size_t getNbRows() const
{
return this->size() / m_nbCols;
}
/**
* Get number of columns.
*/
size_t getNbCols() const
{
return m_nbCols;
}
/**
* Returns a reference to the component an row i, colums j in order to
* provide access to the component.
*/
T& operator()( size_t i, size_t j )
{
assert( j < m_nbCols && i * m_nbCols < this->size() );
return (*this)[i * m_nbCols + j];
}
/**
* Returns a const reference to the component an row i, colums j in order to
* provide read-only access to the component.
*/
const T& operator()( size_t i, size_t j ) const
{
assert( j < m_nbCols && i * m_nbCols < this->size() );
return (*this)[i * m_nbCols + j];
}
/**
* Compares two matrices and returns true if they are equal.
*/
bool operator==( const WMatrix& rhs ) const
{
return WValue< T >::operator==( rhs ) && m_nbCols == rhs.m_nbCols;
}
/**
* Compares two matrices and returns true if they are not equal.
*/
bool operator!=( const WMatrix& rhs ) const
{
return WValue< T >::operator!=( rhs ) || m_nbCols != rhs.m_nbCols;
}
/**
* Assigns the argument WMatrix to this WMatrix.
*/
WMatrix& operator=( const WMatrix& rhs )
{
WValue< T >::operator=( rhs );
m_nbCols = rhs.m_nbCols;
return *this;
}
protected:
private:
size_t m_nbCols;
};
} // End of namespace
#endif // WMATRIX_H
......@@ -277,5 +277,5 @@ template< typename T > const WValue< T > operator*( double lhs, const WValue< T
result *= lhs;
return result;
}
} // End of namepsace
} // End of namespace
#endif // WVALUE_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WMATRIX_TEST_H
#define WMATRIX_TEST_H
#include <cxxtest/TestSuite.h>
#include "../WMatrix.hpp"
using wmath::WMatrix;
class WMatrixTest : public CxxTest::TestSuite
{
public:
/**
* Instantiation should throw nothing.
*/
void testInstantiation( void )
{
TS_ASSERT_THROWS_NOTHING( WMatrix< double > matrix( 3, 2 ) );
TS_ASSERT_THROWS_NOTHING( WMatrix< float > matrix( 3, 2 ) );
}
/**
* Instantiation with copy constructor should throw nothing.
*/
void testCopyInstantiation( void )
{
WMatrix< double > matrix( 3, 2 );
TS_ASSERT_THROWS_NOTHING( WMatrix< double > matrix2( matrix ) );
}
/**
* Number of rows and columns should be return correctly.
*/
void testGetNbRowsAndCols( void )
{
const size_t nbRows = 3, nbCols = 2;
WMatrix< double > matrix( nbRows, nbCols );
TS_ASSERT_EQUALS( matrix.getNbRows(), nbRows );
TS_ASSERT_EQUALS( matrix.getNbCols(), nbCols );
}
/**
* Element access operator should work correctly.
*/
void testElementAccessOperator( void )
{
const size_t nbRows = 3, nbCols = 2;
WMatrix< double > matrix( nbRows, nbCols );
TS_ASSERT_EQUALS( matrix( 0 , 0 ), 0. );
TS_ASSERT_EQUALS( matrix( 0 , 1 ), 0. );
TS_ASSERT_EQUALS( matrix( 1 , 0 ), 0. );
TS_ASSERT_EQUALS( matrix( 1 , 1 ), 0. );
TS_ASSERT_EQUALS( matrix( 2 , 0 ), 0. );
TS_ASSERT_EQUALS( matrix( 2 , 1 ), 0. );
const double a = 3.14;
matrix( 2, 1 ) = a;
TS_ASSERT_EQUALS( matrix( 2, 1 ), a );
}
/**
* Constant element access operator should work correctly.
*/
void testConstElementAccessOperator( void )
{
const size_t nbRows = 3, nbCols = 2;
const WMatrix< double > matrix( nbRows, nbCols );
TS_ASSERT_EQUALS( matrix( 0 , 0 ), 0. );
TS_ASSERT_EQUALS( matrix( 0 , 1 ), 0. );
TS_ASSERT_EQUALS( matrix( 1 , 0 ), 0. );
TS_ASSERT_EQUALS( matrix( 1 , 1 ), 0. );
TS_ASSERT_EQUALS( matrix( 2 , 0 ), 0. );
TS_ASSERT_EQUALS( matrix( 2 , 1 ), 0. );
}
void testEqualityOperator( void )
{
const size_t nbRows = 3, nbCols = 2;
const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7;
WMatrix< double > matrix1( nbRows, nbCols );
WMatrix< double > matrix2( nbRows, nbCols );
WMatrix< double > matrix3( nbCols, nbRows );
matrix1( 0, 0 ) = a;
matrix1( 0, 1 ) = b;
matrix1( 1, 0 ) = c;
matrix1( 1, 1 ) = d;
matrix1( 2, 0 ) = e;
matrix1( 2, 1 ) = f;
matrix2( 0, 0 ) = a;
matrix2( 0, 1 ) = b;
matrix2( 1, 0 ) = c;
matrix2( 1, 1 ) = d;
matrix2( 2, 0 ) = e;
matrix2( 2, 1 ) = f;
matrix3( 0, 0 ) = a;
matrix3( 0, 1 ) = b;
matrix3( 0, 2 ) = c;
matrix3( 1, 0 ) = d;
matrix3( 1, 1 ) = e;
matrix3( 1, 2 ) = f;
TS_ASSERT_EQUALS( matrix1 == matrix2, true );
TS_ASSERT_EQUALS( matrix1 == matrix3, false );
matrix2( 0, 0 ) += 1.;
TS_ASSERT_EQUALS( matrix1 == matrix2, false );
}
void testInequalityOperator( void )
{
const size_t nbRows = 3, nbCols = 2;
const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7;
WMatrix< double > matrix1( nbRows, nbCols );
WMatrix< double > matrix2( nbRows, nbCols );
WMatrix< double > matrix3( nbCols, nbRows );
matrix1( 0, 0 ) = a;
matrix1( 0, 1 ) = b;
matrix1( 1, 0 ) = c;
matrix1( 1, 1 ) = d;
matrix1( 2, 0 ) = e;
matrix1( 2, 1 ) = f;
matrix2( 0, 0 ) = a;
matrix2( 0, 1 ) = b;
matrix2( 1, 0 ) = c;
matrix2( 1, 1 ) = d;
matrix2( 2, 0 ) = e;
matrix2( 2, 1 ) = f;
matrix3( 0, 0 ) = a;
matrix3( 0, 1 ) = b;
matrix3( 0, 2 ) = c;
matrix3( 1, 0 ) = d;
matrix3( 1, 1 ) = e;
matrix3( 1, 2 ) = f;
TS_ASSERT_EQUALS( matrix1 != matrix2, false );
TS_ASSERT_EQUALS( matrix1 != matrix3, true );
matrix2( 0, 0 ) += 1.;
TS_ASSERT_EQUALS( matrix1 != matrix2, true );
}
void testAssignmentOperator( void )
{
const size_t nbRows = 3, nbCols = 2;
const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7;
WMatrix< double > matrix1( nbRows, nbCols );
WMatrix< double > matrix2( nbRows, nbCols );
matrix1( 0, 0 ) = a;
matrix1( 0, 1 ) = b;
matrix1( 1, 0 ) = c;
matrix1( 1, 1 ) = d;
matrix1( 2, 0 ) = e;
matrix1( 2, 1 ) = f;
matrix2( 0, 0 ) = a + 1.;
matrix2( 0, 1 ) = b + 2.;
matrix2( 1, 0 ) = c + 3.;
matrix2( 1, 1 ) = d + 4.;
matrix2( 2, 0 ) = e + 5.;
matrix2( 2, 1 ) = f + 6.;
// this should be the precondition for the test
TS_ASSERT_EQUALS( matrix1 == matrix2, false );
// test simple assignment
matrix1 = matrix2;
TS_ASSERT_EQUALS( matrix1 == matrix2, true );
WMatrix< double > matrix3( nbRows, nbCols );
WMatrix< double > matrix4( nbRows, nbCols );
// this should be the precondition for the test
TS_ASSERT_EQUALS( matrix2 == matrix3, false );
TS_ASSERT_EQUALS( matrix2 == matrix4, false );
// test whether return the reference to self works
// for multiple assignment
matrix4 = matrix3 = matrix2;
TS_ASSERT_EQUALS( matrix2 == matrix3, true );
TS_ASSERT_EQUALS( matrix2 == matrix4, true );
TS_ASSERT_EQUALS( matrix3 == matrix4, true );
}
};
#endif // WMATRIX_TEST_H
......@@ -39,7 +39,6 @@ private:
double delta;
public:
/**
* Called before every test.
*/
......@@ -49,7 +48,7 @@ public:
}
/**
* Instatiation should throw nothing.
* Instantiation should throw nothing.
*/
void testInstantiation( void )
{
......@@ -166,7 +165,7 @@ public:
// this should be the precondition for the test
TS_ASSERT_EQUALS( value1 == value2, false );
// test simple assignement
// test simple assignment
value1 = value2;
TS_ASSERT_EQUALS( value1 == value2, true );
......
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