Boost logo

Ublas :

Subject: [ublas] bounds-checking and rounding policies for ublas.vector?
From: Geoff Hilton (geoff.hilton_at_[hidden])
Date: 2009-05-12 21:20:37


Hi, I would like to have the ublas:vector operator+, operator+=,
operator- and operator-= use a custom bounds-checking/rounding policy
class (like one that can be supplied to boost.numeric.interval types
explicitly for example). How would I go about making this happen in the
least convoluted way possible?

...in pseudo-code:

namespace ublas = boost::numeric::ublas;
//...
ublas::vector<int
        ,std::vector<int>
        ,plus<int>
        ,minus<int> //not listed here
> vecOne, vecTwo, vecThree;
//fill both with values.
...
vecThree = vecOne + vecTwo;

would call the function written below (ignoring the vector_expression
abstraction) once for each element involved in the vector addition
rather than the default built-in "int ::operator+(int , int)" function:

template<>
int plus<int>::operator ()(const int lhs, const int rhs) const
{
     if (boost::integer_traits<int>::const_max - lhs < rhs) {
         //rather than cause an error, just return max.
         return boost::integer_traits<int>::const_max;
     }
     return lhs + rhs;
}

Of course the class template interface of vector shown above could be
cleaner with one policy which points to the above plus<int> and a
matching minus<int>, but I didn't want to go into more detail than was
necessary to get my point across.

Thanks,
Geoff