Horner Rule)

Source: Internet
Author: User

Horner Rule is a common Rule, but there are not many related information on the Internet. I mainly found two.
1. http://blog.csdn.net/lwj1396/archive/2008/07/18/2669993.aspx

2. http://flynoi.blog.hexun.com/31272178_d.html
In the second article, the reason for the emergence of the Horna law is described: it provides an efficient method for polynomial evaluation.
"The most common algorithm for polynomial evaluation is to find the value of each item and accumulate the value. This algorithm is inefficient at both time and space, because the data size is not large, it is intuitive and simple and easy to use. However, once the data size is too large, this algorithm is powerless"

This is a more detailed concept of the Horna law:
Suppose there are n + 2 real numbers a0, a1 ,..., An, and x sequence, to polynomial Pn (x) = anxn + an-1xn-1 +... + A1x + a0 evaluate the value. The direct method is to evaluate each item separately and accumulate the value of each item. This method is very inefficient and requires n + (n-1) +... + 1 = n (n + 1)/2 multiplication and n addition operations. Is there any more efficient algorithm? The answer is yes. Through the following transformation we can get a much faster algorithm, that is, Pn (x) = anxn + an-1xn-1 +... + A1x + a0 = ((... (Anx + an-1) x + an-2) x + an-3 )...) X + a1) x + a0, which is called the Horna rule.

Here I wrote a small program of the Horna rule:

 

 

// Author: Tanky Woo
// Blog: www.WuTianQi.com
# Include <iostream>
Using namespace std;
 
// Calculate the value of an * x ^ n + an-1 * x ^ (n-1) +... a2 * x ^ 2 + a1 * x + a0
Double HornerRule (double a [], int n, double x );
 
Int main ()
{
Double *;
Int n;
Double x;
 
Cout <"Input the n (a0, a1, a2... ):";
Cin> n;
 
A = new double [n + 1];
Cout <"input the A0, A1, A2,..., an (n + 1 Numbers ):";
For (INT I = 0; I <= N; ++ I)
Cin> A [I];
 
Cout <"input the X :";
Cin> X;
 
For (INT I = N; I> = 0; -- I)
{
If (I! = N)
Cout <"+ ";
Cout <a [I] <"* x ^" <I;
}
Cout <"=" <HornerRule (a, n, x) <endl;
 
Return 0;
}
 
 
Double HornerRule (double a [], int n, double x)
{
Double res = 0.0;
 
For (int I = n; I> = 0; -- I)
Res = x * res + a [I];
 
Return res;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.