Mario.Tapilouw

Saturday, October 30, 2010

Resolving the error caused by GNU Scientific Library in Windows


When I tried some function in Gnu Scientific Library, I experienced an error message like this:

It happens to be many people are having the same problem with me when working in Windows version of GSL. So, it's not difficult for me to find a way to resolve this problem. The answer is very simple, just add these two lines into your code:

#define WIN32
#define GSL_DLL

And you can run your GSL based software smoothly. It works for me, hope it will work for you too.

Good luck!


Labels: ,

Wednesday, May 12, 2010

Linear Least Square Fitting using GSL

Sometimes for scientific calculation we need to do a least square fitting to a set of data. Depends on the usage, if you need to fit with a line then you need to perform a linear least square fitting, and if you need to fit with a polynomial with higher order, you need to perform non-linear least square fitting.

I have been trying to do this in C++ for a period of time, until I found that I can do it using Gnu Scientific Library. This really helps me a lot and make my job easier.

So, here's how it goes:
1. Make sure Gnu GSL library is configured with your compiler. Please Google it out to find out how.

2. Include the libraries into your program. You might need to visit this site to see how to configure it in Borland C++ Builder 6.

3. Include the header file for fitting into your source code:
#include < gsl/gsl_fit.h >

4. Basically, the function for linear least square fitting is (quoted from this website):
int gsl_fit_linear (const double * x, const size_t xstride, const double * y, const size_t ystride, size_t n, double * c0, double * c1, double * cov00, double * cov01, double * cov11, double * sumsq)

This function computes the best-fit linear regression coefficients (c0,c1) of the model Y = c_0 + c_1 X for the dataset (x, y), two vectors of length n with strides xstride and ystride. The errors on y are assumed unknown so the variance-covariance matrix for the parameters (c0, c1) is estimated from the scatter of the points around the best-fit line and returned via the parameters (cov00, cov01, cov11). The sum of squares of the residuals from the best-fit line is returned in sumsq. Note: the correlation coefficient of the data can be computed using gsl_stats_correlation (see Correlation), it does not depend on the fit.

5. So, we have to use that function. Before that, we should make our data for testing the algorithm. A simple array with random values will be enough, something like this:

spreadData[i] = (double)(rand() % 20 + 1) + (0.5 * i);


6. Then, finally call the line fitting function with the respective input and output arguments:

gsl_fit_linear (dataAxis, 1, spreadData, 1, 100, &c0, &c1, &cov00, &cov01, &cov11, &chisq);

Finally, you can plot the fitted value together with the data. The output that I got is something like this, for this example I plot the residue values together with the data and the line.


Hope it will be useful for you too. Good luck.

Labels: , ,

Thursday, April 29, 2010

GNU Scientific Library

Recently, I spend some time in looking for libraries that can fasten my software development. The reason is simple, if I want to develop my software for example doing a common algorithm, I don't need to spend time for developing the algorithm which might not be optimized and unstable.


The fact is that many people out there who share the same need and they have already created a group for developing an open source softwares/libraries. The advantage of such a group is that many people evaluates the software and many people will criticize when there's a bug in the software while the others will contribute to resolve the bug. Quite wonderful right? You have a lot of bright people from around the world assisting you (not exaggerating but it's quite true..).

GNU Scientific Library provides some scientific calculation such as linear least squares, FFT, Linear Algebra, Numerical Differentiation, etc. You might want to see this website for the available routines provided by GSL. It helps you focus on your task and appreciate others who has contributed their time developing this library.

After spending the whole afternoon configuring my compiler to work with this library, got a lot of error messages which are discouraging, finally I found the library that has been ported into Windows platform here

Before using this library, the compiler has to be configured. The configuration includes setting the
additional include path and the library path. After that your compiler is ready to use. My compiler is Borland C++ Builder, you need to convert the library into the Borland format instead of using Ms VC++ format (see my note about implib)

Finally, I tried this sample code from the documentation:

#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;
}


and it works :D

I plan to use this library for my projects, it will reduce my work in implementing the basic functions.


Labels: , , ,