[Matrix-SIG] New Behavior for NumPy scalars?

Travis Oliphant Oliphant.Travis@mayo.edu
Mon, 6 Sep 1999 16:21:09 -0500 (CDT)


> > My personal solution preference is to not use PyArray_Return for the
> > ufuncs and let users who really need a scalar do the indexing to get it
> > from the rank-0 array.  Using PyArray_Return seems like trying to
> > second-guess the user and leads to these kinds of issues.
> 
> It does what a naive user would expect,
>

Perhaps, perhaps, not.  When I was new to Python I remember the thing that
kept getting me was the difference between lists and multiarrays.  This
caused a couple of minor hangups until I finally got it in my head that
Python had multiple objects and not just one (like the MATLAB I was used
to.)  Once somebody realizes that it's not such a big leap to have Python
scalars and Numeric scalars.

> and what works well for the vast majority of applications.

Hmm.  Unless you are trying to actually use the single precision
datatypes.  Then, you have to be extremely careful not to lose precision
because your NumPy arrays are always getting converted to Python scalars.

If operations with NumPy arrays always returned NumPy arrays the precision
upcasting problem wouldn't really be the problem it is, one would just
have to define all of there constant scalars.  Not a bad programming
practice at that.

> Anyone who thinks that returning
> rank-0 arrays is a good idea should volunteer for writing the
> chapter in the documentation which explains the difference between
> scalars and rank-0 arrays and why it is necessary to have both!

I'd do it.  I think the reason is the same as why is it necessary to have
both lists and NumPy arrays for the beginning user.

> If you want to introduce rank-0 arrays consistently, you also have
> to return a rank-0 result from indexing, for example. And then code
> like

Yes, probably true.

> 
>     a = Numeric.zeros((3,))
>     a[0] = 1
>     ...
>     if a[0] == 1:
>        ...
> would do something very unexpected.
> 

This would be confusing for beginners it is agreed.  But there are always
confusing issues when learning a new system.  Changing something now would
undoubtedly break all kinds of old code.

Still, it would be nice if rank-0 arrays could exist less ephemerally.
I know let's introduce a global flag.... :-)

> The best solution would be to integrate the basic array module with
> the standard Python distribution and make all Python scalars rank-0
> arrays. Perhaps this could be proposed seriously for Python 2.0.

I agree this would be the best solution.

Now, either way you have pain under some circumstances.  The
question is which pain is more tolerable.  Lately, for me the precision
pain has been, well, a pain.

Travis