[NumPy-Tickets] [NumPy] #1590: complex array to scalar conversion fails

NumPy Trac numpy-tickets@scipy....
Mon Jan 3 19:27:10 CST 2011


#1590: complex array to scalar conversion fails
--------------------+-------------------------------------------------------
 Reporter:  drizzd  |       Owner:  somebody
     Type:  defect  |      Status:  new     
 Priority:  normal  |   Milestone:  2.0.0   
Component:  Other   |     Version:  1.3.0   
 Keywords:          |  
--------------------+-------------------------------------------------------

Comment(by jpeel):

 {{{
 complex(array([1j]))
 }}}
 fails as it does because Python first checks if the input to complex is of
 type complex, then sees if the input has a __complex__ method, and finally
 tries setting the real part to be the input converted to a float.

 What we need then is for complex arrays to have a __complex__ method that
 works similarly to the __float__ methods: it will only return a value if
 the length of the array is 1. However, I currently don't see a simple way
 to add the __complex__ method to only complex arrays. We could add it to
 all arrays I suppose, but that doesn't seem right to me.

 Regarding the second bit of code:

 {{{
 a = zeros(2, complex)
 b = ones(1) * 1j
 a[0] = b
 }}}

 this fails in a similar way because, in
 core/src/multiarray/arraytypes.c.src, setitem currently tries to use
 PyComplex_AsCComplex to set the item. Thus, fixing the first problem will
 mostly fix this problem as well. However, some accuracy could be lost if
 the type is complex192 for instance. On the other hand, accuracy is
 already lost when calling a[0] where a is a complex192 array because the
 *_getitem() function returns a PyComplex object.

 So, the most pressing point question is: should we put a __complex__
 method on every ndarray? If not, then what is the easiest way to add the
 __complex__ method to only the complex arrays?

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1590#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list