1 year ago

#363582

test-img

Andre

Eigen: broadcasting does not work for right-hand side argument

The Eigen library for matrix operations supports broadcasting to perform row-wise or column-wise operations between a matrix and a vector, e.g. add a column-vector to all columns of a matrix.

However, this does not seem to work when broadcasting is used for the operand on the right-hand side. Slightly adapting the example code from the documentation:

#include <Eigen/Dense>
#include <iostream>

int main()
{
    Eigen::MatrixXd mat(2, 4);
    Eigen::VectorXd v(2);
    
    mat << 1, 2, 6, 9,
           3, 1, 7, 2;
    
    v << 1,
         2;
    
    // This works
    Eigen::MatrixXd res1 = mat.colwise() + v;
    
    // This should be equivalent since addition is commutative, but gives a compiler error
    Eigen::MatrixXd res2 = v + mat.colwise();
    
    std::cout << "Broadcasting result: " << std::endl;
    std::cout << res1 << std::endl;
}

I'm using the current release Eigen 3.4.0 and compiling with gcc 9.3. The compiler error is:

main.cpp: In function ‘int main()’:
main.cpp:19:30: error: no match for ‘operator+’ (operand types are ‘Eigen::VectorXd’ {aka ‘Eigen::Matrix<double, -1, 1>’} and ‘Eigen::DenseBase<Eigen::Matrix<double, -1, -1> >::ColwiseReturnType’ {aka ‘Eigen::VectorwiseOp<Eigen::Matrix<double, -1, -1>, 0>’})
   19 |     Eigen::MatrixXd res2 = v + mat.colwise();
      |                            ~ ^ ~~~~~~~~~~~~~
      |                            |              |
      |                            |              Eigen::DenseBase<Eigen::Matrix<double, -1, -1> >::ColwiseReturnType {aka Eigen::VectorwiseOp<Eigen::Matrix<double, -1, -1>, 0>}
      |                            Eigen::VectorXd {aka Eigen::Matrix<double, -1, 1>}
In file included from ../libs/eigen-3.4.0/Eigen/Core:19,
                 from ../libs/eigen-3.4.0/Eigen/Dense:1,
                 from main.cpp:1:
../libs/eigen-3.4.0/Eigen/src/Core/../plugins/CommonCwiseBinaryOps.h:27:1: note: candidate: ‘template<class OtherDerived> const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<typename Eigen::internal::traits<T>::Scalar, typename Eigen::internal::traits<OtherDerived>::Scalar>, const Derived, const OtherDerived> Eigen::MatrixBase<Derived>::operator+(const Eigen::MatrixBase<OtherDerived>&) const [with OtherDerived = OtherDerived; Derived = Eigen::Matrix<double, -1, 1>]’
   27 | EIGEN_MAKE_CWISE_BINARY_OP(operator+,sum)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~
../libs/eigen-3.4.0/Eigen/src/Core/../plugins/CommonCwiseBinaryOps.h:27:1: note:   template argument deduction/substitution failed:
main.cpp:19:44: note:   ‘Eigen::DenseBase<Eigen::Matrix<double, -1, -1> >::ColwiseReturnType’ {aka ‘Eigen::VectorwiseOp<Eigen::Matrix<double, -1, -1>, 0>’} is not derived from ‘const Eigen::MatrixBase<Derived>’
   19 |     Eigen::MatrixXd res2 = v + mat.colwise();
      |                                            ^

Am I doing something wrong here, is this a bug, or intentionally unsupported?

c++

eigen

0 Answers

Your Answer

Accepted video resources