Boost logo

Ublas :

Subject: Re: [ublas] [Boost-ublas] How to use stateful functors in ublas expression
From: dariomt (dariomt_at_[hidden])
Date: 2011-09-20 10:45:44


David Bellot <david.bellot <at> gmail.com> writes:
>
> I'm not sure I understand what you want to achieve with stateful functors ?If
for example you want to do some statistics on vectors or matrices, I could
recommend using Boost.Accumulators
>
>

I want to be able to extend the ublas expression template mechanism so that I
can write expressions that apply a functor to each element of a ublas expression.

for example:

Using a stateful functor, a-la-STL

struct bounded
{
    bounded(double ub, double lb) : m_ub(ub), m_lb(lb) {}
    double operator()(double x) const {
        return std::min(m_ub, std::max(m_lb, x));
    }
};

ublas::vector<double> x, y, z, w;
x = apply_to_all(
        bounded(0, 1),
        element_prod(
            apply_to_all(
                bounded(-1, 1),
                y + z ),
            w));

Or use Boost.Function
boost::function<double(double)> f, g;
x = apply_to_all(g, element_prod( apply_to_all( f, y + z ), w));

Or use Boost.Bind, Boost.Lambda, etc...