C++ 如何计算本征函数中张量的外积?

C++ 如何计算本征函数中张量的外积?,c++,eigen,C++,Eigen,在eigen中,使用以下方法可以很容易地进行张量收缩: Tensor<double, 1> tensor1; Tensor<double, 2> tensor2; // fill with data so that // tensor1 is of dimensions [10] and tensor2 of dimensions [5,10] std::array<Eigen::IndexPair<int>, 1> product_dims1

在eigen中,使用以下方法可以很容易地进行张量收缩:

Tensor<double, 1> tensor1;
Tensor<double, 2> tensor2;

// fill with data so that
// tensor1 is of dimensions [10] and tensor2 of dimensions [5,10]

std::array<Eigen::IndexPair<int>, 1> product_dims1 = { IndexPair<int>(1, 0) };

auto tensor = tensor2.contract(tensor1, product_dims1);

// now tensor is of dimensions [5]
如果必要的话,我可以很容易地编写这样一个方法,但是我感觉如果我使用本机的本征方法,它会更加优化。我还希望能够使用GPU的支持,这是很容易与伊根如果你使用本机方法


谢谢。

您可以通过重塑输入张量,用额外的一维张量填充维度,然后在新维度上广播,来实现外积

对于两个秩2和一个秩4张量,你有
C_ijkl=A_ij*B_kl
它看起来像:

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

using namespace Eigen;

int main() {

Tensor<double, 2> A_ij(4, 4);
Tensor<double, 2> B_kl(4, 4);
Tensor<double, 4> C_ijkl(4, 4, 4, 4);

Tensor<double, 4>::Dimensions A_pad(4, 4, 1, 1);
array<int, 4> A_bcast(1, 1, 4, 4);

Tensor<double, 4>::Dimensions B_pad(1, 1, 4, 4);
array<int, 4> B_bcast(4, 4, 1, 1);

C_ijkl = A_ij.reshape(A_pad).broadcast(A_bcast) * 
         B_kl.reshape(B_pad).broadcast(B_bcast);

}
#包括
#包括
使用名称空间特征;
int main(){
张量A_ij(4,4);
张量B_-kl(4,4);
张量C_ijkl(4,4,4,4);
张量:量纲A_垫(4,4,1,1);
数组A_bcast(1,1,4,4);
张量:维数B_-pad(1,1,4,4);
阵列B_bcast(4,4,1,1);
C_ijkl=A_ij.重塑(A_pad).广播(A_bcast)*
B_kl.整形(B_pad).广播(B_bcast);
}

解决方案可能太简单了:你必须在没有索引的情况下收缩

Eigen::数组空索引列表={};
张量A_ij(4,4);
张量B_-kl(4,4);
张量C_ijkl=A_ij.契约(B_kl,空索引列表);

你试过
*
操作符吗?@Kerrek这不管用,请看这里:这对我很管用,谢谢!
#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>

using namespace Eigen;

int main() {

Tensor<double, 2> A_ij(4, 4);
Tensor<double, 2> B_kl(4, 4);
Tensor<double, 4> C_ijkl(4, 4, 4, 4);

Tensor<double, 4>::Dimensions A_pad(4, 4, 1, 1);
array<int, 4> A_bcast(1, 1, 4, 4);

Tensor<double, 4>::Dimensions B_pad(1, 1, 4, 4);
array<int, 4> B_bcast(4, 4, 1, 1);

C_ijkl = A_ij.reshape(A_pad).broadcast(A_bcast) * 
         B_kl.reshape(B_pad).broadcast(B_bcast);

}