# Formatting Fortran complex number output for use in Mathematica

Here is a list that was orignially output from Fortran.

{{"(0.277336296055697, 0.120000000000000E+000)"},
{"(-1.489297392708939E-002, 0.340000000000000E+000)"}}


which is to be interpreted as a list of two complex numbers.

Of course, Mathematica does not understand this, so, I would like to convert it into something like

{{0.277336296055697 + 0.12I}, {-0.01489297392708939 + 0.34I}}


Can I do this programmatically in Mathematica?

• How did you "get" the array from Fortran to look like this in first place and have it land inside Mathematica? Was the data in file and you imported it into Mathematica? Commented Jul 1, 2014 at 15:44
• Yes, I used "Import" to import a list of numbers outputted by Fortran. Commented Jul 1, 2014 at 16:14
• You might be able to tweak Import directly, but you should provide the original data you import for that. Commented Jul 1, 2014 at 16:17
• How did you use "Import"? Please provide example of the data in the file, and the command you used. M can import Fortran data reference.wolfram.com/mathematica/tutorial/… Commented Jul 1, 2014 at 16:21
• (0.277336296055697,0.000000000000000E+000) (-1.489297392708939E-002,0.000000000000000E+000) This is the Fortran file "chi0.dat", all the complex numbers in a column. Now, in Mathematica, I import it by doing chi=Import["chi0.dat"]; Commented Jul 2, 2014 at 8:43

Yet another one:

n = {{"(0.277336296055697,0.120000000000000E+000)"}, \
{"(-1.489297392708939E-002,0.340000000000000E+000)"}};

Complex @@@ ToExpression[StringReplace[Flatten[n], {"(" -> "{", ")" -> "}", "E" -> "*10^"}]]


{0.277336 + 0.12 I, -0.014893 + 0.34 I}

Another way

data = {{"(0.277336296055697,0.120000000000000E+000)"},
{"(-1.489297392708939E-002,0.340000000000000E+000)"}};
data = Flatten[StringCases[#, "(" ~~ x__ ~~ "," ~~ y__ ~~ ")"->{x, y}]&/@ a,Infinity];
Complex @@@ Partition[InternalStringToDouble[#] & /@ data, 2]


a = {{"(0.277336296055697,0.120000000000000E+000)"}, {"(-1.489297392708939E-002,0.340000000000000E+000)"}};

Complex @@ (ToExpression /@ {StringDrop[First@#, 1], StringTake[Last@#, 5]}) &
[Flatten@StringSplit[a[[#]], ","]] & /@ Range@Length@a
`

{0.277336 + 0.12 I, -6.04833 + 0.34 I}

• The second number does look suspicious... Commented Jul 1, 2014 at 17:09
• @YvesKlett - It not only "looks suspicious" - it's simply "wrong" (was distracted by Suisse vs. Argentina). Will correct tomorrow.
– eldo
Commented Jul 1, 2014 at 20:23