使用 C 中的 gsl 库查找具有复数系数的高次多项式的根
Finding roots of higher degree polynomials with complex coefficients using gsl library in C
我有一个五元方程要解。它是长期计算的一部分。我有一个 C 代码,我想使用 gsl 进行计算。特别是,我想使用函数 gsl_complex_poly_complex_eval
但我在定义它的属性时遇到了问题。以下是我想使用的简单代码。
#include<studio.h>
#include<math.h>
#include<complex.h>
#include<gsl/gsl_poly.h>
#include<gsl/gsl_complex.h>
int
main (void)
{
double omgo,omgM,omge,omgA;
double g1,g2,g3,g4,g5,g6;
omgM = 5000;
omgo = 490;
omgA=9.97;
omge=0.00035;
g1 = 1.0;
g2 = 2*omge;
g3 = (omgA*omgA+omge*omge+2*omgM*omgM+4*omgo*omgo);
g4 = -2*omge*(omgA*omgA+omgM*omgM+4*omgo*omgo);
g5 = (cpow(omgM,4.0)+omgA*omgA(omge*omge+omgM*omgM)+4*cpow(omge,2.0)*cpow(omgo,2));
g6 = omgA*omgA*omge*omgM*omgM;
gsl_complex a[6],z;
a[0] = -I*g6;
a[1] = g5;
a[2] = -I*g4;
a[3] = -g3;
a[4] = -2*I*g2;
a[5] = g1;
gsl_complex_poly_complex_eval(a,6,z);
print("%.30lf, %.30lf\n",creal(z),cimag(z));
return 0;
}
我收到如下错误:
quintic_roots.c: In function ‘main’:
quintic_roots.c:28:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘complex double’
a[0] = -I*g6;
^
quintic_roots.c:29:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘double’
a[1] = g5;
^
quintic_roots.c:30:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘complex double’
a[2] = -I*g4;
^
quintic_roots.c:31:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘double’
a[3] = -g3;
^
quintic_roots.c:32:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘complex double’
a[4] = -2*I*g2;
^
quintic_roots.c:33:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘double’
a[5] = g1;
^
quintic_roots.c:38:36: error: incompatible type for argument 1 of ‘cimag’
printf ("%.30lf, %.30lf\n", cimag(z),creal(z));
^
In file included from quintic_roots.c:3:0:
/usr/include/x86_64-linux-gnu/bits/cmathcalls.h:127:1: note: expected ‘complex double’ but argument is of type ‘gsl_complex {aka struct <anonymous>}’
__MATHDECL (_Mdouble_,cimag, (_Mdouble_complex_ __z));
^
quintic_roots.c:38:45: error: incompatible type for argument 1 of ‘creal’
printf ("%.30lf, %.30lf\n", cimag(z),creal(z));
^
In file included from quintic_roots.c:3:0:
/usr/include/x86_64-linux-gnu/bits/cmathcalls.h:130:1: note: expected ‘complex double’ but argument is of type ‘gsl_complex {aka struct <anonymous>}’
__MATHDECL (_Mdouble_,creal, (_Mdouble_complex_ __z));
根据 libgsl-documentation 的指示进行了许多更改。您需要了解文档的强大功能,我从未使用过libgsl
,但在这里我正在使用它编写代码,这一切都是因为文档。
quintic_roots.c
#include<stdio.h>
#include<math.h>
#include<complex.h>
#include<gsl/gsl_poly.h>
#include<gsl/gsl_complex.h>
int
main (void)
{
double omgo,omgM,omge,omgA;
double g1,g2,g3,g4,g5,g6;
omgM = 5000;
omgo = 490;
omgA=9.97;
omge=0.00035;
g1 = 1.0;
g2 = 2*omge;
g3 = (omgA*omgA+omge*omge+2*omgM*omgM+4*omgo*omgo);
g4 = -2*omge*(omgA*omgA+omgM*omgM+4*omgo*omgo);
g5 = (cpow(omgM,4.0)+omgA*omgA*(omge*omge+omgM*omgM)+4*cpow(omge,2.0)*cpow(omgo,2));
g6 = omgA*omgA*omge*omgM*omgM;
gsl_complex *a = (gsl_complex*) calloc(6, sizeof(gsl_complex));
GSL_SET_IMAG(a, -g6);
GSL_SET_REAL(a+1, g5);
GSL_SET_IMAG(a+2, -g4);
GSL_SET_REAL(a+3, -g3);
GSL_SET_IMAG(a+4, -2*g2);
GSL_SET_REAL(a+5, g1);
gsl_complex z;
GSL_SET_COMPLEX(&z, 1, 1);
gsl_complex res = gsl_complex_poly_complex_eval(a,6,z);
printf("%.15lf, %.15lf\n",GSL_REAL(res),GSL_IMAG(res));
return 0;
}
编译
gcc quintic_roots.c -lgsl -lm
输出
625002586907152.250000000000000, 625002382231741.375000000000000
输出为z = 1 + 1i
,请根据您的输入修改z。
我有一个五元方程要解。它是长期计算的一部分。我有一个 C 代码,我想使用 gsl 进行计算。特别是,我想使用函数 gsl_complex_poly_complex_eval
但我在定义它的属性时遇到了问题。以下是我想使用的简单代码。
#include<studio.h>
#include<math.h>
#include<complex.h>
#include<gsl/gsl_poly.h>
#include<gsl/gsl_complex.h>
int
main (void)
{
double omgo,omgM,omge,omgA;
double g1,g2,g3,g4,g5,g6;
omgM = 5000;
omgo = 490;
omgA=9.97;
omge=0.00035;
g1 = 1.0;
g2 = 2*omge;
g3 = (omgA*omgA+omge*omge+2*omgM*omgM+4*omgo*omgo);
g4 = -2*omge*(omgA*omgA+omgM*omgM+4*omgo*omgo);
g5 = (cpow(omgM,4.0)+omgA*omgA(omge*omge+omgM*omgM)+4*cpow(omge,2.0)*cpow(omgo,2));
g6 = omgA*omgA*omge*omgM*omgM;
gsl_complex a[6],z;
a[0] = -I*g6;
a[1] = g5;
a[2] = -I*g4;
a[3] = -g3;
a[4] = -2*I*g2;
a[5] = g1;
gsl_complex_poly_complex_eval(a,6,z);
print("%.30lf, %.30lf\n",creal(z),cimag(z));
return 0;
}
我收到如下错误:
quintic_roots.c: In function ‘main’:
quintic_roots.c:28:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘complex double’
a[0] = -I*g6;
^
quintic_roots.c:29:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘double’
a[1] = g5;
^
quintic_roots.c:30:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘complex double’
a[2] = -I*g4;
^
quintic_roots.c:31:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘double’
a[3] = -g3;
^
quintic_roots.c:32:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘complex double’
a[4] = -2*I*g2;
^
quintic_roots.c:33:7: error: incompatible types when assigning to type ‘gsl_complex {aka struct <anonymous>}’ from type ‘double’
a[5] = g1;
^
quintic_roots.c:38:36: error: incompatible type for argument 1 of ‘cimag’
printf ("%.30lf, %.30lf\n", cimag(z),creal(z));
^
In file included from quintic_roots.c:3:0:
/usr/include/x86_64-linux-gnu/bits/cmathcalls.h:127:1: note: expected ‘complex double’ but argument is of type ‘gsl_complex {aka struct <anonymous>}’
__MATHDECL (_Mdouble_,cimag, (_Mdouble_complex_ __z));
^
quintic_roots.c:38:45: error: incompatible type for argument 1 of ‘creal’
printf ("%.30lf, %.30lf\n", cimag(z),creal(z));
^
In file included from quintic_roots.c:3:0:
/usr/include/x86_64-linux-gnu/bits/cmathcalls.h:130:1: note: expected ‘complex double’ but argument is of type ‘gsl_complex {aka struct <anonymous>}’
__MATHDECL (_Mdouble_,creal, (_Mdouble_complex_ __z));
根据 libgsl-documentation 的指示进行了许多更改。您需要了解文档的强大功能,我从未使用过libgsl
,但在这里我正在使用它编写代码,这一切都是因为文档。
quintic_roots.c
#include<stdio.h>
#include<math.h>
#include<complex.h>
#include<gsl/gsl_poly.h>
#include<gsl/gsl_complex.h>
int
main (void)
{
double omgo,omgM,omge,omgA;
double g1,g2,g3,g4,g5,g6;
omgM = 5000;
omgo = 490;
omgA=9.97;
omge=0.00035;
g1 = 1.0;
g2 = 2*omge;
g3 = (omgA*omgA+omge*omge+2*omgM*omgM+4*omgo*omgo);
g4 = -2*omge*(omgA*omgA+omgM*omgM+4*omgo*omgo);
g5 = (cpow(omgM,4.0)+omgA*omgA*(omge*omge+omgM*omgM)+4*cpow(omge,2.0)*cpow(omgo,2));
g6 = omgA*omgA*omge*omgM*omgM;
gsl_complex *a = (gsl_complex*) calloc(6, sizeof(gsl_complex));
GSL_SET_IMAG(a, -g6);
GSL_SET_REAL(a+1, g5);
GSL_SET_IMAG(a+2, -g4);
GSL_SET_REAL(a+3, -g3);
GSL_SET_IMAG(a+4, -2*g2);
GSL_SET_REAL(a+5, g1);
gsl_complex z;
GSL_SET_COMPLEX(&z, 1, 1);
gsl_complex res = gsl_complex_poly_complex_eval(a,6,z);
printf("%.15lf, %.15lf\n",GSL_REAL(res),GSL_IMAG(res));
return 0;
}
编译
gcc quintic_roots.c -lgsl -lm
输出
625002586907152.250000000000000, 625002382231741.375000000000000
输出为z = 1 + 1i
,请根据您的输入修改z。