一次进行多个单独的 2D 旋转



我有一组(n)个几何形状,这些形状由固定数量的2D点(p)定义。这些形状是独立的,但出于效率原因,我将时间存储在单个 numpy 数组中。缩放或平移这些形状很容易,但我想旋转它们,但我不知道该怎么做。我怀疑np.tensordot是我的朋友,但我找不到正确使用它的方法。

n = 100 # Number of shape
p = 4   # Points per shape
P = np.random.uniform(0, 1, (n, p, 2))
# Scaling
S = 0.5*np.ones(n)
P *= S
# Translating
T = np.random.uniform(0, 1, (n, 1, 2))
P += T
# Rotating
A = np.random.uniform(0, 2*np.pi, n)
cosA, sinA = np.cos(A), np.sin(A)
R = np.empty((n,2,2))
R[:,0,0] = cosA
R[:,1,0] = sinA
R[:,0,1] = -sinA
R[:,1,1] = cosA
np.tensordot(P, R, axes=???)

您似乎将两个数组之间的第一个轴 -PR对齐,并在输入数组的其余轴上各sum-reducing一个轴。因此,我们可以使用np.einsum因为它将允许我们轴对齐标准。

您正在使用P中的最后一个轴进行总和缩减。现在,根据您在旋转计算中失去sum-reductionR轴,其中一个应该可以完成这项工作 -

np.einsum('ijk,ilk->ijl',P,R) # Using last dim of R for sum-reduction
np.einsum('ijk,ikl->ijl',P,R) # Using second dim of R for sum-reduction

最新更新