Passing a Numeric Matrix to a Fortran Function

Each element of an IML numeric matrix is stored as a double-precision floating-point number. This corresponds to the type double in C and Java and to the type REAL(8) in Fortran. An IML matrix can be either one or two dimensional. A two-dimensional matrix is stored in row-major order. Row-major order means that a matrix is stored one row after another with the elements along a row stored contiguously in memory. Thus, the matrix

11 12 13
21 22 23

is stored in memory as

11 12 13 21 22 23

IMLPlus passes a numeric matrix to a Fortran function as an array of type REAL(8). Usually, the Fortran function will need to accept additional parameters that specify the dimensions of the matrix. If the matrix is one dimensional, the Fortran function should accept an INTEGER(4) parameter that specifies the number of elements. If the matrix is two dimensional, the Fortran function should accept two INTEGER(4) parameters that specify the number of rows and columns. You can use the IML functions NROW and NCOL to retrieve the dimensions of a matrix.

Passing a numeric matrix as an input parameter

The Fortran function's parameter should be an array of type REAL(8). Pass the matrix to the function by passing the matrix to the method NextArgIsDoubleArray of the DllFunction class. IMLPlus will convert the matrix to a Java array of type double automatically. The Java type double corresponds to the Fortran type REAL(8).

Obtaining a numeric matrix from an output parameter

The Fortran function's parameter should be an array of type REAL(8). Create a Java array of type double and pass the array to the method NextArgIsDoubleArray of the DllFunction class. When the Fortran function returns, assign the Java array to a matrix variable. IMLPlus will convert the Java array to a matrix in the form of a column vector. If the returned matrix is not a column vector, use the IML SHAPE function to shape the matrix as needed.

Note that it is not possible for a Fortran function to allocate memory for a matrix and then pass a pointer to that memory back to the IMLPlus program. The memory management models of Fortran, Java, and IMLPlus are different and memory allocated in one environment cannot be freed in another. An IMLPlus program that needs to obtain data from a Fortran function must allocate a Java array to contain the data and then pass the Java array to the appropriate NextArgIsXXXArray method of the DllFunction class.