2

I apologize if this is a stupid question, but this is driving me berserk. I generate a large Table of complex numbers in Mathematica and I want to save it and read it in later. Looking at the documentation I would write:

In[1626]:= foo = Table[i + I j , {i, 1, 3}, {j, 1, 3}];
Export["foo.dat", foo, "Table"]; 
fooRead = Import["foo.dat", "Table"]

which generates:

Out[1628]= {
     {"1+I", "1+2*I", "1+3*I"},
     {"2+I", "2+2*I", "2+3*I"},
     {"3+I", "3+2*I", "3+3*I"}
}

It looks like this is ok, but note the quotation marks in the output above. It's not the same matrix! If you square it, you get weird results!

In[1629]:= foo^2
           fooRead^2

Out[1629]= {
     {    2 I, -3 + 4 I,  -8 + 6 I},
     {3 + 4 I,      8 I, -5 + 12 I},
     {8 + 6 I, 5 + 12 I,      18 I}
}

Out[1630]= {
     {("1+I")^2, ("1+2*I")^2, ("1+3*I")^2},
     {("2+I")^2, ("2+2*I")^2, ("2+3*I")^2},
     {("3+I")^2, ("3+2*I")^2, ("3+3*I")^2}
}

So, my questions are: how can I write out a complex table so that I can read it in again later? And why doesn't Mathematica import the file it exported? (ADthanksVANCE)

1 Answer 1

2

If you only need mathematica to read the file use ".m" format:

 foo = Table[i + I j, {i, 1, 3}, {j, 1, 3}];
 Export["foo.m", foo];
 fooRead = Import["foo.m"]

Thanks @Aakter for noting this is called "Package" format. ( also ".mx" )

You can alternately use:

 ToExpression[fooRead]

on your "Table" export/import, which might be desirable if you want the file more human readable.

why?

To somewhat explain the "Table" behavior, note that table is a space delimited format that can contain arbitrary strings (without quote marks). If you run this:

 Clear[a]
 ExportString[ {a, "a", 1, "1", 1 + I, "1+I",Pi,"Pi"} , "Table" ]

You'll see that symbols, and strings, integers and strings representing integers, complex numbers and strings representing complex numbers all appear exactly the same in the output.

a
a
1
1
1+I
1+I
Pi
Pi

So when you Import such file mathematica has no way to distinguish strings from numbers or expressions. Import treats everything as strings, except for explicit numbers. My guess is if it was to try to recognize things that might be valid expressions and treat them as such you would have a bunch of other issues.

If you instead export as "Package" strings qet quoted

 {a, "a", 1, "1", 1 + I, "1+I", Pi, "Pi"}

so that the file can be Imported without ambiguity..

3
  • 1
    +1, and you could aslo try Export[file, expr, "Package"] and Import[file, "Package"].
    – akater
    Jun 14, 2014 at 16:19
  • Thanks! Your first suggestion does indeed work; the second is a bit too slow. However, why can not Mathematica import a file in Table format that it exported, itself, in Table format? Every other language I know can import the files that it exported! Jun 14, 2014 at 22:32
  • added a bit of explanation. As a total aside, if you are doing numerical work you can write IEEE complex numbers in binary way faster than any of this.
    – agentp
    Jun 16, 2014 at 15:19

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.