GNU GSL

GSL

The GNU Scientific Library (GSL) is a numerical library for C and C++ programmers. It is a free software under the GNU General Public License.The library provides a wide range of mathematical routines such as random number generators, special functions and least-squares fitting. There are over 1000 functions in total with an extensive test suite. The complete range of subject areas covered by the library includes:

Complex NumbersRoots of PolynomialsSpecial FunctionsVectors and MatricesPermutationsSortingBLAS SupportLinear AlgebraEigensystemsFast Fourier TransformsQuadratureRandom NumbersQuasi-Random SequencesRandom DistributionsStatisticsHistogramsN-TuplesMonte Carlo IntegrationSimulated AnnealingDifferential EquationsInterpolationNumerical DifferentiationChebyshev ApproximationSeries AccelerationDiscrete Hankel TransformsRoot-FindingMinimizationLeast-Squares FittingPhysical ConstantsIEEE Floating-PointDiscrete Wavelet TransformsBasis spline

Unlike the licenses of proprietary numerical libraries, the license of GSL does not restrict scientific cooperation. It allows you to share your programs freely with others.

Installed Version

Check the version of the GSL

gsl-config --version

output:

1.16

Linker and Compiler Flags

Check the linker and compiler flags

gsl-config --libs --cflags

outputs:

-L/usr/local/lib -lgsl -lgslcblas -lm

-I/usr/local/include

GSL in HPC

Request the compute node:

srun --pty bash

The following short program demonstrates the use of the library by computing the value of the Bessel function J_0(x) for x=5, Copy the following content in a file example.c to your home directory.

example.c

#include <stdio.h>

#include <gsl/gsl_sf_bessel.h>

int main(void)

{

double x = 5.0;

double y = gsl_sf_bessel_J0 (x);

printf ("J0(%g) = %.18e\n", x, y);

return 0;

}

The header files for GSL are installed in their own 'gsl' directory. You should consult the GSL documentation to determine the required header file to use with each GSL function, and then write any preprocessor include statements with a 'gsl/' directory prefix thus:

#include <gsl/gsl_math.h>

Compiling

A typical compilation command for a source file 'example.c' with the GNU C compiler gcc is

gcc -Wall -c example.c

This produces an object file 'example.o'.

Linking

The library is installed in '/usr/local/lib' as a single file, 'libgsl.a', and a shared version of the library, 'libgsl.so', is also installed. To link against the library you need to specify both the main library and a supporting CBLAS library, which provides standard basic linear algebra subroutines. A suitable CBLAS implementation is provided in the library 'libgslcblas.a' if your system does not provide one. The following example shows how to link an application with the library:

gcc example.o -lgsl -lgslcblas -lm

Executing

./a.out

Output:

The output is shown below, and should be correct to double-precision accuracy,

J0(5) = -1.775967713143382920e-01

To compile a statically linked version of the program, use the -static flag in gcc,

gcc -static example.o -lgsl -lgslcblas -lm