~ubuntu-branches/ubuntu/utopic/mricron/utopic

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{ ******************************************************************
  DiGamma and TriGamma functions.
  Contributed by Philip Fletcher (FLETCHP@WESTAT.com)
  ****************************************************************** }

unit udigamma;

interface

uses
  utypes;

function DiGamma(X : Float ) : Float;
function TriGamma(X : Float ) : Float;

implementation

function DiGamma(X : Float ) : Float;
{ ------------------------------------------------------------------

  Digamma calculates the Digamma or Psi function =
  d ( LOG ( GAMMA ( X ) ) ) / dX


   Reference:

     J Bernardo,
     Psi ( Digamma ) Function,
     Algorithm AS 103,
     Applied Statistics,
     Volume 25, Number 3, pages 315-317, 1976.

   Modified:

     03 January 2000

   Parameters:

     Input, real X, the argument of the Digamma function.
     0 < X.

     Output, real Digamma, the value of the Digamma function at X.
  ------------------------------------------------------------------ }

const
  c  = 20                     ;
  d1 = -0.57721566490153286061;  { DiGamma(1) }
  s  = 0.00001                ;

  { Sterling coefficient S(n) = B(n) / 2n
    where B(n) = Bernoulli number          }

const
  S2  =  0.08333333333333333333    ;  { B(2)/2   }
  S4  = -0.83333333333333333333E-2 ;  { B(4)/4   }
  S6  =  0.39682539682539682541E-2 ;  { B(6)/6   }
  S8  = -0.41666666666666666666E-2 ;  { B(8)/8   }
  S10 =  0.75757575757575757576E-2 ;  { B(10)/10 }
  S12 = -0.21092796092796092796E-1 ;  { B(12)/12 }
  S14 =  0.83333333333333333335E-1 ;  { B(14)/14 }
  S16 = -0.44325980392156862745    ;  { B(16)/16 }

var
  dg, p, r, y : Float ;

begin
  if X <= 0.0 then
    begin
      DiGamma := DefaultVal(FSing, MaxNum);
      Exit;
    end;

  SetErrCode(FOk);

  if X = 1.0 then
    begin
      DiGamma := D1;
      Exit;
    end;

  { Use approximation if argument <= S }

  if X <= s then
    dg := d1 - 1.0 / x
  else
    { Reduce the argument to dg(X + N) where (X + N) >= C }
    begin
      dg := 0.0;
      y := x ;

      while y < c do
        begin
          dg := dg - 1.0 / y;
          y := y + 1.0;
        end ;

    { Use Stirling's (actually de Moivre's) expansion if argument > C }

      r := 1.0 / sqr ( y ) ;
      p := (((((((S16 * r + S14) * r + S12) * r + S10) * r + S8) * r +
               S6) * r + S4) * r + S2) * r ;
      dg := dg + ln ( y ) - 0.5 / y - p ;
    end ;

  DiGamma := dg ;
end ;

function TriGamma(X : Float) : Float;
{ ------------------------------------------------------------------
   Trigamma calculates the Trigamma or Psi Prime function =
   d**2 ( LOG ( GAMMA ( X ) ) ) / dX**2


   Reference:

     Algorithm As121 Appl. Statist. (1978) vol 27, no. 1
 ******************************************************************** }

const
  a     = 1.0E-4 ;
  b     = 20 ;
  zero  = 0 ;
  one   = 1 ;
  half  = 0.5 ;

  { Bernoulli numbers }

const
  B2  =  0.1666666666666667     ;
  B4  = -3.333333333333333E-002 ;
  B6  =  2.380952380952381E-002 ;
  B8  = -3.333333333333333E-002 ;
  B10 =  7.575757575757576E-002 ;
  B12 = -0.2531135531135531     ;

var
  y, z, Res : Float ;

begin
  if X <= 0.0 then
    begin
      TriGamma := DefaultVal(FSing, MaxNum);
      Exit;
    end;

  SetErrCode(FOk);

  Res := 0 ;
  z := x ;

  if z <= a then  { Use small value approximation }
    begin
      TriGamma := one / sqr ( z ) ;
      Exit ;
    end ;

  while z < b do  { Increase argument to (x+i) >= b }
    begin
      Res := Res + one / sqr ( z ) ;
      z := z + one ;
    end ;

  { Apply asymptotic formula where argument >= b }
  y := one / sqr ( z ) ;
  Res := Res + Half * y + (One + y * (B2 + y * (B4 + y * (B6 + y *
                         (B8 + y* (B10 + y * B12)))))) / z;
  TriGamma := Res;
end ;

end.