[NumPy-Tickets] [NumPy] #2092: numpy.ma.average casts complex to float under certain conditions

NumPy Trac numpy-tickets@scipy....
Tue Mar 27 06:52:54 CDT 2012


#2092: numpy.ma.average casts complex to float under certain conditions
------------------------------------------+---------------------------------
 Reporter:  wucknitz                      |       Owner:  pierregm   
     Type:  defect                        |      Status:  new        
 Priority:  high                          |   Milestone:  Unscheduled
Component:  numpy.ma                      |     Version:  1.6.1      
 Keywords:  numpy.ma.average for complex  |  
------------------------------------------+---------------------------------
 Hi,

 when calling numpy.ma.average with a complex array, with weights and an
 argument for "axis", the complex number is reduced to real.
 See attached test script and the output here:

 {{{
 numpy version 1.3.0
 with mask, no axis and with axis:
 (2+3j)
 2.0
 without mask, no axis and with axis:
 (2+2j)
 (2+2j)
 }}}

 Version 1.6.1 still has the problem, even though (under python 2.7) it at
 leasts complains about discarding an imaginary part.

 It is easy to track the bug down in numpy/ma/extras.py, where the type is
 sometimes cast to float. For me it does the trick to add the following
 lines:

 {{{
 # use type complex if necessary
 if np.iscomplexobj (a):
     type= complex
 else:
     type= float
 }}}

 And then use "type" instead of "float" in all lines with "n = ..." that
 now use float explicitly.

 There may be better ways to fix the problem.


 Cheers,
 Olaf

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


More information about the NumPy-Tickets mailing list