// poly.java polyprt polycopy polyval polyderiv polyintegrate // polyadd polysub polymul polydiv // polyshrink polymake polyroots polyfit // polyfindaroot polyremovearoot // polynomial stored p[0] + p[1]*x + ...p[pwr]x^pwr public class poly { boolean debug = false; public poly() { // nothing to do } // end poly // polyprt print polynomial p with highest power pwr public void polyprt(int pwr, double p[]) { System.out.println("p[0]= "+p[0]); for(int i=1; i<=pwr; i++) { System.out.println("p["+i+"]= "+p[i]+" * x^"+i); } } // end polyprt // polycopy copy polynomial pin to polynomial pout public 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 polycopy // polyval return value of pokygon p at x // polyval simple Horners evaluation y = p[0] + p[1]*x + ...p[pwr]x^pwr public double polyval(int pwr, double p[], double x) { double y = p[pwr]*x; for(int i=pwr-1; i>0; i--) y = (p[i]+y)*x; return y + p[0]; } // end polyval // pwr of pderiv is one less than pwr input, pderiv[] smaller // run again on output to get next order derivative public void polyderiv(int pwr, double p[], double pderiv[]) { for(int i=1; i<=pwr; i++) // pwr is power, coef is [pwr+1] { pderiv[i-1] = p[i]*(double)(i); } } // end polyderiv // pwr of pintg is one more than pwr input, pintg[] bigger // pintg[0] is returned 0.0, put in your own non zero integration constant // run again on output to get next order integral public void polyintegrate(int pwr, double p[], double pintg[]) { pintg[0] = 0.0; for(int i=0; i<=pwr; i++) { pintg[i+1] = p[i]/(double)(i+1); } } // end polyintegrate // add polynomial p to polynomial q and place result in polynomial // npr must be one entry integer arrays to be set public void polyadd(int npp, double p[], int npq, double q[], int npr[], double r[]) { npr[0] = Math.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 polyadd // subtract polynomial q from polynomial p and place result in polynomial r // npr must be one entry integer arrays to be set public void polysub(int npp, double p[], int npq, double q[], int npr[], double r[]) { npr[0] = Math.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 public 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 public 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 public 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 polynomial p from given roots roots x public 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(Math.abs(v)