Class DensePoly

java.lang.Object
it.unimi.di.prog2.h18.AbstractPoly
it.unimi.di.prog2.h18.DensePoly
All Implemented Interfaces:
Poly, Iterable<Poly.Term>

public class DensePoly extends AbstractPoly
A dense implementation of non-zero Polys.
  • Field Details

    • coefficient

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

    • DensePoly

      private DensePoly(int[] coefficient)
      Initializes the polynomial given an array of coefficients.
      Parameters:
      coefficient - must satisfy the RI (this is a partial method).
  • Method Details

    • copyOf

      public static DensePoly copyOf(DensePoly poly) throws NullPointerException
      Copies the dense polynomial.
      Parameters:
      poly - the polynomial to copy.
      Returns:
      the copy of the polynomial.
      Throws:
      NullPointerException - if poly is null.
    • copyOf

      public static DensePoly copyOf(Poly poly) throws NullPointerException
      Copies the polynomial.
      Parameters:
      poly - the polynomial to copy.
      Returns:
      the copy of the polynomial.
      Throws:
      NullPointerException - if poly is null.
    • ofCoefficients

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

      The coefficient array can have trailing zeros, in case all coefficients are zero, this will return the zero polynomial.

      Parameters:
      coefficient - the array of coefficients.
      Returns:
      the polynomial.
      Throws:
      NullPointerException - if coefficient is null.
    • ofCoefficientDegree

      public static Poly ofCoefficientDegree(int c, int n) throws IllegalArgumentException
      Creates the polynomial \(p = cx^n\).

      The coefficient can be zero, resulting in the zero polynomial.

      Parameters:
      c - the coefficient.
      n - the degree.
      Returns:
      the polynomial \( p = cx^n \).
      Throws:
      IllegalArgumentException - if n < 0.
    • coefficient

      public int coefficient(int d)
      Description copied from interface: Poly
      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 Poly add(Poly 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 Poly mul(Poly 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.
    • minus

      public Poly minus()
      Returns the negate polynomial.

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

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

      public Iterator<Poly.Term> iterator()