简体   繁体   English

使用numpy中的旋转矩阵有效旋转一组点

[英]Efficiently rotate a set of points with a rotation matrix in numpy

I have a list of 3D points stored in numpy array A with shape (N,3) and a rotation matrix R with shape (3,3) .我有一个存储在形状为(N,3) numpy 数组A中的 3D 点列表和形状为(3,3)的旋转矩阵R I'd like to compute the dot product of Rx for each point x in A in-place.我想就地计算A中每个点xRx点积。 Naively I can do this:我天真地可以这样做:

for n in xrange(N):
    A[n,:] = dot(R, A[n,:]) 

Is there a way to vectorize this with a native numpy call?有没有办法用原生的 numpy 调用将其矢量化? If it matters, N is on order of a couple thousand.如果重要的话,N 大约是几千。

您可以将 A 与旋转矩阵的转置相乘:

A = dot(A, R.T)

There's a couple of minor updates/points of clarification to add to Aapo Kyrola's (correct) answer.有几个小的更新/澄清点要添加到 Aapo Kyrola 的(正确)答案中。 First, the syntax of the matrix multiplication can be slightly simplified using the recently added matrix multiplication operator @ :首先,可以使用最近添加的矩阵乘法运算符@稍微简化矩阵乘法的语法:

A = A @ R.T

Also, you can arrange the transformation in the standard form (rotation matrix first) by taking the transpose of A prior to the multiplication, then transposing the result:此外,您可以通过在乘法之前对A进行转置,然后对结果进行转置,以标准形式(先旋转矩阵)排列变换:

A = (R @ A.T).T

You can check that both forms of the transformation produce the same results via the following assertion:您可以通过以下断言检查两种形式的转换是否产生相同的结果:

np.testing.assert_array_equal((R @ A.T).T, A @ R.T)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM