HP MLIB User's Guide Vol. 2 7th Ed.
1086 HP MLIB User’s Guide
General interlanguage programming rules
• By default, the first element of a Fortran array is X(1), but the first element
of a C array is X[0]. As a consequence, you must adjust any input or output
arguments or function values that are indices into arrays by decrementing
the quantity by 1. For example, because idamax returns the one-based
index of the element of an array that has maximum magnitude, you might
code
i = idamax(&n, x, &incx) - 1;
• MLIB expects arrays to be stored in column-major order; however, C stores
them in row-major order. Hence, an MLIB subprogram sees the transpose of
the C program’s matrix. Three ways to handle this incompatibility are:
❏ In your C program, store the transpose of the matrix you are using so it
would appear to be in column-major order to the MLIB subprogram.
❏ Some MLIB subprograms have a “transpose” option that uses the
transpose of the matrix to do its calculations. In this case, store the
matrix in normal order (for C) and use the transpose option to solve the
intended problem. For example, to solve a system of linear equations
Ax = b, factor the matrix you call A but which MLIB sees as A
T
with
DGEFA, and use job = 1 in DGESL to solve for x.
❏ Recast the computation so that it operates on C’s row-major order. For
example, the matrix-matrix multiplication C = AB can be recast as
C
T
= B
T
A
T
. Thus, for example, VECLIB subroutine DGEMM can be
used with transa = transb = ’NonTransposed’ by reversing the order
of the matrices A and B and their transposition options, sizes, and
leading dimensions in the CALL statement.
• Another consequence in the difference in storage order of arrays is that
“leading dimension” arguments, such as lda in subroutine DGEMV, must be
defined based on the last dimension of the C array declaration. For example,
the C declaration
double a[2][4];
is equivalent to the Fortran declaration
REAL*8 a(4,2)
and corresponds to lda = 4.
• MLIB subprograms that expect character arguments can be called from C
with a string constant, a char array, or a pointer to a char variable for each
character argument. In addition, an int must be passed at the end of the
argument list for each character argument. These extra arguments are in
the same order as the character arguments and indicate the length of the
character string, not counting any “\0” terminator. For subroutine XERBLA,
the quantity passed for argument name must be at least six characters long,
and only six characters are significant.