Sie sind auf Seite 1von 2

CHL711 Lab-1 Report

Vivek Bhaskar 2011CH70190


January 22, 2015

Introduction

There are several numerical methods of polyomial evaluation using computer codes like Horners
and Estrins methods. In this report, we have generated codes for Horners method and the basic
or brute-force method and also evaluated the time taken by the same software and hardware to
carry out the operations involved.

Polynomials

A polynomial is a mathematical expression composed of constants and variables, using only addition, subtraction, multiplication and positive integer powers. A uni-variate polynomial is a polynomial in one independent variable, whereas a multi-variate polynomial is a polynomial in multiple
independent variables In this report we will only be focussing on implementation for uni-variate
polynomials.

Horners Form/Method

For a uni-variate polynomial, Horners Form is known to be the fastest way to compute its value.
This is because it is the method with the smallest possible number of arithmetic operations (additions and multiplications). Horners form solves by converting the expression into a nested set of
multiply-add instructions; therefore Horners form is good for processors with fused multiply-add
instructions.
A fused multiply-add instruction calculates the result of the sum a+bc in one step, with a single
rounding error. This is an improvement over processors without fused multiply-add instructions
as they would evaluate first b c, round the result, then add the result to a and round the result.
Therefore processors with a fused multiply-add instruction improve the accuracy and speed of
polynomial evaluation.
Horners rule computes f(x) by nested multiply-adds as follows:
f (x) = ai xi
It is generally the default method for evaluating polynomials in libraries. The major downside
to Horners form is that it is purely serial; later results rely on the results of previous instructions
i.e. the expression tree is deep rather than wide . On modern architectures a deep expression tree is
generally not desirable; rather the expression tree should be wide which would allow the sub-trees
to be evaluated in parallel method.
1

Code

#PROGRAM TO EVALUATE POLYNOMIALS AND THEIR COMPUTATION TIME

#reading coefficients of a polynomial from a file named pol.txt and converting it into a list
c = [line.strip() for line in open(poly.txt)]
c = map(float, c)
l=len(c) #length of the list of coefficients
def Hornerfunc(x,c): #defining a fucntion to implementb= c[0]*x #-Horners method of plynomial evaluation
for i in range(1, l-1):
b= (b + c[i])*x
b = b + c[l-1]
print b
def basic(x,c):
p=0
for j in range(0, l-1):
p= p + c[j]*x**(l-j-1)
p = p + c[l-1]
print p
x= input("input value of x at which the polynomial is to be evaluated:") #call for input
import time
start= time.clock() #calculating time taken
basic(x,c) #calling the function basic
end= time.clock()
time2= (end-start)
start= time.clock() #calculating time taken
Hornerfunc(x,c) #calling the function Hornerfunc
end= time.clock()
time1= (end-start)

print ("time taken for Horners method:"), time1


print ("time taken for basic method:"), time2

Conclusion

Horners method is the fastest and very accurate mthod for polynomial evaluation. Also, it was
observed that the difference in computation time becomes larger with larger degrees of polynomials
in for Horners form and brute-force form.
2

Das könnte Ihnen auch gefallen