Why does the following fail with Eigen?

2019-07-22 06:26发布

问题:

I have the following piece of code, which I hoped would do outer product between tensor2 and tensor3 and then add the result to tensor, however I get a dimension mismatch issue:

#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>
#include <iostream>

using namespace Eigen;
using namespace std;


int main()
{
        Eigen::Tensor<double, 2> tensor(1,9);
        Eigen::Tensor<double, 1> tensor2(1);
        Eigen::Tensor<double, 1> tensor3(9);

        tensor = tensor + tensor2 * tensor3;
}

The error is an assertion error (the code compiles) and it is coming on the line tensor = tensor + tensor2 * tensor. Actually, even auto v = tensor2*tensor3 doesn't work.

TensorEvaluator.h:328: Eigen::TensorEvaluator<const Eigen::TensorCwiseBinaryOp<BinaryOp, LeftArgType, RightArgType>, Device>::TensorEvaluator(const XprType&, const Device&) [with BinaryOp = Eigen::internal::scalar_product_op<double, double>; LeftArgType = const Eigen::Tensor<double, 1>; RightArgType = const Eigen::Tensor<double, 1>; Device = Eigen::DefaultDevice; Eigen::TensorEvaluator<const Eigen::TensorCwiseBinaryOp<BinaryOp, LeftArgType, RightArgType>, Device>::XprType = Eigen::TensorCwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const Eigen::Tensor<double, 1>, const Eigen::Tensor<double, 1> >]: Assertion `dimensions_match(m_leftImpl.dimensions(), m_rightImpl.dimensions())' failed.
标签: c++ eigen