[Numpy-discussion] rank-0 arrays

Konrad Hinsen hinsen at cnrs-orleans.fr
Sun Sep 15 12:41:04 CDT 2002

> > And it should fail, because a rank-0 array is not a sequence, so it
> > doesn't have a length.
> I disagree. You should not have to write special code to check for a
> specific case.  It breaks one of the beauties of Numeric -- i.e. you can

It is not a specific case, more like a specific value (for the rank).
1/a fails for a == 0, should that be changed as well?

Let's examine some equivalent code pieces:

- len(a) == a.shape[0]
  the second fails for rank 0, so the first one should fail as well

- for i in range(len(a)):  print a[i]
  works for all sequences. If len(a) doesn't fail (and I assume it would
  then return 1), a[1] shouldn't fail either.

- len(a) == len(list(a))
  for all sequences a. Should list(a) return [a] for a rank-0 array?
  For a scalar it fails.

Actually this might be an argument for not having rank-0 arrays at all.
Arrays are multidimensional sequences, but rank-0 arrays aren't.

> returns its length along its "first" axis.  This will always be 1.  It
> allows for generic code.

Then please give an example where this genericity would be useful.

> On this note:
> I do not see the benefit of making a scalar type object that is separate
> for 0d arrays.  It seems to remove instead of enhance capabilities.
> What does a scalar object buy that simply using 0d arrays for that
> purpose does not?

Compatibility, for example the ability to index a sequence with an
element of an integer array. Also consistency with other Python
sequence types. For example,

   [a][0] == a

so one would expect also

   array([a])[0] == a

but this would not be fully true if the left-hand side is a rank-0

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