转自http://www.cs.rochester.edu/~bh/cs400/using_lapack.html

Since many of you don't know how to use CLAPACK and have to turn to MATLAB, which, BTW, is just a pretty wrapper of LAPACK, I put together this small page to explain things a little. When I say CLAPACK, I mean LAPACK most of the time. The former is a f2c'ed version of the latter.

Naming conventions of CLAPACK routines

CLAPACK routines have names of the form XYYZZZ. The first letter X indicates the data type the routine is expecting. The following table shows the mapping.

S Real, the same as float in C
D Double precision, the same as double in C
C Complex
Z Double complex
The letters YY tell the type of matrices the routine is dealing with. GE and TR, meaning general and triangular, are the ones we will use. ZZZ is the name of the actual computation, e.g., SV routines are simple drivers for Ax=b type systems, LS are linear square drivers for the same type of systems.

Routines to solve Ax=b type linear systems

We'll be using simple drivers or least square drivers for double type general matrices, so the names of the routines are (or start with) either DGESV or DGELS. The C-prototype of DGESV is

void dgesv_(const int *N, const int *nrhs, double *A, const int *lda, int
*ipiv, double *b, const int *ldb, int *info);

Notice that all parameters are passed by reference. That's the good old Fortran way. The meanings of the parameters are:
N number of columns of A
nrhs number of columns of b, usually 1
lda number of rows (Leading Dimension of A) of A
ipiv pivot indices
ldb number of rows of b
For details, refer to the manual. The prototype of DGELS is

void dgels_(const char *trans, const int *M, const int *N, const int *nrhs,
double *A, const int *lda, double *b, const int *ldb, double *work, const
int * lwork, int *info);

You can guess what the parameters are and check the manual for details.

Routines doing SVD

You might need to do SVD (singular value decomposition) at certain moment. The routines doing SVD are XGESVD. The C prototype of dgesvd_ is

dgesvd_(const char* jobu, const char* jobvt, const int* M, const int* N,
double* A, const int* lda, double* S, double* U, const int* ldu,
double* VT, const int* ldvt, double* work,const int* lwork, const
int* info);

Sample code


#include < stdio.h>

void dgesv_(const int *N, const int *nrhs, double *A, const int *lda, int
*ipiv, double *b, const int *ldb, int *info);

void dgels_(const char *trans, const int *M, const int *N, const int *nrhs,
double *A, const int *lda, double *b, const int *ldb, double *work,
const int * lwork, int *info);

int
main(void)
{
/* 3x3 matrix A
* 76 25 11
* 27 89 51
* 18 60 32
*/
double A[9] = {76, 27, 18, 25, 89, 60, 11, 51, 32};
double b[3] = {10, 7, 43};

int N = 3;
int nrhs = 1;
int lda = 3;
int ipiv[3];
int ldb = 3;
int info;

dgesv_(&N, &nrhs, A, &lda, ipiv, b, &ldb, &info);

if(info == 0) /* succeed */
printf("The solution is %lf %lf %lf\n", b[0], b[1], b[2]);
else
fprintf(stderr, "dgesv_ fails %d\n", info);


return info;
}

Compile with cc lapack_tut.c -L/usr/grads/lib -llapack -lblas -lF77 -lI77. Notice that A is in column major order. Another Fortran surprise.