Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Importing complex numbers into mathematica

426 views
Skip to first unread message

mike

unread,
Nov 1, 2002, 9:35:24 PM11/1/02
to
Hi

This may be a stupid question but is there an easy way to import
complex numbers from a file into mathematica. I have written some c++
code that outputs a set of complex numbers to a file and I have been
trying to get them into mathematica. What format should I use?

To give myself a clue I exported a couple of complex numbers from
Mathematica to see what the file would look like:

Export["test.dat", {3 + 2 I, 1 + 0.5 I}, "List"];

which gives me the following test.dat:
3 + 2 I
1 + 0.5 I

so far so good - looks like thats the format I need my c++ code to
output but when i try

test = Import["test.dat", "List"];
i get the following output
test = {3, "+", 2, "I", 1, "+", 0.5, "I"}

Can anyone help me? I want to be able to Import the file directly - I
would rather not simply import a big list of numbers and write a
function that makes the relavent ones complex. Any help would be much
appreciated

Mike

Dale Horton

unread,
Nov 1, 2002, 9:54:51 PM11/1/02
to
The separation between list elements is determined by a ConversionOption

Import["test.dat", "List",
ConversionOptions -> {"ListSeparators" -> {"\n", "\r"}}]

-Dale

Sseziwa Mukasa

unread,
Nov 1, 2002, 9:58:57 PM11/1/02
to

On Thursday, October 31, 2002, at 04:41 AM, mike wrote:

> Hi
>
> This may be a stupid question but is there an easy way to import
> complex numbers from a file into mathematica. I have written some c++
> code that outputs a set of complex numbers to a file and I have been
> trying to get them into mathematica. What format should I use?
>
> To give myself a clue I exported a couple of complex numbers from
> Mathematica to see what the file would look like:
>
> Export["test.dat", {3 + 2 I, 1 + 0.5 I}, "List"];
>
> which gives me the following test.dat:
> 3 + 2 I
> 1 + 0.5 I
>
> so far so good - looks like thats the format I need my c++ code to
> output but when i try
>
> test = Import["test.dat", "List"];
> i get the following output
> test = {3, "+", 2, "I", 1, "+", 0.5, "I"}
>
> Can anyone help me? I want to be able to Import the file directly - I
> would rather not simply import a big list of numbers and write a
> function that makes the relavent ones complex. Any help would be much
> appreciated
>

If you are only going to read the file in Mathematica output the
results as a Mathematica expression e.g. {3+2 I, 1+0.5 I} and use << to
read it in Mathematica.

Regards,

Ssezi


Tomas Garza

unread,
Nov 1, 2002, 10:03:02 PM11/1/02
to
One possibility is that you keep your source data as a "CSV" file. For
example,

In[1]:=
Export["test.dat", {3 + 2*I, 1 + 0.5*I}, "CSV"];

In[2]:=
test = Import["test.dat", "CSV"]
Out[2]=
{{"3 + 2*I", "1 + 0.5*I"}}

As you can see, the imported data are strings. Then use ToExpression to
convert them to numbers:

In[3]:=
a = ToExpression[test]
Out[3]=
{{3 + 2*I, 1 + 0.5*I}}

In[4]:=
Head[a[[1,1]]]
Out[4]=
Complex

I guess this is what you were looking for.

Tomas Garza
Mexico City


----- Original Message -----
From: "mike" <mike_u...@yahoo.co.uk>
Subject: Importing complex numbers into mathematica


> Hi
>
> This may be a stupid question but is there an easy way to import
> complex numbers from a file into mathematica. I have written some c++
> code that outputs a set of complex numbers to a file and I have been
> trying to get them into mathematica. What format should I use?
>
> To give myself a clue I exported a couple of complex numbers from
> Mathematica to see what the file would look like:
>
> Export["test.dat", {3 + 2 I, 1 + 0.5 I}, "List"];
>
> which gives me the following test.dat:
> 3 + 2 I
> 1 + 0.5 I
>
> so far so good - looks like thats the format I need my c++ code to
> output but when i try
>
> test = Import["test.dat", "List"];
> i get the following output
> test = {3, "+", 2, "I", 1, "+", 0.5, "I"}
>
> Can anyone help me? I want to be able to Import the file directly - I
> would rather not simply import a big list of numbers and write a
> function that makes the relavent ones complex. Any help would be much
> appreciated
>

> Mike
>
>

Liguo Song

unread,
Nov 1, 2002, 10:07:06 PM11/1/02
to
Hi, Mike,

Using Export["Test.dat", {3 + 2 I, 1 + 0.5 I}, "Expression"] will get you the
file with the following content:

{3 + 2*I, 1 + 0.5*I}

Then, use Import["Test.dat","Expression"] will get {3+2 I, 1+0.5 I}. So, try to
make your program out in this format and then import as Expression.

Hope it helps.


Liguo

Jens-Peer Kuska

unread,
Nov 5, 2002, 5:07:24 AM11/5/02
to
Hi,

and

ReadList["test.dat"]

does what you want.

Regards
Jens

0 new messages