File: upolynom.pas

package info (click to toggle)
mricron 0.20140804.1~dfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,480 kB
  • ctags: 8,011
  • sloc: pascal: 114,853; sh: 49; makefile: 32
file content (55 lines) | stat: -rwxr-xr-x 1,568 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{ ******************************************************************
  Polynomials and rational fractions
  ****************************************************************** }

unit upolynom;

interface

uses
  utypes;

function Poly(X : Float; Coef : PVector; Deg : Integer) : Float;
{ ------------------------------------------------------------------
  Evaluates the polynomial :
  P(X) = Coef[0] + Coef[1] * X + Coef[2] * X^2 + ...
       + Coef[Deg] * X^Deg
  ------------------------------------------------------------------ }

function RFrac(X : Float; Coef : PVector; Deg1, Deg2 : Integer) : Float;
{ ------------------------------------------------------------------
  Evaluates the rational fraction :

           Coef[0] + Coef[1] * X + ... + Coef[Deg1] * X^Deg1
  F(X) = -----------------------------------------------------
         1 + Coef[Deg1+1] * X + ... + Coef[Deg1+Deg2] * X^Deg2
  ------------------------------------------------------------------ }

implementation

function Poly(X : Float; Coef : PVector; Deg : Integer) : Float;
var
  I : Integer;
  P : Float;
begin
  P := Coef^[Deg];
  for I := Pred(Deg) downto 0 do
    P := P * X + Coef^[I];
  Poly := P;
end;

function RFrac(X : Float; Coef : PVector; Deg1, Deg2 : Integer) : Float;
var
  I    : Integer;
  P, Q : Float;
begin
  P := Coef^[Deg1];
  for I := Pred(Deg1) downto 0 do
    P := P * X + Coef^[I];
  Q := 0.0;
  for I := (Deg1 + Deg2) downto Succ(Deg1) do
    Q := (Q + Coef^[I]) * X;
  RFrac := P / (1.0 + Q);
end;

end.