Next: A generalised Runge-Kutta Up: Splice and template Previous: Defining a subroutine

Combining code

The adherence to ANSI compiled language functions is illustrated with an example of a function template file for use with the subroutine of the previous section.


In[4]:= !!func.mf

        double precision function f(x)
        double precision x
<*  (***** Function Definition *****)

    f = D[(Sin[x] + Cos[x]) Cot[x],x];

    (***** FORTRAN Translation *****)

    FortranAssign[f,f]  *>
        return
        end
In addition, Mathematica could be used to specify the type declaration (real, complex etc.) depending on the requested precision.


In[5]:= Splice["func.mf",FormatType->OutputForm];

In[6]:= !!func.f

        double precision function f(x)
        double precision x
        f=-((cos(x)+sin(x))/sin(x)**2)+1.d0*(cos(x)-sin(x))/tan(x)
        return
        end
Any definitions made in an external file will remain unless cleared.


In[7]:= Clear[f];
A simple main routine, driver.f, is provided for driving the processed function func.f and subroutine sub.f. For example, in a UNIX environment, link the driver, subroutine and function files by issuing the command:

f77 driver.f func.f sub.f
and execute the resulting file (the default is a.out on UNIX systems). Various options enable renaming of the executable file (-o) and compiler optimization (-O) and the user should consult their compiler documentation for more information.

The driver routine can also be generalised using a template file approach. Often it is more convenient to merge the routines in a single template file. It is also good practice to keep all information required for the problem in the template file (and not in additional Mathematica files). This ensures that the program is self-contained and eases the burden of code maintenance.


bondaren@thsun1.jinr.ru