Ondrej Certik wrote:
...
> Where func2 is:
>
> def func2(a, MyFunc mf):
> cdef f2 f = mf.get_f()
> return array([f(x) for x in a])
>
> This works nice. Is this the way to do it? Or is there some
> better/simpler way. I don't know if it's a good idea to make Cython
> clever enough to wrap things like this automatically?
I think the way you did it is more than elegant enough :-) As others have
commented, this is fine.
This is just a note on performance: That list comprehension is really
going to kill performance. If you do this instead:
def func2(np.ndarray[right_dtype_t, ndim=1] a, MyFunc mf):
cdef f2 f = mf.get_f()
cdef np.ndarray[right_dtype_t, ndim=1] result = np.empty(a.shape,
right_dtype)
cdef unsigned int i
for i in range(a.shape[0]):
result[i] = f(a[i])
return result
...then it should be much, much faster (avoiding conversion of every
single array element back and forth from/to Python objects).
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev