[Numpy-discussion] reinterpret complex <-> float

Robert Kern robert.kern at gmail.com
Fri Jun 3 14:55:36 EDT 2011


On Fri, Jun 3, 2011 at 13:46, 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?

[~]
|1> carr = np.arange(10, dtype=complex)

[~]
|2> carr
array([ 0.+0.j,  1.+0.j,  2.+0.j,  3.+0.j,  4.+0.j,  5.+0.j,  6.+0.j,
7.+0.j,  8.+0.j,  9.+0.j])

[~]
|3> carr.view(float)
array([ 0.,  0.,  1.,  0.,  2.,  0.,  3.,  0.,  4.,  0.,  5.,  0.,
6.,  0.,  7.,  0.,  8.,  0.,  9.,  0.])

[~]
|4> _.view(complex)
array([ 0.+0.j,  1.+0.j,  2.+0.j,  3.+0.j,  4.+0.j,  5.+0.j,  6.+0.j,
7.+0.j,  8.+0.j,  9.+0.j])


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list