GNU科学计算函数库GSL(GNU Scientific Library)是一个强大的C/C++数值计算函数库,它是一个自由软件,是GNU项目软件的一个部分,遵循GPL协议。该函数库提供了大量的数值计算程序,如随机函数、拟合函数、快速傅里叶变换、求直方图和解算最小二乘拟合等等,整个函数库大约有1000多个函数,几乎涵盖了科学计算的各个方面。
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
Discrete Wavelet Transforms Basis splines
在Windows平台下可以选用GSL for Windows库,该库的主页在http://gnuwin32.sourceforge.net/packages/gsl.htm,对于仅使用该库做开发的人员来说,仅下载Complete package, except sources就可以了。
下载后,安装gsl-1.8.exe,并将其include目录和lib目录添加到IDE的配置选项中。在安装后的lib目录下,gsl提供了静态库libgsl.a,libgslcblas.a和动态库libgsl.dll.a,libgslcblas.dll.a文件,依据windows平台下用户的使用习惯,进入gsl库的lib目录下依次输入以下两条语句:
lib /machine:i386 /def:libgsl.def
lib /machine:i386 /def:libgslcblas.def
便生成了相应的libgsl.lib和libgslcblas.lib两个文件。
若开发的程序以动态连接的方式使用了gsl库,则需要在应用程序入口处,添加:
#define GSL_DLL,
否则程序会出现运行时错误。
以下程序Demo转自:http://blog.csdn.net/mooncircle/archive/2010/04/30/5545448.aspx
#define GSL_DLL
#include <gsl/gsl_spline.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <gl/glut.h>
#include <gl/gl.h>
void Display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
const size_t n = 4;
double x[] = {0,0.333336,0.666666,1};
double y[] = {0,0.5,0.9,0};
gsl_interp* interps[3] = {NULL,NULL,NULL};
interps[0] = gsl_interp_alloc(gsl_interp_linear,n);
interps[1] = gsl_interp_alloc(gsl_interp_polynomial,n);
interps[2] = gsl_interp_alloc(gsl_interp_cspline,n);
gsl_interp_init(interps[0],x,y,n);
gsl_interp_init(interps[1],x,y,n);
gsl_interp_init(interps[2],x,y,n);
gsl_interp_accel* acc = gsl_interp_accel_alloc();
glBegin(GL_LINE_STRIP);
for(double t=0.0; t<=1.025; t+=0.025)
{
glColor3f(1,0,0);
glVertex3f(t,gsl_interp_eval(interps[0],x,y,t,acc),0.0);
}
glEnd();
glBegin(GL_LINE_STRIP);
for(double t=0.0; t<=1.025; t+=0.025)
{
glColor3f(0,1,0);
glVertex3f(t,gsl_interp_eval(interps[1],x,y,t,acc),0.0);
}
glEnd();
glBegin(GL_LINE_STRIP);
for(double t=0.0; t<=1.025; t+=0.025)
{
glColor3f(0,0,1);
glVertex3f(t,gsl_interp_eval(interps[2],x,y,t,acc),0.0);
}
glEnd();
gsl_interp_accel_free(acc);
gsl_interp_free(interps[0]);
gsl_interp_free(interps[1]);
gsl_interp_free(interps[2]);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(512,512);
glutCreateWindow("GSL Interpolation");
glutDisplayFunc(Display);
glClearColor(1,1,1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-1,-1,0);
glScalef(2,2,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,512,512);
glLineWidth(4.0);
glutMainLoop();
return 0;
}运行结果:
说明Gsl函数库已经可以使用了。