In mathematics and computer science, polynomial evaluation refers to computation of the value of a polynomial when its indeterminates are substituted for some values. In other words, evaluating the polynomial
P(x1,x2)=2x1x2+
3 | |
x | |
1 |
+4
x1=2,x2=3
P(2,3)=2 ⋅ 2 ⋅ 3+23+4=24.
n+a | |
a | |
n-1 |
xn-1+ … +a0,
n
anxn
n-1
an-1xn-1
\tfrac{n(n+1)}{2}
n
n
n
This problem arises frequently in practice. In computational geometry, polynomials are used to compute function approximations using Taylor polynomials. In cryptography and hash tables, polynomials are used to compute k-independent hashing.
In the former case, polynomials are evaluated using floating-point arithmetic, which is not exact. Thus different schemes for the evaluation will, in general, give slightly different answers. In the latter case, the polynomials are usually evaluated in a finite field, in which case the answers are always exact.
See also: Horner's method.
Horner's method evaluates a polynomial using repeated bracketing:This method reduces the number of multiplications and additions to just
n
Horner's method is so common that a computer instruction "multiply–accumulate operation" has been added to many computer processors, which allow doing the addition and multiplication operations in one combined step.
If the polynomial is multivariate, Horner's rule can be applied recursively over some ordering of the variables.E.g.
P(x,y)=4+x+2xy+2x2y+x2y2
\begin{align} P(x,y)&=4+x(1+y(2)+x(y(2+y))) or\\ P(x,y)&=4+x+y(x(2+x(2))+y(x2)). \end{align}
An efficient version of this approach was described by Carnicer and Gasca.[1]
See also: Estrin's scheme.
While it's not possible to do less computation than Horner's rule (without preprocessing), on modern computers the order of evaluation can matter a lot for the computational efficiency.A method known as Estrin's scheme computes a (single variate) polynomial in a tree like pattern:
Combined by Exponentiation by squaring, this allows parallelizing the computation.
Arbitrary polynomials can be evaluated with feweroperations than Horner's rule requires if we first "preprocess"the coefficients
an,...,a0
An example was first given by Motzkin[2] who noted that
P(x)=x4+a3x3+a2x2+a1x+a0
y=(x+\beta0)x+\beta1, P(x)=(y+x+\beta2)y+\beta3,
\beta0,...,\beta3
a0,...,a3
The values for each
\betai
P(x)
\begin{align} \beta0&=\tfrac12(a3-1), &z&=a2-\beta0(\beta0+1), &\beta1&=a1-\beta0z,\\ \beta2&=z-2\beta1, &\beta3&=a0-\beta1(\beta1+\beta2).\end{align}
\exp(x) ≈ 1+x+x2/2+x3/6+x4/24
y=(x+1.5)x+11.625, P(x)=(y+x-15)y/24+2.63477.
P(x)=1+x(1+x(1/2+x(1/6+x/24)))
Some general methods include the Knuth–Eve algorithm and the Rabin–Winograd algorithm.
Evaluate of a
n
P(x)
x1,...,xm
mn
m
mn/2
It is possible to reduce the time requirement to just
O((n+m)log2(n+m))
m0(x)=(x-x1) … (x-xn/2)
m1(x)=(x-xn/2+1) … (x-xn)
R0=P\bmodm0
R1=P\bmodm1
O(nlogn)
P(x)=Q(x)m0(x)+R0(x)
P(x)=Q(x)m1(x)+R1(x)
R0
R1
n/2
m0
m1
\begin{align} R0(xi)&=P(xi) fori\len/2 and\\ R1(xi)&=P(xi) fori>n/2. \end{align}
P
n
xi
R0
R1
T(n)=2T(n/2)+nlogn
T(n)=O(n(logn)2)
In the case where the points in which we wish to evaluate the polynomials have some structure, simpler methods exist.For example, Knuth[4] section 4.6.4gives a method for tabulating polynomial values of the type
P(x0+h),P(x0+2h),....
In the case where
x1,...,xm
Fq
(logn)O(1)(log2q)1+o(1)
The idea is to transform
P(x)
n
f(x1,x2,...,xm)
P(x)=f(x,xd,
d2 | |
x |
,...,
dm | |
x |
)
f
d
\bmodq
f
Z
M=dm(q-1)dm
f
p1,...,p\ell
M
logM=O(dmlogq)
\ell
loglogq
f
T=(loglogq)m
d=logq
m=\tfrac{logn}{loglogq}
| ||||
n |
.
Kedlaya and Umans further show how to combine this preprocessing with fast (FFT) multipoint evaluation.This allows optimal algorithms for many important algebraic problems, such as polynomial modular composition.
While general polynomials require
\Omega(n)
P(x)=x2+2x+1
P(x)=(x+1)2
See main article: Exponentiation by squaring and Addition-chain exponentiation.
A particularly interesting type of polynomial is powers like
xn
O(logn)
x16
x
x
x2
x4
x8
x16
x5
x4
x
The most efficient way to compute a given power
xn
Often polynomials show up in a different form than the well known
anxn+...+a1x+a0
The fact that some polynomials can be computed significantly faster than "general polynomials" suggests the question: Can we give an example of a simple polynomial that cannot be computed in time much smaller than its degree?Volker Strassen has shown[8] that the polynomial
n | |
P(x)=\sum | |
k=0 |
| |||||
2 |
xk
\tfrac12n-2
n-4
<n2/logn
The polynomial given by Strassen has very large coefficients, but by probabilistic methods, one can show there must exist even polynomials with coefficients just 0's and 1's such that the evaluation requires at least
\Omega(n/logn)
For other simple polynomials, the complexity is unknown.The polynomial
(x+1)(x+2) … (x+n)
(logn)c
c
Sometimes the computational cost of scalar multiplications (like
ax
x2
M
m x m
aM
m2
M2
m3
m2.3
Matrix polynomials are important for example for computing the Matrix Exponential.
Paterson and Stockmeyer [10] showed how to compute a degree
n
O(\sqrtn)
O(n)
O(m2.3\sqrt{n}+m2n)
m=n
O(m3)
This method works as follows: For a polynomial
P(M)=an-1Mn-1+...+a1M+a0I,
\sqrt{n}.
M,M2,...,Mk
k
M2k,M3k,...,
k2-k | |
M |
Mk.
\begin{align}P(M)= &(a0I+a1M+...+ak-1Mk-1) \\+&(akI+ak+1M+...+a2k-1Mk-1
k \\+&... \\+&(a | |
)M | |
n-k |
I+an-k+1M+...+an-1Mk-1
k2-k | |
)M |
, \end{align}
ai=0
k
We can write this succinctly using the Kronecker product:
P(M)= \begin{bmatrix}I\\M\\\vdots\\Mk-1
T \left(\begin{bmatrix} a | |
\end{bmatrix} | |
0 |
&a1&a2&...\\ ak&ak+1&\ddots\\ a2k&\ddots\\ \vdots\end{bmatrix} ⊗ I\right) \begin{bmatrix}I\\Mk\\M2k\\\vdots\end{bmatrix}
The direct application of this method uses
2\sqrt{n}
\sqrt{2n}
Methods based on matrix polynomial multiplications and additions have been proposed allowing to save nonscalar matrix multiplications with respect to the Paterson-Stockmeyer method.[11]