/** * Implements immutable polynomials with integer coefficients */ public class Poly extends RFunc implements Function{ private int coefs[] = {0}; /** * Constructor from coefficient array */ public Poly(int[] coefs){ if(coefs != null){ this.coefs = new int[coefs.length]; for(int i=0;i=0;i--){ val = (val * x) + coefs[i]; } return(val); } /** * Return the definate integral using anti-derivative. */ public double defIntegral(double a, double b, int N){ double[] newcoefs = new double[coefs.length + 1]; newcoefs[0] = 0; for(int i=0;i=0;i--){ if(coefs[i] == 0){ if(degree() > 0 || i > 0) continue; } if(i > 0){ buf += coefs[i] + " x^" + i; buf += " + "; } else buf += coefs[i]; } return(buf); } /** * Evaluate the polynomial at x. */ public double evaluate(double x){ int i; double val = 0; for(i=coefs.length-1;i>=0;i--){ val = (val * x) + coefs[i]; } return(val); } /** * Returns the sum of two polynomials. */ public Poly add(Poly a){ int len = coefs.length; if(a.coefs.length > len) len = a.coefs.length; int[] newcoefs = new int[len]; for(int i=0;i len) len = a.coefs.length; int[] newcoefs = new int[len]; for(int i=0;i