Class DensePoly
java.lang.Object
it.unimi.di.prog2.h09.DensePoly
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final int[]The array of coefficients, thecoeff[i]is the coefficient of \( x^i \). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionPerforms polynomial addition.intcoeff(int d) Returns the coefficient of the term of given exponent.intdegree()Returns the degree of this polynomial.booleanisZero()Checks whether this polynomial is the zero polynomial.minus()Returns the negate polynomial.Performs polynomial multiplication.Performs polynomial subtraction.
-
Field Details
-
coefficient
private final int[] coefficientThe array of coefficients, thecoeff[i]is the coefficient of \( x^i \).
-
-
Constructor Details
-
DensePoly
public DensePoly()Initializes this to be the zero polynomial, that is \( p = 0 \). -
DensePoly
Initializes this to be the polynomial \(p = cx^n\).- Parameters:
c- the coefficient.n- the degree.- Throws:
IllegalArgumentException- ifn< 0.
-
DensePoly
private DensePoly(int n) Initializes a polynomial of given degree (with all coefficients equal to 0).- Parameters:
n- the degree, must be non-negative.
-
-
Method Details
-
degree
public int degree()Returns the degree of this polynomial.The degree is defined as the largest exponent with a non-zero coefficient, except for the zero polynomial, whose degree is defined to be 0.
- Returns:
- the largest exponent with a non-zero coefficient; returns 0 if this is the zero
Poly.
-
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.
-
isZero
public boolean isZero()Checks whether this polynomial is the zero polynomial.- Returns:
trueif this polynomial is the zero polynomial,falseotherwise.
-
add
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- ifqisnull.
-
mul
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- ifqisnull.
-
sub
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- ifqisnull.
-
minus
Returns the negate polynomial.If \( p \) is this polynomial, returns \( -p \).
- Returns:
- this polynomial multiplied by \( -1 \).
-