[Matrix-SIG] functions for numbers & arrays

Konrad Hinsen hinsen@ibs.ibs.fr
Thu, 16 Apr 1998 17:26:25 +0200


> work for both arrays and lists.  For example, I have functions which I
> want to evaluate at grids of parameter values (x = arange(2,4,.2),
> y=arange(1,2,.01)) and specific points (x = 2, y = 3).
> 
> I haven't come up with a good way to do this that does the right thing in
> most circumstances.  [for example, I'd like such a function to return an
> array in the first case, but a scalar in the second]

There are currently two cases. If your function can be written
entirely in terms of umath functions, everything will work as expected
automatically. Otherwise, you'll need explicit type checks,
unfortunately.

> What do other array languages do to address this problem?  Anything we can
> steal?

Other array languages treat scalars as rank-0 arrays, not as a
separate data type. We don't have that option for compatibility
reasons.

What could be done is a careful modification of both scalar types and
NumPy arrays to make them all behave as similarly as possible. For
example, Python scalars could respond to array methods such as
shape(), typecode(), astype(), etc.

However, even with "real" array languages such as APL, designing a
function such that it will do the right thing for both scalar and
array arguments is not always trivial. In many cases it requires
adapting the shapes of input arguments and results. But at least
it can all be done without explicit type checks.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-4.76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------