[PYTHON MATRIX-SIG] returning scalars or rank-0 arrays..

James Hugunin jjh@Goldilocks.LCS.MIT.EDU
Tue, 20 Feb 96 15:22:09 EST


   From: da@maigret.cog.brown.edu (David Ascher)

   One issue I've come accross is the "what do I return, a rank-0 entity or
   a scalar?".  My understanding of the current scheme is that indexing
   returns scalars, while slicing returns ranked entities, even if that
   rank is 0.  Is that correct?

   >>> a = arange(9).reshape(3,3)
   >>> b = UserArray(a)
   >>> b
   UserArray([[0,1,2],[3,4,5],[6,7,8]], 'l')
   >>> b[0]
   UserArray([0,1,2], 'l')
   >>> b[0,2]
   2
   >>> b[0,2:3]
   UserArray([2], 'l')

   If that's correct, then I guess that's what subclasses of UserArray
   should do, although it's not exactly intuitive.  Also, should there be
   an _as_scalar method in UserArray which returns a scalar in the cases
   where self.array is rank-0?

Note: UserArray([2], 'l') is a rank 1 array of shape (1,).  It is not
a rank 0 array.  According to the standard python notation, a slice
does not reduce the dimensionality of an array, even if it selects
only one element.  Scalars are ALWAYS returned as python scalars
(unless you explictitly ask for array(3.14)).

In general, a user should never manage to produce a UserArray or a C
array of rank-0, unless they somehow specifically ask for it.

Consider the following using standard python lists:

>>> b = [1,2,3]
>>> b[1:2]
[2]
>>> b[1]
2

-Jim

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================