FW: [Numpy-discussion] Bug: extremely misleading array behavior

Perry Greenfield perry at stsci.edu
Tue Jun 4 14:18:05 EDT 2002


<Konrad Hinsen wrote>
  <Eric Jones wrote>
> > I don't think education is the answer here. We need to change
> > Numeric to have uniform behavior across all typecodes.
> 
> I agree that this would be the better solution. But until this is
> done...
> 
> > Having alternative behaviors for indexing based on the typecode can
> > lead to very difficult to find bugs. Generic routines meant to work
> 
> The differences are not that important, in most circumstances rank-0
> arrays and scalars behave in the same way. The problems occur mostly
> with code that does explicit type checking.
> 
> The best solution, in my opinion, is to provide scalar objects
> corresponding to low-precision ints and floats, as part of NumPy.
> 
There is another approach that I think is more sensible.
>From what I can tell, the driving force behind rank-0 arrays
as scalars are the Numeric coercion rules. One needs to retain
the 'lesser' integer and float types so that operations with 
these psuedo-scalars and other arrays does not coerce arrays to
a higher type than would have been done when using the nearest
equivalent of Python scalars (if there is some other reason, I'd
like to know). For example if a and b are Int16 1-d arrays, if
indexing an element out of them produced a Python integer value
then 

a[0]*b becomes an Int32 (or even Int64 on some platforms?) array.

Numarray has different coercion rules so that this doesn't
happen. Thus one doesn't need c[1,1] to give a rank-0 array.
(Eric Jones has pointed out privately that another reason is
to use different error handling, but if I'm not mistaken so
long as one can group all calculations so that no scalar-scalar
calculation is done, one doesn't really need rank-0 arrays other
than in unusual circumstances.)

So I'd argue that numarray solves this issue. For those
that can't wait (because numarray currently lacks a feature,
library, it's too slow on small arrays or whatever) and you really 
must modify Numeric I think you would be much better off changing
the coercion rules and eliminating rank-0 arrays resulting from
ordinary indexing rather than one of the other proposed changes
(if that isn't too hard to implement). Of course you get into
backward compatibility issues. But really, to get it right, some
incompatibility is necessary if you want to eliminate this particular
wart.

Perry Greenfield






More information about the NumPy-Discussion mailing list