Wrapperless External Calls to C and Fortran Routines
by Waterloo Maple
Fortran External Calls
Introduction
The Fortran external API (given in libmaplefortran.so) provides routines that translate Maple data type to Fortran data type and vice versa. This allows you to invoke C and Fortran routines from the Maple environment, including from within your Maple procedures. Here are the steps to create C and Fortran external calls from Maple (Fortran 77 is assumed). Note that for Maple 7 and higher, no wrapper is needed. Maple will do all the data type translations automatically and hence eliminates the need for a wrapper program. This method is cleaner and requires less overhead than the wrapper method because array input does not have to be copied to a "container".
As an example, the following Maple worksheet will invoke a Fortran external call fftwrap that computes the FFT of a rectangular real signal. FFT is the fundamental operation of signal processing applications. FFT transforms a signal from the time domain to its spectrum in the frequency domain. The Fortran routines to compute the FFT are taken from SLATEC Common Mathematical Library, Version 4.1. Their source code can be downloaded from the website http://netlib2.cs.utk.edu/slatec
Below is the Fortran routine realfft (the source file is realfft.f). It is the front end to the FFT routines of the SLATEC package.
C***FFT routine - it is the front end to the routines of the SLATEC library C X - real input signal C RY - real part of FFT(X) C IY - imaginary part of FFT(X) C N - length of input signal SUBROUTINE realfft( X, RY, IY, N ) REAL X(*) DOUBLEPRECISION RY(*) DOUBLEPRECISION IY(*) INTEGER N PARAMETER( MAXFFT = 4096 ) REAL W( 2*MAXFFT + 15 ) C Call the SLATEC routines to compute the FFT of the real signal X CALL RFFTI( N, W ) CALL RFFTF( N, X, W ) C Since the input is a real signal, the FFT routine only computes the first C half of the spectrum. The other half can be obtained by taking the C conjugate of the first half. C Real part of the FFT RY(1) = X(1) DO 10, I = 1, N/2-1 RY(I+1) = X(2*I) RY(N-I+1) = X(2*I) 10 CONTINUE RY(N/2+1) = X(N) C Imaginary part of the FFT IY(1) = 0 DO 20, I = 1, N/2-1 IY(I+1) = X(2*I+1) IY(N-I+1) = -X(2*I+1) 20 CONTINUE IY(N/2+1) = 0
END
C External Calls
Below is the code of a C routine matmult to compute matrix multiplication of two real matrices (the source file is matmult.c)
void matmult( double *A, double *B, double *C, int I, int J, int K ) { int i, j, k; double t; for( i = 0; i < I; ++i ) { for( k = 0; k < K; ++k ) { t = 0.0; for( j = 0; j < J; ++j ) { t += A[i*J+j] * B[j*K+k]; } C[i*K+k] = t; } } }
The following Maple code will directly invoke the C routine matmult to compute matrix multiplication.
The empty matrix that will be filled with the values of AB