public class Polynomial extends TreeMap<Integer,Double>
p(x) = a0 + a1 x + a2 x2 + ... + an xn
where a0, a1, ..., an ∈ ℝ, and an ǂ 0. Then n is called the degree of the polynomial. Internally, a polynomial is represented by a sorted map, where the key represents the unique exponent and the value the respective coefficient, i.e., is given by the map[<n,an>, ..., <0,a0>]
The default comparator for the exponents, i.e., the keys of this TreeMap, isExponentComparator
, with a descending order.
The simplest way to create a polynomial
is given by the following code snippet:
Polynomial p = new Polynomial(); p.put(1023, 1.0); p.put(2, 3.5); p.put(1, 3.); p.put(0, -1.);Here the order of put instructions is arbitrary. This object then represents the polynomial
p(x) = x1023 - 3.5x2 + 3x - 1
Additionally, this class provides some easy to use and comparably fast static methods for polynomial operations where a polynomial is represented by an array of double values. The principle of this representation is as follows: The element at index i of the array denotes the coefficient ai of the polynomial, and the length of the array is >= the degree of the polynomial. If the degree of the involved polynomials is not too large, these method are fast.PolynomialZ
,
Serialized FormAbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
Polynomial()
Creates an empty polynomial with a new
ExponentComparator . |
Polynomial(ExponentComparator ec)
Creates an empty polynomial with the given
ExponentComparator . |
Modifier and Type | Method and Description |
---|---|
int |
deg()
Returns the degree of this polynomial.
|
static int |
deg(double[] p)
Determines the degree of the polynomial represented by the given array.
|
static double[][] |
divide(double[] u,
double[] v)
Comparably fast method which returns an array {q,r} of two arrays
representing the quotient q of the two given polynomials u/v
and the remainder r.
|
Polynomial[] |
divide(Polynomial v)
Divides this polynomial by the given polynomial v
and returns an array {q,r} holding the quotient q as the first entry
and the remainder r as the second entry.
|
static double[] |
multiply(double[] p,
double[] q)
Comparably fast method which returns an array representing the polynomial given
by the polynomial product of p and q.
|
Polynomial |
multiply(Polynomial q)
Multiplies this polynomial with the given polynomial q.
|
Double |
put(Integer exponent,
Double coefficient)
Adds the term +ae xe to this
polynomial.
|
String |
toString()
Returns a string representation of this polynomial.
|
static String |
toString(double[] p)
Returns a string representation of the given polynomial.
|
ceilingEntry, ceilingKey, clear, clone, comparator, containsKey, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, forEach, get, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, navigableKeySet, pollFirstEntry, pollLastEntry, putAll, remove, replace, replace, replaceAll, size, subMap, subMap, tailMap, tailMap, values
equals, hashCode, isEmpty
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove
public Polynomial()
ExponentComparator
.public Polynomial(ExponentComparator ec)
ExponentComparator
.ec
- the exponent comparatorpublic static double[] multiply(double[] p, double[] q)
multiply(Polynomial)
.p
- array representing the first polynomialq
- array representing the second polyniomialdivide(double[],double[])
public static double[][] divide(double[] u, double[] v)
divide(Polynomial)
.u
- array representing the first polynomialv
- array representing the second polyniomialmultiply(double[],double[])
public static int deg(double[] p)
multiply(double[],double[])
By definition, an array consisting only of zeros represents a polynomial of degree zero.p
- array representing the polynomialdeg()
public static String toString(double[] p)
multiply(double[],double[])
p
- array representing a polynomial ptoString()
public Double put(Integer exponent, Double coefficient)
put
in interface Map<Integer,Double>
put
in class TreeMap<Integer,Double>
exponent
- the exponent e in the term ae xecoefficient
- the coefficient ae in the term ae xepublic Polynomial multiply(Polynomial q)
q
- the polynomial to be multiplied with this polynomialmultiply(double[],double[])
public Polynomial[] divide(Polynomial v)
v
- the polynomial to divide this polynomialdivide(double[],double[])
public int deg()
deg(double[])
public String toString()
toString
in class AbstractMap<Integer,Double>
toString(double[])