# [PYTHON MATRIX-SIG] scalars and arrays

**Chris Chase SRM
**
chris.chase@jhuapl.edu

*Mon, 28 Oct 1996 21:33:44 -0500*

I am having a difficult time conceptually with the equivalence of
scalars and rank-0 arrays. So far, my problem is with functions that
take some kind of index vector. I would like the user [of course I am
the only user of my functions currently :-)] to be able to specify a
scalar or a rank 1 vector and have them treated the same. So I end up
using if statements to always check for a scalar. For example, with
take():
v - a scalar or sequence object
a - array
ind = array(v)
if len(ind.shape) == 0:
x = take(a,[v])
else:
x = take(a,ind)
Similarly, there are times when I don't want a scalar index to reduce
the rank of an array which requires: a[v:v,other_indexes]
Is there a better way to test for a scalar versus a sequence?
I have thought about writing a convience function similar to asarray()
with the only difference being it returns scalars as rank-1 arrays.
I think I understand the consistency part of Konrad's eplanations
(going back through the list archive) for the scalars/rank-0 issue. I
guess my difficulty is that in all my programming the past 7 years in
Matlab and IDL scalars can be indexed and treated as arrays
(e.g. scalars can be subscripted as arrays of arbitrary rank with
indexes equal to 0). I suppose the solution for me would be to stop
holding onto my past conceptions - and always require that my
functions that can take sequence arguments only allow sequences for
those arguments.
Maybe someone can help me with a good explanation of the
scalars/rank-0 concept and a paradigm for programming with them in the
Numeric context. If it is difficult for me then it will also confuse
all the people for whom I provide software analysis tools at our lab.
Chris
=================
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================