2

i have to save a matrix shown below with complex data into a file with .H extension using savetxt command of numpy but i am not able to save it.the matrix to be saved is

[[ 1.0+0.j 0.0+0.j 0.0+0.j] [ 1.0+0.j 1.0+0.j 0.0+0.j] [ 2.0+0.j 2.0+0.j 0.0+0.j] ..., [ 683.0+0.j 688.0+0.j -2.7+0.j] [ 684.0+0.j 689.0+0.j -2.7+0.j] [ 685.0+0.j 690.0+0.j -2.7+0.j]] 

i have tried this command but its giving error

>>savetxt('H.H',H_new.H,fmt='%.4e%+.4j%.4e%+.4j%.4e%+.4j',delimiter=' '); Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 974, in savetxt % fmt) AttributeError: fmt has wrong number of % formats. %.4e%+.4j%.4e%+.4j%.4e%+.4j 
6
  • What version of numpy are you using? (Check numpy.__version__.) Commented Jan 9, 2014 at 6:09
  • i am using 1.6.1 version of numpy Commented Jan 26, 2014 at 6:39
  • I updated my answer with a method that works in numpy 1.6.1. Commented Jan 26, 2014 at 13:40
  • thanks for ur reply but i updated my numpy to 1.7.1 and ur earliar solution is working fine Commented Jan 27, 2014 at 4:53
  • i have one more query that if i want to import this same complex values containing file in my program what procedure should i follow? Commented Jan 27, 2014 at 5:07

1 Answer 1

2

Your format string is missing some format characters. Try something like

fmt='%.4e%+.4ej %.4e%+.4ej %.4e%+.4ej' 

But it looks like you'll need numpy 1.7.0 or later; see https://github.com/numpy/numpy/commit/1475319160baed782daa39e0de0c0655c2abe4b5

It worked for me with numpy 1.8.0, but not with 1.6.1.

With numpy 1.6.1, you can save the data in this format by first creating a real view of the complex data.

In this example (an ipython session), I'm using numpy 1.6.1:

In [28]: np.__version__ Out[28]: '1.6.1' 

z is a complex array, with shape (2, 3):

In [29]: z Out[29]: array([[ 1.+2.j, -3.+4.j, 5.-6.j], [-1.+0.j, 0.+2.j, 3.+0.j]]) 

Use the view method to create a real array with shape (2, 6):

In [30]: rz = z.view(float) In [31]: rz Out[31]: array([[ 1., 2., -3., 4., 5., -6.], [-1., 0., 0., 2., 3., 0.]]) 

Save the array using savetxt. The format string uses the entire row, so there is no need to give the delimiter argument:

In [32]: savetxt('z.txt', rz, fmt='%.4e%+.4ej %.4e%+.4ej %.4e%+.4ej') 

Take a look at the output:

In [33]: !cat z.txt 1.0000e+00+2.0000e+00j -3.0000e+00+4.0000e+00j 5.0000e+00-6.0000e+00j -1.0000e+00+0.0000e+00j 0.0000e+00+2.0000e+00j 3.0000e+00+0.0000e+00j 

This file can be read back into a numpy array with np.loadtxt or np.genfromtxt. To use loadtxt, it must be told that the data type is complex:

In [8]: loadtxt('z.txt', dtype=np.complex128) Out[8]: array([[ 1.+2.j, -3.+4.j, 5.-6.j], [-1.+0.j, 0.+2.j, 3.+0.j]]) 

genfromtxt works with the same arguments. genfromtxt can also attempt to determine the data type on its own, by giving the argument dtype=None. This works for complex values:

In [9]: genfromtxt('z.txt', dtype=None) Out[9]: array([[ 1.+2.j, -3.+4.j, 5.-6.j], [-1.+0.j, 0.+2.j, 3.+0.j]]) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.