codegen
fortran
generate Fortran code
Calling Sequence
Parameters
Description
Examples
fortran(s);
fortran(s, options);
s
-
expression, array of expressions, or list of equations
Important: The codegen[fortran] command has been deprecated. Use the superseding command CodeGeneration[Fortran] instead. See CodeGeneration for information on translation to other languages.
The codegen[fortran] function generates Fortran 77 code for evaluating the input. The input s must be a single algebraic expression, an array of algebraic expressions, a list of equations of the form name = algebraic that is understood to mean a sequence of assignment statements, or a Maple procedure. If the array is not named, then the name unknown is used. For help on translating Maple procedures into Fortran, see codegen/fortran/procedure . The fortran command takes the following optional arguments for controlling the output.
The filename option: By default the output is sent to standard output. An optional argument of the form filename = "foo" can be used to direct the output to the file foo. The output will be appended to the file.
The optimized option: If the keyword optimized is specified as an optional argument, common subexpression optimization is performed. The result is a sequence of assignment statements in which temporary values are stored in local variables beginning with the letter t. The global names t0, t1, t2, ... are reserved for use by fortran for this purpose. The input to the optimizer must satisfy certain conditions. See codegen/optimize for more information.
The digits option: Non-floating point constants, such as integers, rationals, and symbols such as Pi, are converted using evalf to floating point constants where necessary; for example, as arguments to real functions such as sqrt. By default, the number of digits used is 7 for single precision, and 16 for double precision. This can be set to n digits by specifying an optional argument digits = n.
The precision option: If the optional argument precision=single or precision=double is given, this specifies whether floating-point constants are to be output in E (single precision) or D (double precision) notation, respectively. It also specifies how function names are to be translated. The default is single precision if the mode=single or mode=complex option is provided and double precision otherwise.
The mode option. The mapping of functions names from Maple to Fortran can be controlled by providing the optional argument mode=t, where t is one of single, double, generic or complex. If the mode option is not given, the default is mode=generic. For example, the natural logarithm function (ln or log in Maple) is translated as follows
mode translation
single alog
double dlog
complex clog
generic log
The renaming of functions, such as arctan in Maple to atan in Fortran, is done by the function `codegen/fortran/function_name`. You can change how functions are renamed. For example,
codegen/fortran/function_name⁡arctan,1,double≔datan
codegen/fortran/function_name⁡arctan,2,single≔atan2
In translating arrays (Maple vectors and matrices and other arrays), the fortran function does not reindex the arrays to the 1-based indexing that Fortran requires. Since Maple vectors and matrices are indexed from 1, this is not a problem. If you have an array indexed from 0 or something else, you have to convert it to 1-based indexing first. This can easily be done by using the convert function. See the help page for convert[array]. If the array contains unassigned entries, the value output in the Fortran code is the string undefined.
The Fortran language has a continuation line limit of 19 lines which, if exceeded, results in an error during compilation. For large expressions that exceed this limit, the fortran routine will automatically break up the expression. The global names s0, s1, s2, ... are reserved for use by fortran for this purpose.
The function fortran produces Fortran code as a side-effect and returns NULL as the function value. Therefore, the ditto commands % and %% will not recall the output from the fortran command.
The command with(codegen,fortran) allows the use of the abbreviated form of this command.
Important: The codegen[fortran] command has been deprecated. Use the superseding command CodeGeneration[Fortran] instead.
with⁡codegen,fortran:
f≔1−2⁢x+3⁢x2−2⁢x3+x4
f≔x4−2⁢x3+3⁢x2−2⁢x+1
fortran⁡f
t0 = x**4-2*x**3+3*x**2-2*x+1
fortran⁡f,optimized
t1 = x**2 t2 = t1**2 t7 = -2*t1*x+3*t1+t2-2*x+1
fortran⁡convert⁡f,horner,x
t0 = 1+(-2+(3+(-2+x)*x)*x)*x
f≔π⁢ln⁡x2−sqrt⁡2⁢ln⁡x22
f≔π⁢ln⁡x2−2⁢ln⁡x22
t0 = 0.3141592653589793D1*log(x**2)-sqrt(2.D0)*log(x**2)**2
t1 = x**2 t2 = log(t1) t4 = sqrt(2.D0) t5 = t2**2 t7 = t2*0.3141592653589793D1-t5*t4
fortran⁡s=x2,t=ln⁡s,r=π⁢t−sqrt⁡2⁢s2,precision=single
s = x**2 t = log(s) r = 0.3141593E1*t-sqrt(2.E0)*s**2
A≔array⁡1..2,1..2,symmetric:
A1,1≔log⁡x:A1,2≔1−log⁡x:
print⁡A
ln⁡x1−ln⁡x1−ln⁡x`?`2,2
fortran⁡A,mode=single
A(1,1) = alog(x) A(1,2) = 1-alog(x) A(2,1) = 1-alog(x) A(2,2) = 0.E0
fortran⁡A,optimized,mode=double
t1 = dlog(x) t2 = 1-t1 A(1,1) = t1 A(1,2) = t2 A(2,1) = t2 A(2,2) = (0.D0/0.D0)
f≔convert⁡1−2⁢x+3⁢x2−2⁢x3+x4,horner
f≔1+−2+3+−2+x⁢x⁢x⁢x
f≔unapply⁡f,x
f≔x↦1+−2+3+−2+x⋅x⋅x⋅x
c The options were : operatorarrow doubleprecision function f(x) doubleprecision x f = 1+(-2+(3+(-2+x)*x)*x)*x return end
See Also
codegen,fortran,procedure(deprecated)
CodeGeneration
CodeGeneration[Fortran]
Writing Efficient Code
Download Help Document