[NumPy-Tickets] [NumPy] #2089: Wrong method resolution for `multiply` & other arithmetic ufuncs

NumPy Trac numpy-tickets@scipy....
Thu Mar 22 05:58:37 CDT 2012

#2089: Wrong method resolution for `multiply` & other arithmetic ufuncs
 Reporter:  pv          |       Owner:  somebody   
     Type:  defect      |      Status:  new        
 Priority:  high        |   Milestone:  Unscheduled
Component:  numpy.core  |     Version:  1.6.1      
 Keywords:              |  
 `multiply` and the other arithmetic ufuncs fall back to `PyNumber_*`
 methods before trying `obj.<ufunc_name>` methods.

 This is problematic for sparse matrices and matrices:
 import numpy as np
 import scipy.sparse as sp
 A = sp.rand(20,20,density=0.1)
 B = sp.rand(20,20,density=0.1)
 print (np.multiply(A,B) - A.dot(B)).todense().max()
 # out: 0.0
 So, `multiply` actually calls the matrix multiply.

 The reason seems to be that in
 `numpy/core/code_generators/generate_umath.py` for these ufuncs the
 resolution ends with:
         TD(O, f='PyNumber_Multiply'),
 which ends up calling `__mul__`. The fix could be to change this to
         TD(O, f='NPy_ElementwiseMultiply'),
 which would try to call `multiply` before falling back to `PyNumber_*`.

Ticket URL: <http://projects.scipy.org/numpy/ticket/2089>
NumPy <http://projects.scipy.org/numpy>
My example project

More information about the NumPy-Tickets mailing list