Boost logo

Ublas :

From: Vyacheslav E. Andrejev (vandrejev_at_[hidden])
Date: 2005-02-25 02:21:57


Hello All

vector_norm_2::apply has a little bug when compiled with BOOST_UBLAS_SCALED_NORM
defined. If first component of vector is zero, returned result will be division
zero by zero (Quiet NaN). I suggest following modification of code (from
boost/numeric/ublas/functional.hpp, lines 435-463):

        template<class E>
        static BOOST_UBLAS_INLINE
        result_type apply (const vector_expression<E> &e) {
#ifndef BOOST_UBLAS_SCALED_NORM
            real_type t = real_type ();
            size_type size (e ().size ());
            for (size_type i = 0; i < size; ++ i) {
                real_type u (type_traits<value_type>::norm_2 (e () (i)));
                t += u * u;
            }
            return type_traits<real_type>::sqrt (t);
#else
            real_type scale = real_type ();
            real_type sum_squares (1);
            size_type size (e ().size ());
            for (size_type i = 0; i < size; ++ i) {
                real_type u (type_traits<value_type>::norm_2 (e () (i)));
                if (0 == u) continue;
                if (scale < u) {
                    real_type v (scale / u);
                    sum_squares = sum_squares * v * v + real_type (1);
                    scale = u;
                } else {
                    real_type v (u / scale);
                    sum_squares += v * v;
                }
            }
            return scale * type_traits<real_type>::sqrt (sum_squares);
#endif
        }

-- 
Vyacheslav E. Andrejev
System Architect, Excelsior, LLC