GNU Scientific Library(GSL)是一个为C和C++程序员提供的科学数值运算库。该科学计算库异常强大,提供了如下方面的支持:
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
对于经常处理复杂数学计算的开发人员来说,无疑是莫大的解脱~
该项目的主页是:http://www.gnu.org/software/gsl/gsl.html
遗憾的是GSL不支持windows平台,不过所幸的是有人做了GSL在windows上的移植工作,名唤WinGSL(http://www6.in.tum.de/~kiss/WinGsl.htm),虽然版本比较老,只更新到04年,但对于一般人来说,还是足够了。
之前写过一点计算机视觉方面的程序,涉及到矩阵运算都通过VC调用matlab计算引擎来处理。虽然比较方便,但是前提是要安装matlab,并且调用计算引擎进行数据交换的速度也不快。这次数据挖掘作业,会设计到一些矩阵分解,特征值求解之类的运算,为了偷懒,决定试用一下WinGSL。
在VC6.0下配置WinGSL极为简单:
1.从http://www6.in.tum.de/~kiss/WinGsl.htm下载最新的WinGSL(当前是1.4版),解压到某个目录,假设为D:WinGSL
2.将WinGSL使用的lib和头文件加入vc6的设置:
vc6.0->tools->options->directories
将D:WinGSLlib,D:WinGSL分别加入到库文件和头文件的搜索路径中。
3.将WinGsl.dll和WinGslD.dll复制到%VC_HOME%/VC98Bin下,或者是复制到新建的VC6工程的主目录下。
4.在新建的工程中将要使用的库加入到连接器中:
vc6.0->project->settings->link->object/library modules。
配置完毕后测试如下代码:
#include <stdio.h>
#include <gsl/gsl_linalg.h>
int
main (void)
{
double a_data[] = { 0.18, 0.60, 0.57, 0.96,
0.41, 0.24, 0.99, 0.58,
0.14, 0.30, 0.97, 0.66,
0.51, 0.13, 0.19, 0.85 };
double b_data[] = { 1.0, 2.0, 3.0, 4.0 };
gsl_matrix_view m
= gsl_matrix_view_array (a_data, 4, 4);
gsl_vector_view b
= gsl_vector_view_array (b_data, 4);
gsl_vector *x = gsl_vector_alloc (4);
int s;
gsl_permutation * p = gsl_permutation_alloc (4);
gsl_linalg_LU_decomp (&m.matrix, p, &s);
gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x);
printf ("x = ");
gsl_vector_fprintf (stdout, x, "%g");
gsl_permutation_free (p);
gsl_vector_free (x);
return 0;
}
编译,链接通过,说明配置成功。但是运行结果居然出错!
这可是GSL网站上的标准示例…
debug了一下,发现x计算正确,但是调用 gsl_vector_fprintf (pf, x, “%g”);时即出错…
汗~~
于是重新google了一下,发现sourceforge上还有个GnuWin32,专门移植负责GNU的项目到win32平台。自然也包括了GSL for windows移植版,当前版是1.8,比WinGSL要新一些,可以再这里下载: http://gnuwin32.sourceforge.net/packages/gsl.htm。
于是乎高高兴兴的down了下来,类似的重新配置了一遍,运行上述程序,结果…
可以正确的输出结果,但是冒出个debug错误~~程序结束时chkesp检查ESP指针时发现不一致…汗~~~
lib.exe /machine:i386 /def:libgsl.def
lib.exe /machine:i386 /def:libgslcblas.def
然后将GSL的BIN目录下的libgslcblas.dll、libgsl.dll拷贝到C:WINDOWSsystem32目录下即可。
最后,强烈建议直接在linux下使用最新版的gsl。