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

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);

`Maple 2023.2, X86 64 WINDOWS, Nov 24 2023, Build ID 1762575`

(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)

30

(3)

fmultf(10.1,3.3)

33.3299999999999983

(4)

fmultf(100.234,67.901)

6805.98883399999886

(5)

 

Matrix multiplication example

 

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

Matrix(3, 3, {(1, 1) = 1.0, (1, 2) = 4.0, (1, 3) = 7.0, (2, 1) = 2.0, (2, 2) = 5.0, (2, 3) = 8.0, (3, 1) = 3.0, (3, 2) = 6.0, (3, 3) = 9.0})

(6)

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

Matrix(3, 3, {(1, 1) = 1.0, (1, 2) = 4.0, (1, 3) = 7.0, (2, 1) = 2.0, (2, 2) = 5.0, (2, 3) = 8.0, (3, 1) = 3.0, (3, 2) = 6.0, (3, 3) = 9.0})

(7)

c:=Matrix(1 .. 3, 1 .. 3, fill = 0.,datatype=float[8],order=Fortran_order);

Matrix(3, 3, {(1, 1) = 0., (1, 2) = 0., (1, 3) = 0., (2, 1) = 0., (2, 2) = 0., (2, 3) = 0., (3, 1) = 0., (3, 2) = 0., (3, 3) = 0.})

(8)

a.b

Matrix(3, 3, {(1, 1) = 30.0, (1, 2) = 66.0, (1, 3) = 102.0, (2, 1) = 36.0, (2, 2) = 81.0, (2, 3) = 126.0, (3, 1) = 42.0, (3, 2) = 96.0, (3, 3) = 150.0})

(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)

Error, (in fmat_mult) unhandled return type

 

 

mult-Fortran_dll.mw

 

Please Wait...