~ubuntu-branches/ubuntu/raring/mricron/raring

« back to all changes in this revision

Viewing changes to fpmath/upolynom.pas

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-07-29 22:07:43 UTC
  • Revision ID: james.westby@ubuntu.com-20100729220743-q621ts2zj806gu0n
Tags: upstream-0.20100725.1~dfsg.1
ImportĀ upstreamĀ versionĀ 0.20100725.1~dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{ ******************************************************************
 
2
  Polynomials and rational fractions
 
3
  ****************************************************************** }
 
4
 
 
5
unit upolynom;
 
6
 
 
7
interface
 
8
 
 
9
uses
 
10
  utypes;
 
11
 
 
12
function Poly(X : Float; Coef : PVector; Deg : Integer) : Float;
 
13
{ ------------------------------------------------------------------
 
14
  Evaluates the polynomial :
 
15
  P(X) = Coef[0] + Coef[1] * X + Coef[2] * X^2 + ...
 
16
       + Coef[Deg] * X^Deg
 
17
  ------------------------------------------------------------------ }
 
18
 
 
19
function RFrac(X : Float; Coef : PVector; Deg1, Deg2 : Integer) : Float;
 
20
{ ------------------------------------------------------------------
 
21
  Evaluates the rational fraction :
 
22
 
 
23
           Coef[0] + Coef[1] * X + ... + Coef[Deg1] * X^Deg1
 
24
  F(X) = -----------------------------------------------------
 
25
         1 + Coef[Deg1+1] * X + ... + Coef[Deg1+Deg2] * X^Deg2
 
26
  ------------------------------------------------------------------ }
 
27
 
 
28
implementation
 
29
 
 
30
function Poly(X : Float; Coef : PVector; Deg : Integer) : Float;
 
31
var
 
32
  I : Integer;
 
33
  P : Float;
 
34
begin
 
35
  P := Coef^[Deg];
 
36
  for I := Pred(Deg) downto 0 do
 
37
    P := P * X + Coef^[I];
 
38
  Poly := P;
 
39
end;
 
40
 
 
41
function RFrac(X : Float; Coef : PVector; Deg1, Deg2 : Integer) : Float;
 
42
var
 
43
  I    : Integer;
 
44
  P, Q : Float;
 
45
begin
 
46
  P := Coef^[Deg1];
 
47
  for I := Pred(Deg1) downto 0 do
 
48
    P := P * X + Coef^[I];
 
49
  Q := 0.0;
 
50
  for I := (Deg1 + Deg2) downto Succ(Deg1) do
 
51
    Q := (Q + Coef^[I]) * X;
 
52
  RFrac := P / (1.0 + Q);
 
53
end;
 
54
 
 
55
end.