围绕原点的旋转是一个矩阵乘积,可以用numpy的点函数来完成,
import numpy as np
points = np.random.rand(100,3) # 100 X, Y, Z tuples. shape = (100,3)
rotation = np.identity(3) # null rotation for example
out = np.empty(points.shape)
for idx, point in enumerate(points):
out[idx,:] = np.dot(rotation, point)这涉及到一个for循环,或者numpy瓦片可以用来矢量化。我认为有一个涉及np.tensordot的实现,但这个函数对我来说是魔法。这个是可能的吗?
发布于 2019-01-31 18:17:35
有几种方法可以做到这一点。使用np.matmul,您可以执行以下操作:
out = np.matmul(rotation, points[:, :, np.newaxis])[:, :, 0]或者,如果您使用的是Python 3.5或更高版本:
out = (rotation @ points[:, :, np.newaxis])[:, :, 0]另一种方法是使用np.einsum
out = np.einsum('ij,nj->ni', rotation, points)最后,按照您的建议,您还可以使用np.tensordot
out = np.tensordot(points, rotation, axes=[1, 1])请注意,在本例中,points是第一个参数,rotation是第二个参数,否则输出处的尺寸将颠倒。
https://stackoverflow.com/questions/54445195
复制相似问题