dumb question about creating a complex array
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) Mathew --
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
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
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)
It does. If it was a float array, you may even be able to do it without any copying at all. Anyway, this should work:
a = N.array([[1,2,3,4],[2,3,4,5],[4,5,6,7]], dtype=N.byte) a array([[1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]], dtype=int8) # have I got that right? b = N.empty((a.shape[0],a.shape[1]/2), dtype=N.complex) b.real = a[:,range(0,a.shape[1],2)] b.imag = a[:,range(1,a.shape[1],2)] b array([[ 1.+2.j, 3.+4.j], [ 2.+3.j, 4.+5.j], [ 4.+5.j, 6.+7.j]])
Is that what you wanted? By the way, I think there is a trick for doing the every other column trick without using range(), but I can't find it at the moment. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
Thanks for the replies. I think I have enough to work with. Mathew Christopher Barker wrote:
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)
It does. If it was a float array, you may even be able to do it without any copying at all. Anyway, this should work:
a = N.array([[1,2,3,4],[2,3,4,5],[4,5,6,7]], dtype=N.byte) a array([[1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]], dtype=int8) # have I got that right? b = N.empty((a.shape[0],a.shape[1]/2), dtype=N.complex) b.real = a[:,range(0,a.shape[1],2)] b.imag = a[:,range(1,a.shape[1],2)] b array([[ 1.+2.j, 3.+4.j], [ 2.+3.j, 4.+5.j], [ 4.+5.j, 6.+7.j]])
Is that what you wanted?
By the way, I think there is a trick for doing the every other column trick without using range(), but I can't find it at the moment.
-Chris
--
participants (3)
-
Christopher Barker -
Mathew Yeates -
Stefan van der Walt