[Numpy-discussion] strange multiplication behavior with numpy.float64 and ndarray subclass
Charles R Harris
charlesr.harris@gmail....
Sun Mar 8 18:00:39 CDT 2009
On Sun, Mar 8, 2009 at 4:42 PM, Darren Dale <dsdale24@gmail.com> wrote:
> On Sun, Mar 8, 2009 at 12:31 PM, Darren Dale <dsdale24@gmail.com> wrote:
>
>> On Wed, Jan 21, 2009 at 12:43 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
>>
>>>
>>> On Jan 21, 2009, at 11:34 AM, Darren Dale wrote:
>>>
>>> > I have a simple test script here that multiplies an ndarray subclass
>>> > with another number. Can anyone help me understand why each of these
>>> > combinations returns a new instance of MyArray:
>>> >
>>> > mine = MyArray()
>>> > print type(np.float32(1)*mine)
>>> > print type(mine*np.float32(1))
>>> > print type(mine*np.float64(1))
>>> > print type(1*mine)
>>> > print type(mine*1)
>>> >
>>> > but this one returns a np.float64 instance?
>>>
>>> FYI, that's the same behavior as observed in ticket #826. A first
>>> thread addressed that issue
>>> http://www.mail-archive.com/numpy-discussion@scipy.org/msg13235.html
>>> But so far, no answer has been suggested.
>>> Any help welcome.
>>
>>
>> I believe ticket #826 can be solved with the application of this patch:
>>
>>
>> $ svn diff scalarmathmodule.c.src
>> Index: scalarmathmodule.c.src
>> ===================================================================
>> --- scalarmathmodule.c.src (revision 6566)
>> +++ scalarmathmodule.c.src (working copy)
>> @@ -566,6 +566,10 @@
>> Py_DECREF(descr1);
>> return ret;
>> }
>> + else if (PyArray_GetPriority(a, PyArray_SUBTYPE_PRIORITY) > \
>> + PyArray_SUBTYPE_PRIORITY) {
>> + return -2;
>> + }
>> else if ((temp = PyArray_ScalarFromObject(a)) != NULL) {
>> int retval;
>> retval = _@name@_convert_to_ctype(temp, arg1);
>>
>>
>> I've run the unit tests and get the same results with and without the
>> patch applied, but it solves the problem in my script and also the problem
>> with masked arrays.
>
>
> Here is a test for this patch, maybe issue #826 can be closed.
>
> Index: numpy/core/tests/test_umath.py
> ===================================================================
> --- numpy/core/tests/test_umath.py (revision 6575)
> +++ numpy/core/tests/test_umath.py (working copy)
> @@ -253,6 +253,17 @@
> self.failUnless(isinstance(x, with_wrap))
> assert_array_equal(x, np.array((1, 2, 3)))
>
> + def test_priority_with_scalar(self):
> + # test fix for bug #826:
> + class A(np.ndarray):
> + __array_priority__ = 10
> + def __new__(cls):
> + return np.asarray(1.0, 'float64').view(cls).copy()
> + a = A()
> + x = np.float64(1)*a
> + self.failUnless(isinstance(x, A))
> + assert_array_equal(x, np.array(1))
> +
> def test_old_wrap(self):
> class with_wrap(object):
> def __array__(self):
>
> __
Added in r6578... Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090308/6500e718/attachment.html
More information about the Numpy-discussion
mailing list