Sign in Join
Wiki copytemporary

Revision 1 of 1, by Trev on 2-23-2020. The page as it is now.

Polynomial evaluation

Introduction

It is often useful to evaluate a polynomial at a given point. The method shown below allows to calculate it by using only multiplication and addition, without any power.

Horner's method

Only 1-degree polynomial are evaluated, e.g. 3.x^2 + 2.x + 1, can be written, by means of an astute factorization, ((3.x +2).x + 1).

pascal
program PolyTest;

type 
  // coef's rank = it's degree
  TDegres = 0..10; 
  TPolynome = record 
    Deg: TDegres; 
    Coefs: array[TDegres] of Double; 
  end;

function PolyEval(const aPoly: TPolynome; X: Double): Double; 
var 
  i: TDegres; 
begin 
  with aPoly do 
  begin 
    Result := 0; 
    for i := Deg downto Low(Coefs) do 
      Result := Result * X + Coefs[i]; 
  end; 
End; 
  
(* Exemples *) 
var 
  P: TPolynome; 
begin 
  P.Deg := 2; 
  P.Coefs[2] := 3; 
  P.Coefs[1] := 2; 
  P.Coefs[0] := 1;   
  WriteLn('P(',7,') = ', PolyEval(7):10:2);  
  
  P.Deg := 0; 
  P.Coefs[2] := 0; 
  P.Coefs[1] := 0; 
  P.Coefs[0] := 1;   
  WriteLn('P(',7,') = ', PolyEval(7):10:2);  
End

See also

[https://en.wikipedia.org/wiki/Horner%27s_method| Horner's method (wiki)]


From the Free Pascal wiki, page "Polynomial evaluation", as it stood on 2020-02-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.