[Numpy-discussion] reinterpret complex <-> float

Warren Weckesser warren.weckesser at enthought.com
Fri Jun 3 14:56:46 EDT 2011


On Fri, Jun 3, 2011 at 1:46 PM, Neal Becker <ndbecker2 at gmail.com> wrote:

> Can I arrange to reinterpret an array of complex of length N as an array of
> float of length 2N, and vice-versa?  If so, how?
>


You can use the view() method (if the data is contiguous):

In [14]: z = array([1.0+1j, 2.0+3j, 4, 9j])

In [15]: x = z.view(float64)

In [16]: x
Out[16]: array([ 1.,  1.,  2.,  3.,  4.,  0.,  0.,  9.])


But this won't work if the data in the array is not contiguous:

In [17]: z2 = z[::2]

In [18]: z2
Out[18]: array([ 1.+1.j,  4.+0.j])

In [19]: z2.view(float64)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/warren/<ipython console> in <module>()

ValueError: new type not compatible with array.


(I'm using ipython with the -pylab option; 'array' and 'float64' are from
numpy.)


Warren



>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110603/5cb3b870/attachment.html>


More information about the NumPy-Discussion mailing list