Polynomial Evaluation

6
Polynomial Evaluation

description

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 - PowerPoint PPT Presentation

Transcript of Polynomial Evaluation

Page 1: Polynomial Evaluation

Polynomial Evaluation

Page 2: Polynomial Evaluation

Straightforward Evaluation

• P(x) = 3xP(x) = 3x55+2x+2x44+7x+7x33+8x+8x22+2x+4+2x+4

• t1 = (3*x*x*x*x*x)t1 = (3*x*x*x*x*x)

• t2 = t1 + (2*x*x*x*x)t2 = t1 + (2*x*x*x*x)

• t3 = t2 + (7*x*x*x)t3 = t2 + (7*x*x*x)

• t4 = t3 + (8*x*x)t4 = t3 + (8*x*x)

• P = t4 +(2*x) + 4P = t4 +(2*x) + 4

• 15 Multiplications, 5 Additions15 Multiplications, 5 Additions

Page 3: Polynomial Evaluation

A Little Smarter• P(x) = 3xP(x) = 3x55+2x+2x44+7x+7x33+8x+8x22+2x+4+2x+4• t1 := 4; xp := x;t1 := 4; xp := x;• t2 := (2*xp) + t1; xp := xp * x;t2 := (2*xp) + t1; xp := xp * x;• t3 := (8*xp) + t2; xp := xp * x;t3 := (8*xp) + t2; xp := xp * x;• t4 := (7*xp) + t3; xp := xp * x;t4 := (7*xp) + t3; xp := xp * x;• t5 := (2*xp) + t4; xp := xp * x;t5 := (2*xp) + t4; xp := xp * x;• P := (3*xp) + t5;P := (3*xp) + t5;• 9 Multiplications, 5 Additions9 Multiplications, 5 Additions

Page 4: Polynomial Evaluation

Horner’s Rule

• P(x) = 3xP(x) = 3x55+2x+2x44+7x+7x33+8x+8x22+2x+4+2x+4

• t1 = (3*x) + 2t1 = (3*x) + 2

• t2 = (t1*x) + 7t2 = (t1*x) + 7

• t3 = (t2*x) + 8t3 = (t2*x) + 8

• t4 = (t3*x) + 2t4 = (t3*x) + 2

• P = (t4*x) + 4P = (t4*x) + 4

• 5 Multiplications, 5 Additions5 Multiplications, 5 Additions

Page 5: Polynomial Evaluation

Computing Powers

xp := x; pwork := power; Res := 1;xp := x; pwork := power; Res := 1;While pwork > 0 DoWhile pwork > 0 Do If pwork mod 2 = 1 thenIf pwork mod 2 = 1 then Res := Res * xp;Res := Res * xp; End IfEnd If xp := xp * xp;xp := xp * xp; pwork := pwork / 2; /* integer division */pwork := pwork / 2; /* integer division */End WhileEnd While

Page 6: Polynomial Evaluation

For Power = 15

• 6 Multiplications by Squaring Algorithm6 Multiplications by Squaring Algorithm

• An Alternative Procedure:An Alternative Procedure:

• p = x * xp = x * x

• p = p * p *xp = p * p *x

• p = p * p * pp = p * p * p

• 5 Multiplications by Factorization5 Multiplications by Factorization