[Numpy-discussion] rank-0 arrays

Konrad Hinsen hinsen at cnrs-orleans.fr
Sun Sep 15 03:13:02 CDT 2002

"eric jones" <eric at enthought.com> writes:

> Reductions and indexing return different types based on the number of
> dimensions of the input array:
> >>> b = sum(a)
> >>> l = len(b) # or whatever
> This code works happily if "a" is 2 or more dimensions, but will fail if
> it is 1d because the sum(a) will return a scalar in this case.  To write

And it should fail, because a rank-0 array is not a sequence, so it
doesn't have a length.

But there are valid examples in which it would be nice if scalars
were arrays (but probably if  *all* scalars supported array operations,
not just those that were generated by indexing from arrays):

- a.shape  should return () for a scalar (and (len(a),) for any
           sequence type)

- a.astype(N.Float) should also work for scalars

Similarly, it would be nice if complex operations (real/imaginary
part) would work on integers and floats.

There's one more annoying difference between scalars and arrays of
any rank which I think should be removed in numarray:

  >>> 3 % -2
  >>> array(3) % 2
  >>> fmod(3, -2)

I.e. the mod operation uses fmod() for arrays, but different rules
for standard Python numbers.

Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-
Rue Charles Sadron                       | Fax:  +33-
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais

More information about the Numpy-discussion mailing list