[eigen] Overloading componentwise binary operators for vectors

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]



Hello,

I try to overload binary operators for a VectorWrapper but I don't manage to do it.
My wrapper looks like this :

========================================================
template<typename ScalarType>
class Vector
{
    public:
        typedef Eigen::Matrix<ScalarType,Eigen::Dynamic,1> VectorType;

    private:
        VectorType wrapped_;
};
========================================================

Don't worry about names, data structure, etc, or the necessity of wrapping such a thing: it is just an example to illustrate my problem.
So, I would like to overload binary operators for Vector class, for example the sum of two vectors.
It seems that the classical version:

========================================================
template<typename ScalarType>
Vector<ScalarType> operator+(Vector<ScalarType> const & lhs, Vector<ScalarType> const & rhs)
{ return lhs.wrapped_ + right.wrapped_; }
========================================================

does not work. I guess it is because an _expression_ template is returned by operator + in Eigen.
That's why I tried this version:

========================================================
template<typename ScalarType>
 CwiseBinaryOp< internal::scalar_sum_op <ScalarType>,
                              typename const Vector<ScalarType>::VectorType,
                              typename const Vector<ScalarType>::VectorType> operator+(Vector<ScalarType> const & lhs, Vector<ScalarType> const & rhs)
{ return lhs.wrapped_ + right.wrapped_; }
========================================================

However, I obtain the following error message :

========================================================

erreur: ambiguous overload for ‘operator+’ (operand types are ‘Vector<double>’ and ‘Vector<double>’)
CPPUNIT_ASSERT_EQUAL(sum, left+right);

========================================================


in the toy unit test:


========================================================

void VectorTest::test_sum()
{
    Vector<double> left(3);
    Vector<double> right(3);
    Vector<double> sum(3);

    for (size_t i=0;i<3;++i)
    {
        left(i) = 1;
        right(i) = 2;
        sum(i) = 3;
    }
    CPPUNIT_ASSERT_EQUAL(sum, left+right);
}

========================================================


What am I doing wrong?


Best regards,


Cédric Doucet

Inria Paris-Rocquencourt

France



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/