每行乘以不同的旋转矩阵

时间:2019-03-21 07:51:49

标签: python numpy vectorization numpy-broadcasting

此函数将n的{​​{1}}行中的每行乘以不同的旋转矩阵。是否可以通过使用旋转矩阵的3d张量来避免循环?

pose

1 个答案:

答案 0 :(得分:2)

这里是np.einsum-

# Setup 3D rotation matrix
cs = np.empty((n,2,2))
cs[:,0,0] = c
cs[:,1,1] = c
cs[:,0,1] = s
cs[:,1,0] = -s

# Perform 3D matrix multiplications with einsum
p_out = ref[:, :d].copy()
p_out[:,:2] += np.einsum('ij,ijk->ik',pose[:,:2],cs)

或者,将c的两个分配步骤替换为一个涉及另外一个einsum的步骤-

np.einsum('ijj->ij',cs)[...] = c[:,None]

使用optimize中带有True值的np.einsum标志来利用BLAS

或者,我们可以使用np.matmul/@ operator in Python 3.x代替einsum部分-

p_out[:,:2] += np.matmul(pose[:,None,:2],cs)[:,0]