[Numpy-discussion] strange multiplication behavior with numpy.float64 and ndarray subclass

Darren Dale dsdale24 at gmail.com
Sun Mar 8 12:31:10 EDT 2009


On Wed, Jan 21, 2009 at 12:43 PM, Pierre GM <pgmdevlist at 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 = _ at 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.


Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090308/87336ea7/attachment.html>


More information about the NumPy-Discussion mailing list