Qin Jiushao algorithm for solving polynomial

Source: Internet
Author: User

Qin Jiushao algorithm is a polynomial simplification algorithm proposed by the mathematician Qin Jiushao in the Southern Song Dynasty of China. In the West is called the Horner algorithm. It is an algorithm that transforms the evaluation problem of a unary n polynomial into n one-time .

In general, the evaluation of a unary n-th polynomial needs to undergo [n (n+1)]/2 and N-times addition, whereas the Qin Jiushao algorithm requires only n multiplication and n-th addition. It greatly simplifies the calculation process, even in modern times, using computer to solve the problem of polynomial evaluation, the Qin Jiushao algorithm is still the optimal algorithm.

  title : The Write program calculates the value of the given polynomial at the given point x f (x) = a0 + a1x + ... + an-1xn-1 + anxn

  Analysis : Compare the use of conventional and qin Jiushao algorithms to solve the polynomial and output their respective time consumption.

Here we use the total clock () function of the time.h header file, whose function prototype is as follows:

_crtimp clock_t __cdecl __mingw_nothrow clock (void);    // it can be directly regarded as clock_t clock (void)  ; 

     

The return value clock_t is the data type used to hold the time, which is a long integer number. This function returns the number of CPU clock ticks (clock tick, or clocking) between "Open this program process" to "call clock () function in program "

In the Time.h file, you also define a constant clocks_per_sec, which is used to indicate how many clock ticks are in a second. It is defined as follows:

#define


You can see that every 1 per thousand seconds (1 milliseconds), the value returned by the call to the clock () function increases by 1. So want to get a program from start to now execution time (in seconds), just use (double) clock ()/clocks_per_sec , you can also use clk_tck instead Clocks_per_sec, see official documentation

Here we only need to test our custom function execution time, this time is often less than a clock tick, so we have to repeat the call to the measured function , so that the total clock is measured in the interval is sufficiently long, and finally divided by the number of calls, To get the single execution time of the measured function.

  Source :

#include <stdio.h>#include<time.h>#include<math.h>#defineMaxk 1e7//maximum number of repeated calls to the function being measured#defineMAXN 10//Polynomial maximum number of items, that is, polynomial order +1 (n-order polynomial has n+1 items, including a constant term)clock_t Start,stop; //clock_t is the return variable type of the clock () functionDoubleDuration//record the elapsed time of the measured function in secondsDoubleF1 (intNDoubleA[],Doublex);//General Algorithm DeclarationDoubleF2 (intNDoubleA[],Doublex);//qin Jiushao algorithm function declarationintMain () {inti; DoubleA[MAXN];//coefficients for storing polynomial     for(i=0; i<maxn;i++) A[i]= (Double) I; /*no longer testing the scope of the preparation work before the clock () call*/Start= Clock ();//Start Timing     for(i=0; i<maxk;i++) F1 (MAXN-1A1.1);//repeatedly call the test function F1 to get plenty of time to hit pointsstop = Clock ();//Stop Timing    /*other processing that no longer tests the scope is written in the back*/Duration= (Double) (Stop-start)/clocks_per_sec/maxk;//Calculate the test function single run timeprintf"tricks1 =%f\t",(Double) (stop-start)); printf ("Duration1 =%6.2e\n", duration); /*no longer testing the scope of the preparation work before the clock () call*/Start= Clock ();//Start Timing     for(i=0; i<maxk;i++) F2 (MAXN-1A1.1);//repeatedly call the test function F2 to get plenty of time to hit pointsstop = Clock ();//Stop Timing    /*other processing that no longer tests the scope is written in the back*/Duration= (Double) (Stop-start)/clocks_per_sec/maxk;//Calculate the test function single run timeprintf"Tricks2 =%f\t",(Double) (stop-start)); printf ("Duration2 =%6.2e\n", duration); return 0;}DoubleF1 (intNDoubleA[],Doublex) {    inti; Doublep = a[0];  for(i=1; i<=n; i++) P+ = (A[i] *Pow (x, i)); returnp;}DoubleF2 (intNDoubleA[],Doublex) {    inti; Doublep =A[n];  for(I=n; i>0; i--) P= a[i-1] + x*p; returnp;}

  Operation result :

  

It is shown from the results that the running time of the two methods is not above an order of magnitude, and the efficiency is greatly improved by using the Qin Jiushao algorithm F2 computational polynomial.

Qin Jiushao algorithm for solving polynomial

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.