MathGroup Archive 1994

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

Search the Archive

Re: Fortran code

  • To: mathgroup at
  • Subject: Re: Fortran code
  • From: rubin at (Paul A. Rubin)
  • Date: Tue, 28 Jun 94 11:02:52 EDT

>Dear MMA users,
>I wish to pass some parameter values generated as a list by MMA to a Fortran
>code. The following is an example.
>In[12]:= FortranForm[{s1 -> 1., s2 -> 2.}]
>Out[12]//FortranForm= List(Rule(s1,1.),Rule(s2,2.))
>VAX Fortran does not understand "List" and "Rule". How does one circumvent
>this ?
It depends on whether you want to put the parameters into a data file your
program will read, or embed them directly into the source code.  To create a
data file, use PutAppend, and map it across the list of values resulting
from the substitutions.  For instance:

In[]:=  r = {s1 -> 1., s2 -> 2. 10^9};  (* creates the substitution rules *)

In[]:=  PutAppend[ FortranForm[ # ], "g:\\out" ]& /@ ({s1, s2} /. r);

The result is a text file ("out") containing the values in FORTRAN format:


Note that:  (a) you have to apply the substitution rules to a list {s1, s2} to
extract the values; and (b) you have to Map PutAppend across the list (if
you just Put the list, I'm pretty sure you get the braces and all).  BTW, I
multiplied your s2 value by 10^9 to make sure the exponent was being
converted to a format suitable for FORTRAN.

The easiest way to embed the numbers in the source code is probably to use
Splice[].  Write the source code, and where you want the numbers to go, put
a Mma expression to generate them, bracketing the expression with "<*" and
"*>" (these can be changed using an option to Splice if they present a
problem.  Example:

=== Mma code ===
In[]:=  r = {s1 -> 1., s2 -> 2. 10^9};  (* creates the substitution rules *)

In[]:=  Splice[ "g:\\test", "g:\\test.2", FormatType -> FortranForm ];

=== FORTRAN source file before splicing ("test") ===

      REAL S1, S2
      S1 = <* s1 /. r *>
      S2 = <* s2 /. r *>
      PRINT *, S1, S2

=== FORTRAN source file after splicing ("test.2") ===

      REAL S1, S2
      S1 = 1.
      S2 = 2.e9
      PRINT *, S1, S2


* Paul A. Rubin                                  Phone: (517) 336-3509   *
* Department of Management                       Fax:   (517) 336-1111   *
* Eli Broad Graduate School of Management        Net:   RUBIN at MSU.EDU    *
* Michigan State University                                              *
* East Lansing, MI  48824-1122  (USA)                                    *
Mathematicians are like Frenchmen:  whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different.                                    J. W. v. GOETHE

  • Prev by Date: Re: Question about Cases command.
  • Previous by thread: Fortran code
  • Next by thread: help number instruction