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

Sturla Molden sturla at molden.no
Wed Mar 4 07:18:35 EST 2009


On 3/4/2009 7:50 AM, Hoyt Koepke wrote:

 > In cython, the above would be (something like):

It also helps to turn off bounds checks:

  from numpy cimport ndarray

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

  @cython.boundscheck(False)
  cpdef ndarray[double, ndim=2] rotation(ndarry[double] theta,
      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[2,2] = cy

      return R


S.M.



More information about the NumPy-Discussion mailing list