numpy中Kronecker积的双循环矢量化和整形

问题描述 投票:0回答:2

我有两个矩阵

import numpy as np
n = 10
a = 2*np.ones((n,n,3))
b = 3*np.ones((n,n,3))

我想以提醒Kronecker产品的方式将它们相乘,然后进行总结

s = 0
for i in range(n):
    for j in range(n):
        s +=  a*b[i,j]

是否存在将其向量化为numpy的方法?

python numpy matrix-multiplication tensor
2个回答
0
投票

您的代码可以重写为:

enter image description here

因此,这应该起作用:

s = a * np.sum(np.sum(b,axis=1),axis=0)

0
投票

也许这可以用np.einsum()更加优雅地写:

np.einsum()
© www.soinside.com 2019 - 2024. All rights reserved.