[PYTHON MATRIX-SIG] scalars and arrays

Konrad Hinsen hinsen@ibs.ibs.fr
Tue, 29 Oct 96 09:26:09 GMT


> I am having a difficult time conceptually with the equivalence of
> scalars and rank-0 arrays.  So far, my problem is with functions that

Basically, rank-0 arrays and scalars are identical. We just need
rank-0 arrays to provide scalars for data types that don't exist
as Python scalars.

> 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

You don't say *why* you want them treated identically. In general,
I would expect code to work on the widest possible range of ranks,
but not necessarily treat them as if they were the same.

But if that's what you want, I suggest the following procedure to
convert anything to a rank-1 array:

      a = array(a).flat

> 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]

Not too bad I would say...

> (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

That is a convenient convention for languages that don't have powerful
structural operations, for which the rank of an array is essential.
Essentially they ignore or create on demand axes of length 1, i.e. there
is no difference between an array of shape (1,2,1) and an array of shape
(2,). Using such a convention with complex structural operations makes
debugging a nightmare, and possibly other things as well...

> holding onto my past conceptions - and always require that my
> functions that can take sequence arguments only allow sequences for
> those arguments.

I have never felt a need to impose any such restrictions. Could you
give an example of a function that must take a sequence as a parameter?

Konrad.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------

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

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