Skip to main content

lapack

Versions and Availability

About the Software

LAPACK is written in Fortran90 and provides routines for solving systems of simultaneous linear equations, least-squares solutions of linear systems of equations, eigenvalue problems, and singular value problems. - Homepage: http://www.netlib.org/lapack/

Usage

LAPACK is a binary library, so it is linked to your program by the compiler during the build process by adding the -llapack flag to the link line:

Fortran

$ ifort sample.f -llapack

Open Fortran Example?

Fortran Source

!**********************************************
! THIS EXAMPLE USES THE LAPACK ROUTINE DGESV 
! TO SOLVE A SYSTEM OF LINEAR EQUATIONS AX=B 
! A = [1, 2, 3; 4, 5, 6; 7, 8 10]            
! B = [1, 0; 0, 1; 0, 0]
! *********************************************
 program lapack_test
  integer ipiv(3), info, i, j
  double precision A(3,3), B(3,2)
  A(1,1)=1
  A(1,2)=2
  A(1,3)=3
  A(2,1)=4
  A(2,2)=5
  A(2,3)=6
  A(3,1)=7
  A(3,2)=8
  A(3,3)=10
  
  B(1,1)=1
  B(2,1)=0
  B(3,1)=0
  B(1,2)=0
  B(2,2)=1
  B(3,2)=0

  call dgesv (3,2,A,3,ipiv,B,3,info)
  if(info .EQ. 0) then
     do i=1,3
        write(*,'(2F8.3)') (B(i,j), j=1,2)
     enddo
  endif
 end program lapack_test

Build and Execute

$ ifort lapack_test.f90 -llapack
$ ./a.out
  -0.667  -1.333
  -0.667   3.667
   1.000  -2.000

C

The LAPACK routines must be declared with extern, the routine name must be in lowercase, and it must be followed by an _ (i.e. underscore):

extern void dgetrf_(int*, int*, double*, int*, int*, int*);

Be sure when calling the LAPACK routing that all arguments are passed by reference.

Note: Since C matrices are stored in row major order, and Fortran matrices are stored in column major order, a transpose is necessary to go from C to Fortran order, and the result transposed again from Fortran to C order. It is more efficient to change the array indexing to take this into account.

$ icc sample.c -llapack

Open C Example?

C Source

$ cat lapack_test.c
/**********************************************
 * THIS EXAMPLE USES THE LAPACK ROUTINE DGESV 
 * TO SOLVE A SYSTEM OF LINEAR EQUATIONS AX=B 
 * A = [1, 2, 3; 4, 5, 6; 7, 8 10]            
 * B = [1, 0; 0, 1; 0, 0]
 **********************************************/
#include 

extern void dgesv_(int*, int*, double*, int*, int*, double*, int*, int*);

int main()
{
  int n, nrhs, lda, ldb, IPIV[3], info, i;
  double A[3][3], B[2][3];  // Matrices must be transposed
  A[0][0] = 1; A[1][0] = 2; A[2][0] = 3;
  A[0][1] = 4; A[1][1] = 5; A[2][1] = 6;
  A[0][2] = 7; A[1][2] = 8; A[2][2] = 10;

  B[0][0] = 1; B[0][1] = 0; B[0][2] = 0;
  B[1][0] = 0; B[1][1] = 1; B[1][2] = 0;

  n = 3;
  nrhs = 2;
  lda = 3;
  ldb = 3;
  
  dgesv_(&n, &nrhs, (double *)A, &lda, IPIV, (double *)B, &ldb, &info);

  if(info == 0)
    {
      for(i = 0; i < 3; i++)
	printf("%8.3f %8.3f\n", B[0][i], B[1][i]);
    }
}

Build and Execute

C++

The LAPACK routines must be declared with extern "C", the routine name must be in lowercase, and it must be followed by an _ (i.e. underscore):

extern "C" void dgetrf_(int*, int*, double*, int*, int*, int*);

Be sure when calling the LAPACK routing that all arguments are passed by reference.

Note: Since C++ matrices are stored in row major order, and Fortran matrices are stored in column major order, a transpose is necessary to go from C to Fortran order, and the result transposed again from Fortran to C order. It is more efficient to change the array indexing to take this into account.

$ icpc sample.c -llapack

Open C++ Example?

C Source

$ cat lapack_test.c
/**********************************************
 * THIS EXAMPLE USES THE LAPACK ROUTINE DGESV 
 * TO SOLVE A SYSTEM OF LINEAR EQUATIONS AX=B 
 * A = [1, 2, 3; 4, 5, 6; 7, 8 10]            
 * B = [1, 0; 0, 1; 0, 0]
 **********************************************/
#include 

extern void dgesv_(int*, int*, double*, int*, int*, double*, int*, int*);

int main()
{
  int n, nrhs, lda, ldb, IPIV[3], info, i;
  double A[3][3], B[2][3];  // Matrices must be transposed
  A[0][0] = 1; A[1][0] = 2; A[2][0] = 3;
  A[0][1] = 4; A[1][1] = 5; A[2][1] = 6;
  A[0][2] = 7; A[1][2] = 8; A[2][2] = 10;

  B[0][0] = 1; B[0][1] = 0; B[0][2] = 0;
  B[1][0] = 0; B[1][1] = 1; B[1][2] = 0;

  n = 3;
  nrhs = 2;
  lda = 3;
  ldb = 3;
  
  dgesv_(&n, &nrhs, (double *)A, &lda, IPIV, (double *)B, &ldb, &info);

  if(info == 0)
    {
      for(i = 0; i < 3; i++)
	printf("%8.3f %8.3f\n", B[0][i], B[1][i]);
    }
}

Build and Execute

Resources

  • LAPACK Users Guide
  • Individual LAPACK routines are documented in their own man page. To get more information on a given routine, use the command:
    $ man routine_name
    

    For example, to get informantion on dgesvd:

    $ man dgesvd
    

Last modified: September 10 2020 11:58:50.