我有一个3D点的数组(n,3),它们将使用一个3x3旋转矩阵(以nx3x3数组的形式存储)围绕原点旋转。
目前,我只是在使用matmul的for循环中这样做,但我认为这是没有意义的,因为必须有一种更快的广播方式。
当前代码
n = 10
points = np.random.random([10,3])
rotation_matrices = np.tile(np.random.random([3,3]), (n,1,1))
result = []
for point in range(len(points)):
rotated_point = np.matmul(rotation_matrices[point], points[point])
result.append(rotated_point)
result = np.asarray(result)注意:在这个例子中,我只是平铺了相同的旋转矩阵,但在实际情况下,每一个3x3旋转矩阵都是不同的。
我想做什么
我猜,当点云变得非常大时,for循环会变得非常慢,所以一定有某种方式来广播这个消息。我想这么做:
np.matmul(rotation_matrices, points)其中,row中的每个points被乘以它对应的旋转矩阵。使用np.einsum可能有办法做到这一点,但我找不出签名。
发布于 2021-04-06 14:55:20
如果您看到医生,np.einsum('ij,jk', a, b)是matmul的签名。
因此,您可以尝试使用签名的np.einsum:
np.einsum('kij,kj->ki', rotation_matrices, points)测试
einsum = np.einsum('kij,kj->ki', rotation_matrices, points)
manual = np.array([np.matmul(x,y) for x,y in zip (rotation_matrices, points)])
np.allclose(einsum, manual)
# Truehttps://stackoverflow.com/questions/66970990
复制相似问题