main() { int i,polish; fcomplex roots[MP1]; static fcomplex a[MP1] = {{0.0,2.0}, {0.0,0.0}, {-1.0,-2.0}, {0.0,0.0}, {1.0,0.0} }; printf("\nRoots of the polynomial x^4-(1+2i)*x^2+2i\n"); polish=FALSE; zroots(a,M,roots,polish); printf("\nUnpolished roots:\n"); printf("%14s %13s %13s\n","root #","real","imag."); for(i=1;i<=M;i++) printf("%11d %18.6f %12.6f\n",i,roots[i].r,roots[i].i); printf("\nCorrupted roots:\n"); for(i=1;i<=M;i++) roots[i] = RCmul(1+0.01*i,roots[i]); printf("%14s %13s %13s\n","root #","real","imag."); for(i=1;i<=M;i++) printf("%11d %18.6f %12.6f\n",i,roots[i].r,roots[i].i); polish=TRUE; zroots(a,M,roots,polish); printf("\nPolished roots:\n"); printf("%14s %13s %13s\n","root #","real","imag."); for(i=1;i<=M;i++) printf("%11d %18.6f %12.6f \n",i,roots[i].r,roots[i].i); }
void fixrts(float d[], int m) { void zroots(fcomplex a[], int m, fcomplex roots[], int polish); int i,j,polish; fcomplex a[NMAX],roots[NMAX]; a[m]=ONE; for (j=m-1;j>=0;j--) a[j]=Complex(-d[m-j],0.0); polish=1; zroots(a,m,roots,polish); for (j=1;j<=m;j++) if (Cabs(roots[j]) > 1.0) roots[j]=Cdiv(ONE,Conjg(roots[j])); a[0]=Csub(ZERO,roots[1]); a[1]=ONE; for (j=2;j<=m;j++) { a[j]=ONE; for (i=j;i>=2;i--) a[i-1]=Csub(a[i-2],Cmul(roots[j],a[i-1])); a[0]=Csub(ZERO,Cmul(roots[j],a[0])); } for (j=0;j<=m-1;j++) d[m-j] = -a[j].r; }
int main(void) { int i,polish=TRUE; static float d[NP1]= {0.0,6.0,-15.0,20.0,-15.0,6.0,0.0}; fcomplex zcoef[NP1],zeros[NP1],z1,z2; /* finding roots of (z-1.0)^6=1.0 */ /* first write roots */ zcoef[NPOLES]=ONE; for (i=NPOLES-1;i>=0;i--) zcoef[i] = Complex(-d[NPOLES-i],0.0); zroots(zcoef,NPOLES,zeros,polish); printf("Roots of (z-1.0)^6 = 1.0\n"); printf("%24s %27s \n","Root","(z-1.0)^6"); for (i=1;i<=NPOLES;i++) { z1=Csub(zeros[i],ONE); z2=Cmul(z1,z1); z1=Cmul(z1,z2); z1=Cmul(z1,z1); printf("%6d %12.6f %12.6f %12.6f %12.6f\n", i,zeros[i].r,zeros[i].i,z1.r,z1.i); } /* now fix them to lie within unit circle */ fixrts(d,NPOLES); /* check results */ zcoef[NPOLES]=ONE; for (i=NPOLES-1;i>=0;i--) zcoef[i] = Complex(-d[NPOLES-i],0.0); zroots(zcoef,NPOLES,zeros,polish); printf("\nRoots reflected in unit circle\n"); printf("%24s %27s \n","Root","(z-1.0)^6"); for (i=1;i<=NPOLES;i++) { z1=Csub(zeros[i],ONE); z2=Cmul(z1,z1); z1=Cmul(z1,z2); z1=Cmul(z1,z1); printf("%6d %12.6f %12.6f %12.6f %12.6f\n", i,zeros[i].r,zeros[i].i,z1.r,z1.i); } return 0; }
int main() { int i; double complex a[5]; double complex roots[5]; a[0] = 1.; a[1] = 1.; a[2] = 1.; a[3] = 1.; a[4] = 1.; zroots(a, 2, roots, 1); for(i = 0; i < 2; i++) { printf("%f %f %f %f\n", creal(roots[i]), cimag(roots[i]), creal(a[0]+a[1]*roots[i]+a[2]*roots[i]*roots[i]), cimag(a[0]+a[1]*roots[i]+a[2]*roots[i]*roots[i])); } return(0); }
double curve_segment_distance(double k,double *p1,double *p2,double *p3, double *p4,double *p,double q1,double q2) /******************************************************************************* LAST MODIFIED : 30 August 1996 DESCRIPTION : Calculates distance potential at a point p from a curve segment defined by p1-p2, slope p3,p4 with charge(q1,q2) the distance Q(t)-p is minimized by translating to the origin and solving the minimum sum Q(t)*Q(t) => 0 = sum Q'(t).Q(t) ==============================================================================*/ { double at,bt,ct,dist,dt,min_dist,min_t = 0.0,pt1[3],pt2[3],pt3[3],pt4[3],t; /* intermediate calculations */ double a[3],b[3],c[3],d[3],e[3],f[3],g[3],r[3]; /* coefficients of Q'(t).Q(t) */ double return_code; fcomplex coeff[6]; /* roots */ fcomplex roots[6]; int real_roots[6]; int i,j,min,n_real_roots; static double val; ENTER(curve_segment_distance); /* default return value */ return_code=0.0; /* checking arguments */ if (p1&&p2&&p3&&p4&&p) { for (i=0;i<6;i++) { coeff[i].r=0; coeff[i].i=0; roots[i].r=0; roots[i].i=0; } for (i=0;i<3;i++) { /* translate to origin */ pt1[i]=p1[i]-p[i]; pt2[i]=p2[i]-p[i]; pt3[i]=p3[i]-p[i] ; pt4[i]=p4[i]-p[i]; /* Q(t) */ a[i]=-pt1[i]+3*pt2[i]-3*pt3[i]+pt4[i]; b[i]=3*pt1[i]-6*pt2[i]+3*pt3[i]; c[i]=-3*pt1[i]+3*pt2[i]; d[i]=pt1[i]; /* Q'(t) */ e[i]=-3*pt1[i]+9*pt2[i]-9*pt3[i]+3*pt4[i]; f[i]=6*pt1[i]-12*pt2[i]+6*pt3[i]; g[i]=-3*pt1[i]+3*pt2[i]; } /* calculate coefficients of minimized polynomial */ for (i=0;i<3;i++) { coeff[0].r += (GLfloat)d[i]*g[i]; coeff[1].r += (GLfloat)c[i]*g[i]+d[i]*f[i]; coeff[2].r += (GLfloat)b[i]*g[i]+c[i]*f[i]+d[i]*e[i]; coeff[3].r += (GLfloat)a[i]*g[i]+b[i]*f[i]+c[i]*e[i]; coeff[4].r += (GLfloat)a[i]*f[i]+b[i]*e[i]; coeff[5].r += (GLfloat)a[i]*e[i]; } /* find roots of polynomial */ /* the real root is the t parameter value of the curve pt nearest p */ zroots(coeff,5,roots,1); min=1; n_real_roots=0; /* choose closest to real root if no real root exists*/ for (i=1;i<6;i++) { /* printf"Root[%d] = (%lf + %lfi)\n",i,roots[i].r,roots[i].i);*/ if ((roots[i].i*roots[i].i<roots[min].i*roots[min].i)||(roots[i].i == 0)) { if (roots[i].i == 0) { real_roots[n_real_roots]=i; n_real_roots++; } min=i; } } if (n_real_roots == 0) { real_roots[n_real_roots]=min; n_real_roots=1; } /* we have our best selection of real roots - now must find */ /* which one is the minimum */ min_dist=0.; for (i=0;i<n_real_roots;i++) { t=(double)roots[real_roots[i]].r; at=(1-t)*(1-t)*(1-t); bt=3.0*t*(1-t)*(1-t); ct=3.0*t*t*(1-t); dt=t*t*t; for (j=0;j<3;j++) { r[j]=at*pt1[j]+bt*pt2[j]+ct*pt3[j]+dt*pt4[j]; } dist=norm3(r); if (dist<min_dist||i==0) { min_dist=dist; min_t=t; } } if ((min_t>=0)&&(min_t<=1.0)) { if (0!=min_dist) { val=k/(min_dist*min_dist)*(q1+min_t*(q2-q1)); return_code=val; } else { return_code=TEXTURE_LINE_INFINITY; } } else { return_code=0.0; } } else { display_message(ERROR_MESSAGE, "curve_segment_distance. Invalid argument(s)"); return_code=0.0; } LEAVE; return (return_code); } /* curve_segment_distance */