将矩阵列表中的每个矩阵与向量中找到的唯一标量相乘

Multiply each matrix in a list of matrices with a unique scalar found in a vector

我有一个矩阵的 numpy 数组列表,即“3d 矩阵”(如果存在的话)。

x = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(x[:,:,None]*x[:,None,:])

[[[ 1  2  3]
  [ 2  4  6]
  [ 3  6  9]]

 [[16 20 24]
  [20 25 30]
  [24 30 36]]

 [[49 56 63]
  [56 64 72]
  [63 72 81]]]

我想将此数组中的每个矩阵乘以一个唯一的标量,但这样做:

scalars = np.array([1,2,3])
print(scalars*x[:,:,None]*x[:,None,:])

结果

[[[  1   4   9]
  [  2   8  18]
  [  3  12  27]]

 [[ 16  40  72]
  [ 20  50  90]
  [ 24  60 108]]

 [[ 49 112 189]
  [ 56 128 216]
  [ 63 144 243]]]

即每列乘以该值。我该怎么做?

我自己找到了答案。应该这样做:

print(scalars[:,None,None]*x[:,:,None]*x[:,None,:])