comparison scripts/specfun/nchoosek.m @ 11895:a85bde34b8b9 release-3-0-x

Set max_recursion_depth and use a subfunction in nchoosek
author Francesco Potortì <pot@gnu.org>
date Mon, 08 Dec 2008 13:18:26 +0100
parents a1dbe9d80eee
children 343f0fbca6eb 5032328e940b
comparison
equal deleted inserted replaced
11894:af0adfbd3d16 11895:a85bde34b8b9
73 elseif (k == 1) 73 elseif (k == 1)
74 A = v(:); 74 A = v(:);
75 elseif (k == n) 75 elseif (k == n)
76 A = v(:).'; 76 A = v(:).';
77 else 77 else
78 m = round (exp (sum (log (k:n-1)) - sum (log (2:n-k)))); 78 oldmax = max_recursion_depth ();
79 A = [v(1)*ones(m,1), nchoosek(v(2:n),k-1); 79 unwind_protect
80 nchoosek(v(2:n),k)]; 80 max_recursion_depth (n);
81 A = nck (v, k);
82 unwind_protect_cleanup
83 max_recursion_depth (oldmax);
84 end_unwind_protect
81 endif 85 endif
86 endfunction
82 87
88 function A = nck (v, k)
89 n = length (v);
90 if (n == 1 || k < 2 || k == n)
91 A = nchoosek (v, k);
92 else
93 m = nchoosek (n-1, k-1);
94 A = [v(1)*ones(m,1), nck(v(2:n),k-1);
95 nck(v(2:n), k)];
96 endif
83 endfunction 97 endfunction
98
99 %!assert (nchoosek(100,45), bincoeff(100,45))
100 %!assert (nchoosek(1:5,3),[1:3;1,2,4;1,2,5;1,3,4;1,3,5;1,4,5;2:4;2,3,5;2,4,5;3:5])