next up previous contents
Next: Maintaining appropriate precision Up: Implementation Previous: Conforming to ANSI standards

Utilising intrinsic compiled language functions

It is desirable to convert Mathematica functions into intrinsic (built-in) compiled language functions whenever possible. The reason for this adherence is not merely syntactic. In many cases the code produced is more efficient.

Mathematica converts Exp[x] to Power[E,x] which is then formatted in a way that does make use of the FORTRAN exp function. It is inefficient to calculate the exponential of a function as a power of a constant E in this way.

In[4]:= FortranForm[Exp[x]]

Out[4]//FortranForm= E**x
FortranAssign converts to the exponential function in FORTRAN.

In[5]:= FortranAssign[x Exp[y],AssignIndent->""]

Out[5]//OutputForm= x*exp(y)
Consider the inverse trigonometric and inverse hyperbolic family of functions.

In[6]:= FortranForm[ArcSin[x]]

Out[6]//FortranForm= ArcSin(x)
ArcSin is not a legitimate intrinsic FORTRAN function. The corresponding FORTRAN function is asin.

In[7]:= FortranAssign[ArcSin[x],AssignIndent->""]

Out[7]//OutputForm= asin(x)
Some related functions have no compiled language analogy.

In[8]:= FortranForm[ArcCot[x]]

Out[8]//OutputForm= ArcCot(x)
ArcCot is not a legitimate FORTRAN function (and neither is acot). FortranAssign converts such functions by default, controlled via the option AssignTrig.

In[9]:= FortranAssign[ArcCot[x],AssignIndent->""]

Out[9]//OutputForm= atan(1.d0/x)
Notice that ArcCot is not uniquely defined at x=0, despite the fact that Mathematica returns the result Pi/2 (perhaps it should return $\pm
Pi/2$). There is therefore no loss of generality in using the above definition. The handling of branch cuts and choice of principal values is a common criticism of computer algebra systems [33]. If principal values are not used in compiled languages, substantial loss of precision can result.

The existence of inverse hyperbolic functions is compiler dependent since they are not part of the ANSI standard. Some compilers provide intrinsic functions for these. When the compiler does not support such functions, an option for rewriting in terms of Log and Sqrt is provided which return the unique principal value.

In[10]:= FortranAssign[ArcTanh[x],AssignHyperbolic->True]

Out[10]//OutputForm=
        5.d-1*log((1.d0+x)/(1.d0-x))
Moreover, Mathematica's representation of the two argument function ArcTan is somewhat unusual in reversing the standard convention.

In[11]:= FortranAssign[ArcTan[y,x]]

Out[11]//OutputForm=
        atan2(x,y)
In summary the options AssignTrig and AssignHyperbolic may be used to format trigonometric and hyperbolic functions, which may not be supported by an ANSI C or FORTRAN conforming compiler. A method of quantifying the magnification of errors in expression sequences is the condition number [26]. Transformations such as those performed above should also be justified on this basis.


next up previous contents
Next: Maintaining appropriate precision Up: Implementation Previous: Conforming to ANSI standards

Jorge Romao
5/14/1998