python - np.tensordot 用于点云旋转?

标签 python numpy tensor

绕原点旋转是一个矩阵乘积,可以使用 numpy 的 dot 函数来完成,

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 循环,或者可以使用 numpytile 进行矢量化。我认为有一个涉及 np.tensordot 的实现,但该函数对我来说是巫术。这可能吗?

最佳答案

有多种方法可以做到这一点。与np.matmul你可以这样做:

out = np.matmul(rotation, points[:, :, np.newaxis])[:, :, 0]

或者,同等地,如果您使用的是 Python 3.5 或更高版本:

out = (rotation @ points[:, :, np.newaxis])[:, :, 0]

另一种方法是 np.einsum :

out = np.einsum('ij,nj->ni', rotation, points)

最后,按照您的建议,您还可以使用np.tensordot :

out = np.tensordot(points, rotation, axes=[1, 1])

请注意,在这种情况下,points 是第一个参数,rotation 是第二个参数,否则输出的尺寸将被反转。

关于python - np.tensordot 用于点云旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54445195/

相关文章:

python - 使用Python检查关键算法

python - 缺少 PyQt QML 错误控制台

python - 广播 np.dot 与 tf.matmul 进行张量矩阵乘法(形状必须为 2 级,但为 3 级错误)

python-3.x - 为什么keras.backend.pool3d要求tensor_in是5维的?

python - 在工作人员上加载本地(不可序列化)对象

python - 在 pandas DataFrame 的每一列中找到第一个非零值

python - 从 NumPy 数组中删除一个值

python - 矩阵乘法。 python

python - numpy.直方图 : retrieve sum of weights squared in each bin

c++ - 没有 -On 就无法构建平凡的 Eigen3 张量程序