kloop
kloop

Reputation: 4721

can't do contraction on two dimensions with eigen?

I am trying to do tensor contraction of a third rank tensor (1 x 1 x 1) on the last two dimensions with a two rank tensor (1 x 1). The result should be a vector.

The following gives an assertion:

#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>
#include <iostream>
#include <array>
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>



using namespace Eigen;
using namespace std;


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

        Eigen::Tensor<double, 1> tensor1;

        std::array<Eigen::IndexPair<int>, 1> product_dims;

        product_dims[0] =  { IndexPair<int>(1, 0) };
        product_dims[1] =  { IndexPair<int>(2, 1) };

        auto vv = tensor.contract(tensor2, product_dims);

        cerr<<"value: "<<vv<<endl;

        tensor1 = vv;
}

but prints value: 0 as expected.

The assertion is:

a.out: /eigen-eigen-7c567a7c10e1/unsupported/Eigen/CXX11/src/Tensor/TensorAssign.h:125: bool Eigen::TensorEvaluator<const Eigen::TensorAssignOp<LhsXprType, RhsXprType>, Device>::evalSubExprsIfNeeded(Eigen::TensorEvaluator<const Eigen::TensorAssignOp<LhsXprType, RhsXprType>, Device>::Scalar*) [with LeftArgType = Eigen::Tensor<double, 0>; RightArgType = const Eigen::TensorContractionOp<const std::array<Eigen::IndexPair<int>, 1ul>, const Eigen::Tensor<double, 3>, const Eigen::Tensor<double, 2> >; Device = Eigen::DefaultDevice; Eigen::TensorEvaluator<const Eigen::TensorAssignOp<LhsXprType, RhsXprType>, Device>::Scalar = double]: Assertion `dimensions_match(m_leftImpl.dimensions(), m_rightImpl.dimensions())' failed.

any ideas why I have this dimension mismatch assertion?

Upvotes: 4

Views: 769

Answers (1)

Benoit Steiner
Benoit Steiner

Reputation: 1469

Your array is one size 1, so you're effectively contracting a single pair of dimensions. You should instead create your array as follow:

std::array<Eigen::IndexPair<int>, 2> product_dims;

Upvotes: 3

Related Questions