python - np.tensordot 用于点云的旋转?
问题描述
关于原点的旋转是一个矩阵乘积,可以用 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 循环,或者可以使用 numpy tile 进行矢量化。我认为有一个涉及 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
第二个参数,否则输出的尺寸将被反转。
推荐阅读
- amazon-web-services - NettyNioAsyncHttpClient 客户端 NullPointer 异常
- javascript - 从javascript中的文件中删除空行
- nebula-graph - 如何从 Nebula Graph 中的顶点分离标签?
- r - 我在 R,AOV 函数中遇到了这个问题
- postgresql - 在 Postgres 表上出现错误“错误:列不存在”,这对我来说没有意义
- flutter - Flutter Firebase Pubspec.yaml 更新——获取 [!] CocoaPods 找不到 pod “Firebase/CoreOnly”的兼容版本:
- flutter - 如何在 ReorderableListView 中的项目之间添加空格?
- postgresql - 如何将 dart 应用程序与 Heroku Postgres 连接
- vb.net - 如何使用 sqlkata 将大括号添加到 where-query
- facebook-graph-api - 如何获取 Instagram 业务帐户用户和媒体帖子 (IG Graph API) 的参与度数据?