Hoyt Koepke wrote:
> As a quick thought: I don't know how difficult it would be to
> implement, but a mode for this kind of use would be nice. Something
> like mode="unordered" to emphasize the use case. I guess you already
> have that with the above code, but I've found the new buffer syntax so
> nice that I don't particularly like going back to the direct method.
>
Yes, "ndim-generic" syntax would certainly be nice. However it is almost
there already (sorry I forgot about this earlier):
cdef np.ndarray[mode="c", ndim=1] arr = otherarr.ravel()
The ravel() method gives a 1D view, and if the data was contiguous
originally then no copy is made and modifications to arr will also be
made to otherarr (bether make an explicit check for contiguousness and
copy back if needed).
There's a related issue though:
Unfortunately I don't have time myself to work much on Cython these
days; but what I thought would be nice in this area is to have some kind
of magic ndenumerate function, much like NumPy's (but works with all
buffers):
# Multiply array with 2
np.ndarray[np.float, ndim=None] X = ...
for index, value in cython.ndenumerate(X):
X[index] = value * 2
Here "index" would be kind of magic, as it could contain multiple
dimensions (a tuple, but if it is never unpacked it is never actually
constructed as a tuple). The order of traversal would always be the most
efficient. If mode="c" extra optimizations could then be made.
Until that happens (if it ever will), you can use NumPy's generic
ndim-iterator objects (which are available in the NumPy C interface). I
haven't exposed them in numpy.pxd but would be very happy for a patch to
that file and an example for use of the ndim-iterators in Cython.
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev