[Numpy-discussion] Numpy-discussion Digest, Vol 7, Issue 13
Matthew Koichi Grimes
Fri Apr 6 14:23:46 CDT 2007
> Short answer: No, they are not bugs.
> The rule is:
> In any mixed-type operation between two objects of the same
> fundamental "kind" (i.e. integer, float, complex) arrays always have
> precedence over "scalars" (where a 0-d array is considered a scalar
> in this context).
I guess it's not hard to accept and remember that operations between
arrays and scalars behave differently than operations between two
arrays, since arrays and scalars are obviously different classes.
However, the fact that array-array operations behave differently
depending on whether one of the arrays is 0-dimensional still strikes me
as bad. Bad in the sense that it can lead to surprising and
hard-to-identify bugs in vectorized code designed to work with tensors
of various ranks, including 0. This equivalency between rank-0 arrays
and scalars seems like another instance of the rank-0 special-casing
that has been deemed bad in recarray and elsewhere.
A usage case my friend encountered is when finding the small difference
between two tensors whose values are close to within 10e-8, precisely
the case where one would want 64-bit precision. Say these tensors are of
different rank, so this is a broadcasted subtraction:
smallDifference = largeRank_float32 - smallRank_float64
This code works just fine as long as smallRank_float64 has a rank of at
least one, but if it's a scalar, all of a sudden smallDifference is
filled with 0.0s. If the ranks of these tensors are dependent on the
input arguments (as is the case in vectorized code), this leads to
pretty subtle bugs.
> It's a rule whose reason has a long history.
I'd be interested to read the discussion that led to making the type
promotion rules different for array-array vs array-scalar operations.
Does anybody remember the approximate date that this thread occurred in
numpy-discussion, so I can look it up?
More information about the Numpy-discussion