简体   繁体   中英

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) . I'd like to compute the dot product of Rx for each point x in A in-place. 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? If it matters, N is on order of a couple thousand.

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

A = dot(A, R.T)

There's a couple of minor updates/points of clarification to add to Aapo Kyrola's (correct) answer. 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 = (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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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