Algorithm Implementation/Mathematics/Polynomial evaluation

Horner's method allows the efficient computation of the value of $p(x_{0})$ for any polynomial $p(x)$ = $a_{0} + a_{1}x + ... + a_{n}x^{n}$ and value $x_{0}$.

Algorithm
Rather than the naive method of computing each term individually, in Horner's method you rewrite the polynomial as $p(x)$ = $a_{0} + a_{1}x + ... + a_{n}x^{n}$ = $a_{0} + x(a_{1} + x(a_{2} + ... x(a_{n}))...))$, and then use a recursive method to compute its value for a specific $x_{0}$. The result is an algorithm requiring $n$ multiplies, rather than the 2$n$ multiplies needed by the best variant of the naive approach (and much more if each $x^{i}$ is computed separately).

In both the pseudocode and each implementation below, the polynomial $p(x)$ is represented as an array of it's coefficients.

Pseudocode
input: ($a_{0}$, ..., $a_{n}$) input: $x_{0}$ output: $p(x_{0})$

accum := 0 for i = n, n-1, n-2, ..., 2, 1, 0 accum := $x_{0}$(accum + $a_{i}$) return accum

Variants
The Compensated Horner Scheme is a variant of Horner's Algorithm that compensates for floating-point inaccuracies. It takes 1.5x the time of normal Horner to calculate for 2x the accuracy.

Loop unrolling can be used with Horner's Scheme to evalulate multiple multiplications at once. This is called a kth-order Horner.

There are also non-Horner schemes for evalulating polynomials: the naive method, the Estrin method, and factorization.