Lably
Skip to content

Horner's method

Algorithm for polynomial evaluation

  • 11 min read
  • 4 chapters

Related on Lably

Through Mathematics, Computer science and Polynomial

The algorithm is based on Horner's rule, in which a polynomial is written in nested form: a0+a1x+a2x2+a3x3+⋯+anxn=a0+x(a1+x(a2+x(a3+⋯+x(an−1+xan)⋯))).

This allows the evaluation of a polynomial of degree n with only n multiplications and n additions. This is optimal, as it is impossible to evaluate polynomials of degree n with fewer arithmetic operations when both x and the coefficients a0, ..., an are given as input.

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 polynomialp(x)=∑i=0naixi=a0+a1x+a2x2+a3x3+⋯+anxn,where a0,…,an are constant coefficients, the problem is to evaluate the polynomial at a specific value x0 of x.

For this, a new sequence of constants is defined recursively as follows:

Then b0 is the value of p(x0).

To see why this works, the polynomial can be written in the form p(x)=a0+x(a1+x(a2+x(a3+⋯+x(an−1+xan)⋯))).

Thus, by iteratively substituting the bi into the expression, p(x0)=a0+x0(a1+x0(a2+⋯+x0(an−1+bnx0)⋯))=a0+x0(a1+x0(a2+⋯+x0bn−1))⋮=a0+x0b1=b0.

Similarly, it can be shown that:

Suggesting a convenient procedure for determining the result of the polynomial division p(x)/(x−x0) with b0 (which is equal to p(x0)) being the division's remainder. If x0 is a root of p(x), then b0=0 (meaning the remainder is 0) and x−x0 a factor of p(x).

Examples

Evaluate f(x)=2x3−6x2+2x−1 for x=3.

We use synthetic division as follows:

32−62−16062025

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 a3=2,a2=−6,a1=2,a0=−1 we can see that b3=2,b2=0,b1=2,b0=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 x3−6x2+11x−6 by x−2:

21−611−62−861−430

The quotient is x2−4x+3.

Let f1(x)=4x4−6x3+3x−5 and f2(x)=2x−1. Divide f1(x) by f2(x) using Horner's method.

0.54−603−52−2−112−2−11−4

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 f1(x)f2(x)=2x3−2x2−x+1−42x−1.

Efficiency

Evaluation using the monomial form of a degree n polynomial requires at most n additions and (n2+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 xn, and one must also store xn 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, Horner's method is not optimal for evaluation of matrix polynomials (where x is a matrix, but the coefficients are scalar), when we count scalar multiplications and matrix multiplications separately as the former are cheaper than the latter.

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: p(x)=∑i=0naixi=a0+a1x+a2x2+a3x3+⋯+anxn=(a0+a2x2+a4x4+⋯)+(a1x+a3x3+a5x5+⋯)=(a0+a2x2+a4x4+⋯)+x(a1+a3x2+a5x4+⋯)=∑i=0⌊n/2⌋a2ix2i+x∑i=0⌊n/2⌋a2i+1x2i=p0(x2)+xp1(x2).

More generally, the summation can be broken into k parts: p(x)=∑i=0naixi=∑j=0k−1xj∑i=0⌊n/k⌋aki+jxki=∑j=0k−1xjpj(xk) 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. Another use of breaking a polynomial down this way is to calculate steps of the inner summations in an alternating fashion to take advantage of instruction-level parallelism.

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) ai=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: (0.15625)m=(0.00101b)m=(2−3+2−5)m=(2−3)m+(2−5)m=2−3(m+(2−2)m)=2−3(m+2−2(m)).

Method

To find the product of two binary numbers d and m:

  1. A register holding the intermediate result is initialized to d.
  2. Begin with the least significant (rightmost) non-zero bit in m.
    1. 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.
    2. Using that value, perform a left-shift operation by that number of bits on the register holding the intermediate result
  3. 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 (d3d2d1d0) the product is (d323+d222+d121+d020)m=d323m+d222m+d121m+d020m. 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: =d0(m+2d1d0(m+2d2d1(m+2d3d2(m)))).

The denominators all equal one (or the term is absent), so this reduces to =d0(m+2d1(m+2d2(m+2d3(m)))), or equivalently (as consistent with the "method" described above) =d3(m+2−1d2(m+2−1d1(m+d0(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 20 = 1 is the multiplicative identity element), and a (21) 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 ai 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.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Algebra