# Question:How can I use a compiled Fortran code in Maple worksheet?

## Question:How can I use a compiled Fortran code in Maple worksheet?

Maple

I have entered a few examples from the Maple Example Worksheets on the the topic of using compiled Fortran code. I got two simple examples to work as shown in my worksheet below. The third example was to perform a matrix multiplication and that is where I get the "Error, (in fmat_mult) unhandled return type". Could you point out my mistake? Thank you in advance.

Using compiled FORTRAN code:

1) write a FORTRAN function in Geany

2) compile it to an object file *.o

2) open terminal in the folder where the file is saved

3) generate a *.dll file by: gfortran -shared -o mult.dll mult.o

4) setup the call to your functions: see lines fmult:= define_external( ) and fmultf:= define_external( ).

 > restart: kernelopts(version);
 (1)

Multiply two integers

 > fmult:=define_external('mult', LIB= "C:/Users/familee/OneDrive/Documents/Maple/DLL-example/mult.dll", FORTRAN, 'a'::(integer[4]), 'b'::(integer[4]), RETURN::(integer[4]))
 (2)

Multiply two floating point numbers

 > fmultf:=define_external('multf', LIB= "C:/Users/familee/OneDrive/Documents/Maple/DLL-example/multf.dll", FORTRAN, 'a'::(float[8]), 'b'::(float[8]), RETURN::(float[8]))
 > fmult(10,3)
 (3)
 > fmultf(10.1,3.3)
 (4)
 > fmultf(100.234,67.901)
 (5)

Matrix multiplication example

 > a:=Matrix([[1.,4.,7.],[2.,5.,8.],[3.,6.,9.]],datatype=float[8],order=Fortran_order);
 (6)
 > b:=Matrix([[1.,4.,7.],[2.,5.,8.],[3.,6.,9.]],datatype=float[8],order=Fortran_order);
 (7)
 > c:=Matrix(1 .. 3, 1 .. 3, fill = 0.,datatype=float[8],order=Fortran_order);
 (8)
 > a.b
 (9)

Is it possible to use a subroutine to pass a matrix?

mat_mult,f subroutine returns c = a.b

FORTRAN code for mat_mult.f

SUBROUTINE mat_mult(a,b,c,m,n)
C
C      Compute matrix multiplication.
C
INTEGER i,j,k
INTEGER m,n

REAL*8 tmp
C      m rows by n cols
REAL*8 a(m,n)
REAL*8 b(m,n)
REAL*8, intent (out)::c(m,n)
C      m = n
DO 11 j=1,n
DO 12 i=1,n
tmp = 0.0
DO 13 k=1,n
tmp = tmp + a(i,k) * b(k,j)
13         CONTINUE
c(i,j) = tmp
12      CONTINUE
11    CONTINUE
RETURN
END

Compiled to get the object file: mat_mult.o

Generate a dll: gfortran -shared -o mat_mult.dll mat_mult.o

 > fmat_mult:=define_external( 'mat_mult', LIB= "C:/Users/familee/OneDrive/Documents/Maple/DLL-example/mat_mult.dll", FORTRAN, 'a'::(ARRAY(datatype=float[8])), 'b'::(ARRAY(datatype=float[8])), 'c'::(ARRAY(datatype=float[8])), 'm'::(integer[4]), 'n'::(integer[4]), RETURN::(ARRAY(datatype=float[8])) )
 (10)

The results should be the Matrix c

 > fmat_mult(a,b,c,3,3)
 >
mult-Fortran_dll.mw

﻿