Table des matières

GNU Scientific Library

GNU Scientific Library (ou GSL) est une bibliothèque libre écrite en C fournissant des outils de calculs numériques.

La GSL fournit des outils dans les domaines suivants :

Versions installées

$ module avail numlib/gsl
numlib/gsl/1.15

Exemples

Systèmes linéaires

Le programme résout le système linéaire A x = b.

   [ 0.18 0.60 0.57 0.96 ] [x0]   [1.0]
   [ 0.41 0.24 0.99 0.58 ] [x1] = [2.0]
   [ 0.14 0.30 0.97 0.66 ] [x2]   [3.0]
   [ 0.51 0.13 0.19 0.85 ] [x3]   [4.0]
Algebre.c
#include <stdio.h>
#include <gsl/gsl_linalg.h>
 
     int
     main (void)
     {
       double a_data[] = { 0.18, 0.60, 0.57, 0.96,
                           0.41, 0.24, 0.99, 0.58,
                           0.14, 0.30, 0.97, 0.66,
                           0.51, 0.13, 0.19, 0.85 };
 
       double b_data[] = { 1.0, 2.0, 3.0, 4.0 };
 
       gsl_matrix_view m = gsl_matrix_view_array (a_data, 4, 4);
 
       gsl_vector_view b = gsl_vector_view_array (b_data, 4);
 
       gsl_vector *x = gsl_vector_alloc (4);
 
       int s;
 
       gsl_permutation * p = gsl_permutation_alloc (4);
 
       gsl_linalg_LU_decomp (&m.matrix, p, &s);
 
       gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
 
       printf ("x = \n");
 
       gsl_vector_fprintf (stdout, x, "%g");
 
       gsl_permutation_free (p);
       gsl_vector_free (x);
       return 0;
     }

Matrix Multiply

The following program computes the product of two matrices using the Level-3 blas function dgemm,

     [ 0.11 0.12 0.13 ]  [ 1011 1012 ]     [ 367.76 368.12 ]
     [ 0.21 0.22 0.23 ]  [ 1021 1022 ]  =  [ 674.06 674.72 ]
                        [ 1031 1032 ]

The matrices are stored in row major order, according to the C convention for arrays.

gslBlas3.c
     #include <stdio.h>
     #include <gsl/gsl_blas.h>
 
     int
     main (void)
     {
       double a[] = { 0.11, 0.12, 0.13,
                      0.21, 0.22, 0.23 };
 
       double b[] = { 1011, 1012,
                      1021, 1022,
                      1031, 1032 };
 
       double c[] = { 0.00, 0.00,
                      0.00, 0.00 };
 
       gsl_matrix_view A = gsl_matrix_view_array(a, 2, 3);
       gsl_matrix_view B = gsl_matrix_view_array(b, 3, 2);
       gsl_matrix_view C = gsl_matrix_view_array(c, 2, 2);
 
       /* Compute C = A B */
 
       gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
                       1.0, &A.matrix, &B.matrix,
                       0.0, &C.matrix);
 
       printf ("[ %g, %g\n", c[0], c[1]);
       printf ("  %g, %g ]\n", c[2], c[3]);
 
       return 0;  
     }

Compilation

Tout d'abord il faut charger le module GSL :

$ module load numlib/gsl

Pour connaître les options de compilation :

$ gsl-config --libs  
$ gsl-config --cflags
$ gcc program.c -I$GSL_HOME/include -L$GSL_HOME/lib -o program -lgsl -lgslcblas -lm

Liens