2
$\begingroup$

I've read similar-but-not-quite-the-same questions : I have to read a python-generated data file which contains complex numbers. So, the numbers appear as (real+imag j).

And of course Mathematica read them as strings using

Import["","Table"]

I've tried fiddling with StringSplit or Read[StringToStream,] to no avail...

Any help would be much appreciated.

$\endgroup$
  • 2
    $\begingroup$ Paste a little snippet of the data file so we can see what the format is exactly. $\endgroup$ – Jason B. Feb 10 '16 at 10:04
  • 1
    $\begingroup$ If you are exporting the data from Python yourself, the best solution will be to split the complex numbers into real and imaginary parts and export them as distinct units. Parsing special forms in Mathematica is possible but not fast. If your data file is a simple table with only real numbers, there are very fast builtin functions to read it. $\endgroup$ – Szabolcs Feb 10 '16 at 10:06
  • $\begingroup$ Unfortunately, I am not the one generating the data file. Here are the first few elements : (1e-07+0j) (-0.998722569699-0.000124681423362j) (-0.00620217196207+0.0501472284707j) (-53.0662993987-543.342932199j) (1837.15+0j) (3.7656639408e-16+0j) $\endgroup$ – coussin Feb 10 '16 at 10:18
  • 1
    $\begingroup$ @coussin, is the file like that, all on one line? Or is each complex number on a separate line, with parantheses? $\endgroup$ – Jason B. Feb 10 '16 at 10:44
  • $\begingroup$ @coussin please edit your question to add any extra information, such as data examples. It would be a good idea to offer a permanent paste on pastebin.com so we can see the format of the line and fields separators and headers if present at all. See this question on meta for ways for sharing data. Also please try to share code in formatted form. $\endgroup$ – rhermans Feb 10 '16 at 10:56
4
$\begingroup$

So I have a data file, whose contents look like this:

(-0.998722569699-0.000124681423362j)    (-0.00620217196207+0.0501472284j)   (-53.0662993987-543.342932199j) (1837.15+0j)    (3.7656639408e-16+0j)
(-0.998722569699-0.000124681423362j)    (-0.00620217196207+0.0501472284j)   (-53.0662993987-543.342932199j) (1837.15+0j)    (3.7656639408e-16+0j)
(-0.998722569699-0.000124681423362j)    (-0.00620217196207+0.0501472284j)   (-53.0662993987-543.342932199j) (1837.15+0j)    (3.7656639408e-16+0j)
(-0.998722569699-0.000124681423362j)    (-0.00620217196207+0.0501472284j)   (-53.0662993987-543.342932199j) (1837.15+0j)    (3.7656639408e-16+0j)

I have this saved as "~/Documents/pythonfile.dat". This works with the example given:

Map[ToExpression@StringReplace[#, "e" -> "*^"] &, 
  Import["~/Documents/pythonfile.dat", "Table"], 1] /. j -> I

(*
{{-0.998723 - 0.000124681 I, -0.00620217 + 0.0501472 I, -53.0663 - 
   543.343 I, 1837.15, 
  3.76566*10^-16}, {-0.998723 - 0.000124681 I, -0.00620217 + 
   0.0501472 I, -53.0663 - 543.343 I, 1837.15, 
  3.76566*10^-16}, {-0.998723 - 0.000124681 I, -0.00620217 + 
   0.0501472 I, -53.0663 - 543.343 I, 1837.15, 
  3.76566*10^-16}, {-0.998723 - 0.000124681 I, -0.00620217 + 
   0.0501472 I, -53.0663 - 543.343 I, 1837.15, 3.76566*10^-16}}
*)

The main points are

  1. First import it as a "Table", this will put every complex number inside a table as a string.
  2. Next you need to process the strings. You could just use ToExpression to turn the string "(1+2j)" into the expression 1+2j, at which point you replace the variable j with the imaginary unit I
  3. But there is the added problem of the "e" notation for base ten. This wouldn't be a problem on its own, but in combination with the imaginary unit and string recognition, we have to adjust a bit, and replace "e" with "*^" before converting the strings to expressions.
$\endgroup$
  • $\begingroup$ Thanks Jason B, that works ! Thanks everyone :) $\endgroup$ – coussin Feb 10 '16 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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