 codegen,fortran - Maple Help

codegen

 fortran
 generate Fortran code Calling Sequence fortran(s); fortran(s, options); Parameters

 s - expression, array of expressions, or list of equations Description

 • 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,

$\mathrm{codegen/fortran/function_name}\left(\mathrm{arctan},1,\mathrm{double}\right)≔\mathrm{datan}$

$\mathrm{codegen/fortran/function_name}\left(\mathrm{arctan},2,\mathrm{single}\right)≔\mathrm{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. Examples

Important: The codegen[fortran] command has been deprecated.  Use the superseding command CodeGeneration[Fortran] instead.

 > $\mathrm{with}\left(\mathrm{codegen},\mathrm{fortran}\right):$
 > $f≔1-2x+3{x}^{2}-2{x}^{3}+{x}^{4}$
 ${f}{≔}{{x}}^{{4}}{-}{2}{}{{x}}^{{3}}{+}{3}{}{{x}}^{{2}}{-}{2}{}{x}{+}{1}$ (1)
 > $\mathrm{fortran}\left(f\right)$
 t0 = x**4-2*x**3+3*x**2-2*x+1
 > $\mathrm{fortran}\left(f,\mathrm{optimized}\right)$
 t1 = x**2       t2 = t1**2       t7 = -2*t1*x+3*t1+t2-2*x+1
 > $\mathrm{fortran}\left(\mathrm{convert}\left(f,\mathrm{horner},x\right)\right)$
 t0 = 1+(-2+(3+(-2+x)*x)*x)*x
 > $f≔\mathrm{\pi }\mathrm{ln}\left({x}^{2}\right)-\mathrm{sqrt}\left(2\right){\mathrm{ln}\left({x}^{2}\right)}^{2}$
 ${f}{≔}{\mathrm{\pi }}{}{\mathrm{ln}}{}\left({{x}}^{{2}}\right){-}\sqrt{{2}}{}{{\mathrm{ln}}{}\left({{x}}^{{2}}\right)}^{{2}}$ (2)
 > $\mathrm{fortran}\left(f\right)$
 t0 = 0.3141592653589793D1*log(x**2)-sqrt(2.D0)*log(x**2)**2
 > $\mathrm{fortran}\left(f,\mathrm{optimized}\right)$
 t1 = x**2       t2 = log(t1)       t4 = sqrt(2.D0)       t5 = t2**2       t7 = 0.3141592653589793D1*t2-t4*t5
 > $\mathrm{fortran}\left(\left[s={x}^{2},t=\mathrm{ln}\left(s\right),r=\mathrm{\pi }t-\mathrm{sqrt}\left(2\right){s}^{2}\right],\mathrm{precision}=\mathrm{single}\right)$
 s = x**2       t = log(s)       r = 0.3141593E1*t-sqrt(2.E0)*s**2
 > $A≔\mathrm{array}\left(1..2,1..2,\mathrm{symmetric}\right):$
 > $A\left[1,1\right]≔\mathrm{log}\left(x\right):$$A\left[1,2\right]≔1-\mathrm{log}\left(x\right):$
 > $\mathrm{print}\left(A\right)$
 $\left[\begin{array}{cc}{\mathrm{ln}}{}\left({x}\right)& {1}{-}{\mathrm{ln}}{}\left({x}\right)\\ {1}{-}{\mathrm{ln}}{}\left({x}\right)& {{\mathrm{?}}}_{{2}{,}{2}}\end{array}\right]$ (3)
 > $\mathrm{fortran}\left(A,\mathrm{mode}=\mathrm{single}\right)$
 A(1,1) = alog(x)       A(1,2) = 1-alog(x)       A(2,1) = 1-alog(x)       A(2,2) = 0.E0
 > $\mathrm{fortran}\left(A,\mathrm{optimized},\mathrm{mode}=\mathrm{double}\right)$
 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≔\mathrm{convert}\left(1-2x+3{x}^{2}-2{x}^{3}+{x}^{4},\mathrm{horner}\right)$
 ${f}{≔}{1}{+}\left({-}{2}{+}\left({3}{+}\left({-}{2}{+}{x}\right){}{x}\right){}{x}\right){}{x}$ (4)
 > $f≔\mathrm{unapply}\left(f,x\right)$
 ${f}{≔}{x}{↦}{1}{+}\left({-}{2}{+}\left({3}{+}\left({-}{2}{+}{x}\right){\cdot }{x}\right){\cdot }{x}\right){\cdot }{x}$ (5)
 > $\mathrm{fortran}\left(f\right)$
 c The options were    : operatorarrow       doubleprecision function f(x)       doubleprecision x         f = 1+(-2+(3+(-2+x)*x)*x)*x         return       end