[Numpy-discussion] rank-0 arrays

Konrad Hinsen hinsen at cnrs-orleans.fr
Fri Sep 13 03:22:04 EDT 2002


Pearu Peterson <pearu at cens.ioc.ee> writes:

> I think it would be confusing if the result of repr would be `2' and not
> `array(2)' because 2 and array(2) are not equivalent in all usages
> but it should be clear from repr results as a first way to learn more
> about the objects.

I agree. There will already be some inevitable confusion with both
rank-0 arrays and scalars around, with similar but not identical
behaviour. Rank-0 arrays shouldn't make it worse by using camouflage.

> > The second issue is an efficiency one. Currently numarray uses
> > Python objects for arrays. If we return rank-0 arrays for
> > single item indexing, then some naive uses of larger arrays
> > as sequences may lead to an enormous number of array objects
> > to be created. True, there will be equivalent means of doing
> > the same operation that won't result in massive object creations
> > (such as specifically converting an array to a list, which would 
> > be done much faster). Is this a serious problem?
> 
> Could array.__getitem_ and __getslice__ detect if their argument is
> an array and skip using Python objects when iterating over indices?

Of course they know that they are indexing an array, they are
defined at the level of the array class/type. However, they cannot
detect an iteration as opposed to a single item access.

I don't know if this efficiency problem could be important in
practice, probably only practice can tell. I have no idea how many
single-item indexing operations into arrays occur in my code, this is
not something I worried about when writing it.

If there will be scalar and rank-0 array returning variants of
indexing anyway, then I suppose that changing the index syntax
to one or the other is not a big effort. So my suggestion is to
make a test release and see what the reactions are.

Konrad.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24
Rue Charles Sadron                       | Fax:  +33-2.38.63.15.17
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais
-------------------------------------------------------------------------------




More information about the NumPy-Discussion mailing list