OUARDA MEHDI wrote:
> 
> and here my optimised function from 'homogeneousmatrix_c.pyx' module:
> 
> # coding=utf-8
> """
> Functions for working with homogeneous matrices.
> 
> toto
> """
> import numpy as np
> #mes modif...python-->cython
> # "cimport" is used to import special compile-time information
> # about the numpy module (this is stored in a file numpy.pxd which is
> # currently part of the Cython distribution).
> 
> cimport numpy as np
> 
> # We now need to fix a datatype for our arrays. I've used the variable
> # DTYPE for this, which is assigned to the usual NumPy runtime
> # type info object.
> 
> DTYPE = np.float
> 
> # "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
> # every type in the numpy module there's a corresponding compile-time
> # type with a _t-suffix.
> 
> ctypedef np.float_t DTYPE_t
> *
> def np.ndarray [DTYPE_t,ndim=1] rotzyx(np.ndarray [DTYPE_t,ndim=1] 
> angles) :*
>     """homogeneous transformation matrix from pitch-roll-yaw angles)
>    
>     In short:  R = Rz * Ry * Rx
> 
>     example:
>     >>> rotzyx((3.14/6, 3.14/4, 3.14/3))
>     array([[ 0.61271008,  0.27992274,  0.73907349,  0.        ],
>            [ 0.35353151,  0.73930695, -0.57309746,  0.        ],
>            [-0.70682518,  0.61242835,  0.35401931,  0.        ],
>            [ 0.        ,  0.        ,  0.        ,  1.        ]])
>     """
>    
>     cdef float sz = np.sin(angles[0])
>     cdef float cz = np.cos(angles[0])
>     cdef float sy = np.sin(angles[1])
>     cdef float cy = np.cos(angles[1])
>     cdef float sx = np.sin(angles[2])
>     cdef float cx = np.cos(angles[2])
> 
>     cdef np.ndarray[DTYPE_t, ndim=2] value = np.array (
>         ([ cz*cy, cz*sy*sx-sz*cx, cz*sy*cx+sz*sx, 0.],
>          [ sz*cy, sz*sy*sx+cz*cx, sz*sy*cx-cz*sx, 0.],
>          [-sy   , cy*sx         , cy*cx         , 0.],
>          [ 0.   ,    0.         , 0.            , 1.]),        dtype=DTYPE)
> 
>     return value
> 
> as i said when i define my rotzyx(..)  function as cpdef i have this as 
> compilation error: 

This will not be faster than in Python.

a) It's only called for one iteration -- you need to bring your loops 
into Cython!

b) You are constructing Python lists which are then converted to 
objects; you must instead first create an array with np.empty and then 
set element by element as arr[i, j] = value.

c) You should call sin and cos from C instead (see Cython docs for 
calling C functions)

d) You cannot type the return value of the function as ndarray[...], 
just drop the return value.

e) Finally, your compilation error points to a line you don't even 
include in your snippet, and which contain an obvious syntax error.

-- 
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to