[Numpy-discussion] extension to xrange for numpy

Gary Ruben gruben at bigpond.net.au
Sat Apr 1 18:46:07 CST 2006


A few rough thoughts:

I'm a bit ambivalent about this. It's not very n-dimensional and 
enforces an x,y,z,(t?) ordering of the array dimensions which some 
programmers may not want to adhere to. On the occasions I've had to 
write code which loops over multiple dimensions, I've found the python 
cookbook routines for permutation and combination generators really useful

<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465>
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474124>

so I'd find some sort of numpy iterator equivalents of these more 
useful. This would allow list comprehensions like

[f(x,y,z) for (x,y,z) in ndrange(10,10,10)]

It would also be good to have it able to specify the rank of the object 
returned to allow whole array rows or matrices to be returned i.e. array 
slices. Maybe the ndrange function could allow something like

[f(xy,z) for (xy,z) in ndrange((10,0,1),10)]
where you use a tuple to specify a range and the axes to slice out.
[f(x,yz) for (x,yz) in ndrange(10,(10,1,2))]
[f(xz,y) for (xz,y) in ndrange((10,0,2),(10,1))]

On the other hand your idea would potentially make some code a lot 
easier to understand, so I'm not against it and if it was picked up, I'd 
propose "t" or "w" for the 4th dimension. It might help to post some 
code that you think might benefit from your idea.

Gary R.

Arnd Baecker wrote:
> Dear numpy enthusiasts,
> 
> one python command which is extremely useful in 1D situations
> is `xrange`. However, for higher dimensional
> settings we strongly lack the commands `yrange` and `zrange`.
> These could be shorthands for the corresponding
> constructs with `:,NewAxis` added.
> 
> Any comments, suggestion and even implementations are very welcome,
> 
> Arnd
> 
> P.S.: What I am not sure about is the right command for
> the 4-dimensional case - which letter should be used after the "z"?
> (it seems that "a" would be a very natural choice...)





More information about the Numpy-discussion mailing list