From f2541bb90af059680aa7036f315f052175999355 Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Wed, 8 Apr 2015 03:09:47 +0000 Subject: Imported from /home/lorry/working-area/delta_boost-tarball/boost_1_58_0.tar.bz2. --- libs/numeric/ublas/test/common/testhelper.hpp | 64 +++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) (limited to 'libs/numeric/ublas/test/common/testhelper.hpp') diff --git a/libs/numeric/ublas/test/common/testhelper.hpp b/libs/numeric/ublas/test/common/testhelper.hpp index c4a2fb060..b554511fa 100644 --- a/libs/numeric/ublas/test/common/testhelper.hpp +++ b/libs/numeric/ublas/test/common/testhelper.hpp @@ -54,8 +54,13 @@ std::pair getResults() { template < class M1, class M2 > bool compare( const boost::numeric::ublas::matrix_expression & m1, const boost::numeric::ublas::matrix_expression & m2 ) { - size_t size1 = (std::min)(m1().size1(), m2().size1()); - size_t size2 = (std::min)(m1().size2(), m2().size2()); + if ((m1().size1() != m2().size1()) || + (m1().size2() != m2().size2())) { + return false; + } + + size_t size1 = m1().size1(); + size_t size2 = m1().size2(); for (size_t i=0; i < size1; ++i) { for (size_t j=0; j < size2; ++j) { if ( m1()(i,j) != m2()(i,j) ) return false; @@ -67,11 +72,64 @@ bool compare( const boost::numeric::ublas::matrix_expression & m1, template < class M1, class M2 > bool compare( const boost::numeric::ublas::vector_expression & m1, const boost::numeric::ublas::vector_expression & m2 ) { - size_t size = (std::min)(m1().size(), m2().size()); + if (m1().size() != m2().size()) { + return false; + } + + size_t size = m1().size(); for (size_t i=0; i < size; ++i) { if ( m1()(i) != m2()(i) ) return false; } return true; } +// Compare if two matrices or vectors are equals based on distance. + +template +typename AE::value_type mean_square(const boost::numeric::ublas::matrix_expression &me) { + typename AE::value_type s(0); + typename AE::size_type i, j; + for (i=0; i!= me().size1(); i++) { + for (j=0; j!= me().size2(); j++) { + s += boost::numeric::ublas::scalar_traits::type_abs(me()(i,j)); + } + } + return s / (me().size1() * me().size2()); +} + +template +typename AE::value_type mean_square(const boost::numeric::ublas::vector_expression &ve) { + // We could have use norm2 here, but ublas' ABS does not support unsigned types. + typename AE::value_type s(0); + typename AE::size_type i; + for (i=0; i!= ve().size(); i++) { + s += boost::numeric::ublas::scalar_traits::type_abs(ve()(i)); + } + return s / ve().size(); +} + +template < class M1, class M2 > +bool compare_to( const boost::numeric::ublas::matrix_expression & m1, + const boost::numeric::ublas::matrix_expression & m2, + double tolerance = 0.0 ) { + if ((m1().size1() != m2().size1()) || + (m1().size2() != m2().size2())) { + return false; + } + + return mean_square(m2() - m1()) <= tolerance; +} + +template < class M1, class M2 > +bool compare_to( const boost::numeric::ublas::vector_expression & m1, + const boost::numeric::ublas::vector_expression & m2, + double tolerance = 0.0 ) { + if (m1().size() != m2().size()) { + return false; + } + + return mean_square(m2() - m1()) <= tolerance; +} + + #endif -- cgit v1.2.1