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

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


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)

One other solution may be to use record arrays:

In [40]: x = N.array([[10,10,20,20],[20,20,30,30]],N.uint8)

In [41]: y = x.view([('real',N.uint8),('imag',N.uint8)]).reshape(x.shape[0],-1)

In [42]: y['real']
Out[42]: 
array([[10, 20],
       [20, 30]], dtype=uint8)

In [43]: y['imag']
Out[43]: 
array([[10, 20],
       [20, 30]], dtype=uint8)

Then you can at least do some arithmetic manually without any copying
of data taking place.

Regards
Stéfan


More information about the Numpy-discussion mailing list