2313
|
1 ## Copyright (C) 1996 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
1026
|
19 |
2311
|
20 ## usage: gammai (a, x) |
|
21 ## |
|
22 ## Computes the incomplete gamma function |
|
23 ## |
2325
|
24 ## gammai (a, x) |
2311
|
25 ## = (integral from 0 to x of exp(-t) t^(a-1) dt) / gamma(a). |
|
26 ## |
|
27 ## If a is scalar, then gammai(a, x) is returned for each element of x |
|
28 ## and vice versa. |
|
29 ## |
|
30 ## If neither a nor x is scalar, the sizes of a and x must agree, and |
|
31 ## gammai is applied pointwise. |
|
32 |
2312
|
33 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
34 ## Created: 13 August 1994 |
|
35 ## Adapted-By: jwe |
|
36 |
1026
|
37 function y = gammai (a, x) |
2325
|
38 |
715
|
39 if (nargin != 2) |
1026
|
40 usage ("gammai (a, x)"); |
715
|
41 endif |
2325
|
42 |
1026
|
43 [r_a, c_a] = size (a); |
|
44 [r_x, c_x] = size (x); |
715
|
45 e_a = r_a * c_a; |
|
46 e_x = r_x * c_x; |
2325
|
47 |
2303
|
48 ## The following code is rather ugly. We want the function to work |
2325
|
49 ## whenever a and x have the same size or a or x is scalar. |
2303
|
50 ## We do this by reducing the latter cases to the former. |
2325
|
51 |
1026
|
52 if (e_a == 0 || e_x == 0) |
|
53 error ("gammai: both a and x must be nonempty"); |
715
|
54 endif |
1026
|
55 if (r_a == r_x && c_a == c_x) |
715
|
56 n = e_a; |
1026
|
57 a = reshape (a, 1, n); |
|
58 x = reshape (x, 1, n); |
715
|
59 r_y = r_a; |
|
60 c_y = c_a; |
|
61 elseif (e_a == 1) |
|
62 n = e_x; |
1026
|
63 a = a * ones (1, n); |
|
64 x = reshape (x, 1, n); |
715
|
65 r_y = r_x; |
|
66 c_y = c_x; |
|
67 elseif (e_x == 1) |
|
68 n = e_a; |
1026
|
69 a = reshape (a, 1, n); |
|
70 x = x * ones (1, n); |
715
|
71 r_y = r_a; |
|
72 c_y = c_a; |
|
73 else |
2325
|
74 error ("gammai: a and x must have the same size if neither is scalar"); |
715
|
75 endif |
|
76 |
2303
|
77 ## Now we can do sanity checking ... |
2325
|
78 |
904
|
79 if (any (a <= 0) || any (a == Inf)) |
1026
|
80 error ("gammai: all entries of a must be positive anf finite"); |
715
|
81 endif |
904
|
82 if (any (x < 0)) |
1026
|
83 error ("gammai: all entries of x must be nonnegative"); |
715
|
84 endif |
2325
|
85 |
1586
|
86 y = zeros (1, n); |
715
|
87 |
2303
|
88 ## For x < a + 1, use summation. The below choice of k should ensure |
2325
|
89 ## that the overall error is less than eps ... |
904
|
90 |
1026
|
91 S = find ((x > 0) & (x < a + 1)); |
|
92 s = length (S); |
715
|
93 if (s > 0) |
1026
|
94 k = ceil (- max ([a(S), x(S)]) * log (eps)); |
715
|
95 K = (1:k)'; |
1586
|
96 M = ones (k, 1); |
|
97 A = cumprod ((M * x(S)) ./ (M * a(S) + K * ones(1, s))); |
1026
|
98 y(S) = exp (-x(S) + a(S) .* log (x(S))) .* (1 + sum (A)) ./ gamma (a(S)+1); |
715
|
99 endif |
|
100 |
2303
|
101 ## For x >= a + 1, use the continued fraction. |
|
102 ## Note, however, that this converges MUCH slower than the series |
|
103 ## expansion for small a and x not too large! |
904
|
104 |
1586
|
105 S = find ((x >= a + 1) & (x < Inf)); |
|
106 s = length (S); |
715
|
107 if (s > 0) |
1586
|
108 t1 = zeros (1, s); |
|
109 t2 = ones (1, s); |
|
110 u = [t1; t2]; |
|
111 v = [t2; x(S)]; |
715
|
112 c_old = 0; |
|
113 c_new = v(1,:) ./ v(2,:); |
|
114 n = 1; |
1026
|
115 while (max (abs (c_old ./ c_new - 1)) > 10 * eps) |
715
|
116 c_old = c_new; |
1026
|
117 u = v + u .* (ones (2, 1) * (n - a(S))); |
|
118 v = u .* (ones (2, 1) * x(S)) + n * v; |
715
|
119 c_new = v(1,:) ./ v(2,:); |
|
120 n = n + 1; |
|
121 endwhile |
1026
|
122 y(S) = 1 - exp (-x(S) + a(S) .* log (x(S))) .* c_new ./ gamma (a(S)); |
715
|
123 endif |
2325
|
124 |
1026
|
125 y (find (x == Inf)) = ones (1, sum (x == Inf)); |
2325
|
126 |
1026
|
127 y = reshape (y, r_y, c_y); |
715
|
128 |
1585
|
129 endfunction |