horndn1-3

6
 Efficient Computations and Horner’s Method

Upload: damien-banda

Post on 07-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

8/6/2019 horndn1-3

http://slidepdf.com/reader/full/horndn1-3 1/6

Efficient Computationsand

Horner’s Method

8/6/2019 horndn1-3

http://slidepdf.com/reader/full/horndn1-3 2/6

Evaluating Expressions Mathematics vs Computer Science

Mathematics makes no distinction between what a positive integral power meansand repeated multiplication. For example, in mathematics we see the following two

expressions as identical:

x3 and x·x·x

There is a big difference when it come to applying this and have a machine dothis. One of the methods is actually preferred over the other in a computer because it will increase both speed and accuracy.

Accuracy

Evaluating x·x·x is more accurate than x3 because the expression x·x·x is evaluatedusing a hardware device (inside the ALU) that will exactly calculate the value if thenumbers stay within the machines precision (i.e. they number is not too big or toosmall). This is not true in Mathematica, but is true in most compiled languages likeJAVA and C. The expression x3 is estimated using software. For most machinecomputations this is ok (machine words have gotten long).

8/6/2019 horndn1-3

http://slidepdf.com/reader/full/horndn1-3 3/6

Speed

Speed can be improved first bykeeping the processing in the ALU

instead of the memory whichperforms slower. This is whymultiplication is preferred over exponentiation.

There are ways to code expressionsthat cut down on the number of computations that even need to bedone.

CPU

Disk Memory

Computer

R

AM Memory

Control

Unit

ALU

Horner’s form of a polynomialAny polynomial in the form a 0+a 1 x+a 2 x2+…+a n xn can be written

a 0+x(a 1+x(a 2+x(…+a n x))…)

In this form we will reduce the number of computations that need to be done.

8/6/2019 horndn1-3

http://slidepdf.com/reader/full/horndn1-3 4/6

Example: 5+7 x+9 x2+2 x3 (Standard Form)

Horner’s Form:5+(7+(9+2· x)· x)· x

If you count the number of operation that are performed there are:

Standard form: 3 additions and 6 multiplications 7· x,9· x· x,2· x· x· x

Horner’s form: 3 additions and 3 multiplications

From this we see that Horner’s form for evaluating a polynomial is an improvementover the standard way.

Horner’s Algorithm

Here is the algorithm for Horner’s form

get list of coefficients { a0,a 1,a 2,…,a n}

express=0

For i=n,i ≥ 0, i-1

express=a i+ x·express

8/6/2019 horndn1-3

http://slidepdf.com/reader/full/horndn1-3 5/6

Improvements in Horner's Algorithm

There are several ways to improve Horner's Algorithm when it comes to howmuch multiplication needs to be done (i.e. how the powers are evaluated). A

binary approach can be used to limit the number of computations.

( )135+ x

Evaluating this expression in this form requires:

13 additions and 13 multiplications

14813

448

224

112

1 5

y y y y

y y y

y y y

y y y

x y

⋅⋅=

⋅=

⋅=

⋅=

+= Breaking the number 13 into its binaryrepresentation we see we can dramatically cutdown on the number of computations required.

1 addition and 5 multiplications

This gives a total of 6 computations compared tothe 26 that were required before. This uses onlyabout 23% of the computations that werepreviously required.

8/6/2019 horndn1-3

http://slidepdf.com/reader/full/horndn1-3 6/6

Find the most efficient way to evaluatethe polynomial to the right. ( ) ( ) ( ) ( )7737274

359−+−−−+− x x x x

Apply the standard Horner Method first.

From this we see the onlypowers of ( x-7)that arerequired are 1, 2 and 4.

( ) ( ) ( ) ( )( )( ) ( ) ( ) ( )( )( )( ) ( ) ( ) ( )( )( )( )422

622

842

74273717

74723717

74727317

−+−+−−−−

−+−+−−−−

−+−+−−−

x x x x

x x x x

x x x x

( )( )( )4221

224

112

1

4231

7

y y y y

y y y

y y y

x y

++−−

⋅=

⋅=

−=

We rewrite the polynomialusing only the powers

required from the standardHorner's Form.