ccgsl: a C++ interface for the gnu Scientific Library


Home ·
Download ·
Design ·
Documentation ·
Development ·
Example ·
Bugs ·


The gnu Scientific Library (gsl) is an excellent package for scientific computing. However, it is written in C and it is not always easy to mix gsl functions with C++. It uses C-style arrays or its own vector struct, which are not easy to use with the C++ Standard Template Library (stl). It uses alloc and free functions where C++ users would normally use new and delete. And gsl functions that take a function as an argument are not easily adapted to work with C++ function objects.

The ccgsl interface is derived from code I wrote to use gsl vectors in C++. I wanted to be able to use shared pointers (e.g. std::shared_ptr to gsl_vector objects). But the gsl_vector_alloc and gsl_vector_free functions make for clunky C++ code. And the vectors can’t be used with stl or with some of features of C++11. So I created a gsl::vector class that would combine the features of a shared pointer and an stl container while giving easy access to all the gsl_vector functions. The gsl::vector functions are defined inline in a header file so that the compiler can easily optimise away the implementation detail.

The ccgsl interface is now extended to work with a large proportion of gsl functions. But it is designed with the same principles. The interface should always be contained only in header classes with inline functions so that the compiler can optimise away the detail and give nearly the same speed of code as if the gsl functions were called directly. No gsl object need be constructed directly: instead ccgsl can construct an corresponding object that contains a shared pointer to the original. Thus, ccgsl objects are really references to gsl objects and can be passed as arguments much in the same way that Java makes it easy to create and pass objects. Objects in ccgsl need not be deleted. And ccgsl works with the stl and with function objects when this is helpful.


I have released ccgsl under the gnu General Public License, version 2. This means that you may freely download it and use it in the same way you may download and use the gnu Scientific Library.

Last modified: Sun 22 Jan 2012 08:12 pm Logo