Class DensePoly

java.lang.Object
it.unimi.di.prog2.h11.DensePoly

public class DensePoly extends Object
DensePolys are immutable polynomials with integer coefficients.

A typical Poly is \( p = c_0 + c_1 x + c_2 x^2 + \cdots + c_n x^n \).

  • Field Details

    • coefficient

      private final int[] coefficient
      The array of coefficients, the coeff[i] is the coefficient of \( x^i \).
    • ZERO

      public static final DensePoly ZERO
      The (only instance) of the zero polynomial.
  • Constructor Details

    • DensePoly

      private DensePoly(int[] coefficient)
      Initializes the polynomial given an array of coefficients.

      This constructor is partial, it's up to the factory methods to ensure that the representation invariant is preserved.

      Parameters:
      coefficient - the array of coefficients (must satisfy the representation invariant).
    • DensePoly

      public DensePoly(DensePoly poly) throws NullPointerException
      Copy constructor.
      Parameters:
      poly - the polynomial to copy.
      Throws:
      NullPointerException - if poly is null.
  • Method Details

    • ofCoefficients

      public static DensePoly ofCoefficients(int[] coefficient)
      Initializes a polynomial given an array of coefficients.

      The coefficient array can have trailing zeros, they will not be considered in the representation of the polynomial.

      Parameters:
      coefficient - the array of coefficients.
      Returns:
      the polynomial represented by the given array of coefficients.
    • ofCoefficientDegree

      public static DensePoly ofCoefficientDegree(int c, int n) throws IllegalArgumentException
      Initializes this to be the polynomial \(p = cx^n\).
      Parameters:
      c - the coefficient.
      n - the degree.
      Returns:
      the polynomial \( p = cx^n \).
      Throws:
      IllegalArgumentException - if n < 0.
    • isZero

      public boolean isZero()
      Checks whether this polynomial is the zero polynomial.
      Returns:
      true if this polynomial is the zero polynomial, false otherwise.
    • degree

      public int degree()
      Returns the degree of this polynomial.

      The degree is defined as the largest exponent with a non-zero coefficient.

      Returns:
      the largest exponent with a non-zero coefficient.
      Throws:
      IllegalStateException - if this is the zero polynomial.
    • coeff

      public int coeff(int d)
      Returns the coefficient of the term of given exponent.
      Parameters:
      d - the exponent of the term to consider.
      Returns:
      the coefficient of the considered term.
    • add

      public DensePoly add(DensePoly q) throws NullPointerException
      Performs polynomial addition.

      If \( p \) is this polynomial, returns \( p + q \).

      Parameters:
      q - the polynomial to add to this one.
      Returns:
      the sum among this and the given polynomial.
      Throws:
      NullPointerException - if q is null.
    • mul

      public DensePoly mul(DensePoly q) throws NullPointerException
      Performs polynomial multiplication.

      If \( p \) is this polynomial, returns \( p q \).

      Parameters:
      q - the polynomial to multiply by this one.
      Returns:
      the product among this and the given polynomial.
      Throws:
      NullPointerException - if q is null.
    • sub

      public DensePoly sub(DensePoly q) throws NullPointerException
      Performs polynomial subtraction.

      If \( p \) is this polynomial, returns \( p - q \).

      Parameters:
      q - the polynomial to subtract from this one.
      Returns:
      the subtraction among this and the given polynomial.
      Throws:
      NullPointerException - if q is null.
    • minus

      public DensePoly minus()
      Returns the negate polynomial.

      If \( p \) is this polynomial, returns \( -p \).

      Returns:
      this polynomial multiplied by \( -1 \).
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object