Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy read complex numbers from text file

Tags:

python

numpy

I have used numpy to save a matrix of complex numbers. The output file looks like that:

(1.100412357301083777e-02+1.471303433818593742e-02j)     (1.511426586599529109e-02+-2.516143258497194335e-03j)   
(1.084202636262432407e-02+1.438252996657629623e-02j)     (1.447620213198375083e-02+4.471111098343749646e-03j)    

Now, I tried reading it in using numpy data = np.loadtxt('PsiPfree1.out', delimiter='\t', dtype=np.complex128), but I get the following error:

items = [conv(val) for (conv, val) in zip(converters, vals)]  
ValueError: complex() arg is a malformed string`

Any ideas how I could get this to work?

Edit: I now also tried without the parenthesis making the matrix look like this:

1.100412357301083777e-02+1.471303433818593742e-02j  1.511426586599529109e-02+-2.516143258497194335e-03j
1.084202636262432407e-02+1.438252996657629623e-02j  1.447620213198375083e-02+4.471111098343749646e-03j

This results in the same issue.


1 Answers

The problem is, that the numpy savetxt function np.savetxt('PsiPges.out',PsiPges , delimiter='\t') I used exports the data matrix, but for all negative imaginary parts it writes +- (see matrix posted above). If this is replaced by - only, the loadtxt function works correctly.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!