2011年3月12日

GSL(GNU Scientific Library) in Visual studio 2008

1.安裝好visual studio 2008
2.下載好GSL安裝檔(gsl-1.8-src.exe),並安裝
http://gnuwin32.sourceforge.net/downlinks/gsl-src.php

3.C:\Program Files\GnuWin32\src\gsl\1.8\gsl-1.8\VC8底下,執行專案:libgsl.sln
4.libgsl要建置四次(右鍵-->建置),分別為Debug-DLL,Debug-StaticLib,Release-DLL,Release-StaticLib模式下(選擇上方工具列的下拉式選單即可)
5.install_libgsl專案右鍵-->建置,C槽底下就會出現編譯好的檔案,但lib bin是空的
6.修改install_libgsl右鍵中的屬性-->組態屬性-->建置事件-->建置前事件-->命令列,內容為


mkdir %LIBGSL_INSTALL_DIR%\bin
if exist .\..\libgsl\Debug-DLL\libgsl_d.dll copy .\..\libgsl\Debug-DLL\libgsl_d.dll %LIBGSL_INSTALL_DIR%\bin
if exist .\..\libgsl\Release-DLL\libgsl.dll copy .\..\libgsl\Release-DLL\libgsl.dll %LIBGSL_INSTALL_DIR%\bin
if exist .\..\libgslcblas\Debug-DLL\libgslcblas_d.dll copy .\..\libgslcblas\Debug-DLL\libgslcblas_d.dll %LIBGSL_INSTALL_DIR%\bin
if exist .\..\libgslcblas\Release-DLL\libgslcblas.dll copy .\..\libgslcblas\Release-DLL\libgslcblas.dll %LIBGSL_INSTALL_DIR%\bin
mkdir %LIBGSL_INSTALL_DIR%\lib
if exist .\..\libgsl\Debug-StaticLib\libgsl_d.lib copy .\..\libgsl\Debug-StaticLib\libgsl_d.lib %LIBGSL_INSTALL_DIR%\lib
if exist .\..\libgsl\Release-StaticLib\libgsl.lib copy .\..\libgsl\Release-StaticLib\libgsl.lib %LIBGSL_INSTALL_DIR%\lib
if exist .\..\libgslcblas\Debug-StaticLib\libgslcblas_d.lib copy .\..\libgslcblas\Debug-StaticLib\libgslcblas_d.lib %LIBGSL_INSTALL_DIR%\lib
if exist .\..\libgslcblas\Release-StaticLib\libgslcblas.lib copy .\..\libgslcblas\Release-StaticLib\libgslcblas.lib %LIBGSL_INSTALL_DIR%\lib
if exist .\..\libgsl\Debug-DLL\libgsl_dll_d.lib copy .\..\libgsl\Debug-DLL\libgsl_dll_d.lib %LIBGSL_INSTALL_DIR%\lib
if exist .\..\libgsl\Release-DLL\libgsl_dll.lib copy .\..\libgsl\Release-DLL\libgsl_dll.lib %LIBGSL_INSTALL_DIR%\lib
if exist .\..\libgslcblas\Debug-DLL\libgslcblas_dll_d.lib copy .\..\libgslcblas\Debug-DLL\libgslcblas_dll_d.lib %LIBGSL_INSTALL_DIR%\lib
if exist .\..\libgslcblas\Release-DLL\libgslcblas_dll.lib copy .\..\libgslcblas\Release-DLL\libgslcblas_dll.lib %LIBGSL_INSTALL_DIR%\lib

7.install_libgsl專案右鍵-->建置,C槽底下就會出現編譯好的lib bin

8.vs2008中,開啟專案empty-->工具-->選項-->專案和方案
可執行檔加入:C:\libgsl-1.8\bin
include檔案加入:C:\libgsl-1.8\include
程式庫檔加入:C:\libgsl-1.8\lib



9.專案右鍵-->屬性-->連結器-->輸入-->其他相依性加入:libgsl_d.lib libgslcblas_d.lib

10.
範例1:

#include <gsl/gsl_math.h>
#include <iostream>
using namespace std;

int main()
{
double x = 5.0;
double y = gsl_pow_int(x,2);

cout << x << " " << y;
system("pause");

}


程式碼範例2

#include <iostream>
using namespace std;
#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_roots.h>
    
struct quadratic_params
{
    double a, b, c;
};
    
double quadratic (double x, void *params);
double quadratic_deriv (double x, void *params);
void quadratic_fdf (double x, void *params, double *y, double *dy);

double quadratic (double x, void *params)
{
    struct quadratic_params *p = (struct quadratic_params *) params;
    
    double a = p->a;
    double b = p->b;
    double c = p->c;
    
    return (a * x + b) * x + c;
}
    
double quadratic_deriv (double x, void *params)
{
    struct quadratic_params *p = (struct quadratic_params *) params;
    
    double a = p->a;
    double b = p->b;
    double c = p->c;
    
    return 2.0 * a * x + b;
}
    
void quadratic_fdf (double x, void *params, double *y, double *dy)
{
    struct quadratic_params *p = (struct quadratic_params *) params;
    
    double a = p->a;
    double b = p->b;
    double c = p->c;
    
    *y = (a * x + b) * x + c;
    *dy = 2.0 * a * x + b;
}
    
int main (void)
{
    struct quadratic_params params = {1.0, 0.0, -5.0};
    double r = 0, r_expected = sqrt (5.0);


//////////// ROOT BRACKETING ////////////////////////////
    gsl_function F;  
    F.function = &quadratic;
    F.params = &params;

const gsl_root_fsolver_type *T = gsl_root_fsolver_brent;
    gsl_root_fsolver *s = gsl_root_fsolver_alloc (T);
    double x_lo = 0.0, x_hi = 5.0;
    gsl_root_fsolver_set (s, &F, x_lo, x_hi);
    
    printf ("using %s method\n", gsl_root_fsolver_name (s));
    
    printf ("%5s [%9s, %9s] %9s %10s %9s\n",
            "iter", "lower", "upper", "root",
            "err", "err(est)");
    
int status;
    int iter = 0, max_iter = 100;
  
    do {
        iter++;
        status = gsl_root_fsolver_iterate (s);
        r = gsl_root_fsolver_root (s);
        x_lo = gsl_root_fsolver_x_lower (s);
        x_hi = gsl_root_fsolver_x_upper (s);
        status = gsl_root_test_interval (x_lo, x_hi,
                                         0, 0.001);
    
        if (status == GSL_SUCCESS)
            printf ("Converged:\n");
    
        printf ("%5d [%.7f, %.7f] %.7f %+.7f %.7f\n",
                   iter, x_lo, x_hi,
                   r, r - r_expected,
                   x_hi - x_lo);
    } while (status == GSL_CONTINUE && iter < max_iter);
    
    gsl_root_fsolver_free (s);
    
//////////// ROOT POLISHING ////////////////////////////
    gsl_function_fdf Fdf;  
Fdf.f = &quadratic;
Fdf.df = &quadratic_deriv;
    Fdf.fdf = &quadratic_fdf;
    Fdf.params = &params;

const gsl_root_fdfsolver_type *Tdf = gsl_root_fdfsolver_steffenson;
    gsl_root_fdfsolver *sdf = gsl_root_fdfsolver_alloc (Tdf);
    gsl_root_fdfsolver_set (sdf, &Fdf, 1.0);
    
    printf ("using %s method\n", gsl_root_fdfsolver_name (sdf));
    
    printf ("%5s %9s %10s\n",
            "iter", "root", "err");
    
iter = 0;
    do {
        iter++;
        status = gsl_root_fdfsolver_iterate (sdf);
        r = gsl_root_fdfsolver_root (sdf);
        status = gsl_root_test_residual (quadratic (r,&params), 0.001);
    
        if (status == GSL_SUCCESS)
            printf ("Converged:\n");
    
        printf ("%5d %.7f %+.7f\n",
                   iter, r, r - r_expected);
                
} while (status == GSL_CONTINUE && iter < max_iter);
     system("pause");

    gsl_root_fdfsolver_free (sdf);

return status;

}

沒有留言:

張貼留言

新電腦上重新安裝 Windows 10

想要在電腦上安裝 Windows 10 嗎? 若要開始使用,您首先必須取得安裝 Windows 10 的授權。然後才可以下載並執行媒體建立工具。如需工具使用方式的詳細資訊,請參閱下列指示。 https://www.microsoft.com/zh-tw/software-...