Re: [eigen] Overloading componentwise binary operators for vectors

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



Hello Gael,

thank you for your answer.

>> This should work if your Vector class has a template constructor from an Eigen::DenseBase<Derived>. Of course, this way you will loose the benefits of Eigen's _expression_ templates.

Sure. But I don't want this.
My goal is to design a wrapper with minimal overhead.

>> Please show what are the multiple versions of operator+ found by the compiler.

Here is the complete error message.
But if you prefer, consider the minimal example I sent just before your answer.

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

In file included from /home/cdoucet/logiciels/simol/build/third-party/cppunit/install/include/cppunit/TestCase.h:6:0,
from /home/cdoucet/logiciels/simol/build/third-party/cppunit/install/include/cppunit/TestCaller.h:5,
from /home/cdoucet/logiciels/simol/build/third-party/cppunit/install/include/cppunit/extensions/HelperMacros.h:9,
from /home/cdoucet/logiciels/simol/src/test/unit/VectorTest.hpp:4,
from /home/cdoucet/logiciels/simol/src/test/unit/VectorTest.cpp:2:
/home/cdoucet/logiciels/simol/src/test/unit/VectorTest.cpp: In member function ‘void simol::VectorTest::test_sum()’:
/home/cdoucet/logiciels/simol/src/test/unit/VectorTest.cpp:31:35: erreur: ambiguous overload for ‘operator+’ (operand types are ‘simol::Vector<double>’ and ‘simol::Vector<double>’)
CPPUNIT_ASSERT_EQUAL(sum, left+right);
^
/home/cdoucet/logiciels/simol/build/third-party/cppunit/install/include/cppunit/TestAssert.h:191:32: note: in definition of macro ‘CPPUNIT_ASSERT_EQUAL’
(actual), \
^
/home/cdoucet/logiciels/simol/src/test/unit/VectorTest.cpp:31:35: note: candidates are:
CPPUNIT_ASSERT_EQUAL(sum, left+right);
^
/home/cdoucet/logiciels/simol/build/third-party/cppunit/install/include/cppunit/TestAssert.h:191:32: note: in definition of macro ‘CPPUNIT_ASSERT_EQUAL’
(actual), \
^
In file included from /home/cdoucet/logiciels/simol/src/test/unit/VectorTest.hpp:9:0,
from /home/cdoucet/logiciels/simol/src/test/unit/VectorTest.cpp:2:
/home/cdoucet/logiciels/simol/src/core/linalg/Vector.hpp:20:37: note: simol::Vector<ScalarType, WrappedLibrary> simol::operator+(const simol::Vector<ScalarType, WrappedLibrary>&, const simol::Vector<ScalarType, WrappedLibrary>&) [with ScalarType = double; WrappedLibrary = simol::eigen]
Vector<ScalarType,WrappedLibrary> operator+(Vector<ScalarType,WrappedLibrary> const & leftVector,
^
/home/cdoucet/logiciels/simol/src/core/linalg/Vector.hpp:63:54: note: typename WrappedLibrary<ScalarType>::OperationType simol::operator+(const simol::Vector<ScalarType, WrappedLibrary>&, const simol::Vector<ScalarType, WrappedLibrary>&) [with ScalarType = double; WrappedLibrary = simol::eigen; typename WrappedLibrary<ScalarType>::OperationType = Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double>, const Eigen::Matrix<double, -1, 1>, const Eigen::Matrix<double, -1, 1> >]
typename WrappedLibrary<ScalarType>::OperationType operator+(Vector<ScalarType,WrappedLibrary> const & leftVector,
^
make[2]: *** [CMakeFiles/test_simol.dir/src/test/unit/VectorTest.cpp.o] Erreur 1
make[1]: *** [CMakeFiles/test_simol.dir/all] Erreur 2
make: *** [all] Erreur 2

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



>>  Anyways, instead of writing a wrapper, maybe extending MatrixBase<> to add compatibility methods or inherit Matrix<> could work for you? See:

I know this very useful functionality of Eigen.
However, in this particular case, the main code is expected to be "weakly" dependent of third-party libraries: such libraries may be replaced by another ones if needed.
Extension would create a strong dependency between the code and Eigen.

Cédric



De: "Gael Guennebaud" <gael.guennebaud@xxxxxxxxx>
À: "eigen" <eigen@xxxxxxxxxxxxxxxxxxx>
Envoyé: Samedi 3 Octobre 2015 00:10:05
Objet: Re: [eigen] Overloading componentwise binary operators for vectors


On Fri, Oct 2, 2015 at 10:25 PM, Cedric Doucet <cedric.doucet@xxxxxxxx> wrote:
========================================================
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..

This should work if your Vector class has a template constructor from an Eigen::DenseBase<Derived>. Of course, this way you will loose the benefits of Eigen's _expression_ templates.


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

Please show what are the multiple versions of operator+ found by the compiler.

Anyways, instead of writing a wrapper, maybe extending MatrixBase<> to add compatibility methods or inherit Matrix<> could work for you? See:

gael






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