0
$\begingroup$

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?

$\endgroup$
6
  • 2
    $\begingroup$ 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? $\endgroup$
    – Nasser
    Commented Jul 1, 2014 at 15:44
  • $\begingroup$ Yes, I used "Import" to import a list of numbers outputted by Fortran. $\endgroup$
    – Mencia
    Commented Jul 1, 2014 at 16:14
  • 1
    $\begingroup$ You might be able to tweak Import directly, but you should provide the original data you import for that. $\endgroup$
    – Yves Klett
    Commented Jul 1, 2014 at 16:17
  • 2
    $\begingroup$ 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/… $\endgroup$
    – Nasser
    Commented Jul 1, 2014 at 16:21
  • $\begingroup$ (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"]; $\endgroup$
    – Mencia
    Commented Jul 2, 2014 at 8:43

3 Answers 3

2
$\begingroup$

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}

$\endgroup$
0
3
$\begingroup$

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[Internal`StringToDouble[#] & /@ data, 2]

Mathematica graphics

$\endgroup$
1
$\begingroup$
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}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.