[英]How to calculate “Kronecker Product” of a vector and a matrix with numpy
I need to compute the following forumula:我需要计算以下公式:
Fromula in TeX: TeX 中的公式:
$\sum_n^N \sum_m^N a_n * a_m * C_{nm}$
Peudocode:伪代码:
a = array of length N
C = NxN matrix
retval = 0
for n in range(N):
for m in range(N):
retval += a[n] * a[m] * C[n][m]
If a
were a NxN matrix constructed as in the product above one could simply use np.kron
for the Kronecker Matrix multiplication and then use np.sum
to get the desired result.如果
a
是按上述乘积构造的 NxN 矩阵,则可以简单地使用np.kron
进行 Kronecker 矩阵乘法,然后使用np.sum
来获得所需的结果。 However I don't know a faster numpy way of constructing a matrix A as in the formula above.但是,我不知道更快的 numpy 构造矩阵 A 的方法,如上面的公式。
Any ideas?有任何想法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.