hello people,
i have just started my first cython coding (cython version 0.11.1) and i 'd
like to optimize at the maximum a python code of matrix calculation based on
numpy. cython compiles normally my code when i declare my function as a python
one with 'def'.but when i define it as 'cdef'
cython won't compile.
here is my pure python function from 'homogenousmatrix.py' module
------------------------------
# coding=utf-8
"""
Functions for working with homogeneous matrices.
toto
"""
import numpy as np
def rotzyx(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. ]])
"""
sz = np.sin(angles[0])
cz = np.cos(angles[0])
sy = np.sin(angles[1])
cy = np.cos(angles[1])
sx = np.sin(angles[2])
cx = np.cos(angles[2])
return 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.]])
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:
Error converting Pyrex file to C:
------------------------------------------------------------
...
#TAB = np.ndarray([DTYPE_t, ndim=1])
#ctypedef np.ndarray[DTYPE_t, ndim=1]_t TAB_t
cpdef object fun(object.np.ndarray[[DTYPE_t], ndim=1] vect):
^
------------------------------------------------------------
/home/tenninos/stage/arboris-python/src/test.pyx:16:50: Expected ']'
building 'test' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
-Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c test.c -o
build/temp.linux-i686-2.6/test.o
test.c:1:2: error: #error Do not use this file, it is the result of a failed
Cython compilation.
error: command 'gcc' failed with exit status 1
please help as quick as its possible...
thanks....
ps: any othre suggestion about best optimisation of the code are very welcome
_________________________________________________________________
Inédit ! Des Emoticônes Déjantées! Installez les dans votre Messenger !
http://www.ilovemessenger.fr/Emoticones/EmoticonesDejantees.aspx_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev