Learning to use the package requires a little effort, especially for C++ programmers, because the code was originally written in Fortran. It requires some care in interpreting and passing arguments. But it is worth the effort.
There are hundreds of routines to choose from in the LAPACK packages. See instructions above for finding the one you want.
Netlib.org, which hosts LAPACK provides another handy search tool . To use it, in the left panel, click on "LAPACK->Modules->LAPACK". You will get a graphic. For a plain, garden-variety matrix, click on the "General Matrices" box. Then follow the other boxes until you get a list of routines. You'll need to scan down until you find the one appropriate for your problem.
The Lighthouse project is developing a search tool for a wide variety of scientific software tools. You may access it as a "guest", but they like you to register.
Alternatively, you may use the LAPACK manual
on the netlib site. Find the table of contents and go to the
"Index of Driver and Computational Routines". Note that to get the
names of the equivalent double precision routines, replace the first S
with a D. To get complex single precision, use C, and complex double
precision, use Z.
g++ -O -c myprog.ccTo link your code with the LAPACK library, do this:
g++ myprog.o -o myprog -L/usr/local/lib64 -llapack -lblas -lgfortran -lmBe sure to put all of the g++ options on one line: Of course compilation and linking can be done in a single step with
g++ -O myprog.cc -o myprog -L/usr/local/lib64 -llapack -lblas -lgfortran -lmagain, be sure to put all of the g++ options on one line.
man dgesvgives the Fortran definition and explains the meaning of the variables. We don't reproduce the explanation here.
SUBROUTINE DGESV( N, NRHS, A, LDA, IPIV, B, LDB, INFO ) INTEGER INFO, LDA, LDB, N, NRHS INTEGER IPIV( * ) DOUBLE PRECISION A( LDA, * ), B( LDB, * )Following instructions in the Fortran binding handout, we write the C++ prototpye declaration as follows:
extern "C" { void dgesv_(int *n, int *nrhs, double *a, int *lda, int *ipivot, double *b, int *ldb, int *info) ; }The man page explains that n is the number of linear equations, nrhs is the number of columns of the rhs matrix B (so you want nrhs = 1), a is a double precision array of dimension (lda,n), and lda is the "leading" dimension of the array a. In C(C++) the subscript convention is reversed, so lda will be the second dimension, as shown below. Continuing with the arguments, ipivot is an output array indicating the rows of the matrix that were interchanged upon pivoting. If this information is of no interest to you, the array must still be supplied, but the result can be ignored. Next b is a double precision array of dimension (ldb,nrhs) specifying the rhs upon entry and containing the solution upon return, ldb is its "leading" dimension, and info tells whether the solution was successful.
Armed with this information we write our code as follows:
#include <iostream>Note that we are not required to use the same names as given in the prototype.extern "C" { void dgesv_(int *n, int *nrhs, double *a, int *lda, int *ipivot, double *b, int *ldb, int *info) ; }#define MAX 10 int main(){ // Values needed for dgesv int n; int nrhs = 1; double a[MAX][MAX]; double b[1][MAX]; int lda = MAX; int ldb = MAX; int ipiv[MAX]; int info; // Other values int i,j; // Read the values of the matrix std::cout << "Enter n \n"; std::cin >> n; std::cout << "On each line type a row of the matrix A followed by one element of b:\n"; for(i = 0; i < n; i++){ std::cout << "row " << i << " "; for(j = 0; j < n; j++)std::cin >> a[j][i]; std::cin >> b[0][i]; } // Solve the linear system dgesv_(&n, &nrhs, &a[0][0], &lda, ipiv, &b[0][0], &ldb, &info); // Check for success if(info == 0) { // Write the answer std::cout << "The answer is\n"; for(i = 0; i < n; i++) std::cout << "b[" << i << "]\t" << b[0][i] << "\n"; } else { // Write an error message std::cerr << "dgesv returned error " << info << "\n"; } return info; }
AB(ka+1+i-j,j) = A(i,j) for max(1,j-ka)<=i<=jThese instructions are appropriate for Fortran arrays and must be translated for use in C++ and C. The rules are simple:
ab[j-1][ka+i-j] = a[j-1][i-1] for max(1,j-ka)<=i<=jWe have changed to the C++ and C bracket subscript notation and use lower case names to emphasize that we are now speaking of C++ and C arrays. Notice that we didn't subtract 1 from the inequality expression. Subtracting 1 applies only to what is inside the subscript brackets []. The subscripts i and j are dummy variables, so we are allowed to redefine them by adding 1 to each everywhere they appear. Making that replacement then gives
ab[j][ka+i-j] = a[j][i] for max(1,j-ka+1)<=i+1<=j+1(Notice that we make this replacement everywhere, including the inequality, because we are redefining the dummy variables). Of course, since a is symmetric, we could also write a[i][j]. With a little algebra the inequalities can also be rewritten more neatly, giving the final result:
ab[j][ka+i-j] = a[i][j] for max(0,j-ka)<=i<=j.The array ab must be declared with
float ab[MAX][ldab]where MAX >= N and ldab >= ka. It is passed to the subprogram as the pointer &ab[0][0].
This page is maintained by: Carleton DeTar Mail Form Last modified 29 March 2017