# Polynomial evaluation

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(x_{1},x_{2})=2x_{1}x_{2}+x_{1}^{3}+4$ at $x_{1}=2,x_{2}=3$ consists of computing $P(2,3)=2\cdot 2\cdot 3+2^{3}+4=24.$ See also Polynomial ring § Polynomial evaluation
For evaluating the univariate polynomial $a_{n}x^{n}+a_{n-1}x^{n-1}+\cdots +a_{0},$ the most naive method would use $n$ multiplications to compute $a_{n}x^{n}$, use $n-1$ multiplications to compute $a_{n-1}x^{n-1}$ and so on for a total of ${\tfrac {n(n+1)}{2}}$ multiplications and $n$ additions. Using better methods, such as Horner's rule, this can be reduced to $n$ multiplications and $n$ additions. If some preprocessing is allowed, even more savings are possible.

## Background

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.

## General methods
### Horner's rule

Horner's method evaluates a polynomial using repeated bracketing: ${\begin{aligned}a_{0}+&a_{1}x+a_{2}x^{2}+a_{3}x^{3}+\cdots +a_{n}x^{n}\\&=a_{0}+x{\bigga_{1}+x{\Biga_{2}+x{\biga_{3}+\cdots +x(a_{n-1}+x\,a_{n})\cdots {\big )}{\Big )}{\bigg )}.\end{aligned}}$ 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.

#### Multivariate

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+2x^{2}y+x^{2}y^{2}$
can be written as
    ${\begin{aligned}P(x,y)&=4+x(1+y(2)+x(y(2+y)))\quad {\text{or}}\\P(x,y)&=4+x+y(x(2+x(2))+y(x^{2})).\end{aligned}}$
An efficient version of this approach was described by Carnicer and Gasca.

#### 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:
${\begin{aligned}P(x)=(a_{0}+a_{1}x)+(a_{2}+a_{3}x)x^{2}+((a_{4}+a_{5}x)+(a_{6}+a_{7}x)x^{2})x^{4}.\end{aligned}}$
Combined by Exponentiation by squaring, this allows parallelizing the computation.

### Evaluation with preprocessing

Arbitrary polynomials can be evaluated with fewer operations than Horner's rule requires if we first "preprocess" the coefficients $a_{n},\dots ,a_{0}$.
An example was first given by Motzkin who noted that
    $P(x)=x^{4}+a_{3}x^{3}+a_{2}x^{2}+a_{1}x+a_{0}$
can be written as
    $y=(x+\beta _{0})x+\beta _{1},\quad P(x)=(y+x+\beta _{2})y+\beta _{3},$
where the values $\beta _{0},\dots ,\beta _{3}$ are computed in advanced, based on $a_{0},\dots ,a_{3}$. Motzkin's method uses just 3 multiplications compared to Horner's 4.
The values for each $\beta _{i}$ can be easily computed by expanding $P(x)$ and equating the coefficients:
    ${\begin{aligned}\beta _{0}&={\tfrac {1}{2}}(a_{3}-1),\quad &z&=a_{2}-\beta _{0}(\beta _{0}+1),\quad &\beta _{1}&=a_{1}-\beta _{0}z,\\\beta _{2}&=z-2\beta _{1},\quad &\beta _{3}&=a_{0}-\beta _{1}(\beta _{1}+\beta _{2}).\end{aligned}}$

#### Example

To compute the Taylor expansion $\exp(x)\approx 1+x+x^{2}/2+x^{3}/6+x^{4}/24$, we can upscale by a factor 24, apply the above steps, and scale back down. That gives us the three multiplication computation
    $y=(x+1.5)x+11.625,\quad P(x)=(y+x-15)y/24+2.63477.$
Improving over the equivalent Horner form (that is $P(x)=1+x(1+x(1/2+x(1/6+x/24)))$) by 1 multiplication.
Some general methods include the Knuth–Eve algorithm and the Rabin–Winograd algorithm.

### Multipoint evaluation

Evaluation of a degree-n polynomial $P(x)$ at multiple points $x_{1},\dots ,x_{m}$ can be done with $mn$ multiplications by using Horner's method $m$ times. Using the above preprocessing approach, this can be reduced by a factor of two; that is, to $mn/2$ multiplications.
However, it is possible to do better and reduce the time requirement to just $O{\big(n+m)\log ^{2}(n+m){\big )}$. The idea is to define two polynomials that are zero in respectively the first and second half of the points: $m_{0}(x)=(x-x_{1})\cdots (x-x_{n/2})$ and $m_{1}(x)=(x-x_{n/2+1})\cdots (x-x_{n})$. We then compute $R_{0}=P{\bmod {m}}_{0}$ and $R_{1}=P{\bmod {m}}_{1}$ using the Polynomial remainder theorem, which can be done in $O(n\log n)$ time using a fast Fourier transform. This means $P(x)=Q(x)m_{0}(x)+R_{0}(x)$ and $P(x)=Q(x)m_{1}(x)+R_{1}(x)$ by construction, where $R_{0}$ and $R_{1}$ are polynomials of degree at most $n/2$. Because of how $m_{0}$ and $m_{1}$ were defined, we have
    ${\begin{aligned}R_{0}(x_{i})&=P(x_{i})\quad {\text{for }}i\leq n/2\quad {\text{and}}\\R_{1}(x_{i})&=P(x_{i})\quad {\text{for }}i>n/2.\end{aligned}}$
Thus to compute $P$ on all $n$ of the $x_{i}$, it suffices to compute the smaller polynomials $R_{0}$ and $R_{1}$ on each half of the points. This gives us a divide-and-conquer algorithm with $T(n)=2T(n/2)+n\log n$, which implies $T(n)=O(n(\log n)^{2})$ by the master theorem.
In the case where the points in which we wish to evaluate the polynomials have some structure, simpler methods exist. For example, Knuth section 4.6.4 gives a method for tabulating polynomial values of the type
    $P(x_{0}+h),P(x_{0}+2h),\dots .$

### Dynamic evaluation

In the case where $x_{1},\dots ,x_{m}$ are not known in advance, Kedlaya and Umans gave a data structure for evaluating polynomials over a finite field of size $F_{q}$ in time $(\log n)^{O(1)}(\log _{2}q)^{1+o(1)}$ per evaluation after some initial preprocessing. This was shown by Larsen to be essentially optimal.
The idea is to transform $P(x)$ of degree $n$ into a multivariate polynomial $f(x_{1},x_{2},\dots ,x_{m})$, such that $P(x)=f(x,x^{d},x^{d^{2}},\dots ,x^{d^{m}})$ and the individual degrees of $f$ is at most $d$. Since this is over ${\bmod {q}}$, the largest value $f$ can take (over $\mathbb {Z}$) is $M=d^{m}(q-1)^{dm}$. Using the Chinese remainder theorem, it suffices to evaluate $f$ modulo different primes $p_{1},\dots ,p_{\ell }$ with a product at least $M$. Each prime can be taken to be roughly $\log M=O(dm\log q)$, and the number of primes needed, $\ell$, is roughly the same. Doing this process recursively, we can get the primes as small as $\log \log q$. That means we can compute and store $f$ on all the possible values in $T=(\log \log q)^{m}$ time and space. If we take $d=\log q$, we get $m={\tfrac {\log n}{\log \log q}}$, so the time/space requirement is just $n^{\frac {\log \log q}{\log \log \log q}}.$
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. It is also possible to use fast matrix multiplication algorithms for polynomial evaluation. .

## Specific polynomials

While general polynomials require $\Omega (n)$ operations to evaluate, some polynomials can be computed much faster. For example, the polynomial $P(x)=x^{2}+2x+1$ can be computed using just one multiplication and one addition since $P(x)=(x+1)^{2}$

### Evaluation of powers

A particularly interesting type of polynomial is powers like $x^{n}$. Such polynomials can always be computed in $O(\log n)$ operations. Suppose, for example, that we need to compute $x^{16}$; we could simply start with $x$ and multiply by $x$ to get $x^{2}$. We can then multiply that by itself to get $x^{4}$ and so on to get $x^{8}$ and $x^{16}$ in just four multiplications. Other powers like $x^{5}$ can similarly be computed efficiently by first computing $x^{4}$ by 2 multiplications and then multiplying by $x$.
The most efficient way to compute a given power $x^{n}$ is provided by addition-chain exponentiation. However, this requires designing a specific algorithm for each exponent, and the computation needed for designing these algorithms are difficult (NP-complete), so exponentiation by squaring is generally preferred for effective computations.

### Polynomial families

Often polynomials show up in a different form than the well known $a_{n}x^{n}+\dots +a_{1}x+a_{0}$. For polynomials in Chebyshev form we can use Clenshaw algorithm. For polynomials in Bézier form we can use De Casteljau's algorithm, and for B-splines there is De Boor's algorithm.

### Hard polynomials

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 that the polynomial
    $P(x)=\sum _{k=0}^{n}2^{2^{kn^{3}}}x^{k}$
cannot be evaluated with less than ${\tfrac {1}{2}}n-2$ multiplications and $n-4$ additions. At least this bound holds if only operations of those types are allowed, giving rise to a so-called "polynomial chain of length $<n^{2}/\log n$".
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/\log n)$ multiplications.
For other simple polynomials, the complexity is unknown. The polynomial $(x+1)(x+2)\cdots (x+n)$ is conjectured to not be computable in time $(\log n)^{c}$ for any $c$. This is supported by the fact that, if it can be computed fast, then integer factorization can be computed in polynomial time, breaking the RSA cryptosystem.

## Matrix polynomials

Sometimes the computational cost of scalar multiplications (like $ax$) is less than the computational cost of "non scalar" multiplications (like $x^{2}$). The typical example of this is matrices. If $M$ is an $m\times m$ matrix, a scalar multiplication $aM$ takes about $m^{2}$ arithmetic operations, while computing $M^{2}$ takes about $m^{3}$ (or $m^{2.3}$ using fast matrix multiplication).
Matrix polynomials are important for example for computing the Matrix Exponential.
Paterson and Stockmeyer showed how to compute a degree $n$ polynomial using only $O({\sqrt {n}})$ non scalar multiplications and $O(n)$ scalar multiplications. Thus a matrix polynomial of degree n can be evaluated in $O(m^{2.3}{\sqrt {n}}+m^{2}n)$ time. If $m=n$ this is $O(m^{3})$, as fast as one matrix multiplication with the standard algorithm.
This method works as follows: For a polynomial
    $P(M)=a_{n-1}M^{n-1}+\dots +a_{1}M+a_{0}I,$
let k be the least integer not smaller than ${\sqrt {n}}.$ The powers $M,M^{2},\dots ,M^{k}$ are computed with $k$ matrix multiplications, and $M^{2k},M^{3k},\dots ,M^{k^{2}-k}$ are then computed by repeated multiplication by $M^{k}.$ Now,
    ${\begin{aligned}P(M)=&\,(a_{0}I+a_{1}M+\dots +a_{k-1}M^{k-1})\\+&\,(a_{k}I+a_{k+1}M+\dots +a_{2k-1}M^{k-1})M^{k}\\+&\,\dots \\+&\,(a_{n-k}I+a_{n-k+1}M+\dots +a_{n-1}M^{k-1})M^{k^{2}-k},\end{aligned}}$,
where $a_{i}=0$ for i ≥ n. This requires just $k$ more non-scalar multiplications.
We can write this succinctly using the Kronecker product:
    $P(M)={\begin{bmatrix}I\\M\\\vdots \\M^{k-1}\end{bmatrix}}^{T}\left({\begin{bmatrix}a_{0}&a_{1}&a_{2}&\dots \\a_{k}&a_{k+1}&\ddots \\a_{2k}&\ddots \\\vdots \end{bmatrix}}\otimes I\right){\begin{bmatrix}I\\M^{k}\\M^{2k}\\\vdots \end{bmatrix}}$.
The direct application of this method uses $2{\sqrt {n}}$ non-scalar multiplications, but combining it with Evaluation with preprocessing, Paterson and Stockmeyer show you can reduce this to ${\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.