home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / miscellaneous / bincoeff.m next >
Text File  |  1999-10-26  |  3KB  |  103 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## -*- texinfo -*-
  18. ## @deftypefn {Mapping Function} {} bincoeff (@var{n}, @var{k})
  19. ## Return the binomial coefficient of @var{n} and @var{k}, defined as
  20. ## @iftex
  21. ## @tex
  22. ## $$
  23. ##  {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!}
  24. ## $$
  25. ## @end tex
  26. ## @end iftex
  27. ## @ifinfo
  28. ## 
  29. ## @example
  30. ## @group
  31. ##  /   \
  32. ##  | n |    n (n-1) (n-2) ... (n-k+1)
  33. ##  |   |  = -------------------------
  34. ##  | k |               k!
  35. ##  \   /
  36. ## @end group
  37. ## @end example
  38. ## @end ifinfo
  39. ## 
  40. ## For example,
  41. ## 
  42. ## @example
  43. ## @group
  44. ## bincoeff (5, 2)
  45. ##      @result{} 10
  46. ## @end group
  47. ## @end example
  48. ## @end deftypefn
  49.  
  50. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  51. ## Created: 8 October 1994
  52. ## Adapted-By: jwe
  53.  
  54. function b = bincoeff (n, k)
  55.   
  56.   if (nargin != 2)
  57.     usage ("bincoeff (n, k)");
  58.   endif
  59.   
  60.   [retval, n, k] = common_size (n, k);
  61.   if (retval > 0)
  62.     error ("bincoeff:  n and k must be of common size or scalars");
  63.   endif
  64.   
  65.   [r, c] = size (n);
  66.   s = r * c;
  67.   n   = reshape (n, s, 1);
  68.   k   = reshape (k, s, 1);
  69.   b   = zeros (s, 1);
  70.   
  71.   ind = find (! (k >= 0) | (k != real (round (k))) | isnan (n));
  72.   if (any (ind))
  73.     b(ind) = NaN * ones (length (ind), 1);
  74.   endif
  75.   
  76.   ind = find (k == 0);
  77.   if (any (ind))
  78.     b(ind) = ones (length (ind), 1);
  79.   endif
  80.  
  81.   ind = find ((k > 0) & ((n == real (round (n))) & (n < 0)));
  82.   if any (ind)
  83.     b(ind) = (-1) .^ k(ind) .* exp (lgamma (abs (n(ind)) + k(ind)) ...
  84.     - lgamma (k(ind) + 1) - lgamma (abs (n(ind))));
  85.   endif
  86.   
  87.   ind = find ((k > 0) & ((n != real (round (n))) | (n >= k)));
  88.   if (length (ind) > 0)
  89.     b(ind) = exp (lgamma (n(ind) + 1) - lgamma (k(ind) + 1) ...
  90.     - lgamma (n(ind) - k(ind) + 1));
  91.   endif
  92.   
  93.   ## clean up rounding errors
  94.   ind = find (n == round (n));
  95.   if (any (ind))
  96.     b(ind) = round (b(ind));
  97.   endif
  98.   
  99.   b = reshape (b, r, c);
  100.   
  101. endfunction
  102.  
  103.