我有一组 (n) 个几何形状,它们由固定数量 (p) 个二维点定义。这些形状是独立的,但出于效率原因,我将时间存储在单个 numpy 数组中。缩放或平移这些形状很容易,但我想旋转它们,但我不确定该怎么做。我怀疑 np.tensordot
是我的 friend ,但我找不到正确使用它的方法。
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=???)
最佳答案
似乎您保持两个数组之间的第一个轴 - P
和 R
对齐,并且 sum-reducing
中的每个轴都与剩余的轴对齐来自输入数组的轴。所以,我们可以使用 np.einsum
因为它将允许我们使用轴对齐标准。
您正在使用 P
的最后一个轴进行总和缩减。现在,根据 R
的哪个轴你丢失了 sum-reduction
用于旋转计算,其中一个应该可以完成这项工作 -
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
关于python - 一次进行多个单独的 2d 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40822983/