From 9bcf20f455512ff59cff79e2a667f70e39b67c7c Mon Sep 17 00:00:00 2001 From: Gunter Winkler Date: Sun, 11 Apr 2010 20:11:14 +0000 Subject: [PATCH] fix #3539: merged [61135], [61136], [61137] into release boost/numeric/ublas/traits.hpp: fixed complex_traits libs/numeric/ublas/test/Jamfile.v2: added testcase libs/numeric/ublas/test/test_complex_norms.cpp: new testcase libs/numeric/ublas/doc/release_notes.htm: updated release notes [SVN r61206] --- doc/release_notes.htm | 9 +++ include/boost/numeric/ublas/traits.hpp | 12 ++-- test/Jamfile.v2 | 2 + test/test_complex_norms.cpp | 82 ++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 test/test_complex_norms.cpp diff --git a/doc/release_notes.htm b/doc/release_notes.htm index c8833ad6..a731a5ac 100644 --- a/doc/release_notes.htm +++ b/doc/release_notes.htm @@ -25,6 +25,15 @@ diff --git a/include/boost/numeric/ublas/traits.hpp b/include/boost/numeric/ublas/traits.hpp index acfaa7ad..857ac5d5 100644 --- a/include/boost/numeric/ublas/traits.hpp +++ b/include/boost/numeric/ublas/traits.hpp @@ -220,8 +220,10 @@ namespace boost { namespace numeric { namespace ublas { static BOOST_UBLAS_INLINE real_type norm_1 (const_reference t) { - return type_traits::type_abs (self_type::real (t)) + - type_traits::type_abs (self_type::imag (t)); + return self_type::type_abs (t); + // original computation has been replaced because a complex number should behave like a scalar type + // return type_traits::type_abs (self_type::real (t)) + + // type_traits::type_abs (self_type::imag (t)); } static BOOST_UBLAS_INLINE @@ -231,8 +233,10 @@ namespace boost { namespace numeric { namespace ublas { static BOOST_UBLAS_INLINE real_type norm_inf (const_reference t) { - return (std::max) (type_traits::type_abs (self_type::real (t)), - type_traits::type_abs (self_type::imag (t))); + return self_type::type_abs (t); + // original computation has been replaced because a complex number should behave like a scalar type + // return (std::max) (type_traits::type_abs (self_type::real (t)), + // type_traits::type_abs (self_type::imag (t))); } static diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 32ca889b..80953464 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -128,4 +128,6 @@ test-suite numeric/uBLAS ] [ run test_coordinate_matrix_sort.cpp ] + [ run test_complex_norms.cpp + ] ; diff --git a/test/test_complex_norms.cpp b/test/test_complex_norms.cpp new file mode 100644 index 00000000..041972ab --- /dev/null +++ b/test/test_complex_norms.cpp @@ -0,0 +1,82 @@ +// Copyright 2010 Gunter Winkler +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include + +#include "libs/numeric/ublas/test/utils.hpp" + +using namespace boost::numeric::ublas; + +static const double TOL(1.0e-5); ///< Used for comparing two real numbers. + +BOOST_UBLAS_TEST_DEF ( test_double_complex_norm_inf ) { + typedef std::complex dComplex; + vector v(4); + for (unsigned int i = 0; i < v.size(); ++i) + v[i] = dComplex(i, i + 1); + + const double expected = abs(v[3]); + + BOOST_UBLAS_DEBUG_TRACE( "norm is " << norm_inf(v) ); + BOOST_UBLAS_TEST_CHECK(std::abs(norm_inf(v) - expected) < TOL); + v *= 3.; + BOOST_UBLAS_TEST_CHECK(std::abs(norm_inf(v) - (3.0*expected)) < TOL); +} + +BOOST_UBLAS_TEST_DEF ( test_double_complex_norm_2 ) { + typedef std::complex dComplex; + vector v(4); + for (unsigned int i = 0; i < v.size(); ++i) + v[i] = dComplex(i, i + 1); + + const double expected = sqrt(44.0); + + BOOST_UBLAS_DEBUG_TRACE( "norm is " << norm_2(v) ); + BOOST_UBLAS_TEST_CHECK(std::abs(norm_2(v) - expected) < TOL); + v *= 3.; + BOOST_UBLAS_TEST_CHECK(std::abs(norm_2(v) - (3.0*expected)) < TOL); +} + +BOOST_UBLAS_TEST_DEF ( test_float_complex_norm_inf ) { + typedef std::complex dComplex; + vector v(4); + for (unsigned int i = 0; i < v.size(); ++i) + v[i] = dComplex(i, i + 1); + + const float expected = abs(v[3]); + + BOOST_UBLAS_DEBUG_TRACE( "norm is " << norm_inf(v) ); + BOOST_UBLAS_TEST_CHECK(std::abs(norm_inf(v) - expected) < TOL); + v *= 3.; + BOOST_UBLAS_TEST_CHECK(std::abs(norm_inf(v) - (3.0*expected)) < TOL); +} + +BOOST_UBLAS_TEST_DEF ( test_float_complex_norm_2 ) { + typedef std::complex dComplex; + vector v(4); + for (unsigned int i = 0; i < v.size(); ++i) + v[i] = dComplex(i, i + 1); + + const float expected = sqrt(44.0); + + BOOST_UBLAS_DEBUG_TRACE( "norm is " << norm_2(v) ); + BOOST_UBLAS_TEST_CHECK(std::abs(norm_2(v) - expected) < TOL); + v *= 3.; + BOOST_UBLAS_TEST_CHECK(std::abs(norm_2(v) - (3.0*expected)) < TOL); +} + +int main() { + BOOST_UBLAS_TEST_BEGIN(); + + BOOST_UBLAS_TEST_DO( test_double_complex_norm_inf ); + BOOST_UBLAS_TEST_DO( test_float_complex_norm_inf ); + BOOST_UBLAS_TEST_DO( test_double_complex_norm_2 ); + BOOST_UBLAS_TEST_DO( test_float_complex_norm_2 ); + + BOOST_UBLAS_TEST_END(); +}