[Numpy-discussion] Help needed with the priority of 0d-arrays and np.scalars

Pierre GM pgmdevlist@gmail....
Sun Sep 28 11:30:56 CDT 2008

In a recent post, I was pointing to a bug in numpy.ma. I think I can 
understand what happens, but I'd need a couple of clarifications.

When multiplying a 0d ndarrray or one of its subclass by a numpy scalar, we 
end up with a numpy scalar cast to the highest type, right ?
So, np.float64(1)*np.array(0.,dtype='g') is a np.float128 object, while 
np.float64(1)*np.array(0,dtype=np.float32) is a np.float64 object.

What surprises me is that numpy scalars have a low priority (listed as -100 
or -1000000 depending on the page of the numpy book), so if I have a 0d 
subclass of ndarray with a higher priority, I should end up with a 0d 
subclass of ndarray, but it doesn't seem to be the case.

The problem comes and bites me with ma.masked, defined as a 0d MaskedArray 
with __array_priority__ of 15 and a np.float64 dtype.
Doing ma.masked*np.float64(1.) yields ma.masked, as it should. 
Doing np.float64(1.)*ma.masked yields np.float64(0.) but shouldn't: I should 
access instead the __rmul__ method of ma.masked.

Any comment/suggestion would be welcome.

More information about the Numpy-discussion mailing list