为什么下面的特征示例无法编译?

Why the following Eigen example won't compile?

本文关键字:编译 特征 为什么      更新时间:2023-10-16

我正在尝试编译这个MWE,但得到很多错误:

#include <eigen/Eigen/Core>
#include <eigen/unsupported/Eigen/CXX11/Tensor>
#include <array>
using namespace Eigen;
int main()
{
// Create 2 matrices using tensors of rank 2
Eigen::Tensor<int, 2> a(2, 3);
a.setValues({{1, 2, 3}, {6, 5, 4}});
Eigen::Tensor<int, 2> b(3, 2);
a.setValues({{1, 2}, {4, 5}, {5, 6}});
// Compute the traditional matrix product
array<IndexPair<int>, 1> product_dims = { IndexPair<int>(1, 0) };
Eigen::Tensor<int, 2> AB = a.contract(b, product_dims);
// Compute the product of the transpose of the matrices
array<IndexPair<int>, 1> transpose_product_dims = { IndexPair<int>(0, 1) };
Eigen::Tensor<int, 2> AtBt = a.contract(b, transpose_product_dims);
}

这实际上是一个本征张量的例子:

https://bitbucket.org/eigen/eigen/src/default/unsupported/Eigen/CXX11/src/Tensor/README.md?fileviewer=file-view-default

关于收缩,但我认为它有一些错误,没有正确编译,我试图修复。

错误:

1.cc:11:3: error: no member named 'setValues' in 'Eigen::Tensor<int, 2, 0, long>'
a.setValues({{1, 2, 3}, {6, 5, 4}});
~ ^
1.cc:11:13: error: expected expression
a.setValues({{1, 2, 3}, {6, 5, 4}});
            ^
1.cc:13:3: error: no member named 'setValues' in 'Eigen::Tensor<int, 2, 0, long>'
a.setValues({{1, 2}, {4, 5}, {5, 6}});
~ ^
1.cc:13:13: error: expected expression
a.setValues({{1, 2}, {4, 5}, {5, 6}});
            ^
1.cc:16:26: error: non-aggregate type 'array<IndexPair<int>, 1>' cannot be initialized with an initializer list
array<IndexPair<int>, 1> product_dims = { IndexPair<int>(1, 0) };
                         ^              ~~~~~~~~~~~~~~~~~~~~~~~~
1.cc:20:26: error: non-aggregate type 'array<IndexPair<int>, 1>' cannot be initialized with an initializer list
array<IndexPair<int>, 1> transpose_product_dims = { IndexPair<int>(0, 1) };
                         ^                        ~~~~~~~~~~~~~~~~~~~~~~~~
6 errors generated.

这个例子需要c++11,所以你需要在编译器上启用它,例如在gcc 6或clang之前使用-std=c++11和gcc。