是否有实现张量积的 C 库?

Is there C library that implement the tensor product?

Mathematica 中,我可以通过内置的 Dot[] 进行张量乘积计算,例如,这里有一个具有秩的张量 pts 6:

SeedRandom[10];
pts = RandomReal[1, {7, 7, 7, 5, 6, 5}];

(*coeffs = {c1, c2, c3, c4}*)
coeff = BernsteinBasis[#1, Range[0, #1], #2] & @@@ 
          Thread@{(Dimensions[pts, 4] - 1), {0.1, 0.2, 0.3, 0.4}};
(*
{{0.531441,0.354294,0.098415,0.01458,0.001215,0.000054,1.*10^-6},  
 {0.262144,0.393216,0.24576,0.08192,0.01536,0.001536,0.000064},
 {0.117649,0.302526,0.324135,0.18522,0.059535,0.010206,0.000729},
 {0.1296,0.3456,0.3456,0.1536,0.0256}}
*)

(*do tensor product calculation --->  c4.(c3.(c2.(c1.pts))) *)
Fold[#2.#1 &, pts, coeff]

我Google关键字tensor product in C,然后我发现大多数张量库都是用C++写的,而不是ANSI C。

所以我想知道:

可能需要用C接口的BLAS矩阵乘积例程如cblas_gemm()来模拟张量乘积。它基本上是重做那些 c++ 张量库已经完成的工作。

你的张量积

c1(1x7) * pts(7x7x7x5x6x5)

可以看作矩阵乘积

c1(1x7) * mat_pts(7x(7*7*5*6*5*6))

其中 pts 是 6 维张量,但 mat_pts 是二维矩阵。

这样就可以用矩阵乘积来计算张量积了。