How To use GSL

LCG Project | LCG Applications Area

SEAL Project | Project Portal

 

$Date: 2004/06/28 13:45:15 $

GSL

Th GNU Scientific Library (GSL) contains a collection of routines written in C for numerical computing. The library covers a wide range of topics in numerical computing. The complete range of subject areas covered by the library includes :

Complex Numbers Roots of Polynomials Special Functions
Vectors and Matrices Permutations Sorting
BLAS Support Linear Algebra Eigensystems
Fast Fourier Transforms Quadrature Random Numbers
Quasi-Random Sequences Random Distributions Statistics
Histograms N-Tuples Monte Carlo Integration
Simulated Annealing Differential Equations Interpolation
Numerical Differentiation Chebyshev Approximation Series Acceleration
Discrete Hankel Transforms Root-Finding Minimization
Least-Squares Fitting Physical Constants IEEE Floating-Point

More information can be found consulting the GSL online reference manual

Obtaining GSL

At CERN GSL is installed in /afs/cern.ch/sw/lcg/external/GSL. The current version is 1.4 and binaries exist for various platforms, including Linux redhat 7.3 with gcc 3.2 and 3.2.3 compilers, Windows with Visual Studio 7.1 and MacOS X 10 with gcc 3.3.

For external users, the library is automatically included when downloading SEAL (see SEAL download page), or alternatively they can download the library directly from the GNU web site.

How to use GSL

To use GSL in a C or C++ application one needs first to include the corresponding GSL header file(s) to the chosen function or algorithm. They are grouped according to functionality, and one needs to consult the online reference manual to find out the right name. At CERN header files are located, for Linux RH 7.3 and compiler gcc 3.2 and GSL version 1.4, in /afs/cern.ch/sw/lcg/external/GSL/1.4/rh73_gcc32/include.

GSL libraries (shared and archived) are under /lib directory.

1) Example program: using a GSL function

The following example illustrates how to use the library for computing the value of the Bessel function J0(x) :

#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main ( )
{
  double x = 5.0;
  double y = gsl_sf_bessel_J0 (x);
  printf ("J0(%g) = %.18e\n", x, y);
  return 0;
}

To compile and link the program add the path to the GSL header files and libraries:

GSL_HOME=/afs/cern.ch/sw/lcg/external/GSL/1.4/rh73_gcc32
g++ -c -I$GSL_HOME/include  test_gsl_bessel.cpp
g++ -o test_gsl_bessel -L$GSL_HOME/lib -lgsl -lgslcblas test_gsl_bessel.o
export LD_LIBRARY_PATH=$GSL_HOME/lib:$LD_LIBRARY_PATH

./test_gsl_bessel

when executed, the program will show the following output, and it should be correct to double-precision accuracy:

J0(5) = -1.775967713143382920e-01

This is the normal way of using GSL Special functions. An alternatively way is to use the error handling form which returns an error code and an estimate of the error in calculating the function value, as shown in the following example:

gsl_sf_result result;
int status = gsl_sf_bessel_J0_e (x, &result);
// if there no error function returns a GSL_SUCCESS
if (status == GSL_SUCCESS) {    
  double y = result.val; 
  double error = result.err;
} 

2) Example program: using GSL random number generators and distributions

GSL provides a large variety of random number generator algorithms and the capabilities of generating random numbers according to some predefined distributions. The following example shows the way to initialize the random number generator and to generate numbers according to a uniform and a Gaussian distribution. The user can choose the generator engine specifying directly in the code, or by using the environment variable GSL_RNG_TYPE (see example in the GSL manual)

#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>

int main (void)
{
  const gsl_rng_type * T;
  gsl_rng * r;
  
  // select random number generator 
  r = gsl_rng_alloc (gsl_rng_mt19937);

	double sigma = 10; 
  for (int i = 0; i < 10; i++) 
    {
      double u = gsl_rng_uniform (r);
      double v = gsl_ran_gaussian(r, sigma); 
    }

  gsl_rng_free (r);

  return 0;
}

For every random number distribution GSL provides also the function to estimate the pdf and the cumulative probability for the lower, P, and upper tail, Q. For example, in the case of the Chi2 distribution, to get a probability to obtain a value of Chi2 greater than x , for degree of freedom nu, one can use:

#include <gsl/gsl_cdf.h>

double prob = gsl_cdf_chisq_Q(x, nu)

Problems and support

If you need support, please use the SEAL Savannah portal. Go directly in Savannah to the SEAL Support page or Bugs page, using category MathLibs, to submit a support request or bug report.

Otherwise you can directly report bugs to GSL following their instructions.


Contact: Lorenzo Moneta