Boost logo

Ublas :

From: Russ (c.r.coggrave_at_[hidden])
Date: 2006-04-25 10:42:54


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;
// 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 );

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.

Russ