# [Numpy-discussion] numarray: shape of scalar

Alexander Schmolck a.schmolck at gmx.net
Wed Mar 10 10:23:38 CST 2004

Sebastian Haase <haase at msg.ucsf.edu> writes:

> Hi,
> Is this intended:
> >>> na.array([1,2,4]).shape
> (3,)
> >>> na.array([1,2]).shape
> (2,)
> >>> na.array([1]).shape
> (1,)
> >>> na.array(1).shape
> ()
>
> Why is na.array([1]).shape not equal to na.array(1).shape ?

It is maybe a bit like n^0 == 1 (rather than 0, which might seem "more
intuitive").

A scalar is not the same as vector of 1 element and neither are the same as a
matrix of 1 element (nonwithstanding what matlab and maybe some dumb math
texts might suggest).

If you pretend they are you run into all sorts of trouble, as matlab (which
doesn't distinguish properly between scalars, vectors and matrices) indeed
does, for one thing because various useful identities suddenly no longer hold
for corner cases (e.g. since functions like diag that do different things
depending on whether they "think" something is a vector or not just do the
wrong thing on what you intend to be a 1xN or Nx1 matrix).

Here is a not particularly interesting identity that shows why you would like
shape(scalar) to be ():

rank == len(shape)
rank([x]) == rank(x) + 1

Here is a more interesting example that shows why you want to distinguish
between arrays of different rank (even if their shape only differes by a
leading 1 and the flat is the same):

rank(a[integer]) = rank(a) - 1

The pleasant indexing facilities of numarray wouldn't be possible without
distinguishing between arrays which differ only by the amount of leading 1s in
their shape.

'as