Calculating the value of a polynomial at a point is one of the simplest classical programming problems.
When performing various kinds of calculations, it is often necessary to determine the values ​​of polynomials for given values ​​of the arguments. Often, the approximate calculation of functions is reduced to the calculation of approximating polynomials.
An ordinary reader Habrahabr can not be called inexperienced in the application of all sorts of perversions. Every second will say that the polynomial must be calculated according
to Horner's rule . But there is always a small “but”, is Horner’s scheme always the most efficient?
I do not set a goal to accurately describe the algorithms for calculating polynomials, but only to show that in some cases it is possible (necessary) to apply the Horner's excellent rules. For those who are interested in the material, at the end of the article is a list of references, which can be found for a more detailed study of the issue.
In addition, sometimes it becomes insulting that the names of our Russian mathematicians remain obscure. Besides, it's just nice to talk about the work of our mathematicians.
Horner's Scheme

When calculating the values ​​of polynomials, Horner's rule was widely applied. The method is named after the British mathematician William George Horner.
In accordance with this rule, the nth degree polynomial:
represented as
The calculation of the polynomial value is performed in the order defined by the brackets. What do we have? To calculate the polynomial
)
according to Horner’s scheme, n multiplications and nk additions must be performed (here k is the number of polynomial coefficients equal to 0). If a

, then the multiplications will be n-1.
It can be shown that for calculating polynomials of a general form, one cannot construct a more economical scheme in terms of the number of operations than the Horner scheme.
The biggest attraction of Horner’s scheme is the simplicity of the algorithm for calculating the value of a polynomial.
Exceptions
When calculating polynomials of a special type, a smaller number of operations may be required than when using the universal Horner scheme. For example, calculating the degree

according to Horner's scheme, means the sequential multiplication of n factors and requires n-1 multiplication. However, every first reader will say that to calculate, for example,

need to consistently calculate

,

,

i.e. perform only 3 multiplications instead of 7.
')
Is there anything else, because Horner’s scheme is the most economical?
In fact, all solve the volume of calculations. If it is necessary to calculate one value of a polynomial, then nothing is invented better than the Horner scheme. But if the values ​​of a polynomial are computed at many points, then it is possible to save a large number of multiplication operations due to preliminary calculations performed exactly once. This can significantly speed up the program.
In some cases, to obtain the values ​​of polynomials, it is advisable to use two-stage schemes. At the first stage, only the coefficients of the polynomial are performed; it is converted to a special form. At the second stage, the value of the polynomial itself is calculated for the given values ​​of the argument. In this case, it may turn out that the number of operations performed in the second stage will be less than in calculations according to the Horner scheme.
Again, I note that such computational methods are appropriate when calculating the values ​​of a polynomial.
)
for a large number of x values. The win is due to the fact that the first stage for the polynomial is performed only once. An example would be the calculation of elementary functions, where the approximating polynomial is prepared in advance.
In further discussion, speaking about the number of operations to calculate
)
I will keep in mind the complexity of the second stage of the calculations.
J.Todt scheme for polynomials 6 degrees
We have the following polynomial:
For calculations we use the following auxiliary polynomials:
%3Dx(x%2B%7B%7Bb%7D_%7B1%7D%7D))
,
%3D(%7B%7Bp%7D_%7B1%7D%7D%2Bx%2B%7B%7Bb%7D_%7B2%7D%7D)(%7B%7Bp%7D_%7B1%7D%7D%2B%7B%7Bb%7D_%7B3%7D%7D))
,
%3D(%7B%7Bp%7D_%7B2%7D%7D%2B%7B%7Bb%7D_%7B4%7D%7D)(%7B%7Bp%7D_%7B1%7D%7D%2B%7B%7Bb%7D_%7B5%7D%7D)%2B%7B%7Bb%7D_%7B6%7D%7D)
.
Coefficients

determined by the method of uncertain coefficients based on the condition
%5Cequiv%20%7B%7BP%7D_%7B6%7D%7D(x))
. From the last condition, we compose a system of equations, equating the coefficients with equal degrees of polynomials.
The system itself, I will not give here. But it is easily solved by the method of substitutions, while having to solve quadratic equations. The coefficients can turn out to be complex, but if the coefficients turn out to be real, then the calculations require three multiplications and seven additions instead of five multiplications and six additions according to the Horner scheme.
Talk about the universality of this scheme is not necessary, but the reader can clearly appreciate the reduction in the number of operations compared to the Horner scheme.
Scheme Yu.L. Ketkova

Finally, I got to our mathematicians.
Yu.L. Ketkov gave a general representation of the nth degree polynomial for n> 5, which always leads to real expressions and requires the nth degree of execution of [(n + 1) / 2] + [n / 4] multiplications and n + 1 additions to compute the polynomial .
For example, for n = 2k, the Ketkov scheme is reduced to finding polynomials:
Where

,

when k is even and

,

if k is odd (k> 2).
All unknown coefficients are equal
%3D%7B%7BN%7D_%7B2k%7D%7D)
. In the works of Ketkov, for solving the resulting systems, a method is given that gives always real coefficients.

.
Schemes V.Ya. Pan
E. Belaga in his works gave a rigorous proof of the impossibility of constructing a scheme for computing arbitrary polynomials of the nth degree, using at the second stage less than [(n + 1) / 2] +1 multiplications and n additions.
V.Ya. Ban dealt with optimal polynomial computation. In particular, they proposed several schemes for calculating real polynomials, which were very close to E. Belagi’s estimates. I will give some Pan schemes for real polynomials.
1. Scheme for calculating fourth degree polynomials.
Considered polynomial
%3D%7B%7Ba%7D_%7B0%7D%7D%7B%7Bx%7D%5E%7B4%7D%7D%2B%7B%7Ba%7D_%7B1%7D%7D%7B%7Bx%7D%5E%7B3%7D%7D%2B%7B%7Ba%7D_%7B2%7D%7D%7B%7Bx%7D%5E%7B2%7D%7D%2B%7B%7Ba%7D_%7B3%7D%7Dx%2B%7B%7Ba%7D_%7B4%7D%7D)
.
Imagine
)
as:
Where
2. Scheme for calculation
)
,

.
We build auxiliary polynomials
)
,
)
,
)
:
%3D%7B%7Bp%7D_%7Bs-1%7D%7D(x)%5Cleft(%20(g(x)%2B%7B%7B%5Clambda%20%7D_%7B4s-2%7D%7D)(h(x)%2B%7B%7B%5Clambda%20%7D_%7B4s-1%7D%7D)%2B%7B%7B%5Clambda%20%7D_%7B4s%7D%7D%20%5Cright)%2B%7B%7B%5Clambda%20%7D_%7B4s%2B1%7D%7D)
, s = 1,2, ..., k.
To calculate the value of a polynomial, use the expression:
%5Cequiv%20%7B%7Ba%7D_%7B0%7D%7D%7B%7Bp%7D_%7Bk%7D%7D(x))
at

,
%5Cequiv%20%7B%7Ba%7D_%7B0%7D%7Dx%7B%7Bp%7D_%7Bk%7D%7D(x)%2B%7B%7B%5Clambda%20%7D_%7Bn%7D%7D)
at

,
%5Cequiv%20%7B%7Ba%7D_%7B0%7D%7D%5Cleft(%20%7B%7Bp%7D_%7Bk%7D%7D(x)(g(x)%2B%7B%7B%5Clambda%20%7D_%7Bn-1%7D%7D)%2B%7B%7B%5Clambda%20%7D_%7Bn%7D%7D%20%5Cright))
at

,
%5Cequiv%20%7B%7Ba%7D_%7B0%7D%7Dx%5Cleft(%20%7B%7Bp%7D_%7Bk%7D%7D(x)(g(x)%2B%7B%7B%5Clambda%20%7D_%7Bn-2%7D%7D)%2B%7B%7B%5Clambda%20%7D_%7Bn-1%7D%7D%20%5Cright)%2B%7B%7B%5Clambda%20%7D_%7Bn%7D%7D)
at

,
This scheme in the second stage requires
![[n / 2] +2](https://tex.s2cms.ru/svg/%5Bn%2F2%5D%2B2)
multiplication and

addition.
The peculiarity of this scheme is that the coefficients

always exist at

and the actual coefficients of the original polynomial.
W.Ya. There are other schemes for calculating polynomials, including complex ones.
Conclusion
Summarizing what has been said, I note that the calculation of one or more values ​​of the polynomial must be undoubtedly carried out using the Horner scheme.
However, if the number of polynomial values ​​that need to be calculated is large, and performance is very important, then it makes sense to consider the use of special methods for calculating polynomials.
Some readers will say that it’s difficult to tinker with the use of schemes other than Horner’s, it’s dreary and you shouldn’t get involved with it. However, in real life there are tasks in which you just need to calculate a huge number of polynomial values ​​with large degrees (for example, months can take them to calculate), and reducing the number of multiplications by half will give a significant time gain, even if you have to spend a couple of days on the implementation of a specific scheme for calculating polynomials.
Literature
- Ketkov Yu.L. About one method of calculating polynomials on mathematical machines. // News of universities. Radio Physics, v.1., â„– 4, 1958
- V. Ya. Pan, “Calculation of polynomials using schemes with preliminary processing of coefficients and a program for automatically finding parameters”, Zh. Vychisl. Math and mat. Phys., 2: 1 (1962), 133–140
- V. Ya. Pan, “On methods of calculating the values ​​of polynomials”, UMN, 21: 1 (127) (1966), 103–134
- V. Ya. Pan, “On the calculation of polynomials of the fifth and seventh degree with real coefficients”, Zh. Vychisl. Math and mat. Phys. 5: 1 (1965), 116–118
- Pan V. Ya. Some schemes for calculating the values ​​of polynomials with real coefficients. Problems of cybernetics. Issue 5. M .: Nauka, 1961, 17–29.
- Belaga .. G. On the calculation of the values ​​of a polynomial of one variable with preliminary processing of coefficients. Problems of cybernetics. Issue 5. M .: Fizmatgiz, 1961, 7–15.