[Numpy-discussion] Accessing rank-0 array value?

Francesc Alted falted at pytables.org
Tue Jun 8 01:44:08 CDT 2004


A Dilluns 07 Juny 2004 19:35, Perry Greenfield va escriure:
> That discussion was centered on whether single element indexing
> should return rank-0 arrays or scalars. We chose scalars 
> as more intuitive (and for performance reasons as well).
> There was hope that rank-0 arrays would be more convenient
> to use, and that they could also be indexed as a[0], however
> this is inconsistent with the shape as Peter points out (and
> as others before did as well).
> 
> That being said, I'd like to understand why you (Francesc)
> would like to use rank-0 arrays. There is certainly a legitimate
> problem to be solved, and perhaps there are better alternatives.

Well, I've come to use rank-0 arrays in a natural way in my libraries, and I
liked to retrieve their content before returning it to the user. I was
simply a bit surprised that retrieving that value was so hidden in
documentation (in case that issue is documented at all, because I have not
found it yet).

My natural try was first to use "[0]" notation, and the error lost me.
Suggestion: it would be possible to change the default error:

>>> a[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: Too many indices

by something like:

>>> a[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: rank-0 arrays don't accept integer indices. Use '()' (empty tuple)
instead. 

In order to provide an universal accessor to numarray objects, what about
adding a .toscalar() (or .toobject()) method to them? That would return a
python object whether you are using rank-0 arrays, regular arrays,
CharArrays or RecArrays. That object would be the minimal python container
that would keep the values inside these objects. In case of rank-0 arrays it
would return a Bool, Int, Float or Complex. For a regular array, a list
(perhaps a tuple?). For CharArrays, a list (tuple?) of strings. And for
RecArrays a list (tuple) of tuples.

Incidentally, I've noted an inconsistency in the .tostring behaviour:

>>> a=array(126)
>>> a.tostring()
''

while I would expect a return value like:

>>> chr(126)
'~'

> The original source of the previous rank-0 discussion was that
> they aided what had been dubbed "generic programming". This 
> was being promoted primarily by the scipy guys and the general
> idea was to make it easier to write code that did not constantly
> have to check whether an input was scalar or an array. With
> scalars always mapping to rank-0 arrays, it was felt that
> this would aid writing code that would work for both scalars
> and arrays without any conditional tests. It's a good thing
> to want. I wonder if we should now develop tools to make writing
> such code much easier.
>
> So if your needs are along this line, this seems like a good time
> to try to figure out ways of dealing with such issues.

My needs are restricted to very few lines of code, so I can pass by using
the conditionals. However, yeah, I would be happy as well if I would be able
to implement that kind of "generic programming". I find that to be elegant,
although it may perfectly not worth the effort, I don't know.

Well, for me the whole issue was getting rank-0 values as python objects.
Sorry if my point of view is adding confusion to these issues.

Cheers,

-- 
Francesc Alted





More information about the Numpy-discussion mailing list