我有一个形状为 (t,3) 的航天器坐标时间序列,以及一个形状为 (3,3,t) 的旋转矩阵时间序列 code>,其中 t 是时间序列的长度。我想找到每次 t 的坐标与每次 t 的旋转矩阵的点积,这样我就得到了一个形状为 (t ,3) 旋转后的坐标
我可以在 for 循环中通过编写来实现这一点:
new_coords = np.zeros_like(input_coords)
for Ci, Vi in enumerate(input_coords):
new_coords[Ci,:] = np.tensordot(Vi, rotation[:,:,Ci], axes = 1)
如何用一行 Python 代码替换这个 for 循环?我尝试了 np.tensordot 的各种排列,但没有成功。
请您参考如下方法:
您可以使用 np.einsum -
np.einsum('ijk,ki->kj',rotation, input_coords)
通用格式的形状 -
rotation : 3 x 3 x N
input_coords : N x 3
那里应用了两个注意事项-
rotation的第一个(轴)与最后一个input_coords的总和减少。- 保持
rotation的最后一个和input_coords的第一个对齐。这与在嵌套循环中使用Ci的方式一致。
