forked from arichar6/raycon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcgamma.m
63 lines (63 loc) · 1.76 KB
/
cgamma.m
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
function out = cgamma(z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Complex gamma function for -10<Re(z)<10, 10<Im(z)<10
%
% Tested 12 digits accuracy for
% cgamma([0 .5+.5i -.5+.5i -.5-.5i .5-.5i 1 1+i i -1+i -1 -1-i -i 1-i]')
%
% A. JAUN, Numerical Analysis, KTH, 100 44 Stockholm, Sweden
% A.N. KAUFMAN, Lawrence Berkeley Laboratory, Berkeley, CA 94720, USA
% E.R. TRACY, College of William & Mary, Williamsburg, VA 23187-8795, USA
%
% Documented under "http://www.nada.kth.se/~jaun"
%
% (C) Version 7.0, 14-Aug-2006. All Rights Reserved.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
x=real(z); y=imag(z); n=prod(size(x));
%
% ----- Map to domain of asymptotic expansion
%
for k=1:n
if (y(k)<0)
z(k)=conj(z(k));
end
end
%
lnCgamma=zeros(size(x));
for k=1:n
for m=1:floor(9-x)
lnCgamma(k)=lnCgamma(k)-log(z(k)); z(k)=z(k)+1;
end
end
%
% ----- First compute log(cgamma) from expansion for 9<Re(z)<10, 0<Im(z)<10
%
zm =1./z; zm1 =zm; zm2=zm1.*zm1; % Avoid power function
zm=zm.*zm2; zm3 =zm;
zm=zm.*zm2; zm5 =zm;
zm=zm.*zm2; zm7 =zm;
zm=zm.*zm2; zm9 =zm;
zm=zm.*zm2; zm11=zm;
zm=zm.*zm2; zm13=zm;
zm=zm.*zm2; zm15=zm;
%
lnCgamma=lnCgamma -zm15*(3617./122400); % Add series in reverse order
lnCgamma=lnCgamma +zm13/156;
lnCgamma=lnCgamma -zm11*(691./360360);
lnCgamma=lnCgamma +zm9 /1188;
lnCgamma=lnCgamma -zm7 /1680;
lnCgamma=lnCgamma +zm5 /1260;
lnCgamma=lnCgamma -zm3 /360;
lnCgamma=lnCgamma +zm1 /12 +(z-0.5).*log(z) -z +0.5*log(2*pi);
%
for k=1:n
% if (y(k)>0) % Don't understand why not <0
if (y(k)<0) % modified by Steve Richardson
lnCgamma(k)=conj(lnCgamma(k));
end
end
%
% ----- Take exponential
%
out = exp(lnCgamma);