使用GSL-GNU科学计算库

这里使用的自带vc工程的1.8版本,地址 http://gnuwin32.sourceforge.net/packages/gsl.htm

这个网页里面有GSL1.8版本的,里面有目录VC8,下面有libgsl.sln, GSL的官方文档在

http://www.gnu.org/software/gsl/doc/latex/gsl-ref.pdf  GSL都是C的API和一些结构体,我们

试了下多项式求根的例子,多项式是x^5+1,配置好编译好的gsl的lib, dll和header

#include <stdio.h>
#include <gsl/gsl_poly.h>

int main(void)
{
    int i;
    /* coefficients of P(x) = -1 + x^5 */
    double a[6] = { -1, 0, 0, 0, 0, 1 };
    double z[10];
    gsl_poly_complex_workspace* w
        = gsl_poly_complex_workspace_alloc(6);
    gsl_poly_complex_solve(a, 6, w, z);
    gsl_poly_complex_workspace_free(w);
    for (i = 0; i < 5; i++)
    {
        printf("z%d = %+.18f %+.18f\n",
            i, z[2 * i], z[2 * i + 1]);
    }
    return 0;
}

最后控制台输出:

z0 = -0.809016994374947673 +0.587785252292473359
z1 = -0.809016994374947673 -0.587785252292473359
z2 = +0.309016994374947507 +0.951056516295152976
z3 = +0.309016994374947507 -0.951056516295152976
z4 = +0.999999999999999889 +0.000000000000000000

 

把实根和复根都求出来了,换一下系数,double a[6] = { -1, 0.1, 0.2, 0.3, 0.4, 1 };

多项式是-1+0.1*x+0.2*x^2+0.3*x^3+0.4*x^4+x^5

输出:

z0 = +0.835460440971803497 +0.000000000000000000
z1 = +0.247415004045392517 +1.007728810338902381
z2 = +0.247415004045392517 -1.007728810338902381
z3 = -0.865145224531293833 +0.602636011854109310
z4 = -0.865145224531293833 -0.602636011854109310

 

上一篇:CS61A HW 03 Church Numerals


下一篇:web开发之字体应用