Maple Generates Fortran and C Code

One useful feature in Maple is its ability to generate Fortran and C code. This allows a user to use Maple to perform a series of mathematical operations in Maple, then take the final output into Fortran and C code, which can be inserted into a program. Below we show this operation.

> with(LinearAlgebra): with(codegen);

Warning, the name eqn has been redefined

[C, GRAD, GRADIENT, HESSIAN, JACOBIAN, cost, declar...
[C, GRAD, GRADIENT, HESSIAN, JACOBIAN, cost, declar...
[C, GRAD, GRADIENT, HESSIAN, JACOBIAN, cost, declar...

Consider the symbolic matrix A .

> A := Matrix( 3, 3, (i,j) -> a[i,j] );

A := _rtable[1551148]

Compute the symbolic inverse of the matrix. Store the result as A_inv :

> A_inv := MatrixInverse(A);

A_inv := _rtable[1180936]
A_inv := _rtable[1180936]
A_inv := _rtable[1180936]
A_inv := _rtable[1180936]
A_inv := _rtable[1180936]
A_inv := _rtable[1180936]
A_inv := _rtable[1180936]
A_inv := _rtable[1180936]
A_inv := _rtable[1180936]
A_inv := _rtable[1180936]

The fortran command generates the necessary FORTRAN code for computing A_inv . This code should be optimized using that option.

> fortran(A_inv);

      unknown(1,1) = (a(2,2)*a(3,3)-a(2,3)*a(3,2))/(a(3,1)*a(1,2)*a(2,3)

     #-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+a(

     #1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

      unknown(1,2) = -(a(1,2)*a(3,3)-a(1,3)*a(3,2))/(a(3,1)*a(1,2)*a(2,3

     #)-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+a

     #(1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

      unknown(1,3) = (a(1,2)*a(2,3)-a(1,3)*a(2,2))/(a(3,1)*a(1,2)*a(2,3)

     #-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+a(

     #1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

      unknown(2,1) = -(-a(3,1)*a(2,3)+a(2,1)*a(3,3))/(a(3,1)*a(1,2)*a(2,

     #3)-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+

     #a(1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

      unknown(2,2) = (-a(3,1)*a(1,3)+a(1,1)*a(3,3))/(a(3,1)*a(1,2)*a(2,3

     #)-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+a

     #(1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

      unknown(2,3) = -(-a(2,1)*a(1,3)+a(1,1)*a(2,3))/(a(3,1)*a(1,2)*a(2,

     #3)-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+

     #a(1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

      unknown(3,1) = (-a(3,1)*a(2,2)+a(2,1)*a(3,2))/(a(3,1)*a(1,2)*a(2,3

     #)-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+a

     #(1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

      unknown(3,2) = -(-a(3,1)*a(1,2)+a(1,1)*a(3,2))/(a(3,1)*a(1,2)*a(2,

     #3)-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+

     #a(1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

      unknown(3,3) = (-a(2,1)*a(1,2)+a(1,1)*a(2,2))/(a(3,1)*a(1,2)*a(2,3

     #)-a(3,1)*a(1,3)*a(2,2)-a(2,1)*a(1,2)*a(3,3)+a(2,1)*a(1,3)*a(3,2)+a

     #(1,1)*a(2,2)*a(3,3)-a(1,1)*a(2,3)*a(3,2))

> fortran(A_inv, optimized);

      t4 = a(3,1)*a(1,2)

      t6 = a(3,1)*a(1,3)

      t8 = a(2,1)*a(1,2)

      t10 = a(2,1)*a(1,3)

      t12 = a(1,1)*a(2,2)

      t14 = a(1,1)*a(2,3)

      t17 = 1/(t4*a(2,3)-t6*a(2,2)-t8*a(3,3)+t10*a(3,2)+t12*a(3,3)-t14*a

     #(3,2))

      unknown(1,1) = (a(2,2)*a(3,3)-a(2,3)*a(3,2))*t17

      unknown(1,2) = -(a(1,2)*a(3,3)-a(1,3)*a(3,2))*t17

      unknown(1,3) = (a(1,2)*a(2,3)-a(1,3)*a(2,2))*t17

      unknown(2,1) = -(-a(3,1)*a(2,3)+a(2,1)*a(3,3))*t17

      unknown(2,2) = (-t6+a(1,1)*a(3,3))*t17

      unknown(2,3) = -(-t10+t14)*t17

      unknown(3,1) = (-a(3,1)*a(2,2)+a(2,1)*a(3,2))*t17

      unknown(3,2) = -(-t4+a(1,1)*a(3,2))*t17

      unknown(3,3) = (-t8+t12)*t17

You can similarly generate C code.

> C( A_inv, optimized );

      t4 = a[2][0]*a[0][1];

      t6 = a[2][0]*a[0][2];

      t8 = a[1][0]*a[0][1];

      t10 = a[1][0]*a[0][2];

      t12 = a[0][0]*a[1][1];

      t14 = a[0][0]*a[1][2];

      t17 = 1/(t4*a[1][2]-t6*a[1][1]-t8*a[2][2]+t10*a[2][1]+t12*a[2][2]-t14*a

[2][1]);

      unknown[0][0] = (-a[2][1]*a[1][2]+a[1][1]*a[2][2])*t17;

      unknown[0][1] = -(a[0][1]*a[2][2]-a[0][2]*a[2][1])*t17;

      unknown[0][2] = (a[0][1]*a[1][2]-a[0][2]*a[1][1])*t17;

      unknown[1][0] = -(-a[2][0]*a[1][2]+a[1][0]*a[2][2])*t17;

      unknown[1][1] = (-t6+a[0][0]*a[2][2])*t17;

      unknown[1][2] = -(-t10+t14)*t17;

      unknown[2][0] = (-a[2][0]*a[1][1]+a[1][0]*a[2][1])*t17;

      unknown[2][1] = -(-t4+a[0][0]*a[2][1])*t17;

      unknown[2][2] = (-t8+t12)*t17;