The mathIT Library
A Java™ API for mathematics
org.mathIT.algebra

## Class Polynomial

• All Implemented Interfaces:
Serializable, Cloneable, Map<Integer,Double>, NavigableMap<Integer,Double>, SortedMap<Integer,Double>

```public class Polynomial
extends TreeMap<Integer,Double>```
This class enables to generate objects representing polynomials with real coefficients. A polynomial with real coefficients has the form

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, is `ExponentComparator`, 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.
This class requires JDK 5 or higher.
Version:
1.1
Author:
Andreas de Vries
`PolynomialZ`, Serialized Form

• ### Nested classes/interfaces inherited from class java.util.AbstractMap

`AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>`
• ### Method Summary

All Methods
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.
• ### Methods inherited from class java.util.TreeMap

`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`
• ### Methods inherited from class java.util.AbstractMap

`equals, hashCode, isEmpty`
• ### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`
• ### Methods inherited from interface java.util.Map

`compute, computeIfAbsent, computeIfPresent, equals, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove`
• ### Constructor Detail

• #### Polynomial

`public Polynomial(ExponentComparator ec)`
Creates an empty polynomial with the given `ExponentComparator`.
Parameters:
`ec` - the exponent comparator
• ### Method Detail

• #### multiply

```public 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. The principle 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, this method is fast and easy to use. Compare this static method to the object method `multiply(Polynomial)`.
Parameters:
`p` - array representing the first polynomial
`q` - array representing the second polyniomial
Returns:
the product pq of the polynomials p and q
`divide(double[],double[])`
• #### divide

```public 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. The principle of the array 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, this method is fast and easy to use. Compare this static method to the object method `divide(Polynomial)`.
Parameters:
`u` - array representing the first polynomial
`v` - array representing the second polyniomial
Returns:
the array {q,r} where q is the quotient polynomial u/v, and r is the remainder polynomial such that u = qv + r
`multiply(double[],double[])`
• #### deg

`public static int deg(double[] p)`
Determines the degree of the polynomial represented by the given array. The degree is defined as the maximum exponent of the polynomial. For the principle how an array represents a polynomial, see `multiply(double[],double[])` By definition, an array consisting only of zeros represents a polynomial of degree zero.
Parameters:
`p` - array representing the polynomial
Returns:
the degree of p
`deg()`
• #### toString

`public static String toString(double[] p)`
Returns a string representation of the given polynomial. For the principle how an array represents a polynomial, see `multiply(double[],double[])`
Parameters:
`p` - array representing a polynomial p
Returns:
a string representation of the polynomial p
`toString()`
• #### put

```public Double put(Integer exponent,
Double coefficient)```
Adds the term +ae xe to this polynomial. Here ae may be negative, whereas e must be a non-negative integer.
Specified by:
`put` in interface `Map<Integer,Double>`
Overrides:
`put` in class `TreeMap<Integer,Double>`
Parameters:
`exponent` - the exponent e in the term ae xe
`coefficient` - the coefficient ae in the term ae xe
Returns:
the previous coefficient associated with the exponent, or null if there did not exist a term with the exponent e in the polynomial.
• #### multiply

`public Polynomial multiply(Polynomial q)`
Multiplies this polynomial with the given polynomial q.
Parameters:
`q` - the polynomial to be multiplied with this polynomial
Returns:
the product of this polynomial times q
`multiply(double[],double[])`
• #### divide

`public 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.
Parameters:
`v` - the polynomial to divide this polynomial
Returns:
the array {q,r} where q is the quotient of this polynomial, say u, over v, and r is the remainder polynomial such that u = qv + r
`divide(double[],double[])`
• #### deg

`public int deg()`
Returns the degree of this polynomial. The degree is defined as the maximum exponent of the polynomial. Since this polynomial is sorted with respect to the exponents in descending order, the first key of this map is the degree. By definition, an empty polynomial has degree zero.
Returns:
the degree of this polynomial
`deg(double[])`
• #### toString

`public String toString()`
Returns a string representation of this polynomial.
Overrides:
`toString` in class `AbstractMap<Integer,Double>`
Returns:
a string representation of this polynomial
`toString(double[])`