Tiago Pereira 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))
>
> The problem is res is an array of pointers (so I guess this does not
> work), but cython also complains that it can't convert float **res to
> Python type. So it seems memcpy is expecting a Python type, which means
> this example would never work...

You should use memcpy from C instead to work with pointers.

Then, do the same thing as before (loop over the rows of the array) but
doing a memcpy instead for each row.

> I can always use brute force and do a couple of loops for setting
> result[i,j]=res[i][j]. But I wonder if there is a more elegant way of
> doing this.

Well, there's no way a float** will fit within the NumPy memory model, so
the memory must be copied somehow. Using a memcpy per row will likely be
faster but is fundamentally the same thing.

Dag Sverre

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

Reply via email to