Ilmar Wilbers wrote:
> I want the users to be able to call the Cython function with any type of
> numpy array, and to use the data from that array as float* for a C
> function. This is what I do now:
> def tridag(a, b):
>     cdef np.ndarray[double, ndim=2] a2 = a
>     cdef np.ndarray[double, ndim=2] b2 = b
>
> but while this will work for float, float64 and double, this will not
> work for float32. Is there any way at all to allow the user calling
> tridag to sent all types of floting point number array (float32 as well
> as float64)?

If the C function expects float*, change the type of those buffers to float.

And yes, then you need to manually convert from arrays of other types
(i.e. do arr.astype(np.float32)). If the user has supplied data in a
different format, then that data needs to be copied somehow anyway, for
the compiled C code to be able to understand it.

So, roughly:

cdef np.ndarray[float, ndim=2, mode="c"] a2
try:
    a2 = a
except:
    a2 = a.astype(np.float32)
call_c_func(<float*>a2.data)
if a2 is not a:
    a[...] = a2 # can be skipped if data not modified by function

Yes, it's some manual work, but as I noted, Cython really hasn't got
features for automatic wrapping of C functions in this respect -- my work
in summer was primarily geared towards use of Cython itself for
programming.

Dag Sverre

_______________________________________________
Cython-dev mailing list
Cython-dev@codespeak.net
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to