Efficiently rotate a set of points with a rotation matrix in numpy

Hooked picture Hooked · Aug 27, 2012 · Viewed 11.7k times · Source

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 R.x 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.

Answer

Aapo Kyrola picture Aapo Kyrola · Aug 27, 2012

You can multiply A with the transpose of the rotation matrix:

A = dot(A, R.T)