[Numpy-discussion] dumb question about creating a complex array

Stefan van der Walt stefan@sun.ac...
Thu Feb 22 17:21:01 CST 2007


Hi Matthew

On Thu, Feb 22, 2007 at 02:50:16PM -0800, Mathew Yeates wrote:
> given an array of floats, 2N columns and M rows, where the elements 
> A[r,2*j] and A[r,2*j+1] form the real and imaginary parts of a complex 
> number ....... What is the simplest way to create a complex array? It's 
> a fairly large array so I want to keep copying to a minimum.
> 
> (Actually, it's not a float array, its elements are byte sized, in case 
> that matters)

That's a bit problematic, since numpy doesn't have Complex16.  If you
had two 32-bit floats, you could do:

In [14]: x = N.array([10,10,20,20],dtype=N.float32)

In [15]: x.view(N.complex64)
Out[15]: array([ 10.+10.j,  20.+20.j], dtype=complex64)

As things stand, you may have to copy:

In [16]: x = N.array([10,10,20,20],dtype=N.uint8)

In [19]: x.astype(N.float32).view(N.complex64)
Out[19]: array([ 10.+10.j,  20.+20.j], dtype=complex64)

Maybe someone else comes up with a better answer.

Cheers
Stéfan


More information about the Numpy-discussion mailing list