Matthew Brett skrev:
> Imagine I have a string 'data', length 'n' bytes, and a numpy dtype
> 'dt', and I wanted to make a 1d array.  In python that is:
>
> arr = np.ndarray(shape=(n,), dtype=dt, buffer=data)
>
> Am I right, that in Cython, this would be a function like the
> following (that does work):
>   
You are thinking too difficult. Forget about numpy's C API, you don't 
have to use it.

In Cython this is:

import numpy as np
cimport numpy as np

cdef np.ndarray[dt_t, ndim=1, mode="c"] arr
arr = np.ndarray(shape=(n,), dtype=dt, buffer=data)

The notice the type dt_t. For simple dtypes, there is mapping in numpy.pxd

np.int32  -> np.int32_t
np.float64 -> np.float64_t

For recarrays, you simply ctypedef a struct similar to your dtype, and 
declare with that.

Cython will now optimize arr[index] if index is a C integer, and 
arr.data gives you a char* pointer to arr's data.

Sturla Molden






























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

Reply via email to