MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: import FortranForm back

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90620] Re: import FortranForm back
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 16 Jul 2008 06:29:53 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <g5hthn$jad$1@smc.vnet.net>

jyan.stat at gmail.com wrote:

> I have a file saved in FortranForm from some previous work. Now I'd
> like to read it back in and continue working on it. For example,
> 
> foo := Table[a i, {i, 1, 4}]
> Export["foo", FortranForm[foo], "Table"]
> Import["foo", "Table"]
> foo
> 
> Note that the last two lines are not the same. How can I recover foo
> from Import? Thanks in advance.

Using *FullForm[]* on the above example it is relatively easy to spot 
that the imported data are not in a correct format to be fully evaluated 
by Mathematica:

     In[1]:= foo = Table[a i, {i, 1, 4}];
     Export["foo", FortranForm[foo], "Table"];
     foo2 = Import["foo", "Table"];
     foo // FullForm
     foo2 // FullForm
     foo2 === foo


     Out[4]//FullForm= List[a, Times[2, a], Times[3, a], Times[4, a]]

     Out[5]//FullForm= List[List["List(a,2*a,3*a,4*a)"]]

     Out[6]= False

So you must massage the imported data to put them into a syntactically 
correct input form:

     In[7]:= ToExpression[StringReplace[Sequence @@ Flatten[foo2],
             {"(" -> "[", ")" -> "]"}]]
      % === foo

     Out[7]= {a, 2 a, 3 a, 4 a}

     Out[8]= True


Anyway, it is more likely that what you want is along the lines,

     In[9]:= foo = Table[a i, {i, 1, 4}];
     Export["foo", FortranForm /@ foo, "Table"];
     foo2 = ToExpression @@@ Import["foo", "Table"];
     foo // FullForm
     foo2 // FullForm
     foo2 === foo


     Out[12]//FullForm= List[a, Times[2, a], Times[3, a], Times[4, a]]

     Out[13]//FullForm= List[a, Times[2, a], Times[3, a], Times[4, a]]

     Out[14]= True


Regards,
-- Jean-Marc



  • Prev by Date: Re: WorldPlot, WorldRotation... with a twist?
  • Next by Date: Re: Another problem with FindMinimum
  • Previous by thread: Re: import FortranForm back
  • Next by thread: A NDSolve within a FindMinimum/NMinimize