[Numpy-discussion] Priority rules between numpy scalars and 0d arrays
Travis E. Oliphant
Mon Oct 13 13:04:33 CDT 2008
Pierre GM wrote:
> Sorry to bring back this subject, but I still haven't got any proper answers:
> * What are the priority rules between numpy scalars and 0d arrays ?
There aren't really any specified. However, there is behavior that
emerges from what is specified.
The problem is that there has never been a formal "resolution" (that I
recall) of when should something be returned as a 0-d array and when it
should be returned as a scalar. There is rather an informal
implementation of what actually happens.
Their are some "rules of thumb" that have emerged (like array-operations
--- e.g. reshaping --- should return 0-d arrays and not scalars).
The other issue is that there is the rule that when scalars and arrays
mix, the "data-type" of the array determines the result, but there
aren't fixed rules about what the "sub-type" should be.
> The problem occurs with numpy.ma.masked, defined as a 0d, np.float64
> MaskedArray, which has the __mul__ and __rmul__ of a MaskedArray.
> np.float(1)*ma.masked gives ma.masked, as it should
> np.float(64)* ma.masked gives 0, when ma.masked should have been obtained:
> that leads me to think that ma.masked.__rmul__ isn't called. Why ? Are 0d
> arrays that special beasts ?
Could you post code to describe what you mean?
np.float(64) should be the same type as np.float(1) so I don't get what
you are saying exactly.
I think the issue is that numpy scalars are currently wrapped into 0-d
arrays for all math and so the 'priority' issue might really an issue
between numpy arrays and masked arrays.
More information about the Numpy-discussion