Function: calcFunc-choose

calcFunc-choose is an autoloaded and byte-compiled function defined in calc-comb.el.gz.

Signature

(calcFunc-choose N M)

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calc-comb.el.gz
(defun calcFunc-choose (n m)   ; [I I I] [F F F] [Public]
  (cond ((and (integerp n) (integerp m) (<= m n) (>= m 0))
	 (if (> m (/ n 2))
	     (math-choose-iter (- n m) n 1 1)
	   (math-choose-iter m n 1 1)))
	((not (math-realp n))
	 (math-reject-arg n 'realp))
	((not (math-realp m))
	 (math-reject-arg m 'realp))
	((not (math-num-integerp m))
	 (if (and (math-num-integerp n) (math-negp n))
	     (list 'calcFunc-choose n m)
	   (math-div (calcFunc-fact (math-float n))
		     (math-mul (calcFunc-fact m)
			       (calcFunc-fact (math-sub n m))))))
        ;; For the extension to negative integer arguments we follow
        ;; M. J. Kronenburg, The Binomial Coefficient for Negative Arguments,
        ;; arXiv:1105.3689v2
        ((and (math-negp n) (not (math-negp m)))
         ;; n<0≤m: (n choose m) = (-1)^m (-n+m-1 choose m)
	 (let ((val (calcFunc-choose (math-add (math-sub m n) -1) m)))
	   (if (math-evenp (math-trunc m))
	       val
	     (math-neg val))))
        ((and (math-negp n) (math-num-integerp n))
         (if (math-lessp n m)
             0
           ;; m≤n<0: (n choose m) = (-1)^(n-m) (-m-1 choose n-m)
           (let ((val (calcFunc-choose (math-sub (math-neg m) 1)
                                       (math-sub n m))))
             (if (math-evenp (math-sub n m))
                 val
               (math-neg val)))))
	((math-negp m) 0)
	((and (math-num-integerp n)
	      (Math-lessp n m))
	 0)
	(t
	 (math-inexact-result)
	 (let ((tm (math-trunc m)))
	   (or (integerp tm) (math-reject-arg tm 'fixnump))
	   (if (> tm 100)
	       (math-div (calcFunc-fact (math-float n))
			 (math-mul (calcFunc-fact (math-float m))
				   (calcFunc-fact (math-float
						   (math-sub n m)))))
	     (math-with-extra-prec 1
	       (math-choose-float-iter tm n 1 1)))))))