package poly; /** * Represents the polynomial abstraction. Holds the coefficient. Has two concrete * subclasses, ConstPoly and NonConstPoly, to form the composite pattern.
 * Copyright 2000, by Dung X. Nguyen, All rights reserved.
 
* @author Dung X. Nguyen * @since 01/26/00 */ public abstract class APolynomial { /** * This APolynomial leading coefficient. */ protected double _coef; /** * Returns the leading coefficient of this APolynomial. */ public double getLeadCoef () { return _coef; } /** * Returns the degree (or order) of this APolynomial. */ public abstract int getDegree (); /** * Returns the lower ordered polynomial of this APolynomial, if any. * @exception throws NoSuchElementException if this APolynomial is a constant. */ public abstract APolynomial getLowerPoly (); /** * Computes and returns the sum of this APolynomial with the parameter APolynomial. * @param p an APolynomial to be added to this APolynomial. * @return an APolynomial representing the sum of this APolynomial and the parameter p. */ public abstract APolynomial add (APolynomial p); /** * Computes and returns the value of this APolynomial at a "point" x. * @param x the "point" at which this APolynomial is to be evaluated. * @return the value of this APolynomial evaluated at the parameter x. */ public abstract double eval (double x); /** * Uses Horner's algorithm to compute and return the value of this APolynomial at a "point" x. * @param x the "point" at which this APolynomial is to be evaluated. * @return the value of this APolynomial evaluated at the parameter x. */ public abstract double evalHorner (double x); /** * Computes and returns the quotient of long polynomial division * by divisor. * @param divisor != zero polynomial. * @throw IllegalArgumentException for zero divisor. */ public APolynomial div (APolynomial divisor) { return divisor.divmodInto(this)._quotient; } /** * Computes and returns the remainder of long polynomial division * by divisor. * @param divisor != zero polynomial. * @throw IllegalArgumentException for zero divisor. */ public APolynomial mod (APolynomial divisor) { return divisor.divmodInto(this)._remainder; } /** * Called by an APolynomial that contains this APolynomial as its lower order polynomial. * Relegates to concrete subclass to compute the appropriate String for the lower * order polynomial if any. This is an example of we call a "helper" method for toString(). */ protected abstract String toString4LowerPoly (); /** * Computes and returns the sum of this APolynomial with the double parameter. * @param c a constant to be added to this APolynomial. * @return an APolynomial representing the sum of this APolynomial and the parameter c. */ protected abstract APolynomial addConst (double c); /** * Intermediate step in Horner's algorithm to compute and return the value of * this APolynomial at a "point" x. * @param x the "point" at which this APolynomial is to be evaluated. * @param acc the accumulated value of this polynomial so far. * @param preDegree the degree of the enclosing higher order APolynomial. * @return the value of this APolynomial evaluated at the parameter x. */ protected abstract double evalHornerHelp (double x, double acc, int preDegree); /** * Divides this APolynomial into a dividend APolynomial. * Since div and mod methods do basically the same thing, we combine them * into a helper method that does both simultaneously. * @param dividend APolynomial to divide into * @return the quotient and the remainder in a QRPair. * @throw IllegalArgumentException if this APolynomial is the zero polynomial. */ protected abstract QRPair divmodInto(APolynomial dividend); /** * Multiplies this APolynomial by a monomial. * @param coef the leadidng coefficient of a monomial. * @param degree the degree of a monomial. * @return the result of multiplying this APolynomial by the monomial with coefficient coef and order degree. */ protected abstract APolynomial multMonomial (double coef, int degree); }