Boost logo

Ublas :

From: Vardan Akopian (vakopian_at_[hidden])
Date: 2006-04-25 16:12:26


On 4/25/06, Russ <c.r.coggrave_at_[hidden]> wrote:
> Vardan Akopian <vakopian <at> gmail.com> writes:
>
> > It seams to me that a more generic construct would be similar to
> > vector_binary, but one, which would keep an instance f_ of the functor
> > as a member, and then call f_.apply(e1_(i), e2_(i)) instead of calling
> > functor_type::apply(e1_(i), e2_(i)).
> > Then you could keep r1 and r2 in f_ and define apply() the way you want.
> > This would be very similar to e.g. how STL containers handle custom
> comparators.
>
> In this scenario then, the call would be...
>
> // Instantiate vectors
> vector<double> v1,v2;
> vector<bool> v3, v4;
> // Create an instance of the lt_functor
> // lt_functor(v1,v2,r1,r2) implements v[i] = v1[i] < v2[i] ? r1 : r2
> lt_functor<vector<double>, vector<double>, vector<bool> > myfunctor;

Maybe I misunderstand your functor, but I thought it should be
lt_functor<double, double, bool > myfunctor;
And its apply() method should be something like

bool apply(double e1, double e2) const { return e1 < e2 ? r1_ : r2_; }

> // Setup the functor arguments
> // These could setup in the functor constructor, but just want to show
> // a more general implementation
> functor.set( true, false );
> // Perform elementwise operation on vectors
> v3 = binary_vector_elementwise( v1, v2, myfunctor );
> // Change functor setup
> functor.set( false, true );
> // Perform elementwise operation on vectors
> v4 = binary_vector_elementwise( v1, v2, myfunctor );

This is ok if binary_vector_elementwise keeps a copy of the functor as
a member variable (which it should), instead of a reference to it. But
just to be on the safe side, I would instanciate a new functor for the
second call. In fact, to force this, I would argue that you don't need
the set() method in your functor, and r1 and r2 should be setup at
construction time:

lt_functor<double, double, bool > myfunctor(true, false);
v3 = binary_vector_elementwise( v1, v2, myfunctor );
lt_functor<double, double, bool > another_functor(false, true);
v4 = binary_vector_elementwise( v1, v2, another_functor);

>
> Presumably we could have a family of such free functions:
>
> unary_vector_elementwise( v, f );
> binary_vector_elementwise( v, v, f );
> unary_matrix_elementwise( m, f );
> binary_matrix_elementwise( m, m, f );
>
> This may be a good solution, however, I am somewhat suspicious that I will
> struggle to implement this in a timely manner. Does anyone else have a interest
> in this sort of development.

Agreed.

-Vardan