Assigning complex values to numpy arrays?

后端 未结 3 1632
北海茫月
北海茫月 2020-12-09 17:25

This gives the expected result

x = random.rand(1) + random.rand(1)*1j
print x.dtype
print x, x.real, x.imag

and this works

         


        
3条回答
  •  醉话见心
    2020-12-09 18:23

    I used astype to change the type to complex and it worked in my case (Python 3), although I am not sure whether it is the best way. One example:

    import numpy as np
    c2 = np.empty([2,2]).astype(complex)
    c2[0] = 5j+2
    

提交回复
热议问题