Boost logo

Ublas :

Subject: Re: [ublas] std::complex norm_inf wrong
From: Joern Ungermann (consulting_at_[hidden])
Date: 2010-04-07 03:04:58


Hi,
the culprit is in traits.hpp lines 306ff:
        static
        BOOST_UBLAS_INLINE
        real_type norm_1 (const_reference t) {
            return type_traits<real_type>::type_abs (self_type::real (t)) +
                   type_traits<real_type>::type_abs (self_type::imag (t));
        }
[...]
        static
        BOOST_UBLAS_INLINE
        real_type norm_inf (const_reference t) {
            return (std::max) (type_traits<real_type>::type_abs (self_type::real (t)),
                             type_traits<real_type>::type_abs (self_type::imag (t)));
        }
I am unsure whether these definitions make sense. This treats complex numbers as two-dimensional vectors and not as scalars. Changing it to
        static
        BOOST_UBLAS_INLINE
        real_type norm_1 (const_reference t) {
            return self_type::type_abs (t);
        }
[...]
        static
        BOOST_UBLAS_INLINE
        real_type norm_inf (const_reference t) {
            return self_type::type_abs (t);
        }

fixes the problem of the bug reporter and seems more plausible to me for handling vectors of complex numbers.

Using the bug report as a basis, this is a very simple testcase:

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <complex>
#include "utils.hpp"

using namespace boost::numeric::ublas;

static const double TOL(1.0e-5); ///< Used for comparing two real numbers.

int main(void) {
  typedef std::complex<double> dComplex;
  vector<dComplex> v(4);
  for (int i=0;i<v.size();++i)
    v[i] = dComplex( i, i+1 );

  BOOST_UBLAS_TEST_BEGIN();
  BOOST_UBLAS_TEST_CHECK(std::abs(norm_inf(v) - abs(v[3])) < TOL);
  v *= 3.;
  BOOST_UBLAS_TEST_CHECK(std::abs(norm_inf(v) - abs(v[3])) < TOL);
  BOOST_UBLAS_TEST_END();
}

Kind regards,
Jörn Ungermann

----- original message --------

Subject: [ublas] std::complex norm_inf wrong
Sent: Tue, 06 Apr 2010
From: Gunter Winkler<guwi17_at_[hidden]>

> Hello,
> has anyone a quick solution to solve
>
> https://svn.boost.org/trac/boost/ticket/3539
>
> (or at least time to make a unit test that shows the error?)
> [...]
--- original message end ----