On Sun, Mar 29, 2009 at 10:53 PM, Tiago Pereira <[email protected]> wrote:

>
> Now, just another question... How do I do the reverse? Ie., convert from
> **float to numpy? I looked in previous emails and I saw something using
> np.memcpy. Adapting for 2d it would be something like this (assuming a
> float **res):
>
> cdef np.ndarray[DTYPE_t,ndim=2]result = np.zeros((N,N),dtype=DTYPE)
> if data != NULL: np.memcpy(result.data,res,N*N*sizeof(float))

You have to think about how things are layered in memory, otherwise
you will keep making those mistakes. That's the fun of C :) A float**
is like this:

a[0] -> address of first row: float* row0 = a[0], then row0, row0+1,
etc... give you the items of the first row.
...

To create a numpy array from those kind of arrays, you have to memcpy every row:

float *dest;
for i in range(nrows):
   mempcy(dest + i * ncols, a[i], number of bytes in a[i])

and then use dest to create a numpy array.

In C, using ragged arrays is generally not a good idea - of course, if
your externaly library uses it, you have no choice, but for your own
code, you are often better using plain float*.

cheers,

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

Reply via email to