Next: Package documentation Up: Construction Previous: Localised definitions

Renaming functions

It turns out to be efficient and flexible to rename functions by applying a head replacement function. The example below illustrates this. The function Csc is redefined in terms of a compiled language equivalent and the result is formatted as a FORTRAN expression. The function FD is used for the FORTRAN Definition.


In[1]:= mainformat[expr_,FortranForm]:= FortranForm[expr];
In[2]:= myformat[expr_] :=
          Block[{Csc,FD},
            FD[Sin]=sin;
            Csc[x_]:= Evaluate[1/FD[Sin][x]];;
            mainformat[expr,FortranForm]
          ];
Evaluate is used to avoid head replacement each time the rule is executed, by inserting the redefinition when the rule is created.

In[3]:= myformat[y Csc[2 x]]
Out[3]= y/sin(2*x)
Using this method, temporary rules defined during execution of the Assign functions do not interfere with global definitions. As a cautionary note it is emphasise that this approach may affect the evaluation of expressions. For example, Csc[2.3] would not evaluate numerically if myformat had the attribute HoldAll. Thus numerical approximation (such as using N) must be performed before this step.


bondaren@thsun1.jinr.ru