1.19.1.1 Calling GNU Scientific Library


This article demonstrate how to use GSL in Origin C. First you need download the GSL dlls from http://gnuwin32.sourceforge.net/packages/gsl.htm. You need just two dlls (libgsl.dll and libgslcblas.dll), and you can put them into the same folder where you are going to keep your Origin C files. For example, in a folder called c:\oc\. When using the downloaded dlls, please pay attention to the version issues.

libgsl.dll
This is the main gsl dll
libgslcblas.dll
This dll is needed by libgsl.dll


To use libgsl.dll in Origin C, you will need a header file that provides the prototypes of the gsl functions. You can copy and translate(if needed) the necessary prototype/definition from GSL header files, for example, call it ocgsl.h, and create in the c:\oc\ folder.


ocgsl.h

// when loading the dll, need to load the correct version, 
// see the "version issues" link above for more details
#pragma dll(libgsl, header) 
// this is OC special pragma, 
// header keyword is to indicate libgsl.dll is in same location as this file

#define GSL_EXPORT	// for OC, this is not needed, so make it empty

// you can directly search and copy gsl function prototypes here

GSL_EXPORT double gsl_sf_zeta_int (const int n);

GSL_EXPORT int gsl_fit_linear (const double * x, const size_t xstride,
                               const double * y, const size_t ystride,
                               const size_t n,
                               double * c0, double * c1,
                               double * cov00, double * cov01, double * cov11,
                               double * sumsq);
Note: Please don't write #pragma dll(libgslcblas, header) in code, or it may cause compile error.


test_gsl.c

The following is a simple OC file to show how to call gsl_sf_zeta_int and gsl_fit_linear

#include <Origin.h>
#include "ocgsl.h"


// Example of using Riemann Zeta Function in GSL
void gsl_test_zeta_function()
{
	double result1 = gsl_sf_zeta_int(2);
	double result2 = pi*pi/6;
	
	printf("Zeta(2) = %f\n", result1);
	printf("pi^2/6  = %f\n", result2);
}


// Example of using linear fit in GSL
void gsl_test_linear_fit(int npts = 10)
{
	vector vx(npts), vy(npts);
	const double ds = 2, di = 10;
		
	for(int ii=0; ii<npts; ++ii)
	{
		vx[ii] = ii;
		vy[ii] = ii*ds + di + (rand()%100-50)*0.05;
	}
	
	for(ii=0; ii<npts; ++ii)
		printf("%.2f\t%.2f\n", vx[ii], vy[ii]);
	
	double c0, c1, cov00, cov01, cov11, sumsq;
	
	gsl_fit_linear(vx, 1, vy, 1, npts, &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
	
	printf("Slope=%f, Intercept=%f", c1, c0);
}


Using GSL in a Fitting Function

There is also an exampleto show how to use gsl functions in a fitting function.

Notice on using GSL functions

Origin C doesn't support external functions that return a struct type variable, so those functions return this kind of data can not be used in Origin C, e.g.

gsl_complex gsl_complex_add (gsl_complex a, gsl_complex b)

since it returns a gsl_complex type of data, and gsl_complex is defined as:

typedef struct
{
    double dat[2];
}gsl_complex;