Package it.unimi.di.prog2.h19
Class SparsePoly
java.lang.Object
it.unimi.di.prog2.h19.AbstractPoly
it.unimi.di.prog2.h19.SparsePoly
A sparse implementation of
Poly
.-
Nested Class Summary
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionInitializes this to be the zero polynomial, that is \( p = 0 \).SparsePoly
(int coeff, int degree) Initializes this to be the polynomial \(p = cx^n\).private
SparsePoly
(List<Poly.Term> terms) Initializes this to be the polynomial with given terms and degree. -
Method Summary
Modifier and TypeMethodDescriptionPerforms polynomial addition.private static void
Adds a term to the list.int
coeff
(int degree) Returns the coefficient of the term of given exponent.private static int
findByDegree
(List<Poly.Term> terms, int degree) Finds the index of a term of given degree.iterator()
minus()
Returns the negated polynomial.Performs polynomial multiplication.Methods inherited from class it.unimi.di.prog2.h19.AbstractPoly
degree, equals, hashCode, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
terms
The list of terms.
-
-
Constructor Details
-
SparsePoly
public SparsePoly()Initializes this to be the zero polynomial, that is \( p = 0 \). -
SparsePoly
Initializes this to be the polynomial with given terms and degree.- Parameters:
terms
- must satisfy the RI (this is a partial method).
-
SparsePoly
Initializes this to be the polynomial \(p = cx^n\).- Parameters:
coeff
- the coefficient.degree
- the degree.- Throws:
IllegalArgumentException
- ifn
< 0.
-
-
Method Details
-
findByDegree
Finds the index of a term of given degree.- Parameters:
terms
- a notnull
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
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 notnull
list of terms in increasing degree order.term
- the term.
-
add
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
- ifq
isnull
.
-
mul
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
- ifq
isnull
.
-
minus
Description copied from interface:Poly
Returns the negated polynomial.If \( p \) is this polynomial, returns \( -p \).
- Returns:
- this polynomial multiplied by \( -1 \).
-
iterator
-