void zroots(fcomplex a[], int m, fcomplex roots[], int polish) { void zroots(fcomplex a[], int m, fcomplex *x, int *its); int i, its, j, jj; fcomplex x, b, c, ad[MAXM]; for (j = 0; j <= m; j++) ad[j] = a[j]; for (j = m; j >= 1; j--) { x Complex(0.0, 0.0); laguer(ad, j, &x, &its); if (fabs(x.i) <= 2.0*EPS*fabs(x.r)) x.i = 0.0; roots[j] = x; b = ad[j]; for (jj = j - 1; jj >= 0; jj--) { c = ad[jj]; ad[jj] = b; b = Cadd(Cmul(x, b), c); } } if (polish) for (j = 1; j <= m; j++) laguer(a, m, &roots[j], &its); for (j = 2; j <= m; j++) { x = roots[j]; for (i = j - 1; i >= 1; i--) { if (rootes.r <= x.r) break; roots[i + 1] = roots[i]; } roots[i + 1] = x; } }
int main(void) { fcomplex y[NTRY1],x; static fcomplex a[MP1]={{0.0,2.0}, {0.0,0.0}, {-1.0,-2.0}, {0.0,0.0}, {1.0,0.0} }; int i,iflag,its,j,n=0; printf("\nRoots of polynomial x^4-(1+2i)*x^2+2i\n"); printf("\n%15s %15s %7s\n","Real","Complex","#iter"); for (i=1;i<=NTRY;i++) { x=Complex((i-11.0)/10.0,(i-11.0)/10.0); laguer(a,M,&x,&its); if (n == 0) { n=1; y[1]=x; printf("%5d %12.6f %12.6f %5d\n",n,x.r,x.i,its); } else { iflag=0; for (j=1;j<=n;j++) if (Cabs(Csub(x,y[j])) <= EPS*Cabs(x)) iflag=1; if (iflag == 0) { y[++n]=x; printf("%5d %12.6f %12.6f %5d\n",n,x.r,x.i,its); } } } return 0; }
int zroots(double complex a[], const int m, double complex roots[], const int polish) { int i, j, jj, its, k; double complex x, b, c, ad[1000]; for(j = 0; j < m+1; j++) { ad[j] = a[j]; } for(j = m; j > 0; j--) { x = 0.; if((k = laguer(ad, j, &x, &its, 800)) != 0) { fprintf(stderr, "something wront!\n"); } if(abs(cimag(x)) <= 2.*epss*abs(creal(x))) x = creal(x); roots[j-1] = x; b = ad[j]; for(jj = j-1; jj > -1; jj--) { c = ad[jj]; ad[jj] = b; c = x*b + c; } } if(polish) { for(j = 1; j < m+1; j++) { if((k = laguer(a, m, &roots[j-1], &its, 800)) != 0) { fprintf(stderr, "something wront!\n"); } } } for(j = 2; j < m+1; j++) { x = roots[j-1]; for(i = j-1; i > 0; i--) { if(creal(roots[i-1]) <= creal(x)) break; roots[i] = roots[i-1]; } } return(0); }