Package it.unimi.di.prog2.h08
Class Poly
java.lang.Object
it.unimi.di.prog2.h08.Poly
Poly
s 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 \).
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionPerforms polynomial addition.int
coeff
(int d) Returns the coefficient of the term of given exponent.int
degree()
Returns the degree of this polynomial.minus()
Returns the negate polynomial.static Poly
monomialWithCoeffAndDegree
(int c, int n) A factory method returning a monomial.Performs polynomial multiplication.Performs polynomial subtraction.
-
Constructor Details
-
Poly
public Poly()Initializes this to be the zero polynomial, that is \( p = 0 \). -
Poly
Initializes this to be the polynomial \(p = cx^n\).- Parameters:
c
- the coefficient.n
- the degree.- Throws:
NegativeExponentException
- ifn
< 0.
-
-
Method Details
-
monomialWithCoeffAndDegree
A factory method returning a monomial. (see EJ 2.1)- Parameters:
c
- the coefficient.n
- the degree.- Returns:
- the monomial, if
n
>= 0. - Throws:
NegativeExponentException
- ifn
< 0.
-
degree
public int degree()Returns the degree of this polynomial.- 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.
-
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
- ifq
isnull
.
-
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
- ifq
isnull
.
-
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
- ifq
isnull
.
-
minus
Returns the negate polynomial.If \( p \) is this polynomial, returns \( -p \).
- Returns:
- this polynomial multiplied by \( -1 \).
-