Class SparsePoly

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

public class SparsePoly extends AbstractPoly
A sparse implementation of non-zero Polys.
  • Field Details

  • Constructor Details

    • 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).
  • Method Details

    • copyOf

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

      public static SparsePoly 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.
    • ofTerms

      public static Poly ofTerms(List<Poly.Term> terms)
      Creates a polynomial given a list of terms.

      The list of terms can be empty, and can contain terms in any order (even with the same degree). Terms with the same degree are added and, if the resulting list is empty, the zero polynomial is returned.

      Parameters:
      terms - the list of terms.
      Returns:
      the polynomial.
      Throws:
      NullPointerException - if terms is or contains null.
    • ofTerms

      public static Poly ofTerms(Poly.Term... terms)
      Creates a polynomial given its terms.

      The array of terms can be empty, and can contain terms in any order (even with the same degree). Terms with the same degree are added and, if the resulting list is empty, the zero polynomial is returned.

      Parameters:
      terms - the array of terms.
      Returns:
      the polynomial.
      Throws:
      NullPointerException - if terms is or contains 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.
    • 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.
    • 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 summed (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 Poly 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 Poly 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()