类 Polynomial
- java.lang.Object
-
- edu.princeton.cs.algs4.Polynomial
-
public class Polynomial extends java.lang.Object
ThePolynomial
class represents a polynomial with integer coefficients. Polynomials are immutable: their values cannot be changed after they are created. It includes methods for addition, subtraction, multiplication, composition, differentiation, and evaluation.For additional documentation, see Section 9.9 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
构造器概要
构造器 构造器 说明 Polynomial(int a, int b)
Initializes a new polynomial a x^b
-
方法概要
修饰符和类型 方法 说明 int
compareTo(Polynomial that)
Compares two polynomials by degree, breaking ties by coefficient of leading term.Polynomial
compose(Polynomial that)
Returns the composition of this polynomial and the specified polynomial.int
degree()
Returns the degree of this polynomial.Polynomial
differentiate()
Returns the result of differentiating this polynomial.boolean
equals(java.lang.Object other)
Compares this polynomial to the specified polynomial.int
evaluate(int x)
Returns the result of evaluating this polynomial at the point x.static void
main(java.lang.String[] args)
Unit tests the polynomial data type.Polynomial
minus(Polynomial that)
Returns the result of subtracting the specified polynomial from this polynomial.Polynomial
plus(Polynomial that)
Returns the sum of this polynomial and the specified polynomial.Polynomial
times(Polynomial that)
Returns the product of this polynomial and the specified polynomial.java.lang.String
toString()
Return a string representation of this polynomial.
-
-
-
方法详细资料
-
degree
public int degree()
Returns the degree of this polynomial.- 返回:
- the degree of this polynomial, -1 for the zero polynomial.
-
plus
public Polynomial plus(Polynomial that)
Returns the sum of this polynomial and the specified polynomial.- 参数:
that
- the other polynomial- 返回:
- the polynomial whose value is
(this(x) + that(x))
-
minus
public Polynomial minus(Polynomial that)
Returns the result of subtracting the specified polynomial from this polynomial.- 参数:
that
- the other polynomial- 返回:
- the polynomial whose value is
(this(x) - that(x))
-
times
public Polynomial times(Polynomial that)
Returns the product of this polynomial and the specified polynomial. Takes time proportional to the product of the degrees. (Faster algorithms are known, e.g., via FFT.)- 参数:
that
- the other polynomial- 返回:
- the polynomial whose value is
(this(x) * that(x))
-
compose
public Polynomial compose(Polynomial that)
Returns the composition of this polynomial and the specified polynomial. Takes time proportional to the product of the degrees. (Faster algorithms are known, e.g., via FFT.)- 参数:
that
- the other polynomial- 返回:
- the polynomial whose value is
(this(that(x)))
-
equals
public boolean equals(java.lang.Object other)
Compares this polynomial to the specified polynomial.- 覆盖:
equals
在类中java.lang.Object
- 参数:
other
- the other polynoimal- 返回:
true
if this polynomial equalsother
;false
otherwise
-
differentiate
public Polynomial differentiate()
Returns the result of differentiating this polynomial.- 返回:
- the polynomial whose value is
this'(x)
-
evaluate
public int evaluate(int x)
Returns the result of evaluating this polynomial at the point x.- 参数:
x
- the point at which to evaluate the polynomial- 返回:
- the integer whose value is
(this(x))
-
compareTo
public int compareTo(Polynomial that)
Compares two polynomials by degree, breaking ties by coefficient of leading term.- 参数:
that
- the other point- 返回:
- the value
0
if this polynomial is equal to the argument polynomial (precisely whenequals()
returnstrue
); a negative integer if this polynomialt is less than the argument polynomial; and a positive integer if this polynomial is greater than the argument point
-
toString
public java.lang.String toString()
Return a string representation of this polynomial.- 覆盖:
toString
在类中java.lang.Object
- 返回:
- a string representation of this polynomial in the format 4x^5 - 3x^2 + 11x + 5
-
main
public static void main(java.lang.String[] args)
Unit tests the polynomial data type.- 参数:
args
- the command-line arguments (none)
-
-