// poly.c polyprt polycopy polyval polyderiv polyintegrate // polyadd polysub polymul polydiv // polyshrink polymake polyroots polyfit // polyfindaroot polyremovearoot // polynomal stored p[0] + p[1]*x + ...p[pwr]x^pwr #include #include "simeq.h" #undef max #define max(a,b) ((a)>(b)?(a):(b)) #undef abs #define abs(a) ((a)<0.0?(-(a)):(a)) // polyval.c simple Horners evaluation y = p[0] + p[1]*x + ...p[pwr]x^pwr double polyval(int pwr, double p[], double x) { int i; double y = p[pwr]*x; for(i=pwr-1; i>0; i--) y = (p[i]+y)*x; return y + p[0]; } // polycopy copy polynomial pin to polynomial pout void polycopy(int pwr, double pin[], double pout[]) { for(int i=0; i<=pwr; i++) // pwr is subscript of highest power { pout[i]= pin[i]; } } // end polycpy // polyprt print a polynomial void polyprt(int pwr, double p[]) { printf("p[0]= %f \n", p[0]); for(int i=1; inpp) r[i] = q[i]; else if(i>npq) r[i] = p[i]; else r[i] = p[i] + q[i]; } // end polyadd // subtract polynomial q from polynomial p and place result in polynomial r // npr must be one entry integer arrays to be set void polysub(int npp, double p[], int npq, double q[], int npr[], double r[]) { npr[0] = max(npp,npq); for(int i=0; i<=npr[0]; i++) if(i>npp) r[i] = -q[i]; else if(i>npq) r[i] = p[i]; else r[i] = p[i] - q[i]; } // end polysub // multiply polynomial p by polynomial q and place result in polynomial r // npp is highest power in p, npq is highest power in q void polymul(int npp, double p[], int npq, double q[], int npr[], double r[]) { npr[0] = npp+npq; for(int i=0; i<=npr[0]; i++) r[i] = 0.0; for(int i=0; i<=npp; i++) for(int j=0; j<=npq; j++) r[i+j] += p[i]*q[j]; } // end polymul // divide polynomial p by polynomial d (npp>npd) and place // quotient in polynomial q and remainder in polynomial r // npq and npr must be one entry integer arrays to be set void polydiv(int npp, double p[], int npd, double d[], int npq[], double q[], int npr[], double r[]) { int k; npr[0] = npp; npq[0] = npp-npd; k = npp; for(int j=0; j<=npp; j++) r[j] = p[j]; for(int i=npq[0]; i>=0; i--) { q[i] = r[k]/d[npd]; for(int j=npd; j>=0; j--) r[k-npd+j] = r[k-npd+j] - q[i]*d[j]; k--; } } // end polydiv // shrink size, pwr, such that p[pwr] not zero , return new pwr int polyshrink(int pwr, double p[]) { int npp = pwr; while(p[npp] == 0.0 && npp > 0) npp = npp -1; return npp; } // end polyshrink // polymake make a polygon p from given roots roots x void polymake(int nr, double x[], int npp[], double p[]) { npp[0] = nr; p[0] = -x[0]; p[1] = 1.0; if(nr==1) return; for(int i=1; itol) { v = polyval(npp, p, x); if(abs(v)0.0 || vm>0.0 && v<0.0) // crossing { del = 0.5*del; x = (xm + x)/2.0; } else if(vp<0.0 && v>0.0 || vp>0.0 && v<0.0) // crossing { del = 0.5*del; x = (xp + x)/2.0; } else { vp = abs(vp); v = abs(v); vm = abs(vm); if(vm < v && v < vp) // vm best { del=1.5*del; if(del>3.7) del = 3.7; x=xm-del; if(vm v && v > vp) // vp best { del=1.4*del; if(del>4.0) del = 4.0; x=xp+del; if(vpmaxcnt) break; if(abs(vbest)