Class Polynomial

    • Field Detail

      • debug

        public static boolean debug
        Assign Polynomial.debug to @{code true} or false to turn debugging output on or off.
    • Constructor Detail

      • Polynomial

        public Polynomial()
        no-arg constructor returns a polynomial of degree 1, with value 0
      • Polynomial

        public Polynomial​(int[] coeffsHighToLow)

        Construct polynomial from int array of coefficients. The coefficients are listed in order from highest to lowest degree. The degree is the size of the array - 1.

        Examples:

        • 7x3 - 2x2 + 3 would be represented as {7,-2,0,3}
        • x4 - 4 would be represented as {1,0,0,0,-4}

        NOTE that the order of coefficients is not necessarily the way they will be stored in the array. That is, the order of coefficients in the array passed in is from highest degree down to lowest degree, so for a cubic:

        • coeffs[0] is the x3 coefficient
        • coeffs[1] is the x2 coefficient
        • coeffs[2] is the x coefficient
        • coeffs[3] is the constant term

        It is done this way so that when initializing a polynomial from an array literal, the order of coefficients mirrors the way polynomials are typically written, from highest order term to lowest order term:

        Example: to represent 4x3 - 7x2 + 5,
        use: Polynomial p = new Polynomial (new int [] {4,-7,0,5});
        NOT: Polynomial p = new Polynomial (new int [] {5,0,-7,4});

        Parameters:
        coeffsHighToLow - array of coefficients in order from largest degree down to smallest. If array has length zero, return a polynomial of degree zero with constant value zero.
      • Polynomial

        public Polynomial​(String s)
        Construct polynomial from string representation that matches the output format of the Polynomial toString method. That is, you should be able to do:
             Polynomial p = new Polynomial("0");
             Polynomial p = new Polynomial("1");
             Polynomial p = new Polynomial("-4");
             Polynomial p = new Polynomial("2x - 3");
             Polynomial p = new Polynomial("x^2 - 5x + 6");
             Polynomial p = new Polynomial("x^2 - x - 1");
             Polynomial p = new Polynomial("x^2 - x");
             Polynomial p = new Polynomial("-x^7 - 2x^5 + 3x^3 - 4x");
             
        And for any Polymomial object p, the following test should pass:
             assertEquals(new Polynomial(p.toString()), p);
             
        Parameters:
        s - string representation of Polynomial
    • Method Detail

      • getDegree

        public int getDegree()
        Get the degree of the polynomial. Should be always >=1
        Returns:
        degree of the polynomial
      • toString

        public String toString()

        Return string respresentation of Polynomial according to the following rules:

        • Leading coefficient if negative, has negative sign in front, with no space.
        • Other + and - signs have a space on either side.
        • Coefficients that are ones should be omitted (except in the constant term, i.e. x0 term).
        • Terms with zero coefficients should be omitted, except in the special case where the polynomial is of degree zero, and the constant term is zero. In that case, "0" should be returned.
        Examples of String Representation of Polynomials
        Math notationString Representation
        00
        11
        -4-4
        2x - 32x - 3
        x2 - 5x + 6x^2 - 5x + 6
        x2 - x - 1x^2 - x - 1
        x2 - x x^2 - x
        -x7 - 2x5 + 3x3 - 4x-x^7 - 2x^5 + 3x^3 - 4x
        Overrides:
        toString in class AbstractCollection<Integer>
        Returns:
        string representation of Polynomial
      • equals

        public boolean equals​(Object o)
        determine whether two polynomials are equal (same degree, same coefficients)
        Specified by:
        equals in interface Collection<Integer>
        Specified by:
        equals in interface List<Integer>
        Overrides:
        equals in class ArrayList<Integer>
        Parameters:
        o - arbitrary object to test for equality
        Returns:
        true if objects are equal, otherwise false
      • degreeOfPolynomialCoeffsLowToHigh

        public static int degreeOfPolynomialCoeffsLowToHigh​(int[] coeffsLowToHigh)

        Given an int [] of coefficients from lowest to highest degree (where the index in the array matches the power of the x term), find the degree of the polynomial represented by the coefficients shown.

        This is a utility method that may be useful in converting between the low to high and high to low representations of coefficients, both in user programs that use the Polynomial class, as well as in internal routines used to implement Polynomial methods.

        Parameters:
        coeffsLowToHigh - coefficients of a polynomial in order from lowest degree to highest degree. May have trailing zeros.
        Returns:
        index of the highest degree non-zero term (if any, otherwise 0)
      • degreeOfPolynomialCoeffsHighToLow

        public static int degreeOfPolynomialCoeffsHighToLow​(int[] coeffsHighToLow)

        Given an int [] of coefficients from highest to lowest power (where the x^0 term has the highest index in the array) find the degree of the polynomial represented by the coefficients shown.

        This is a utility method that may be useful in converting between the low to high and high to low representations of coefficients, both in user programs that use the Polynomial class, as well as in internal routines used to implement Polynomial methods.

        Parameters:
        coeffsHighToLow - coefficients of a polynomial in order from highest power of x first to lowest power of x last. May have leading zeros.
        Returns:
        index of the highest degree non-zero term (if any, otherwise 0)
      • lowToHigh

        public static int[] lowToHigh​(int[] coeffsHighToLow)

        Convert a list of coefficients in order from highest degree to lowest degree (the order used for input to the Polynomial constructor) to one where the order is lowest degree to highest degree (where index matches power of the x term).

        Parameters:
        coeffsHighToLow - coefficients of a polynomial in order from highest degree to lowest degree. May have leading zeros.
        Returns:
        An int [] with coefficients in order from lowest degree to highest degree. No trailing zeros.
      • highToLow

        public static int[] highToLow​(int[] coeffsLowToHigh)
        Convert a list of coefficients in order from lowest degree to highest degree (where index matches power of the x term) to one in order from highest degree to lowest degree (the order used for input to the Polynomial constructor).
        Parameters:
        coeffsLowToHigh - coefficients of a polynomial in order from lowest degree to highest degree. May have trailing zeros.
        Returns:
        An int [] with coefficients in order from highest degree to lowest degree. No leading zeros.
      • plus

        public Polynomial plus​(Polynomial p)
        return a new Polynomial which has as its value the this polynomial plus the one passed in as a parameter.
        Parameters:
        p - the Polynomial to add to this one
        Returns:
        sum of this Polynomial and p
      • times

        public Polynomial times​(Polynomial p)
        return a new Polynomial which has as its value the this polynomial times the one passed in as a parameter.
        Parameters:
        p - the Polynomial to multiply this one by
        Returns:
        product of this Polynomial and p
      • minus

        public Polynomial minus​(Polynomial p)
        return a new Polynomial which has as its value the this polynomial minus the one passed in as a parameter.
        Parameters:
        p - the Polynomial to subtract from this one
        Returns:
        the result of this Polynomial minus p
      • usage

        public static void usage()
        Print Usage message for Polynomial main
      • main

        public static void main​(String[] args)

        Main for testing constructing Polynomials from strings, and for testing plus, minus and times.

        At Unix command line:

        • Use single quotes to make the entire Polynomial be a single argument
        • Use \* when operator is * (to avoid having * expanded as a wildcard.)

        Example:

           java -jar target/polynomial-1.0.jar 'x^2 + 2x + 3' \* 'x - 4'
          
        Parameters:
        args - Command Line Arguments