60 likes | 231 Views
Polynomial Evaluation. Straightforward Evaluation . P(x) = 3x 5 +2x 4 +7x 3 +8x 2 +2x+4 t1 = (3*x*x*x*x*x) t2 = t1 + (2*x*x*x*x) t3 = t2 + (7*x*x*x) t4 = t3 + (8*x*x) P = t4 +(2*x) + 4 15 Multiplications, 5 Additions. A Little Smarter. P(x) = 3x 5 +2x 4 +7x 3 +8x 2 +2x+4
E N D
Straightforward Evaluation • P(x) = 3x5+2x4+7x3+8x2+2x+4 • t1 = (3*x*x*x*x*x) • t2 = t1 + (2*x*x*x*x) • t3 = t2 + (7*x*x*x) • t4 = t3 + (8*x*x) • P = t4 +(2*x) + 4 • 15 Multiplications, 5 Additions
A Little Smarter • P(x) = 3x5+2x4+7x3+8x2+2x+4 • t1 := 4; xp := x; • t2 := (2*xp) + t1; xp := xp * x; • t3 := (8*xp) + t2; xp := xp * x; • t4 := (7*xp) + t3; xp := xp * x; • t5 := (2*xp) + t4; xp := xp * x; • P := (3*xp) + t5; • 9 Multiplications, 5 Additions
Horner’s Rule • P(x) = 3x5+2x4+7x3+8x2+2x+4 • t1 = (3*x) + 2 • t2 = (t1*x) + 7 • t3 = (t2*x) + 8 • t4 = (t3*x) + 2 • P = (t4*x) + 4 • 5 Multiplications, 5 Additions
Computing Powers xp := x; pwork := power; Res := 1; While pwork > 0 Do If pwork mod 2 = 1 then Res := Res * xp; End If xp := xp * xp; pwork := pwork / 2; /* integer division */ End While
For Power = 15 • 6 Multiplications by Squaring Algorithm • An Alternative Procedure: • p = x * x • p = p * p *x • p = p * p * p • 5 Multiplications by Factorization