[Numpy-discussion] Rank-0 arrays - reprise

Matthew Brett matthew.brett@gmail....
Sat Jan 5 06:15:55 CST 2013

Hi,

Following on from Nathaniel's explorations of the scalar - array
casting rules, some resources on rank-0 arrays.

The discussion that Nathaniel tracked down on "rank-0 arrays"; it also
makes reference to casting.  The rank-0 arrays seem to have been one
way of solving the problem of maintaining array dtypes other than bool
/ float / int:

http://mail.scipy.org/pipermail/numpy-discussion/2002-September/001612.html

Quoting from an email from Travis in that thread, replying to an email
from Tim Hochberg:

http://mail.scipy.org/pipermail/numpy-discussion/2002-September/001647.html

<quote>
> Frankly, I have no idea what the implimentation details would be, but
> could we get rid of rank-0 arrays altogether? I have always simply found
> them strange and confusing... What are they really neccesary for
> (besides holding scalar values of different precision that standard
> Pyton scalars)?

With new coercion rules this becomes a possibility.  Arguments against it
are that  special rank-0 arrays behave as more consistent numbers with the
rest of Numeric than Python scalars.  In other words they have a length
and a shape and one can right N-dimensional code that works the same even
when the result is a scalar.

Another advantage of having a Numeric scalar is that we can control the
behavior of floating point operations better.

e.g.

if only Python scalars were available and sum(a) returned 0, then

1 / sum(a)  would behave as Python behaves (always raises error).

while with our own scalars

1 / sum(a)   could potentially behave however the user wanted.
</quote>

There seemed then to be some impetus to remove rank-0 arrays and
replace them with Python scalar types with the various numpy
precisions :

http://mail.scipy.org/pipermail/numpy-discussion/2002-September/013983.html

Travis' recent email hints at something that seems similar, but I
don't understand what he means:

http://mail.scipy.org/pipermail/numpy-discussion/2012-December/064795.html

<quote>
Don't create array-scalars.  Instead, make the data-type object a
meta-type object whose instances are the items returned from NumPy
arrays.   There is no need for a separate array-scalar object and in
fact it's confusing to the type-system.    I understand that now.  I
did not understand that 5 years ago.
</quote>

Travis - can you expand?

I remember rank-0 arrays being confusing in that I sometimes get a
python scalar and sometimes a numpy scalar, and I may want a python
scalar, and have to special-case the rank-0 array, but I don't
remember precisely why I needed the python scalar.  Any other comments
/ records of rank-0 arrays being confusing?

Best,

Matthew