// *Finding Roots Of Polynominals* by Krzysztof Bosak 1999-01-24 // Bairstow {complex roots}, // Newton {real roots between x1, x2}, // 2ndDegree {ax^2+bx+c==0}, // RQ {x^2-rx-q==0}, // FindRoot {automated Newton} // polynominals coefficients are stored as follows: ax^2+bx+c ==> a, b, c #include #include #include #include #include void PolyOut(double *poly, char degree) { cout<=0.0) cout<<"\n--> Found root == "< Found root == "< Found root == "<x1); double x=x2, diff, xold; do { xold=x; x-=ValuePoly_PolyD(x, poly, degree); diff=x-xold; if(diff<0.0) diff=-diff; }while(diff>precision); return x; } double* DivRootPoly(double root, double *poly, unsigned char degree) {//returns poly=W(x)/(x-root) double *oldpoly=poly; for(unsigned char i=0; i=0 && x<1E6) x*=2; x1=-x; x2=x; } else { do { double newx; switch(rand()%6) { case 0: newx=double(rand())/double(rand()+1); break; case 1: newx=double(rand()); break; case 2: newx=1.0/double(rand()); break; case 3: newx=(x1+x2)*0.5; break; case 4: newx=(x1+x2)*-4; break; case 5: newx=(x1+x2)*1000000; break; } if(rand()&1) x1=newx; else x2=newx; //cout<1000000000); cout<<"Root = "<1); } void Degree2Solve(double *poly, unsigned char degree) {//Finds all roots of ax^2+bx+c assert(degree>=0 && degree<=2); if(degree==1) { RootOut(poly[1]/ *poly); cout<=0) { RootOut((-poly[1]-sqrt(delta))/2*poly[0]); RootOut((-poly[1]+sqrt(delta))/2*poly[0]); cout<=0) { RootOut((-poly[1]-sqrt(delta))/2*poly[0]); RootOut((-poly[1]+sqrt(delta))/2*poly[0]); cout<=2) { DivRootPoly(0, poly, degree--); RootOut(0); PolyOut(poly, degree); } do { if(degree<=2) { Degree2Solve(poly, degree); return; } double r=1, q=1, dr, dq;//guess q & r static double W1[256], W2[256]; do { /* cout<<"\nStepping: r=="<precision || fabs(dq)>precision); /* cout<