Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting np-array from float to complex

Tags:

python

numpy

I have an numpy-array with a float data type, but an extern function needs it with a complex data type. When I use a.view(np.complex), the real values are also used for the complex values, messing up my further calculations, i.e.:

a = [1, 2, 3]
b = a.view(np.complex)
> b = [1+1i, 2+2i, 3+3i]

Is there a command such that I get

> b = [1+0i, 2+0i, 3+0i]

?

like image 685
arc_lupus Avatar asked Nov 18 '15 16:11

arc_lupus


1 Answers

Yes, use astype():

In [6]: b = a.astype(complex)
In [7]: b
Out[7]: array([ 1.+0.j,  2.+0.j,  3.+0.j])

In [8]: b.dtype
Out[8]: dtype('complex128')
like image 197
xnx Avatar answered Sep 20 '22 04:09

xnx