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 @@
- [3968] fixed coordinate_matrix sort problem on MSVC10
+- [3539]
+ changed computation of
norm_inf for complex types to match
+ mathematical definition.
+ Note: This might cause a performance drop
+ because now std::abs(z) is called for each vector element.
+ The old implementation used std::max(std::abs(real(z)),std::abs(imag(z)).
+ Further norm_inf and norm_1 will now return
+ the same values for complex vector.
+
- [3501] Moved free functions in
concepts.hpp into anonymous namespace.
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();
+}