[Numpy-discussion] Faster way to generate a rotation matrix?

Jonathan Taylor jonathan.taylor@utoronto...
Wed Mar 4 17:02:18 CST 2009


Just for other peoples reference I eventually went with a cython
version that goes about twice as fast as my old post.  Here it is:

import numpy as np
cimport numpy as np

cdef extern from "math.h":
    double cos(double)
    double sin(double)

def rotation(np.ndarray[double] theta):
    cdef np.ndarray[double, ndim=2] R = np.zeros((3,3))
    cdef double cx = cos(theta[0]), cy = cos(theta[1]), cz = cos(theta[2])
    cdef double sx = sin(theta[0]), sy = sin(theta[1]), sz = sin(theta[2])

    R[0,0] = cx*cz - sx*cy*sz
    R[0,1] = cx*sz + sx*cy*cz
    R[0,2] =  sx*sy

    R[1,0] = -sx*cz - cx*cy*sz
    R[1,1] = -sx*sz + cx*cy*cz
    R[1,2] = cx*sy

    R[2,0] = sy*sz
    R[2,1] = -sy*cz
    R[2,2] = cy

    return R

Best,
Jon.

On Wed, Mar 4, 2009 at 10:28 AM, Lou Pecora <lou_boog2000@yahoo.com> wrote:
>
> Whoops.  I see you have profiled your code. Sorry to re-suggest that.
>
> But I agree with those who suggest a C speed up using ctypes or cthyon.
>
> However, thanks for posting your question.  It caused a LOT of very useful responses that I didn't know about.  Thanks to all who replied.
>
> -- Lou Pecora,   my views are my own.
>
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the Numpy-discussion mailing list