[Numpy-discussion] possible bug: __array_wrap__ is not called during arithmetic operations in some cases
Darren Dale
dsdale24@gmail....
Sun Feb 22 18:01:44 CST 2009
On Sun, Feb 22, 2009 at 6:35 PM, Darren Dale <dsdale24@gmail.com> wrote:
> On Sun, Feb 22, 2009 at 6:28 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
>
>>
>> On Feb 22, 2009, at 6:21 PM, Eric Firing wrote:
>>
>> > Darren Dale wrote:
>> >> Does anyone know why __array_wrap__ is not called for subclasses
>> >> during
>> >> arithmetic operations where an iterable like a list or tuple
>> >> appears to
>> >> the right of the subclass? When I do "mine*[1,2,3]", array_wrap is
>> >> not
>> >> called and I get an ndarray instead of a MyArray. "[1,2,3]*mine" is
>> >> fine, as is "mine*array([1,2,3])". I see the same issue with
>> >> division,
>> >
>> > The masked array subclass does not show this behavior:
>>
>> Because MaskedArray.__mul__ and others are redefined.
>>
>> Darren, you can fix your problem by redefining MyArray.__mul__ as:
>>
>> def __mul__(self, other):
>> return np.ndarray.__mul__(self, np.asanyarray(other))
>>
>> forcing the second term to be a ndarray (or a subclass of). You can do
>> the same thing for the other functions (__add__, __radd__, ...)
>
>
> Thanks for the suggestion. I know this can be done, but ufuncs like
> np.multiply(mine,[1,2,3]) will still not work. Plus, if I reimplement these
> methods, I take some small performance hit. I've been putting a lot of work
> in lately to get quantities to work with numpy's stock ufuncs.
>
I should point out:
import numpy as np
a=np.array([1,2,3,4])
b=np.ma.masked_where(a>2,a)
np.multiply([1,2,3,4],b) # yields a masked array
np.multiply(b,[1,2,3,4]) # yields an ndarray
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20090222/0869bfbb/attachment.html
More information about the Numpy-discussion
mailing list