Class SparsePoly

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

public class SparsePoly extends AbstractPoly
A sparse implementation of Poly.
  • Field Details

  • Constructor Details

    • SparsePoly

      public SparsePoly()
      Initializes this to be the zero polynomial, that is \( p = 0 \).
    • SparsePoly

      private SparsePoly(List<Poly.Term> terms)
      Initializes this to be the polynomial with given terms and degree.
      Parameters:
      terms - must satisfy the RI (this is a partial method).
    • SparsePoly

      public SparsePoly(int coeff, int degree) throws IllegalArgumentException
      Initializes this to be the polynomial \(p = cx^n\).
      Parameters:
      coeff - the coefficient.
      degree - the degree.
      Throws:
      IllegalArgumentException - if n < 0.
  • Method Details

    • findByDegree

      private static int findByDegree(List<Poly.Term> terms, int degree)
      Finds the index of a term of given degree.
      Parameters:
      terms - a not null list of terms in increasing degree order.
      degree - the degree.
      Returns:
      the index of a term of given degree, or -1 if none is present.
    • coeff

      public int coeff(int degree)
      Description copied from interface: Poly
      Returns the coefficient of the term of given exponent.
      Parameters:
      degree - the exponent of the term to consider.
      Returns:
      the coefficient of the considered term.
    • addTerm

      private static void addTerm(List<Poly.Term> terms, Poly.Term term)
      Adds a term to the list.

      The list will remain in increasing degree order, in case a term with the same degree was present, the two will be added (and removed if the coefficient will become 0).

      Parameters:
      terms - a not null list of terms in increasing degree order.
      term - the term.
    • add

      public SparsePoly add(Poly q) throws NullPointerException
      Description copied from interface: Poly
      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 SparsePoly mul(Poly q) throws NullPointerException
      Description copied from interface: Poly
      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 SparsePoly minus()
      Description copied from interface: Poly
      Returns the negated polynomial.

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

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

      public Iterator<Poly.Term> iterator()