rank-0 arrays?

Carl Banks imbosol at vt.edu
Mon Sep 30 14:03:43 EDT 2002


brobbins333 at shaw.ca wrote:
> In Numeric Python, what are rank-0 arrays?
> Must not arrays have at least 1 dimension?

Well, in mathematics, a rank-0 array is a scalar, which is considered
a degenerate array.  The reason rank-0 arrays exist is because there
are some operations that produce arrays of various rank depending on
the rank of the operands, and in many cases these operations can
produce arrays of rank 0.

For example, consider cartesian tensors, which can be represented by
Numeric arrays.  Cartesian tensors have an operation called inner
product, which multiplies a rank-n tensor by a rank-m tensor to
produce a tensor of rank abs(n-m).  (I'm probably simplifying this too
much.)  Anyways, suppose you want the inner product of two rank-1
tensors (i.e., vectors).  The result is a rank-0 tensor, i.e., a
scalar.

Another example is a trace operation, which takes a tensor of rank n,
and produces a tensor of rank (n-2).  The trace of a rank-2 tensor is,
as with matrices, a scalar.

Evidently, Numeric supports rank-0 arrays to allow these operations to
be done consistently, without special exceptions to handle degenerate
cases.

However, IIRC, it is good advice not to play around too much with
rank-0 arrays in Python.  They should be converted to regular Python
floats if you're going to do something non-Numeric with them.



-- 
CARL BANKS
http://www.aerojockey.com



More information about the Python-list mailing list