-
Notifications
You must be signed in to change notification settings - Fork 0
/
factd.m
50 lines (40 loc) · 804 Bytes
/
factd.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
function [f] = factd(n)
%FACTD Double Factorial function = n!!
%
%usage: f = factd(n)
%
%tested under version 5.3.1
%
% This function computes the double factorial of N.
% N may be complex and any size. Uses the included
% complex Gamma routine.
%
% f = n*(n-2)*(n-4)*...*5*3*1 for n odd
% f = n*(n-2)*(n-4)*...*6*4*2 for n even
%
%see also: Gamma, Fact
%Paul Godfrey
%pgodfrey@conexant.com
%8-29-00
[siz]=size(n);
n=n(:);
p=cos(pi*n)-1;
f=2.^((-p+n+n)/4).*pi.^(p/4).*gamma(1+n/2);
p=find(round(n)==n & imag(n)==0 & real(n)>=-1);
if ~isempty(p)
f(p)=round(f(p));
end
p=find(round(n/2)==(n/2) & imag(n)==0 & real(n)<-1);
if ~isempty(p)
f(p)=Inf;
end
f=reshape(f,siz);
return
%a demo of this routine is
n=-10:10;
n=n(:);
f=factd(n);
[n f]
ezplot factd
grid on
return