source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/lightspeed/trigamma.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

  • Property svn:executable set to *
File size: 1.5 KB
Line 
1function y = trigamma(x)
2%TRIGAMMA   Trigamma function.
3% TRIGAMMA(X) returns trigamma(x) = d**2 log(gamma(x)) / dx**2
4% If X is a matrix, returns the trigamma function evaluated at each element.
5
6% Reference:
7%
8%    B Schneider,
9%    Trigamma Function,
10%    Algorithm AS 121,
11%    Applied Statistics,
12%    Volume 27, Number 1, page 97-99, 1978.
13%
14% From http://www.psc.edu/~burkardt/src/dirichlet/dirichlet.f
15
16small = 1e-4;
17large = 8;
18c = pi^2/6;
19c1 = -2.404113806319188570799476;
20b2 =  1/6;
21b4 = -1/30;
22b6 =  1/42;
23b8 = -1/30;
24b10 = 5/66;
25
26% Initialize
27y = zeros(size(x));
28
29% illegal values
30i = find(isnan(x) | (x == -inf));
31if ~isempty(i)
32  y(i) = nan;
33end
34
35% zero or negative integer
36i = find((x <= 0) & (floor(x)==x));
37if ~isempty(i)
38  y(i) = Inf;
39end
40
41% Negative non-integer
42i = find((x < 0) & (floor(x) ~= x));
43if ~isempty(i)
44  % Use the derivative of the digamma reflection formula:
45  % -trigamma(-x) = trigamma(x+1) - (pi*csc(pi*x))^2
46  y(i) = -trigamma(-x(i)+1) + (pi*csc(-pi*x(i))).^2;
47end
48 
49% Small value approximation
50i = find(x > 0 & x <= small);
51if ~isempty(i)
52  y(i) = 1./(x(i).*x(i)) + c + c1*x(i);
53end
54
55% Reduce to trigamma(x+n) where ( X + N ) >= large.
56while(1)
57  i = find(x > small & x < large);
58  if isempty(i)
59    break
60  end
61  y(i) = y(i) + 1./(x(i).*x(i));
62  x(i) = x(i) + 1;
63end
64
65% Apply asymptotic formula when X >= large
66i = find(x >= large);
67if ~isempty(i)
68  z = 1./(x(i).*x(i));
69  y(i) = y(i) + 0.5*z + (1.0 + z.*(b2 + z.*(b4 + z.*(b6 + z.*(b8 + z.*b10))))) ./ x(i);
70end
Note: See TracBrowser for help on using the repository browser.