是否有C库实现张量积?
问题描述:
在数学,我可以通过做张量积计算内置Dot[]
,例如,这里是一个张pts
秩6
:是否有C库实现张量积?
SeedRandom[10];
pts = RandomReal[1, {7, 7, 7, 5, 6, 5}];
(*coeffs = {c1, c2, c3, c4}*)
coeff = BernsteinBasis[#1, Range[0, #1], #2] & @@@
[email protected]{(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编写的。
,所以我想知道:
- 有Ç库,实现了
Dot[]
操作?
答
您可能不得不使用BLAS矩阵产品例程,如带有C接口的来模拟张量积。它基本上是重做那些C++张量库所做的。
你的张量积
c1(1x7) * pts(7x7x7x5x6x5)
可以被看作是一个矩阵乘积
c1(1x7) * mat_pts(7x(7*7*5*6*5*6))
pts
其中是6-d张量,但mat_pts
是2- d矩阵。
因此,您可以使用矩阵乘积来计算张量积。
您可能必须使用带有C接口的BLAS矩阵产品例程(如[cblas_gemm()](https://software.intel.com/zh-cn/node/520775)来模拟张量积。它基本上是重做那些C++张量库所做的。 – kangshiyin
@ kangshiyin感谢您的建议。事实上,我需要一个张量积的c库来通过* Mathematica LibraryLink * wrapper实现'b-spline函数'。请参阅[这里](http://mathematica.stackexchange.com/questions/120384/why-does-this-librarylink-function-crash) – xyz
我相信它也可以通过矩阵产品来实现。 – kangshiyin