python - 一次进行多个单独的 2d 旋转

标签 python numpy

我有一组 (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=???)

最佳答案

似乎您保持两个数组之间的第一个轴 - PR 对齐,并且 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/

相关文章:

python - 将分组项保存到不同的 Excel 工作表

python - 在 Python 中读取大型 JSON 文件 (raw_decode)

python - pickle 大型 NumPy 数组

python - 切片 3d numpy 数组并设置像素的 RGB 值

python - 将 2D numpy 数组重组为 3D

python - 如何将 oracle DB 与我的 python 脚本连接?

python - 如何将具有多个模块的 pygame 应用程序转换为 .exe

python - 如何从一年中的一周计算出一个月中的一周?

python - Numpy 中的一维 LDA 输出维度

python - 使用 matplotlib 绘制地震摆动轨迹