]> code.delx.au - gnu-emacs/blobdiff - lisp/calc/calc-comb.el
(ediff-diff-options): fixed the doc string
[gnu-emacs] / lisp / calc / calc-comb.el
index 5772a90f6acc1cf72f0e6581756435955e41d297..7c274d39aa61b99c0001585a6e83c857daef97ed 100644 (file)
@@ -1,10 +1,10 @@
 ;;; calc-comb.el --- combinatoric functions for Calc
 
-;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
+;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
+;;   2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: David Gillespie <daveg@synaptics.com>
-;; Maintainers: D. Goel <deego@gnufans.org>
-;;              Colin Walters <walters@debian.org>
+;; Maintainer: Jay Belanger <belanger@truman.edu>
 
 ;; This file is part of GNU Emacs.
 
 ;;; Code:
 
 ;; This file is autoloaded from calc-ext.el.
-(require 'calc-ext)
 
+(require 'calc-ext)
 (require 'calc-macs)
 
-(defun calc-Need-calc-comb () nil)
-
 (defconst math-primes-table
   [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
      97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181
      4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973
      4987 4993 4999 5003])
 
+;; The variable math-prime-factors-finished is set by calcFunc-prfac to 
+;; indicate whether factoring is complete, and used by calcFunc-factors,
+;; calcFunc-totient and calcFunc-moebius.
+(defvar math-prime-factors-finished)
+
 ;;; Combinatorics
 
 (defun calc-gcd (arg)
          (res (math-prime-test n iters)))
      (calc-report-prime-test res))))
 
+(defvar calc-verbose-nextprime nil)
+
 (defun calc-next-prime (iters)
   (interactive "p")
   (calc-slow-wrapper
                   (if (math-evenp temp)
                       even
                     (math-div (calcFunc-fact n) even))))
-            (list 'calcFunc-dfact max))))
+            (list 'calcFunc-dfact n))))
        ((equal n '(var inf var-inf)) n)
        (t (calc-record-why 'natnump n)
           (list 'calcFunc-dfact n))))
   (math-stirling-number n m 0))
 
 (defvar math-stirling-cache (vector [[1]] [[1]]))
+
+;; The variable math-stirling-local-cache is local to
+;; math-stirling-number, but is used by math-stirling-1
+;; and math-stirling-2, which are called by math-stirling-number.
+(defvar math-stirling-local-cache)
+
 (defun math-stirling-number (n m k)
   (or (math-num-natnump n) (math-reject-arg n 'natnump))
   (or (math-num-natnump m) (math-reject-arg m 'natnump))
   (or (integerp m) (math-reject-arg m 'fixnump))
   (if (< n m)
       0
-    (let ((cache (aref math-stirling-cache k)))
-      (while (<= (length cache) n)
-       (let ((i (1- (length cache)))
+    (let ((math-stirling-local-cache (aref math-stirling-cache k)))
+      (while (<= (length math-stirling-local-cache) n)
+       (let ((i (1- (length math-stirling-local-cache)))
              row)
-         (setq cache (vconcat cache (make-vector (length cache) nil)))
-         (aset math-stirling-cache k cache)
-         (while (< (setq i (1+ i)) (length cache))
-           (aset cache i (setq row (make-vector (1+ i) nil)))
+         (setq math-stirling-local-cache 
+                (vconcat math-stirling-local-cache 
+                         (make-vector (length math-stirling-local-cache) nil)))
+         (aset math-stirling-cache k math-stirling-local-cache)
+         (while (< (setq i (1+ i)) (length math-stirling-local-cache))
+           (aset math-stirling-local-cache i (setq row (make-vector (1+ i) nil)))
            (aset row 0 0)
            (aset row i 1))))
       (if (= k 1)
        (math-stirling-2 n m)))))
 
 (defun math-stirling-1 (n m)
-  (or (aref (aref cache n) m)
-      (aset (aref cache n) m
+  (or (aref (aref math-stirling-local-cache n) m)
+      (aset (aref math-stirling-local-cache n) m
            (math-add (math-stirling-1 (1- n) (1- m))
                      (math-mul (- 1 n) (math-stirling-1 (1- n) m))))))
 
 (defun math-stirling-2 (n m)
-  (or (aref (aref cache n) m)
-      (aset (aref cache n) m
+  (or (aref (aref math-stirling-local-cache n) m)
+      (aset (aref math-stirling-local-cache n) m
            (math-add (math-stirling-2 (1- n) (1- m))
                      (math-mul m (math-stirling-2 (1- n) m))))))
 
 
 ;;; Produce a random 10-bit integer, with (random) if no seed provided,
 ;;; or else with Numerical Recipes algorithm ran3 / Knuth 3.2.2-A.
+
+(defvar var-RandSeed)
+(defvar math-random-cache nil)
+(defvar math-gaussian-cache nil)
+
 (defun math-init-random-base ()
   (if (and (boundp 'var-RandSeed) var-RandSeed)
       (if (eq (car-safe var-RandSeed) 'vec)
     (random t)
     (setq var-RandSeed nil
          math-random-cache nil
-         i 0
          math-random-shift -4)  ; assume RAND_MAX >= 16383
     ;; This exercises the random number generator and also helps
     ;; deduce a better value for RAND_MAX.
-    (while (< (setq i (1+ i)) 30)
-      (if (> (lsh (math-abs (random)) math-random-shift) 4095)
-         (setq math-random-shift (1- math-random-shift)))))
+    (let ((i 0))
+      (while (< (setq i (1+ i)) 30)
+        (if (> (lsh (math-abs (random)) math-random-shift) 4095)
+            (setq math-random-shift (1- math-random-shift))))))
   (setq math-last-RandSeed var-RandSeed
        math-gaussian-cache nil))
 
 ;;; Produce a random digit in the range 0..999.
 ;;; Avoid various pitfalls that may lurk in the built-in (random) function!
 ;;; Shuffling algorithm from Numerical Recipes, section 7.1.
+(defvar math-random-last)
 (defun math-random-digit ()
   (let (i)
     (or (and (boundp 'var-RandSeed) (eq var-RandSeed math-last-RandSeed))
             (aset math-random-cache i (math-random-base))
             (>= math-random-last 1000)))
     math-random-last))
-(setq math-random-cache nil)
 
 ;;; Produce an N-digit random integer.
 (defun math-random-digits (n)
          (setq math-gaussian-cache (cons calc-internal-prec
                                          (math-mul v1 fac)))
          (math-mul v2 fac))))))
-(setq math-gaussian-cache nil)
 
 ;;; Produce a random integer or real 0 <= N < MAX.
 (defun calcFunc-random (max)
 ;;;        (nil unknown) if non-prime with no known factors,
 ;;;        (t) if prime,
 ;;;        (maybe N P) if probably prime (after N iters with probability P%)
+(defvar math-prime-test-cache '(-1))
+
+(defvar math-prime-test-cache-k)
+(defvar math-prime-test-cache-q)
+(defvar math-prime-test-cache-nm1)
+
 (defun math-prime-test (n iters)
   (if (and (Math-vectorp n) (cdr n))
       (setq n (nth (1- (length n)) n)))
                      (1- iters)
                    0)))
     res))
-(defvar math-prime-test-cache '(-1))
 
 (defun calcFunc-prime (n &optional iters)
   (or (math-num-integerp n) (math-reject-arg n 'integerp))
     (if (Math-realp n)
        (calcFunc-nextprime (math-trunc n) iters)
       (math-reject-arg n 'integerp))))
-(setq calc-verbose-nextprime nil)
 
 (defun calcFunc-prevprime (n &optional iters)
   (if (Math-integerp n)
        (aref math-primes-table hi))
     2))
 
+(provide 'calc-comb)
 
-
-
+;;; arch-tag: 1d75ee9b-0815-42bd-a321-bb3dc001cc02
 ;;; calc-comb.el ends here