11/01/2012

clapack example source and method to use


clapack is math library to solve linear algebra in C language.
Originally, it is library to use in fortran language.
but clapack is made to use in C language.

1. You can see the method to use and file download on this site(http://icl.cs.utk.edu/lapack-for-windows/index.html)

Or

2. You can download pre-bulit clapack lib in here. I am using this lib now.
It might not run well, if the environment of your computer was different with me.
My environment is window 7 32bit, vs 2008.
The method to use is easy.
- Copy lib, header files in your project (The compressed file includes 'blas.lib, blasd.lib, lapack.lib, lapackd.lib, libf2c.lib, libf2cd.lib, clapack.h, f2c.h'.)
- Set Additional Dependencies on project property.
Add these libs "blas.lib lapack.lib libf2c.lib"
- include this header file on your source code.
#include "f2c.h"
#include "clapack.h"
- use functions of lapack.


this is example source code.
/*
    DGESV Example.
    ==============
  
    The program computes the solution to the system of linear
    equations with a square matrix A and multiple
    right-hand sides B, where A is the coefficient matrix:
  
      6.80  -6.05  -0.45   8.32  -9.67
     -2.11  -3.30   2.58   2.71  -5.14
      5.66   5.36  -2.70   4.35  -7.26
      5.97  -4.44   0.27  -7.17   6.08
      8.23   1.08   9.04   2.14  -6.87
 
   and B is the right-hand side matrix:
  
      4.02  -1.56   9.81
      6.19   4.00  -4.09
     -8.22  -8.67  -4.57
     -7.57   1.75  -8.61
     -3.03   2.86   8.99
  
    Description.
    ============
  
    The routine solves for X the system of linear equations A*X = B,
    where A is an n-by-n matrix, the columns of matrix B are individual
    right-hand sides, and the columns of X are the corresponding
    solutions.
 
   The LU decomposition with partial pivoting and row interchanges is
    used to factor A as A = P*L*U, where P is a permutation matrix, L
    is unit lower triangular, and U is upper triangular. The factored
    form of A is then used to solve the system of equations A*X = B.
 
   Example Program Results.
    ========================
  
  DGESV Example Program Results
 
 Solution
   -0.80  -0.39   0.96
   -0.70  -0.55   0.22
    0.59   0.84   1.90
    1.32  -0.10   5.36
    0.57   0.11   4.04
 
 Details of LU factorization
    8.23   1.08   9.04   2.14  -6.87
    0.83  -6.94  -7.92   6.55  -3.99
    0.69  -0.67 -14.18   7.24  -5.19
    0.73   0.75   0.02 -13.82  14.19
   -0.26   0.44  -0.59  -0.34  -3.43
 
 Pivot indices
       5      5      3      4      5
 */

#include 
#include 

#include "f2c.h"
#include "clapack.h"


 extern void print_matrix( char* desc, int m, int n, double* a, int lda );
 extern void print_int_vector( char* desc, int n, int* a );
 
/* Parameters */
 #define N 5
 #define NRHS 3
 #define LDA N
 #define LDB N
 
/* Main program */
 int main() {
         /* Locals */
         integer n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
         /* Local arrays */
         integer ipiv[N];
         double a[LDA*N] = {
             6.80, -2.11,  5.66,  5.97,  8.23,
            -6.05, -3.30,  5.36, -4.44,  1.08,
            -0.45,  2.58, -2.70,  0.27,  9.04,
             8.32,  2.71,  4.35, -7.17,  2.14,
            -9.67, -5.14, -7.26,  6.08, -6.87
         };
         double b[LDB*NRHS] = {
             4.02,  6.19, -8.22, -7.57, -3.03,
            -1.56,  4.00, -8.67,  1.75,  2.86,
             9.81, -4.09, -4.57, -8.61,  8.99
         };
         /* Executable statements */
         printf( " DGESV Example Program Results\n" );
         /* Solve the equations A*X = B */
         dgesv_( &n, &nrhs, a, &lda, ipiv, b, &ldb, &info );
         /* Check for the exact singularity */
         if( info > 0 ) {
                 printf( "The diagonal element of the triangular factor of A,\n" );
                 printf( "U(%i,%i) is zero, so that A is singular;\n", info, info );
                 printf( "the solution could not be computed.\n" );
                 exit( 1 );
         }
         /* Print solution */
         print_matrix( "Solution", n, nrhs, b, ldb );
         /* Print details of LU factorization */
         print_matrix( "Details of LU factorization", n, n, a, lda );
         /* Print pivot indices */
         print_int_vector( "Pivot indices", n, (int*)ipiv );
         exit( 0 );
 } /* End of DGESV Example */
 
/* Auxiliary routine: printing a matrix */
 void print_matrix( char* desc, int m, int n, double* a, int lda ) {
         int i, j;
         printf( "\n %s\n", desc );
         for( i = 0; i < m; i++ ) {
                 for( j = 0; j < n; j++ ) printf( " %6.2f", a[i+j*lda] );
                 printf( "\n" );
         }
 }
 
/* Auxiliary routine: printing a vector of integers */
 void print_int_vector( char* desc, int n, int* a ) {
         int j;
         printf( "\n %s\n", desc );
         for( j = 0; j < n; j++ ) printf( " %6i", a[j] );
         printf( "\n" );


 }





This site is useful to know what functions are exist. but there is not source code. http://www.calerga.com/doc/LME_lapk.htm This site is very useful, there are descriptions and  example source codes. http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/index.htm#dgesv.htm