GSL安装使用

安装

最新版下载地址:gsl

wget ftp://ftp.gnu.org/gnu/gsl/gsl-2.4.tar.gz
tar -zxvf gsl-2.4.tar.gz 
cd gsl-2.4
./configure
make
sudo make install

最小二乘

Linear Least-Squares Fitting

Y(c,x)=c0+c1x Y ( c , x ) = c 0 + c 1 x

int gsl_fit_linear(const double * x, const size_t xstride, const double * y, const size_t ystride, size_t n, double * c0, double * c1, double * cov00, double * cov01, double * cov11, double * sumsq)

gsl默认只支持double运算
xstride,ystride: 可能是指针移动步长,一般为1
cov00,cov01,cov11: 协方差
sumsq: 剩余误差

#include 
#include 

int main(void)
{
    int i, n = 4;
    double x[4] = { 1970, 1980, 1990, 2000 };
    double y[4] = {   12,   11,   14,   13 };

    double c0, c1, cov00, cov01, cov11, chisq;

    gsl_fit_linear(x, 1, y, 1, n, &c0, &c1, &cov00, &cov01, &cov11, &chisq);

    printf("# best fit: Y = %g + %g X\n", c0, c1);
    printf("# covariance matrix:\n");
    printf("# [ %g, %g\n#   %g, %g]\n", cov00, cov01, cov01, cov11);
    printf("# chisq = %g\n", chisq);

    for (i = 0; i < n; i++)
        printf("data: %g %g\n", x[i], y[i]);

    printf("\n");

    return 0;
}
gcc lls.cpp -o lls.o -lgsl -lgslcblas -o test //可以使用gsl自带的cblas库
gcc lls.cpp -o lls.o -lgsl -lopenblas -o test //也可以使用openblas之类的第三方cblas库

你可能感兴趣的:(GSL安装使用)