codegen/fortran/procedure
generating Fortran code for Maple procedures
Calling Sequence
Parameters
Description
Examples
fortran(s);
fortran(s, options);
s
-
named Maple procedure
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 can generate a complete Fortran subroutine or function from a Maple procedure. The input must be a named Maple procedure. For help on options see ?codegen[fortran]
A few Maple statements or expressions that appear inside the Maple procedure cannot be translated. The most important are
quotes read, save for var in ...
args[i] package[function](...) do ... end do;
nargs f(...) := ...
procname '...'
sets,tables, series
A limited type deduction algorithm has been implemented. All variables (including lists and arrays) will be given a type. If no type can be deduced from the context then a real or doubleprecision (according to the specified options) type is declared.
An optional declare(...) statement may be used in a Maple procedure to declare types for the parameters, local and global variables. This statement is used to type variables for where no type can be deduced. Each type is an equation of the form <variable> = <type> This statement is not translated into Fortran and it has no effect on the execution of the Maple procedure within Maple. Standard Maple typed parameters may also be used.
Procedures returning arrays are handled. The returned array is added to the parameters and removed from the local variables. Procedures returning sequences are handled. A new parameter called crea_par is added and the corresponding assignments are generated.
All array indices are shifted to make all array references begin at 1 for Fortran.
Maple print, lprint, printf commands as well as the return and error statements are handled. The only error statements that can be translated are those with a single string argument.
The command with(codegen,fortran) allows the use of the abbreviated form of this command.
Computes sin(x)/x accurately for all x; double precision is used as the default
with⁡codegen,fortran:
f := proc(x) if abs(x) < 1.0e-8 then 1-x^2/6; else sin(x)/x; end if; end proc:
fortran⁡f
doubleprecision function f(x) doubleprecision x if (abs(x) .lt. 0.1D-7) then f = 1-x**2/6 return else f = sin(x)/x return endif end
Compute 1+x+x^2/2+...+x^n/n!
f := proc(x::float,n::integer) local i::integer,s::float,t::float; s := 1; t := 1; for i to n do t := t*x/i; s := s+t end do; s end proc:
doubleprecision function f(x,n) doubleprecision x integer n integer i doubleprecision s doubleprecision t s = 1 t = 1 do 1000 i = 1,n,1 t = t*x/i s = s+t 1000 continue f = s return end
f := proc(x::numeric) local i, M; global test; M := array(-2..3, sparse, [(1)=sin(x), (2)=x^3]); for i from -2 to 3 do if test then print(M[i]) else error "Error message"; end if; end do; M; end proc:
subroutine f(x,crea_par) doubleprecision x doubleprecision crea_par(6) doubleprecision M(6) integer i common/global/test logical test M(1) = 0 M(2) = 0 M(3) = 0 M(4) = sin(x) M(5) = x**3 M(6) = 0 do 1000 i = -2,3,1 if (test) then write(6,*) M(i+3) else write(6,*) 'Error message' call exit(0) endif 1000 continue crea_par(1) = M(1) crea_par(2) = M(2) crea_par(3) = M(3) crea_par(4) = M(4) crea_par(5) = M(5) crea_par(6) = M(6) return return end
f := proc(x) sin(x)^2*cos(x) end proc:
g≔D⁡f
g ≔ procx2*sin⁡x*cos⁡x^2 − sin⁡x^3end proc
fortran⁡f,optimized,mode=double
doubleprecision function f(x) doubleprecision x doubleprecision t1 doubleprecision t2 doubleprecision t3 t1 = dsin(x) t2 = t1**2 t3 = dcos(x) f = t2*t3 return end
A Maple procedure which returns an array
g := proc(x,y,z) local dt,grd,t; grd := array(1 .. 3); dt := array(1 .. 3); dt[3] := 2*z; t := z^2; grd[1] := cos(x)*z-sin(x)*t; grd[2] := 0; grd[3] := sin(x)+cos(x)*dt[3]-1/t^2*dt[3]; grd end proc:
fortran⁡g,precision=single
subroutine g(x,y,z,crea_par) real x real y real z real crea_par(3) real dt(3) real grd(3) real t dt(3) = 2*z t = z**2 grd(1) = cos(x)*z-sin(x)*t grd(2) = 0 grd(3) = sin(x)+cos(x)*dt(3)-1/t**2*dt(3) crea_par(1) = grd(1) crea_par(2) = grd(2) crea_par(3) = grd(3) return return end
See Also
codegen
codegen[fortran](deprecated)
CodeGeneration
CodeGeneration[Fortran]
Writing Efficient Maple Code
Download Help Document