Application Center - Maplesoft

# Fortran and C generation

You can switch back to the summary page by clicking here.

fortc6-.mws

Fortran & C Code Generation

Introduction

This worksheet demonstrates Maple's capability to generate Fortran & C code for use in other applications.

> restart;
with(codegen): # The code generation package

This worksheet covers:

Programming

Vectors

> f := 1-exp(-t)*x*y;

> g := x-y*exp(-t)*x^2;

> v := vector([f,g]);

> fortran(v,optimized);

`      t1 = exp(-t)`

`      t6 = x**2`

`      v(1) = 1-t1*x*y`

`      v(2) = x-y*t1*t6`

> C(v,optimized);

`      t1 = exp(-t);`

`      t6 = x*x;`

`      v[0] = 1.0-t1*x*y;`

`      v[1] = x-y*t1*t6;`

Matrices

> A := matrix(4,4,[a11, a12, 0, 0, 0, a22, a23,
0, 0, 0, a33, a34, 0, 0, 0, a44]);

> A_inv := linalg[inverse](A);

> fortran(A_inv,optimized);

`      t1 = 1/a11`

`      t3 = 1/a22`

`      t5 = a12*a23`

`      t6 = t1*t3`

`      t7 = 1/a33`

`      t11 = 1/a44`

`      A_inv(1,1) = t1`

`      A_inv(1,2) = -a12*t1*t3`

`      A_inv(1,3) = t5*t6*t7`

`      A_inv(1,4) = -t5*a34*t6*t7*t11`

`      A_inv(2,1) = 0`

`      A_inv(2,2) = t3`

`      A_inv(2,3) = -a23*t3*t7`

`      A_inv(2,4) = a23*a34*t3*t7*t11`

`      A_inv(3,1) = 0`

`      A_inv(3,2) = 0`

`      A_inv(3,3) = t7`

`      A_inv(3,4) = -a34*t7*t11`

`      A_inv(4,1) = 0`

`      A_inv(4,2) = 0`

`      A_inv(4,3) = 0`

`      A_inv(4,4) = t11`

> C(A_inv,optimized);

`      t1 = 1/a11;`

`      t3 = 1/a22;`

`      t5 = a12*a23;`

`      t6 = t1*t3;`

`      t7 = 1/a33;`

`      t11 = 1/a44;`

`      A_inv[0][0] = t1;`

`      A_inv[0][1] = -a12*t1*t3;`

`      A_inv[0][2] = t5*t6*t7;`

`      A_inv[0][3] = -t5*a34*t6*t7*t11;`

`      A_inv[1][0] = 0.0;`

`      A_inv[1][1] = t3;`

`      A_inv[1][2] = -a23*t3*t7;`

`      A_inv[1][3] = a23*a34*t3*t7*t11;`

`      A_inv[2][0] = 0.0;`

`      A_inv[2][1] = 0.0;`

`      A_inv[2][2] = t7;`

`      A_inv[2][3] = -a34*t7*t11;`

`      A_inv[3][0] = 0.0;`

`      A_inv[3][1] = 0.0;`

`      A_inv[3][2] = 0.0;`

`      A_inv[3][3] = t11;`

Procedures

You can also produce complete C or FORTRAN subroutines from Maple procedures.

> f := proc(x)
if(x > 0) then
x^2
else
x-1
fi
end;

> fortran(f);

`      real function f(x)`

`      real x`

`        if (0 .lt. x) then`

`          f = x**2`

`          return`

`        else `

`          f = x-1`

`          return`

`        endif`

`      end`

> C(f);

`double f(x)`

`double x;`

`{`

`  {`

`    if( 0.0 < x )`

`      return(x*x);`

`    else `

`      return(x-1.0);`

`  }`

`}`

Applications

Automation

Compute the gradient in C, D, and theta as part of a least squares optimization problem. The gradient is computed using automatic differentiation.

> dist := proc(C,D,theta,x,y,h,k,r)
local s,c,xbar,ybar,d1,d2,d;
x:=3*t^2+2*t-4;
y:=5*t^4+t-6;
s := sin(theta);
c := cos(theta);
xbar := (x+C)*c + (y+D)*s;
ybar := (y+D)*c - (x+C)*s;
d1 := (h-xbar)^2;
d2 := (k-ybar)^2;
d := sqrt( d1+d2 ) - r;
d^2;
end:

> fortran(dist);

`      real function dist(C,D,theta,x,y,h,k,r)`

`      real C`

`      real D`

`      real theta`

`      real x`

`      real y`

`      real h`

`      real k`

`      real r`

`      real c`

`      real d`

`      real d1`

`      real d2`

`      real s`

`      real xbar`

`      real ybar`

`        x = 3*t**2+2*t-4`

`        y = 5*t**4+t-6`

`        s = sin(theta)`

`        c = cos(theta)`

`        xbar = (x+C)*c+(y+D)*s`

`        ybar = (y+D)*c-(x+C)*s`

`        d1 = (h-xbar)**2`

`        d2 = (k-ybar)**2`

`        d = sqrt(d1+d2)-r`

`        dist = d**2`

`        return`

`      end`

> C(dist);

`#include <math.h>`

`double dist(C,D,theta,x,y,h,k,r)`

`double C;`

`double D;`

`double theta;`

`double *x;`

`double *y;`

`double h;`

`double k;`

`double r;`

`{`

`  double c;`

`  double d;`

`  double d1;`

`  double d2;`

`  double s;`

`  double xbar;`

`  double ybar;`

`  {`

`    *x = 3.0*t*t+2.0*t-4.0;`

`    *y = 5.0*t*t*t*t+t-6.0;`

`    s = sin(theta);`

`    c = cos(theta);`

`    xbar = (x+C)*c+(y+D)*s;`

`    ybar = (y+D)*c-(x+C)*s;`

`    d1 = pow(h-xbar,2.0);`

`    d2 = pow(k-ybar,2.0);`

`    d = sqrt(d1+d2)-r;`

`    return(d*d);`

`  }`

`}`