windows下的gsl(科学计算库)配置

一、GSL介绍

GNU科学计算函数库GSL(GNU Scientific Library)是一个强大的C/C++数值计算函数库,它是一个自由软件,是GNU项目软件的一个部分,遵循GPL协议。GSL是一个为C和C++程序员提供的科学数值运算库。该科学计算库异常强大,函数库提供了大量的数值计算程序,如随机函数、特殊函数和拟合函数等等,整个函数库大约有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

该函数库的主页是:http://www.gnu.org/software/gsl/gsl.html。不过遗憾的是原始GSL并不支持不支持windows平台,可所幸的是有人做了GSL在windows上的移植工作,详见http://gnuwin32.sourceforge.net/packages/gsl.htm,目前版本是1.8。

二、下载gsl

1、从http://gnuwin32.sourceforge.net/packages/gsl.htm下载Complete package, except sources和Sources两个exe文件。

这是gsl最新版本的下载地址(感谢shaw博友提供): http://gladman.plushost.co.uk/oldsite/computing/gnu_scientific_library.php

三、安装

1、首先安装从http://gnuwin32.sourceforge.net/packages/gsl.htm下载的两个文件gsl-1.8.exe和gsl-1.8-src.exe。

四、设置Visual studio编译环境

 1、生成lib文件。发现安装目录lib下并没有libgsl.lib,libgslcblas.lib这两个文件,倒是有两个扩展名为def和a(linux下库文件包格式)的文件,因此必须进行转换。

l   开始菜单,点击运行,输入cmd。

l  先将lib.exe的所在目录(搜索vs的安装目录)加入到系统/用户环境变量path中,以便在命令行下可以执行lib,进入gsl库的lib目录下依次输入以下两条语句:
    lib /machine:i386 /def:libgsl.def
    lib /machine:i386 /def:libgslcblas.def

注:如果执行这两条命令时缺少dll等文件,仍旧从vs安装目录下搜索之后拷贝到lib.exe或libgsl.def所在目录即可。


 再看lib目录下,发现有了libgsl.lib,libgslcblas.lib这两个文件。

 2、将x:\Program Files\GnuWin32l\bin中的libgsl.dll和libgslcblas.dll复制到x:\VC\Bin;将\include整个Gsl目录复制到\VC\include下;\lib目录下的所有.lib文件全部复制到\VC\Lib下。

3、新建一个工程用于测试,。然后在该项目的project-properties-configuration properties-linker-input,在additional dependencies中加入你用到的库文件,如libgsl.lib libgslcblas.lib,用空格隔开。

五、测试Gsl函数库

#define GSL_DLL    //如果程序用到gsl的动态链接库,一定要加这句,否则连接时报错“access violation。。。。。”,简直无法找错。我就被这个问题折腾了一上午》》》》》》》

#include <stdlib.h>

#include <stdio.h>

#include <math.h>

#include <gsl/gsl_errno.h>

#include <gsl/gsl_spline.h>

#include <conio.h>




int main (void)

{

    int i;

    double xi, yi, x[10], y[10];

    printf ("#m=0,S=2\n");

    for (i = 0; i < 10; i++)

    {

       x[i] = i + 0.5 * sin (1.0*i);

       y[i] = i + cos (i * i*1.0);

       printf ("%g %g\n", x[i], y[i]);

    }

    printf ("#m=1,S=0\n");

    {

       gsl_interp_accel *acc = gsl_interp_accel_alloc ();//a gsl_interp_accel object, which is a kind of iterator for interpolation lookups.  It caches the previous value of an index lookup.  When the subsequent interpolation point falls in the same interval its index value can be returned immediately.创建一个gsl_interp_accel类型的指针并初始化,该类型对象可以加速对数组元素的遍历。

     //equivalent to the corresponding gsl_interp functions but maintain a copy of this data in the gsl_spline object.  This removes the need to pass both xa and ya as arguments on each evaluation. 下面这两个函数功能等同于普通的调用gsl_interp。。。相应函数,但效率较高,不必每计算一个插值点就把x和y传过去一遍。

      gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, 10);

       gsl_spline_init (spline, x, y, 10);

       for (xi = x[0]; xi < x[9]; xi += 0.01)

       {

           yi = gsl_spline_eval (spline, xi, acc);//求x=xi时对应的插值yi。

           printf ("%g %g\n", xi, yi);

       }

       gsl_spline_free (spline);

       gsl_interp_accel_free (acc);

    }

    getch();

    return 0;

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值