现在的位置: 首页 > 综合 > 正文

科学计算库GSL在 windows XP 下的使用

2014年08月29日 ⁄ 综合 ⁄ 共 2283字 ⁄ 字号 评论关闭

假如要你求出一元n次方程的所有根,你会选择什么工具呢?我想,在这个版权意识愈来愈强烈的时代,使用盗版的软件抑或是斥资买一个正版的商业软件绝不是明智的方案,再说了,自由和开源 的世界里本就有 非常优秀的软件和计算库,为何不试试呢。

GSL - GNU Scientific Library,是开源的,强大的C/C++数值计算库,能进行许多常见的科学计算,如一元n次方程的求根,微积分,线性代数,排列组合,快速傅里叶变换,统计,模拟退火等等:

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  然而,GSL只在Linux下才能使用,要想在windows下使用,还得在windows下模拟Linux环境。幸好有热心人把许多Linux软件编译成了适用于windows环境的二进制包,我用的GSL的windows版本就来自于

GnuWin32


下载gsl-1.8.exe:http://sourceforge.net/projects/gnuwin32/files/gsl/
安装好后,在dev-c++配置好bin路径,include路径,lib路径。在code::blocks下的配置还要方便一些,只需指定好 linking settings和search directories就可以了。

自己编的一个测试例子为,附带了Mathematica运行的精确结果,以资参考:

#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_poly.h>
int main (void){
double x=5,roots[3]; gsl_complex z[3];
printf ("%.16f\t%.16f\nis 8 an odd number? %d\n", M_PI, M_EULER,GSL_IS_ODD(8));
printf("J0(%g) = %.16f\n",x,gsl_sf_bessel_J0(x));

gsl_poly_solve_cubic(-5,7,-2,roots,roots+1,roots+2);
printf("\nx^3-5x^2+7x-2=0 has roots:\n");
printf("%.16f\t%.16f\t%.16f\n",roots[0],roots[1],roots[2]);
gsl_poly_complex_solve_cubic(0,1,-2,z,z+1,z+2);
printf("\nx^3+x-2=0 has roots:\n");
printf("%+.16f%+.16f I\n%+.16f%+.16f I\n%+.16f%+.16f I",z[0],z[1],z[2],z[3],z[4],z[5]);
getch();
return 0;
} 
 

另外,如果要想使得生成的exe文件可以正常运行,还需要把GnuWin32\bin目录下的libgsl.dll,libgslcblas.dll两个dll文件拷贝至windows系统的system32目录下或者放置在该exe文件同目录下。。。



求一元n次方程的代码如下:
#include <stdio.h>
#include <gsl/gsl_poly.h>
#define N 9
int main (void)
{int i;
/* coefficients of P(x)=1+x-5x^2+2x^3+3x^4+x^9*/
double a[N+1] = {1,1,-5,2,3,0,0,0,0,1};
double z[2*N];
gsl_poly_complex_workspace *w=gsl_poly_complex_workspace_alloc(N+1);
gsl_poly_complex_solve(a,N+1,w,z);
gsl_poly_complex_workspace_free(w);

for (i =0;i<N;i++)
{printf("z%d = %+.18f %+.18f I\n", i, z[2*i], z[2*i+1]);
}
return 0;
}

在code::blocks下运行结果为:




在GnuWin32\src\gsl\1.8\gsl-1.8目录下,有很多演示例子。。。
在线手册:http://www.gnu.org/software/gsl/manual/html_node/index.html#Top

抱歉!评论已关闭.