# [Numpy-discussion] extension to xrange for numpy

Tim Hochberg tim.hochberg at cox.net
Sun Apr 2 11:20:03 CDT 2006

```Gary Ruben wrote:

> A few rough thoughts:
>
> 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.

Bah, humbug!

"Not every two-line Python function has to come pre-written" -- Tim
Peters on C.L.P

def xrange(*args, **kwargs): return arange(*args, **kwargs)
def yrange(*args, **kwargs): return padshape(arange(*args, **kwargs), 2)
def zrange(*args, **kwargs): return padshape(arange(*args, **kwargs), 3)
def trange(*args, **kwargs): return padshape(arange(*args, **kwargs), 4)

Of course, then you need padshape which I'd be happy to contribute.

I'm of the opinion that we should be trying to improve the usefullness
of a smallish set of core primitives, not adding endless new functions.
Stuff like this, which is of interest in a relatively limited domain and
is trivial to implement when needed, should either not be added at all,
or added in a separate module.

>>> len(dir(numpy))
476

Does anyone know what all of that does? I certainly don't. And I doubt
anyone uses more than a fraction of that interface. I wouldn't be the
least bit suprised if there are old moldy parts of that are essentially
used. And, unused code is buggy code in my experience.

"Perfection is achieved, not when there is nothing more to add, but
when there is nothing left to take away." -- Antoine de Saint-Exupery

It's probably difficult at this point in numpy's life cycle to remove
stuff or even reorganize things substantially. Besides, I'm sure all the
developers  have their hands full doing more important, or at least less
contentious, things. Still, I think we should cast a more critical eye
on new stuff before adding it.

Regards,

-tim

>
> 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
>>
>> 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...)
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> that extends applications into web and mobile media. Attend the live
> webcast
> and join the prime developer group breaking into this new coding
> territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
>

```