【发布时间】:2019-01-30 16:29:59
【问题描述】:
关于原点的旋转是一个矩阵乘积,可以用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 tile 进行矢量化。我认为有一个涉及 np.tensordot 的实现,但该功能对我来说是巫术。这可能吗?
【问题讨论】:
-
你可以做
out = (rotation @ points[:, :, np.newaxis])[:, :, 0]。或out = np.einsum('ij,nj->ni', rotation, points)。或者用张量点out = np.tensordot(points, rotation, axes=[1, 1]). -
谢谢! Einsum 实际上是 100 分中最快的——比 tensordot 快 5 倍,比 for 循环快 33 倍。以 1000 点 tensordot 获胜——我的机器上似乎有 ~14us 的调度开销,但对于大型数组来说,时间相对恒定。我认为 matmul 解决方案是最通用的,因为它始终具有最佳性能的竞争力。再次感谢您 -- 如果您提交答案,我可以将其标记为已接受。