[Numpy-discussion] ndrange, like range but multidimensiontal

Mark Harfouche mark.harfouche at gmail.com
Thu Oct 11 09:41:42 EDT 2018

I’m really open to these kinds of array extensions but, I (personally) just
don’t know how to do this efficiently.
I feel like ogrid and mgrid are probably enough for people that want think
kind of feature.

My implementation would just be based on python primitives which would
yield performance similar to

In [2]: %timeit np.arange(1000)
1.25 µs ± 4.01 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [4]: %timeit np.asarray(range(1000))
99.6 µs ± 1.38 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Here is how mgrid can be used to return something similar to the indices
from ndrange

In [10]: np.mgrid[1:10:3, 2:10:3][:, 1, 1]
Out[10]: array([4, 5])

In [13]: np.ndrange((10, 10))[1::3, 2::3][1, 1]
Out[13]: (4, 5)

On Wed, Oct 10, 2018 at 2:22 PM Allan Haldane <allanhaldane at gmail.com>

> On 10/10/18 12:34 AM, Eric Wieser wrote:
> > One thing that worries me here - in python, |range(...)| in essence
> > generates a lazy |list| - so I’d expect |ndrange| to generate a lazy
> > |ndarray|. In practice, that means it would be a duck-type defining an
> > |__array__| method to evaluate it, and only implement methods already
> > present in numpy.
> Isn't that what arange is for?
> It seems like there are two uses of python3's range: 1. creating a 1d
> iterable of indices for use in for-loops, and 2. with list(range) can be
> used to create a sequence of integers.
> Numpy can extend this in two directions:
>  * ndrange returns an iterable of nd indices (for for-loops).
>  * arange returns an 1d ndarray of integers instead of a list
> The application of for-loops, which is more niche, doesn't need
> ndarray's vectorized properties, so I'm not convinced it should return
> an ndarray. It certainly seems simpler not to return an ndarray, due to
> the dtype question.
> arange on its own seems to cover the need for a vectorized version of
> range.
> Allan
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20181011/d2f325b1/attachment-0001.html>

More information about the NumPy-Discussion mailing list