# Horner's method

In mathematics and computer science, Horner's method (or Horner's scheme) is an algorithm for polynomial evaluation. Although named after William George Horner, this method is much older, as it has been attributed to Joseph-Louis Lagrange by Horner himself, and can be traced back many hundreds of years to Chinese and Persian mathematicians. After the introduction of computers, this algorithm became fundamental for computing efficiently with polynomials.
The algorithm is based on Horner's rule, in which a polynomial is written in nested form: ${\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 allows the evaluation of a polynomial of degree n with only $n$ multiplications and $n$ additions. This is optimal, since there are polynomials of degree n that cannot be evaluated with fewer arithmetic operations.
Alternatively, Horner's method and Horner–Ruffini method also refers to a method for approximating the roots of polynomials, described by Horner in 1819. It is a variant of the Newton–Raphson method made more efficient for hand calculation by application of Horner's rule. It was widely used until computers came into general use around 1970.

## Polynomial evaluation and long division

Given the polynomial $p(x)=\sum _{i=0}^{n}a_{i}x^{i}=a_{0}+a_{1}x+a_{2}x^{2}+a_{3}x^{3}+\cdots +a_{n}x^{n},$ where $a_{0},\ldots ,a_{n}$ are constant coefficients, the problem is to evaluate the polynomial at a specific value $x_{0}$ of $x.$
For this, a new sequence of constants is defined recursively as follows:
Then $b_{0}$ is the value of $p(x_{0})$.
To see why this works, the polynomial can be written in the form $p(x)=a_{0}+x{\bigga_{1}+x{\Biga_{2}+x{\biga_{3}+\cdots +x(a_{n-1}+x\,a_{n})\cdots {\big )}{\Big )}{\bigg )}\ .$
Thus, by iteratively substituting the $b_{i}$ into the expression, ${\begin{aligned}p(x_{0})&=a_{0}+x_{0}{\Biga_{1}+x_{0}{\biga_{2}+\cdots +x_{0}(a_{n-1}+b_{n}x_{0})\cdots {\big )}{\Big )}\\&=a_{0}+x_{0}{\Biga_{1}+x_{0}{\biga_{2}+\cdots +x_{0}b_{n-1}{\big )}{\Big )}\\&~~\vdots \\&=a_{0}+x_{0}b_{1}\\&=b_{0}.\end{aligned}}$
Now, it can be proven that;
This expression constitutes Horner's practical application, as it offers a very quick way of determining the outcome of; $p(x)/(x-x_{0})$ with $b_{0}$ (which is equal to $p(x_{0})$) being the division's remainder, as is demonstrated by the examples below. If $x_{0}$ is a root of $p(x)$, then $b_{0}=0$ (meaning the remainder is $0$), which means you can factor $p(x)$ as $x-x_{0}$.
To finding the consecutive $b$-values, you start with determining $b_{n}$, which is simply equal to $a_{n}$. Then you then work recursively using the formula: $b_{n-1}=a_{n-1}+b_{n}x_{0}$ till you arrive at $b_{0}$.

### Examples

Evaluate $f(x)=2x^{3}-6x^{2}+2x-1$ for $x=3$.
We use synthetic division as follows:
The entries in the third row are the sum of those in the first two. Each entry in the second row is the product of the x-value (3 in this example) with the third-row entry immediately to the left. The entries in the first row are the coefficients of the polynomial to be evaluated. Then the remainder of $f(x)$ on division by $x-3$ is 5.
But by the polynomial remainder theorem, we know that the remainder is $f(3)$. Thus, $f(3)=5$.
In this example, if $a_{3}=2,a_{2}=-6,a_{1}=2,a_{0}=-1$ we can see that $b_{3}=2,b_{2}=0,b_{1}=2,b_{0}=5$, the entries in the third row. So, synthetic division (which was actually invented and published by Ruffini 10 years before Horner's publication) is easier to use; it can be shown to be equivalent to Horner's method.
As a consequence of the polynomial remainder theorem, the entries in the third row are the coefficients of the second-degree polynomial, the quotient of $f(x)$ on division by $x-3$. The remainder is 5. This makes Horner's method useful for polynomial long division.
Divide $x^{3}-6x^{2}+11x-6$ by $x-2$:
The quotient is $x^{2}-4x+3$.
Let $f_{1}(x)=4x^{4}-6x^{3}+3x-5$ and $f_{2}(x)=2x-1$. Divide $f_{1}(x)$ by $f_{2}\,(x)$ using Horner's method.
The third row is the sum of the first two rows, divided by 2. Each entry in the second row is the product of 1 with the third-row entry to the left. The answer is ${\frac {f_{1}(x)}{f_{2}(x)}}=2x^{3}-2x^{2}-x+1-{\frac {4}{2x-1}}.$

### Efficiency

Evaluation using the monomial form of a degree $n$ polynomial requires at most $n$ additions and $(n^{2}+n)/2$ multiplications, if powers are calculated by repeated multiplication and each monomial is evaluated individually. The cost can be reduced to $n$ additions and $2n-1$ multiplications by evaluating the powers of $x$ by iteration.
If numerical data are represented in terms of digits (or bits), then the naive algorithm also entails storing approximately $2n$ times the number of bits of $x$: the evaluated polynomial has approximate magnitude $x^{n}$, and one must also store $x^{n}$ itself. By contrast, Horner's method requires only $n$ additions and $n$ multiplications, and its storage requirements are only $n$ times the number of bits of $x$. Alternatively, Horner's method can be computed with $n$ fused multiply–adds. Horner's method can also be extended to evaluate the first $k$ derivatives of the polynomial with $kn$ additions and multiplications.
Horner's method is optimal, in the sense that any algorithm to evaluate an arbitrary polynomial must use at least as many operations. Alexander Ostrowski proved in 1954 that the number of additions required is minimal. Victor Pan proved in 1966 that the number of multiplications is minimal. However, when $x$ is a matrix, Horner's method is not optimal.
This assumes that the polynomial is evaluated in monomial form and no preconditioning of the representation is allowed, which makes sense if the polynomial is evaluated only once. However, if preconditioning is allowed and the polynomial is to be evaluated many times, then faster algorithms are possible. They involve a transformation of the representation of the polynomial. In general, a degree-$n$ polynomial can be evaluated using only ⌊n/2⌋+2 multiplications and $n$ additions.

#### Parallel evaluation

A disadvantage of Horner's rule is that all of the operations are sequentially dependent, so it is not possible to take advantage of instruction level parallelism on modern computers. In most applications where the efficiency of polynomial evaluation matters, many low-order polynomials are evaluated simultaneously (for each pixel or polygon in computer graphics, or for each grid square in a numerical simulation), so it is not necessary to find parallelism within a single polynomial evaluation.
If, however, one is evaluating a single polynomial of very high order, it may be useful to break it up as follows: ${\begin{aligned}p(x)&=\sum _{i=0}^{n}a_{i}x^{i}\\[1ex]&=a_{0}+a_{1}x+a_{2}x^{2}+a_{3}x^{3}+\cdots +a_{n}x^{n}\\[1ex]&=\left(a_{0}+a_{2}x^{2}+a_{4}x^{4}+\cdots \right)+\left(a_{1}x+a_{3}x^{3}+a_{5}x^{5}+\cdots \right)\\[1ex]&=\left(a_{0}+a_{2}x^{2}+a_{4}x^{4}+\cdots \right)+x\left(a_{1}+a_{3}x^{2}+a_{5}x^{4}+\cdots \right)\\[1ex]&=\sum _{i=0}^{\lfloor n/2\rfloor }a_{2i}x^{2i}+x\sum _{i=0}^{\lfloor n/2\rfloor }a_{2i+1}x^{2i}\\[1ex]&=p_{0}(x^{2})+xp_{1}(x^{2}).\end{aligned}}$
More generally, the summation can be broken into k parts: $p(x)=\sum _{i=0}^{n}a_{i}x^{i}=\sum _{j=0}^{k-1}x^{j}\sum _{i=0}^{\lfloor n/k\rfloor }a_{ki+j}x^{ki}=\sum _{j=0}^{k-1}x^{j}p_{j}(x^{k})$ where the inner summations may be evaluated using separate parallel instances of Horner's method. This requires slightly more operations than the basic Horner's method, but allows k-way SIMD execution of most of them. Modern compilers generally evaluate polynomials this way when advantageous, although for floating-point calculations this requires enabling (unsafe) reassociative math.

### Application to floating-point multiplication and division

Horner's method is a fast, code-efficient method for multiplication and division of binary numbers on a microcontroller with no hardware multiplier. One of the binary numbers to be multiplied is represented as a trivial polynomial, where (using the above notation) $a_{i}=1$, and $x=2$. Then, x (or x to some power) is repeatedly factored out. In this binary numeral system (base 2), $x=2$, so powers of 2 are repeatedly factored out.

#### Example

For example, to find the product of two numbers (0.15625) and m: ${\begin{aligned}(0.15625)m&=(0.00101_{b})m=\left(2^{-3}+2^{-5}\right)m=\left(2^{-3})m+(2^{-5}\right)m\\&=2^{-3}\left(m+\left(2^{-2}\right)m\right)=2^{-3}\left(m+2^{-2}(m)\right).\end{aligned}}$

#### Method

To find the product of two binary numbers d and m:
* A register holding the intermediate result is initialized to d.
* Begin with the least significant (rightmost) non-zero bit in m. Count (to the left) the number of bit positions to the next most significant non-zero bit. If there are no more-significant bits, then take the value of the current bit position.Using that value, perform a left-shift operation by that number of bits on the register holding the intermediate result
* If all the non-zero bits were counted, then the intermediate result register now holds the final result. Otherwise, add d to the intermediate result, and continue in step 2 with the next most significant bit in m.

#### Derivation

In general, for a binary number with bit values ($d_{3}d_{2}d_{1}d_{0}$) the product is $(d_{3}2^{3}+d_{2}2^{2}+d_{1}2^{1}+d_{0}2^{0})m=d_{3}2^{3}m+d_{2}2^{2}m+d_{1}2^{1}m+d_{0}2^{0}m.$ At this stage in the algorithm, it is required that terms with zero-valued coefficients are dropped, so that only binary coefficients equal to one are counted, thus the problem of multiplication or division by zero is not an issue, despite this implication in the factored equation: $=d_{0}\left(m+2{\frac {d_{1}}{d_{0}}}\left(m+2{\frac {d_{2}}{d_{1}}}\left(m+2{\frac {d_{3}}{d_{2}}}(m)\right)\right)\right).$
The denominators all equal one (or the term is absent), so this reduces to $=d_{0}(m+2{d_{1}}(m+2{d_{2}}(m+2{d_{3}}(m)))),$ or equivalently (as consistent with the "method" described above) $=d_{3}(m+2^{-1}{d_{2}}(m+2^{-1}{d_{1}}(m+{d_{0}}(m)))).$
In binary (base-2) math, multiplication by a power of 2 is merely a register shift operation. Thus, multiplying by 2 is calculated in base-2 by an arithmetic shift. The factor (2$^{−1}$) is a right arithmetic shift, a (0) results in no operation (since 2$^0$ = 1 is the multiplicative identity element), and a (2$^1$) results in a left arithmetic shift. The multiplication product can now be quickly calculated using only arithmetic shift operations, addition and subtraction.
The method is particularly fast on processors supporting a single-instruction shift-and-addition-accumulate. Compared to a C floating-point library, Horner's method sacrifices some accuracy, however it is nominally 13 times faster (16 times faster when the "canonical signed digit" (CSD) form is used) and uses only 20% of the code space.

### Other applications

Horner's method can be used to convert between different positional numeral systems – in which case x is the base of the number system, and the a$_i$ coefficients are the digits of the base-x representation of a given number – and can also be used if x is a matrix, in which case the gain in computational efficiency is even greater. However, for such cases faster methods are known.

## Polynomial root finding

Using the long division algorithm in combination with Newton's method, it is possible to approximate the real roots of a polynomial. The algorithm works as follows. Given a polynomial $p_{n}(x)$ of degree $n$ with zeros $z_{n}<z_{n-1}<\cdots <z_{1},$ make some initial guess $x_{0}$ such that $z_{1}<x_{0}$. Now iterate the following two steps:
* Using Newton's method, find the largest zero $z_{1}$ of $p_{n}(x)$ using the guess $x_{0}$.
* Using Horner's method, divide out $(x-z_{1})$ to obtain $p_{n-1}$. Return to step 1 but use the polynomial $p_{n-1}$ and the initial guess $z_{1}$.
These two steps are repeated until all real zeros are found for the polynomial. If the approximated zeros are not precise enough, the obtained values can be used as initial guesses for Newton's method but using the full polynomial rather than the reduced polynomials.

### Example

Consider the polynomial $p_{6}(x)=(x+8)(x+5)(x+3)(x-2)(x-3)(x-7)$ which can be expanded to $p_{6}(x)=x^{6}+4x^{5}-72x^{4}-214x^{3}+1127x^{2}+1602x-5040.$
From the above we know that the largest root of this polynomial is 7 so we are able to make an initial guess of 8. Using Newton's method the first zero of 7 is found as shown in black in the figure to the right. Next $p(x)$ is divided by $(x-7)$ to obtain $p_{5}(x)=x^{5}+11x^{4}+5x^{3}-179x^{2}-126x+720$ which is drawn in red in the figure to the right. Newton's method is used to find the largest zero of this polynomial with an initial guess of 7. The largest zero of this polynomial which corresponds to the second largest zero of the original polynomial is found at 3 and is circled in red. The degree 5 polynomial is now divided by $(x-3)$ to obtain $p_{4}(x)=x^{4}+14x^{3}+47x^{2}-38x-240$ which is shown in yellow. The zero for this polynomial is found at 2 again using Newton's method and is circled in yellow. Horner's method is now used to obtain $p_{3}(x)=x^{3}+16x^{2}+79x+120$ which is shown in green and found to have a zero at −3. This polynomial is further reduced to $p_{2}(x)=x^{2}+13x+40$ which is shown in blue and yields a zero of −5. The final root of the original polynomial may be found by either using the final zero as an initial guess for Newton's method, or by reducing $p_{2}(x)$ and solving the linear equation. As can be seen, the expected roots of −8, −5, −3, 2, 3, and 7 were found.

## Divided difference of a polynomial

Horner's method can be modified to compute the divided difference $(p(y)-p(x))/(y-x).$ Given the polynomial (as before) $p(x)=\sum _{i=0}^{n}a_{i}x^{i}=a_{0}+a_{1}x+a_{2}x^{2}+a_{3}x^{3}+\cdots +a_{n}x^{n},$ proceed as follows ${\begin{aligned}b_{n}&=a_{n},&\quad d_{n}&=b_{n},\\b_{n-1}&=a_{n-1}+b_{n}x,&\quad d_{n-1}&=b_{n-1}+d_{n}y,\\&\ \ \vdots &\quad &\ \ \vdots \\b_{1}&=a_{1}+b_{2}x,&\quad d_{1}&=b_{1}+d_{2}y,\\b_{0}&=a_{0}+b_{1}x.\end{aligned}}$
At completion, we have ${\begin{aligned}p(x)&=b_{0},\\{\frac {p(y)-p(x)}{y-x}}&=d_{1},\\p(y)&=b_{0}+(y-x)d_{1}.\end{aligned}}$ This computation of the divided difference is subject to less round-off error than evaluating $p(x)$ and $p(y)$ separately, particularly when $x\approx y$. Substituting $y=x$ in this method gives $d_{1}=p'(x)$, the derivative of $p(x)$.

## History

Horner's paper, titled "A new method of solving numerical equations of all orders, by continuous approximation", was read before the Royal Society of London, at its meeting on July 1, 1819, with a sequel in 1823. Horner's paper in Part II of Philosophical Transactions of the Royal Society of London for 1819 was warmly and expansively welcomed by a reviewer in the issue of The Monthly Review: or, Literary Journal for April, 1820; in comparison, a technical paper by Charles Babbage is dismissed curtly in this review. The sequence of reviews in The Monthly Review for September, 1821, concludes that Holdred was the first person to discover a direct and general practical solution of numerical equations. Fuller showed that the method in Horner's 1819 paper differs from what afterwards became known as "Horner's method" and that in consequence the priority for this method should go to Holdred (1820).
Unlike his English contemporaries, Horner drew on the Continental literature, notably the work of Arbogast. Horner is also known to have made a close reading of John Bonneycastle's book on algebra, though he neglected the work of Paolo Ruffini.
Although Horner is credited with making the method accessible and practical, it was known long before Horner. In reverse chronological order, Horner's method was already known to:
* Paolo Ruffini in 1809 (see Ruffini's rule)
* Isaac Newton in 1669
* the Chinese mathematician Zhu Shijie in the 14th century
* the Chinese mathematician Qin Jiushao in his Mathematical Treatise in Nine Sections in the 13th century
* the Persian mathematician Sharaf al-Dīn al-Ṭūsī in the 12th century (the first to use that method in a general case of cubic equation)
* the Chinese mathematician Jia Xian in the 11th century (Song dynasty)
* The Nine Chapters on the Mathematical Art, a Chinese work of the Han dynasty (202 BC – 220 AD) edited by Liu Hui (fl. 3rd century).
Qin Jiushao, in his Shu Shu Jiu Zhang (Mathematical Treatise in Nine Sections; 1247), presents a portfolio of methods of Horner-type for solving polynomial equations, which was based on earlier works of the 11th century Song dynasty mathematician Jia Xian; for example, one method is specifically suited to bi-quintics, of which Qin gives an instance, in keeping with the then Chinese custom of case studies. Yoshio Mikami in Development of Mathematics in China and Japan (Leipzig 1913) wrote:
   "... who can deny the fact of Horner's illustrious process being used in China at least nearly six long centuries earlier than in Europe ... We of course don't intend in any way to ascribe Horner's invention to a Chinese origin, but the lapse of time sufficiently makes it not altogether impossible that the Europeans could have known of the Chinese method in a direct or indirect way."
Ulrich Libbrecht concluded: It is obvious that this procedure is a Chinese invention ... the method was not known in India. He said, Fibonacci probably learned of it from Arabs, who perhaps borrowed from the Chinese. The extraction of square and cube roots along similar lines is already discussed by Liu Hui in connection with Problems IV.16 and 22 in Jiu Zhang Suan Shu, while Wang Xiaotong in the 7th century supposes his readers can solve cubics by an approximation method described in his book Jigu Suanjing.