[Numpy-discussion] rank-0 arrays

Tim Hochberg tim.hochberg at ieee.org
Mon Sep 16 12:39:01 CDT 2002

From: "Konrad Hinsen" <hinsen at cnrs-orleans.fr>
> > Travis Oliphant wrote:
> > Would they be recognised as scalars by Python? In particular, could you
> There is no "scalar" category in Python. New scalar datatypes would be
> types with the same behaviour as the existing Python scalar types, but
> different from the. That means that explicitly type-checking code would
> not accept them, but everything else would.
> > use one as an index? Personally, this is what has bit me in the past: I
> At least up to Python 1.5, no, indices have to be of integer type. I
> don't know if that condition was extended in later versions.

In python 2.2 (I'm not sure about 2.0,2.1), you can subclass int and the
resultant class can be used as an index. So I think this could be done.

> > 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
> If we introduce additional scalars, yes.

Given that numarray has changed its coercion rules, is this still necessary?
>From http://stsdas.stsci.edu/numarray/Userguide.html:

----------start quote----------
Type Coercion

In expressions involving only arrays, the normal coercion rules
apply (i.e., the same as Numeric). However, the same rules do
not apply to binary operations between arrays and Python scalars
in certain cases. If the kind of number is the same for the array
and scalar (e.g., both are integer types or both are float types),
then the type of the output is determined by the precision of the
array, not the scalar. Some examples will best illustrate:

Scalar type  *  Array type        Numeric result type    numarray result
   Int            Int16                Int32                    Int16
   Int            Int8                 Int32                    Int8
   Float          Int8                 Float64                  Float64
   Float          Float32              Float64                  Float32

The change in the rules was made so that it would be easy to preserve the
precision of arrays in expressions involving scalars. Previous solutions
with Numeric were either quite awkward (using a function to create a rank-0
array from a scalar with the desired type) or surprising (the savespace
attribute, that never allowed type coercion). The problem arises because
Python has a limited selection of scalar types. This appears to be the
best solution though it admittedy may surprise some who are used to the
classical type coercion model.

----------end quote----------

> Whether or not we would want
> to get rid of them is of course another question.

Agreed. It's possible that rank zero arrays would be useful in certain
unusual situations, although I can't think of any good examples. Regardless
of that choice, if indexing never returns rank-0 arrays most people will
never have to deal with them.


More information about the Numpy-discussion mailing list