I don't understand why Cython needs more Python calls to compile my .pyx file if I define elements of array during array's creation (#-1-).
For the elements pos1 and pos2, PyFloat_FromDouble is called four times, twice for each variable, but this function is not used if I create an empty array or array of zeros and change the elements afterwards (#-2-).
import cython
import numpy as np
cimport numpy as np
from libc.math cimport sin
from libc.math cimport cos
@cython.boundcheck(False)
@cython.binding(False)
@cython.wraparound(False)
cpdef np.ndarray[np.float64_t, ndim = 2] mat (double alfa):
cdef double pos1 = cos(alfa * 0.01745)
cdef double pos2 = sin(alfa * 0.01745)
cdef np.ndarray[np.float64_t, ndim = 2] mat_ret
#-1-
mat_ret = np.array([[pos1, pos2, 0.0],
[pos1, pos2, 0.0],
[ 0.0, 0.0, 0.0]], dtype = np.float64)
#-2-
mat_ret = np.zeros((3,3), dtype = np.float64)
mat_ret[0,0] = pos1
mat_ret[0,1] = pos2
mat_ret[1,0] = pos1
mat_ret[1,1] = pos2
return mat_ret
I'm using Python 2.7.13, NumPy 1.13.1 and Cython 0.25.2