[Numpy-discussion] ndrange, like range but multidimensiontal

Eric Wieser wieser.eric+numpy at gmail.com
Thu Oct 11 22:29:56 EDT 2018


I’m not sure if we ever want the ndrange object to return a full matrix.

np.array(ndrange(...)) should definitely return a full array, because
that’s what the user asked for.

Even if you supply a numpy uint8 to range, it still returns a python int
class.

If we want to design ndrange with the intent of indexing only, then it
should probably always use np.intp, whatever the type of the provided
arguments

Would you like ndrange to return a tuple of uint8 in this case?

Tuples are just one of the four options I listed in a previous message. The
downside of tuples is there’s no easy way to say “take just the first axis
of this range”.
Whatever we pick, the return value should be such that
np.array(ndrange(...))[ind]
== ndrange(...)[idx]
​

On Thu, 11 Oct 2018 at 18:54 Mark Harfouche <mark.harfouche at gmail.com>
wrote:

> Eric, interesting ideas.
>
> > __getitem__(Tuple[int]) which returns numpy scalars
>
> I'm not sure what you mean. Even if you supply a numpy uint8 to range, it
> still returns a python int class.
> Would you like ndrange to return a tuple of `uint8` in this case?
>
> ```
> In [3]: a =
> iter(range(np.uint8(10)))
>
> In [4]:
> next(a).__class__
> Out[4]: int
>
> In [5]:
> np.uint8(10).__class__
> Out[5]: numpy.uint8
> ```
>
> Ravel seems like a cool way to choose iteration order. In the PR, I
> mentionned that one reason that I removed `'F'` order from the PR was:
> 1. My implementation was not competitive with the `C` order implementation
> in terms of speed (can be fixed)
> 2. I don't know if it something that people really need to iterate over
> collections (annoying to maintain if unused)
>
> Instead, I just showed an example how people could iterate in `F` order
> should they need to.
>
> I'm not sure if we ever want the `ndrange` object to return a full matrix.
> It seems like we would be creating a custom tuple class just for this which
> seems pretty niche.
>
>
> On Thu, Oct 11, 2018 at 10:21 AM Eric Wieser <wieser.eric+numpy at gmail.com>
> wrote:
>
>> Isn’t that what arange is for?
>>
>> Imagining ourselves in python2 land for now - I’m proposing arange is to
>> range, as ndrange is to xrange
>>
>> I’m not convinced it should return an ndarray
>>
>> I agree - I think it should return a range-like object that:
>>
>>    - Is convertible via __array__ if needed
>>    - Looks like an ndarray, with:
>>       - a .dtype attribute
>>       - a __getitem__(Tuple[int]) which returns numpy scalars
>>       - .ravel() and .flat for choosing iteration order.
>>
>> On Wed, 10 Oct 2018 at 11:21 Allan Haldane allanhaldane at gmail.com
>> <http://mailto:allanhaldane@gmail.com> wrote:
>>
>> 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
>>>
>>>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at python.org
>> https://mail.python.org/mailman/listinfo/numpy-discussion
>>
> _______________________________________________
> 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/774ad81c/attachment.html>


More information about the NumPy-Discussion mailing list