Re: [Numpy-discussion] Cython: Passing numpy array into C via buffer?

2008-09-26 Thread Dag Sverre Seljebotn
Richard Shaw wrote:
 Hello,
 
 Forgive me if this is a stupid question, I've been looking around all 
 the Cython documentation and I can't find out if this is possible.
 
 What I would like to do is generally is wrap a C function that takes a 
 double array, and be able to pass in a numpy array, I was wondering if 
 it's possible to do this using the buffer interface? I understand I 
 could do it using this method (http://wiki.cython.org/WrappingNumpy), 
 but the buffer interface seems much neater.

Questions like this should be posted to the Cython list (otherwise us 
Cython developers might easily ignore it, we do not follow everything 
that goes on in numpy-discussion).

 Specifically, I have a C function that I would like to wrap with signature:
 
  cfile.h
 double do_something(double * array, int len);
 
 I was hoping with the buffer interface, I could do something like this:
 
  pyfile.pyx
 import numpy
 cimport numpy
 
 cdef extern from cfile.h
   double do_something(double * array, int len)
 
 cdef do_something_python(numpy.ndarray[numpy.float64, ndim = 1] numarr):

numpy.float64_t, currently at least.

   cdef double x
   cdef double * data
   cdef int l
 
   data = ... # Not sure what to do here
   l = numarr.shape[0]

Yes, I understand this. I definitely plan to make this easier in a 
coming Cython release, i.e. something like

cimport cython
...
data = cython.buffer.bufptr(array)

I just haven't got there yet.

For now, there's an ugly hack you can do (untested though):

your_header.h:

#define GET_BUFPTR_HACK(x) __pyx_bstruct_#x.buf

yourfile.pyx:

cdef extern from your_header.h:
 void* GET_BUFPTR_HACK(x)

...
data = GET_BUFPTR_HACK(buf)

(The reason this is so horribly ugly is the #x in the macro, meaning 
this depends very much of the particular mangling scheme used. This will 
likely break in a future version of Cython, but then 
cython.buffer.butptr should be available.)

-- 
Dag Sverre
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Cython: Passing numpy array into C via buffer?

2008-09-26 Thread Dag Sverre Seljebotn
Richard Shaw wrote:
 Hello,
 
 Forgive me if this is a stupid question, I've been looking around all 
 the Cython documentation and I can't find out if this is possible.
 
 What I would like to do is generally is wrap a C function that takes a 
 double array, and be able to pass in a numpy array, I was wondering if 
 it's possible to do this using the buffer interface? I understand I 
 could do it using this method (http://wiki.cython.org/WrappingNumpy), 
 but the buffer interface seems much neater.
 
 Specifically, I have a C function that I would like to wrap with signature:
 
  cfile.h
 double do_something(double * array, int len);
 
 I was hoping with the buffer interface, I could do something like this:
 
  pyfile.pyx
 import numpy
 cimport numpy
 
 cdef extern from cfile.h
   double do_something(double * array, int len)
 
 cdef do_something_python(numpy.ndarray[numpy.float64, ndim = 1] numarr):
   cdef double x
   cdef double * data
   cdef int l
 
   data = ... # Not sure what to do here
   l = numarr.shape[0]
 
   x = do_something(data, l)

I'm sorry, my previous answer was a really, really bad one. Here's a 
better one:

data = double*numarr.data

:-) With the addition of the _t on float64 this should just work.

Please insert an extra check to make sure that your data is contiguous 
though! Otherwise you need to make a copy to pass the data to an 
external lib (one way is by using numpy.array with a contiguouos 
argument to construct a copy, and then copy the data back again afterwards).

-- 
Dag Sverre
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Cython: Passing numpy array into C via buffer?

2008-09-26 Thread Richard Shaw
Dag Sverre Seljebotn wrote:

 I'm sorry, my previous answer was a really, really bad one. Here's a 
 better one:
 
 data = double*numarr.data
 
 :-) With the addition of the _t on float64 this should just work.

That sounds great. I thought there must be something like that, it's 
glad to see that Cython doesn't disappoint. I'll get straight to trying 
it out.

 Please insert an extra check to make sure that your data is contiguous 
 though! Otherwise you need to make a copy to pass the data to an 
 external lib (one way is by using numpy.array with a contiguouos 
 argument to construct a copy, and then copy the data back again afterwards).

That make sense. I'll be sure to do check that one.

Thanks for your very swift response, it's really helpful. I'll be sure 
to post to the Cython-dev list in future if that's more appropriate.

Cheers,
Richard
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion