Efficient Computations and Horner’s Method

Download Report

Transcript Efficient Computations and Horner’s Method

Efficient Computations and Horner’s Method

Evaluating Expressions Mathematics vs Computer Science Mathematics makes no distinction between what a positive integral power means and repeated multiplication. For example, in mathematics we see the following two expressions as identical:

x

3 and

x·x·x

There is a big difference when it come to applying this and have a machine do this. 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

x

3 because the expression

x·x·x

is evaluated using a

hardware

device (inside the ALU) that will exactly calculate the value if the numbers stay within the machines precision (i.e. they number is not too big or too small). This is not true in Mathematica, but is true in most compiled languages like JAVA and C. The expression

x

3 is estimated using software. For most machine computations this is ok (machine words have gotten long).

Speed Speed can be improved first by keeping the processing in the ALU instead of the memory which performs slower. This is why multiplication is preferred over exponentiation. There are ways to code expressions that cut down on the number of computations that even need to be done.

ALU Control Unit Disk Memory CPU Computer Horner’s form of a polynomial Any polynomial in the form

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

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.

Example: 5+7

x

+9

x

2 +2

x

3 (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 improvement over the standard way.

Horner’s Algorithm Here is the algorithm for Horner’s form get list of coefficients { a 0 ,a 1 ,a 2 ,…,a n } express=0 For i=n,i  0, i-1 express=a i + x·express

Improvements in Horner's Algorithm There are several ways to improve Horner's Algorithm when it comes to how much multiplication needs to be done (i.e. how the powers are evaluated). A binary approach can be used to limit the number of computations.

x

 5  13 Evaluating this expression in this form requires: 13 additions and 13 multiplications

y

1 

x

 5

y

2 

y

1 

y

1

y

4 

y

2 

y

2

y

8

y

13  

y

4

y

8  

y

4

y

4 

y

1 Breaking the number 13 into its binary representation we see we can dramatically cut down on the number of computations required.

1 addition and 5 multiplications This gives a total of 6 computations compared to the 26 that were required before. This uses only about 23% of the computations that were previously required.

Find the most efficient way to evaluate the polynomial to the right.

4 

x

 7  9  2 

x

 7  5  3 

x

 7 

x

 7  Apply the standard Horner Method first. From this we see the only powers of (

x

-7)that are required are 1, 2 and 4.

x

x

x

   7 7 7     1    3 

x x x

   7 7 7  2  2  2      3 3 2    

x

2 

x

x

7  4  4 

x

   7 7  2  2  2   4  4 

x x

7    8 7   6 7  4      We rewrite the polynomial using only the powers required from the standard Horner's Form.

y

1 

x

 7

y

2 

y

1 

y

1

y

4

y

1  1  

y

2

y

2 

y

2   3 

y

2  2  4

y

4   