Calculation of a polynomial value. Is everything trivial in this matter?
The calculation of the value of a polynomial at a point is one of the simplest classical programming problems.
When carrying out 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 comes down to the calculation of approximating polynomials.
The average reader Habrahabr can not be called inexperienced in the application of all kinds of perversions. Every second person will say that the polynomial must be calculated according to Horner's rule . But there is always a small “but,” is the Horner scheme always the most efficient?
I do not set the goal of accurately describing the algorithms for computing polynomials, but only to show that in some cases it is possible (necessary) to apply schemes with excellent Horner rules. For those who are interested in the material, at the end of the article is a list of literature, 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 little known. In addition, I am just pleased to talk about the work of our mathematicians.
In calculating the values of polynomials, Horner's rule has received widespread application. The method is named after the British mathematician William George Horner.
In accordance with this rule, an nth degree polynomial:
represented as
The calculation of the value of the polynomial is carried out in the order determined by the brackets. What do we have? To calculate a polynomial according to the Horner scheme, it is necessary to perform n multiplications and nk additions (here k is the number of polynomial coefficients equal to 0). If , then there will be n-1 multiplications.
It can be shown that to calculate polynomials of a general form, it is impossible to construct a scheme that is more economical in 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 the polynomial.
When calculating polynomials of a special kind, fewer operations may be required than when applying the universal Horner scheme. For example, calculating a degree according to Horner's scheme means sequentially multiplying n factors and requires n-1 multiplication. However, each first reader will say that to calculate, for example, need to sequentially calculate , , , i.e. complete only 3 multiplications instead of 7.
In fact, everything is decided by the amount of computation. If it is necessary to calculate one value of the polynomial, then nothing is better than Horner's scheme. But if the values of the polynomial are calculated at many points, then it becomes 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, it is advisable to use two-stage schemes to obtain polynomial values. At the first stage, actions are performed only on the coefficients of the polynomial; it is converted to a special form. At the second stage, the value of the polynomial itself is calculated for 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 calculation methods are useful in calculating polynomial values for a large number of x values. The gain is obtained due to the fact that the first stage for the polynomial is performed only once. An example is the calculation of elementary functions, where an approximating polynomial is prepared in advance.
In further considerations, speaking of the number of operations to calculate , I will keep in mind the complexity of the second stage of calculations.
We have the following polynomial:
For calculations we use the following auxiliary polynomials:
,
,
.
The coefficients are determined by the method of uncertain coefficients based on the condition . From the last condition, we compose a system of equations, equating the coefficients for equal degrees of polynomials.
The system itself, I will not give here. But it is easily solved by the method of substitutions, and one has to solve quadratic equations. The coefficients can be complex, but if the coefficients turn out to be valid, then the calculations require three multiplications and seven additions instead of five multiplications and six additions according to the Horner scheme.
There is no need to talk about the universality of this scheme, but the reader can visually evaluate the decrease in the number of operations in comparison with the Horner scheme.
Finally, I got to our mathematicians.
Yu.L. Ketkov gave a general idea of the nth degree polynomial for n> 5, always leading to real expressions and requiring the calculation of the nth degree polynomial to perform [(n + 1) / 2] + [n / 4] multiplications and n + 1 additions .
For example, for n = 2k, the Ketkov scheme reduces to finding polynomials:
where , for k - even, and , if k is odd (k> 2).
All unknown coefficients are found from equality . In the works of Ketkov, for solving the resulting systems, a method is given that always gives real coefficients .
E. Belaga in his work gave rigorous proof of the impossibility of constructing a scheme for computing arbitrary polynomials of the nth degree, using in the second stage less than [(n + 1) / 2] +1 multiplications and n additions.
V.Ya. Pan dealt with optimal polynomial computation. In particular, he proposed several schemes for calculating real polynomials, which came very close to the estimates of E. Belagi. I will give some Pan schemes for real polynomials.
1. Scheme for computing polynomials of the fourth degree.
The polynomial is considered .
Imagine in the form:
Where
2. The circuit for calculating , .
Building auxiliary polynomials , , :
, s = 1,2, ..., k.
To calculate the value of a polynomial expression using:
at ,
at ,
at ,
at ,
this scheme requires the second stage of multiplication and addition.
A feature of this scheme is that the coefficients always exist for and the real coefficients of the original polynomial.
At V.Ya. There are other schemes for computing polynomials, including complex ones.
Summarizing the above, I note that the calculation of one or more values of the polynomial indisputably needs to be carried out using the Horner scheme.
However, if the number of polynomial values that need to be calculated is large, and the performance is very important, then it makes sense to consider the use of special methods for calculating polynomials.
Some readers will say that fiddling with schemes other than Horner’s is difficult, dreary and not worth messing with. However, in real life there are problems in which you just need to calculate a huge number of polynomial values with large degrees (for example, it can take months to calculate them), and halving the number of multiplications will give a significant gain in time, even if you have to spend a couple of days to implement a specific scheme for computing polynomials.
When carrying out 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 comes down to the calculation of approximating polynomials.
The average reader Habrahabr can not be called inexperienced in the application of all kinds of perversions. Every second person will say that the polynomial must be calculated according to Horner's rule . But there is always a small “but,” is the Horner scheme always the most efficient?
I do not set the goal of accurately describing the algorithms for computing polynomials, but only to show that in some cases it is possible (necessary) to apply schemes with excellent Horner rules. For those who are interested in the material, at the end of the article is a list of literature, 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 little known. In addition, I am just pleased to talk about the work of our mathematicians.
Horner's scheme
In calculating the values of polynomials, Horner's rule has received widespread application. The method is named after the British mathematician William George Horner.
In accordance with this rule, an nth degree polynomial:
represented as
The calculation of the value of the polynomial is carried out in the order determined by the brackets. What do we have? To calculate a polynomial according to the Horner scheme, it is necessary to perform n multiplications and nk additions (here k is the number of polynomial coefficients equal to 0). If , then there will be n-1 multiplications.
It can be shown that to calculate polynomials of a general form, it is impossible to construct a scheme that is more economical in 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 the polynomial.
Exceptions
When calculating polynomials of a special kind, fewer operations may be required than when applying the universal Horner scheme. For example, calculating a degree according to Horner's scheme means sequentially multiplying n factors and requires n-1 multiplication. However, each first reader will say that to calculate, for example, need to sequentially calculate , , , i.e. complete only 3 multiplications instead of 7.
Is there anything else, after all, the Horner scheme is the most economical?
In fact, everything is decided by the amount of computation. If it is necessary to calculate one value of the polynomial, then nothing is better than Horner's scheme. But if the values of the polynomial are calculated at many points, then it becomes 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, it is advisable to use two-stage schemes to obtain polynomial values. At the first stage, actions are performed only on the coefficients of the polynomial; it is converted to a special form. At the second stage, the value of the polynomial itself is calculated for 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 calculation methods are useful in calculating polynomial values for a large number of x values. The gain is obtained due to the fact that the first stage for the polynomial is performed only once. An example is the calculation of elementary functions, where an approximating polynomial is prepared in advance.
In further considerations, speaking of the number of operations to calculate , I will keep in mind the complexity of the second stage of calculations.
J. Todt scheme for polynomials of degree 6
We have the following polynomial:
For calculations we use the following auxiliary polynomials:
,
,
.
The coefficients are determined by the method of uncertain coefficients based on the condition . From the last condition, we compose a system of equations, equating the coefficients for equal degrees of polynomials.
The system itself, I will not give here. But it is easily solved by the method of substitutions, and one has to solve quadratic equations. The coefficients can be complex, but if the coefficients turn out to be valid, then the calculations require three multiplications and seven additions instead of five multiplications and six additions according to the Horner scheme.
There is no need to talk about the universality of this scheme, but the reader can visually evaluate the decrease in the number of operations in comparison with the Horner scheme.
Scheme Yu.L. Ketkova
Finally, I got to our mathematicians.
Yu.L. Ketkov gave a general idea of the nth degree polynomial for n> 5, always leading to real expressions and requiring the calculation of the nth degree polynomial to perform [(n + 1) / 2] + [n / 4] multiplications and n + 1 additions .
For example, for n = 2k, the Ketkov scheme reduces to finding polynomials:
where , for k - even, and , if k is odd (k> 2).
All unknown coefficients are found from equality . In the works of Ketkov, for solving the resulting systems, a method is given that always gives real coefficients .
Schemes V.Ya. Pan
E. Belaga in his work gave rigorous proof of the impossibility of constructing a scheme for computing arbitrary polynomials of the nth degree, using in the second stage less than [(n + 1) / 2] +1 multiplications and n additions.
V.Ya. Pan dealt with optimal polynomial computation. In particular, he proposed several schemes for calculating real polynomials, which came very close to the estimates of E. Belagi. I will give some Pan schemes for real polynomials.
1. Scheme for computing polynomials of the fourth degree.
The polynomial is considered .
Imagine in the form:
Where
2. The circuit for calculating , .
Building auxiliary polynomials , , :
, s = 1,2, ..., k.
To calculate the value of a polynomial expression using:
at ,
at ,
at ,
at ,
this scheme requires the second stage of multiplication and addition.
A feature of this scheme is that the coefficients always exist for and the real coefficients of the original polynomial.
At V.Ya. There are other schemes for computing polynomials, including complex ones.
Conclusion
Summarizing the above, I note that the calculation of one or more values of the polynomial indisputably needs to be carried out using the Horner scheme.
However, if the number of polynomial values that need to be calculated is large, and the performance is very important, then it makes sense to consider the use of special methods for calculating polynomials.
Some readers will say that fiddling with schemes other than Horner’s is difficult, dreary and not worth messing with. However, in real life there are problems in which you just need to calculate a huge number of polynomial values with large degrees (for example, it can take months to calculate them), and halving the number of multiplications will give a significant gain in time, even if you have to spend a couple of days to implement a specific scheme for computing polynomials.
Literature
- Ketkov Yu.L. About one method of computing polynomials on mathematical machines. // Proceedings of the universities. Radiophysics, t.1., No. 4, 1958
- V. Ya. Pan, “Calculation of polynomials according to schemes with preliminary processing of coefficients and a program for automatically finding parameters”, J. Comput. mate. and mate. Fiz., 2: 1 (1962), 133–140
- V. Ya. Pan, “On methods for 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”, J. Comput. mate. and mate. Fiz., 5: 1 (1965), 116–118
- Pan V. Ya. Some schemes for calculating the values of polynomials with real coefficients. The problems of cybernetics. Vol. 5. M .: Nauka, 1961, 17–29.
- Belaga E. G. On the calculation of the values of a polynomial in one variable with preliminary processing of the coefficients. The problems of cybernetics. Vol. 5. M .: Fizmatgiz, 1961, 7–15.