A polynomial of degree is a function of the form where are the coefficients of the polynomial. Therefore, the polynominal can be represented as an array . Our task is to program a function evaluate(p,x) that has as input the array and the number and returns the function value at the position
1 2 3 |
|
Suppose you have implemented the function evaluate(p, x) as efficiently as possible.
How does the runtime of a function evaluation depend on the degree of the polynomial?
The runtime is essentially proportional to the number of multiplications that must be executed at the function call.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The evaluation of a polynomial is particularly efficient with the Horner method. We rewrite the polynomial a bit by factorising all instances of x , so that no exponents appear in the expression: p ( x ) = c 0 + x ⋅ ( c 1 + x ⋅ ( c 2 + x ⋅ ( … ( c n − 1 + x ⋅ c n ) ) … ) ) The polynomial can now be evaluated by calculating the brackets from the inside to the outside. The intermediate results are then b 0 b 1 b 2 ⋮ b k ⋮ b n = c n = c n − 1 + x ⋅ b 0 = c n − 2 + x ⋅ b 1 ⋮ = c n − k + x ⋅ b k − 1 ⋮ = c 0 + x ⋅ b n − 1 with the final result p ( x ) = b n . In each line (except for the first one) exactly one multiplication is executed, so that there is a total of n multiplications. However, one cannot perform the algorithm with much fewer multiplications, since the calculation of all mononomes x k already requires n − 1 multiplications. The runtime therefore scales with O ( n ) .
Corresponding program example in Python: