# Estrin's scheme

In numerical analysis, Estrin's scheme (after Gerald Estrin), also known as Estrin's method, is an algorithm for numerical evaluation of polynomials.
Horner's method for evaluation of polynomials is one of the most commonly used algorithms for this purpose, and unlike Estrin's scheme it is optimal in the sense that it minimizes the number of multiplications and additions required to evaluate an arbitrary polynomial. On a modern processor, instructions that do not depend on each other's results may run in parallel. Horner's method contains a series of multiplications and additions that each depend on the previous instruction and so cannot execute in parallel. Estrin's scheme is one method that attempts to overcome this serialization while still being reasonably close to optimal.

## Description of the algorithm

Estrin's scheme operates recursively, converting a degree-n polynomial in x (for n≥2) to a degree-⌊n/2⌋ polynomial in x$^2$ using ⌈n/2⌉ independent operations (plus one to compute x$^2$).
Given an arbitrary polynomial P(x) = C$_0$ + C$_1$x + C$_2$x$^2$ + C$_3$x$^3$ + ⋯ + C$_n$x$^n$, one can group adjacent terms into sub-expressions of the form (A + Bx) and rewrite it as a polynomial in x$^2$: P(x) = (C$_0$ + C$_1$x) + (C$_2$ + C$_3$x)x$^2$ + (C$_4$ + C$_5$x)x$^4$ + ⋯ = Q(x$^2$).
Each of these sub-expressions, and x$^2$, may be computed in parallel. They may also be evaluated using a native multiply–accumulate instruction on some architectures, an advantage that is shared with Horner's method.
This grouping can then be repeated to get a polynomial in x$^4$: P(x) = Q(x$^2$) = ((C$_0$ + C$_1$x) + (C$_2$ + C$_3$x)x$^2$) + ((C$_4$ + C$_5$x) + (C$_6$ + C$_7$x)x$^2$)x$^4$ + ⋯ = R(x$^4$).
Repeating this ⌊log$_2$n⌋+1 times, one arrives at Estrin's scheme for parallel evaluation of a polynomial:
* Compute D$_i$ = C$_{2i}$ + C$_{2i+1}$x for all 0 ≤ i ≤ ⌊n/2⌋. (If n is even, then C$_{n+1}$ = 0 and D$_{n/2}$ = C$_n$.)
* If n ≤ 1, the computation is complete and D$_0$ is the final answer.
* Otherwise, compute y = x$^2$ (in parallel with the computation of D$_i$).
* Evaluate Q(y) = D$_0$ + D$_1$y + D$_2$y$^2$ + ⋯ + D$_{⌊n/2⌋}$y$^{⌊n/2⌋}$ using Estrin's scheme.
This performs a total of n multiply-accumulate operations (the same as Horner's method) in line 1, and an additional ⌊log$_2$n⌋ squarings in line 3. In exchange for those extra squarings, all of the operations in each level of the scheme are independent and may be computed in parallel; the longest dependency path is ⌊log$_2$n⌋+1 operations long.

## Examples

Take P$_n$(x) to mean the nth order polynomial of the form: P$_n$(x) = C$_0$ + C$_1$x + C$_2$x$^2$ + C$_3$x$^3$ + ⋯ + C$_n$x$^n$
Written with Estrin's scheme we have:
    P$_3$(x) = (C$_0$ + C$_1$x) + (C$_2$ + C$_3$x) x$^2$
    P$_4$(x) = (C$_0$ + C$_1$x) + (C$_2$ + C$_3$x) x$^2$ + C$_4$x$^4$
    P$_5$(x) = (C$_0$ + C$_1$x) + (C$_2$ + C$_3$x) x$^2$ + (C$_4$ + C$_5$x) x$^4$
    P$_6$(x) = (C$_0$ + C$_1$x) + (C$_2$ + C$_3$x) x$^2$ + ((C$_4$ + C$_5$x) + C$_6$x$^2$)x$^4$
    P$_7$(x) = (C$_0$ + C$_1$x) + (C$_2$ + C$_3$x) x$^2$ + ((C$_4$ + C$_5$x) + (C$_6$ + C$_7$x) x$^2$)x$^4$
    P$_8$(x) = (C$_0$ + C$_1$x) + (C$_2$ + C$_3$x) x$^2$ + ((C$_4$ + C$_5$x) + (C$_6$ + C$_7$x) x$^2$)x$^4$ + C$_8$x$^8$
    P$_9$(x) = (C$_0$ + C$_1$x) + (C$_2$ + C$_3$x) x$^2$ + ((C$_4$ + C$_5$x) + (C$_6$ + C$_7$x) x$^2$)x$^4$ + (C$_8$ + C$_9$x) x$^8$
In full detail, consider the evaluation of P$_{15}$(x):
    Inputs: x, C$_0$, C$_1$, C$_2$, C$_3$, C$_4$, C$_5$ C$_6$, C$_7$, C$_8$, C$_9$ C$_{10}$, C$_{11}$, C$_{12}$, C$_{13}$ C$_{14}$, C$_{15}$
    Step 1: x$^2$, C$_0$+C$_1$x, C$_2$+C$_3$x, C$_4$+C$_5$x, C$_6$+C$_7$x, C$_8$+C$_9$x, C$_{10}$+C$_{11}$x, C$_{12}$+C$_{13}$x, C$_{14}$+C$_{15}$x
    Step 2: x$^4$, (C$_0$+C$_1$x) + (C$_2$+C$_3$x)x$^2$, (C$_4$+C$_5$x) + (C$_6$+C$_7$x)x$^2$, (C$_8$+C$_9$x) + (C$_{10}$+C$_{11}$x)x$^2$, (C$_{12}$+C$_{13}$x) + (C$_{14}$+C$_{15}$x)x$^2$
    Step 3: x$^8$, ((C$_0$+C$_1$x) + (C$_2$+C$_3$x)x$^2$) + ((C$_4$+C$_5$x) + (C$_6$+C$_7$x)x$^2$)x$^4$, ((C$_8$+C$_9$x) + (C$_{10}$+C$_{11}$x)x$^2$) + ((C$_{12}$+C$_{13}$x) + (C$_{14}$+C$_{15}$x)x$^2$)x$^4$
    Step 4: (((C$_0$+C$_1$x) + (C$_2$+C$_3$x)x$^2$) + ((C$_4$+C$_5$x) + (C$_6$+C$_7$x)x$^2$)x$^4$) + (((C$_8$+C$_9$x) + (C$_{10}$+C$_{11}$x)x$^2$) + ((C$_{12}$+C$_{13}$x) + (C$_{14}$+C$_{15}$x)x$^2$)x$^4$)x$^8$