Add a provide statement.
[bpt/emacs.git] / lisp / calc / calc-comb.el
CommitLineData
3132f345
CW
1;;; calc-comb.el --- combinatoric functions for Calc
2
bf77c646 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3132f345
CW
4
5;; Author: David Gillespie <daveg@synaptics.com>
43f34ccc 6;; Maintainer: Jay Belanger <belanger@truman.edu>
136211a9
EZ
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY. No author or distributor
12;; accepts responsibility to anyone for the consequences of using it
13;; or for whether it serves any particular purpose or works at all,
14;; unless he says so in writing. Refer to the GNU Emacs General Public
15;; License for full details.
16
17;; Everyone is granted permission to copy, modify and redistribute
18;; GNU Emacs, but only under the conditions described in the
19;; GNU Emacs General Public License. A copy of this license is
20;; supposed to have been given to you along with GNU Emacs so you
21;; can know your rights and responsibilities. It should be in a
22;; file named COPYING. Among other things, the copyright notice
23;; and this notice must be preserved on all copies.
24
3132f345 25;;; Commentary:
136211a9 26
3132f345 27;;; Code:
136211a9
EZ
28
29;; This file is autoloaded from calc-ext.el.
136211a9 30
43f34ccc 31(require 'calc-ext)
136211a9
EZ
32(require 'calc-macs)
33
3132f345
CW
34(defconst math-primes-table
35 [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
36 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181
37 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277
38 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383
39 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487
40 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601
41 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709
42 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827
43 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947
44 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049
45 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151
46 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249
47 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361
48 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459
49 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559
50 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657
51 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759
52 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877
53 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997
54 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089
55 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213
56 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311
57 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411
58 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543
59 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663
60 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741
61 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851
62 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969
63 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089
64 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221
65 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331
66 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461
67 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557
68 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671
69 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779
70 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907
71 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013
72 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129
73 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243
74 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363
75 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493
76 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621
77 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729
78 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871
79 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973
80 4987 4993 4999 5003])
136211a9 81
8d7498c1
JB
82;; The variable math-prime-factors-finished is set by calcFunc-prfac to
83;; indicate whether factoring is complete, and used by calcFunc-factors,
84;; calcFunc-totient and calcFunc-moebius.
85(defvar math-prime-factors-finished)
86
136211a9
EZ
87;;; Combinatorics
88
89(defun calc-gcd (arg)
90 (interactive "P")
91 (calc-slow-wrapper
bf77c646 92 (calc-binary-op "gcd" 'calcFunc-gcd arg)))
136211a9
EZ
93
94(defun calc-lcm (arg)
95 (interactive "P")
96 (calc-slow-wrapper
bf77c646 97 (calc-binary-op "lcm" 'calcFunc-lcm arg)))
136211a9
EZ
98
99(defun calc-extended-gcd ()
100 (interactive)
101 (calc-slow-wrapper
bf77c646 102 (calc-enter-result 2 "egcd" (cons 'calcFunc-egcd (calc-top-list-n 2)))))
136211a9
EZ
103
104(defun calc-factorial (arg)
105 (interactive "P")
106 (calc-slow-wrapper
bf77c646 107 (calc-unary-op "fact" 'calcFunc-fact arg)))
136211a9
EZ
108
109(defun calc-gamma (arg)
110 (interactive "P")
111 (calc-slow-wrapper
bf77c646 112 (calc-unary-op "gmma" 'calcFunc-gamma arg)))
136211a9
EZ
113
114(defun calc-double-factorial (arg)
115 (interactive "P")
116 (calc-slow-wrapper
bf77c646 117 (calc-unary-op "dfac" 'calcFunc-dfact arg)))
136211a9
EZ
118
119(defun calc-choose (arg)
120 (interactive "P")
121 (calc-slow-wrapper
122 (if (calc-is-hyperbolic)
123 (calc-binary-op "perm" 'calcFunc-perm arg)
bf77c646 124 (calc-binary-op "chos" 'calcFunc-choose arg))))
136211a9
EZ
125
126(defun calc-perm (arg)
127 (interactive "P")
128 (calc-hyperbolic-func)
bf77c646 129 (calc-choose arg))
136211a9
EZ
130
131(defvar calc-last-random-limit '(float 1 0))
132(defun calc-random (n)
133 (interactive "P")
134 (calc-slow-wrapper
135 (if n
136 (calc-enter-result 0 "rand" (list 'calcFunc-random
137 (calc-get-random-limit
138 (prefix-numeric-value n))))
139 (calc-enter-result 1 "rand" (list 'calcFunc-random
140 (calc-get-random-limit
bf77c646 141 (calc-top-n 1)))))))
136211a9
EZ
142
143(defun calc-get-random-limit (val)
144 (if (eq val 0)
145 calc-last-random-limit
bf77c646 146 (setq calc-last-random-limit val)))
136211a9
EZ
147
148(defun calc-rrandom ()
149 (interactive)
150 (calc-slow-wrapper
151 (setq calc-last-random-limit '(float 1 0))
bf77c646 152 (calc-enter-result 0 "rand" (list 'calcFunc-random '(float 1 0)))))
136211a9
EZ
153
154(defun calc-random-again (arg)
155 (interactive "p")
156 (calc-slow-wrapper
157 (while (>= (setq arg (1- arg)) 0)
158 (calc-enter-result 0 "rand" (list 'calcFunc-random
bf77c646 159 calc-last-random-limit)))))
136211a9
EZ
160
161(defun calc-shuffle (n)
162 (interactive "P")
163 (calc-slow-wrapper
164 (if n
165 (calc-enter-result 1 "shuf" (list 'calcFunc-shuffle
166 (prefix-numeric-value n)
167 (calc-get-random-limit
168 (calc-top-n 1))))
169 (calc-enter-result 2 "shuf" (list 'calcFunc-shuffle
170 (calc-top-n 1)
171 (calc-get-random-limit
bf77c646 172 (calc-top-n 2)))))))
136211a9
EZ
173
174(defun calc-report-prime-test (res)
175 (cond ((eq (car res) t)
176 (calc-record-message "prim" "Prime (guaranteed)"))
177 ((eq (car res) nil)
178 (if (cdr res)
179 (if (eq (nth 1 res) 'unknown)
180 (calc-record-message
181 "prim" "Non-prime (factors unknown)")
182 (calc-record-message
183 "prim" "Non-prime (%s is a factor)"
184 (math-format-number (nth 1 res))))
185 (calc-record-message "prim" "Non-prime")))
186 (t
187 (calc-record-message
188 "prim" "Probably prime (%d iters; %s%% chance of error)"
189 (nth 1 res)
190 (let ((calc-float-format '(fix 2)))
bf77c646 191 (math-format-number (nth 2 res)))))))
136211a9
EZ
192
193(defun calc-prime-test (iters)
194 (interactive "p")
195 (calc-slow-wrapper
196 (let* ((n (calc-top-n 1))
197 (res (math-prime-test n iters)))
bf77c646 198 (calc-report-prime-test res))))
136211a9 199
8d7498c1
JB
200(defvar calc-verbose-nextprime nil)
201
136211a9
EZ
202(defun calc-next-prime (iters)
203 (interactive "p")
204 (calc-slow-wrapper
205 (let ((calc-verbose-nextprime t))
206 (if (calc-is-inverse)
207 (calc-enter-result 1 "prvp" (list 'calcFunc-prevprime
208 (calc-top-n 1) (math-abs iters)))
209 (calc-enter-result 1 "nxtp" (list 'calcFunc-nextprime
bf77c646 210 (calc-top-n 1) (math-abs iters)))))))
136211a9
EZ
211
212(defun calc-prev-prime (iters)
213 (interactive "p")
214 (calc-invert-func)
bf77c646 215 (calc-next-prime iters))
136211a9
EZ
216
217(defun calc-prime-factors (iters)
218 (interactive "p")
219 (calc-slow-wrapper
220 (let ((res (calcFunc-prfac (calc-top-n 1))))
221 (if (not math-prime-factors-finished)
222 (calc-record-message "pfac" "Warning: May not be fully factored"))
bf77c646 223 (calc-enter-result 1 "pfac" res))))
136211a9
EZ
224
225(defun calc-totient (arg)
226 (interactive "P")
227 (calc-slow-wrapper
bf77c646 228 (calc-unary-op "phi" 'calcFunc-totient arg)))
136211a9
EZ
229
230(defun calc-moebius (arg)
231 (interactive "P")
232 (calc-slow-wrapper
bf77c646 233 (calc-unary-op "mu" 'calcFunc-moebius arg)))
136211a9
EZ
234
235
236(defun calcFunc-gcd (a b)
237 (if (Math-messy-integerp a)
238 (setq a (math-trunc a)))
239 (if (Math-messy-integerp b)
240 (setq b (math-trunc b)))
241 (cond ((and (Math-integerp a) (Math-integerp b))
242 (math-gcd a b))
243 ((Math-looks-negp a)
244 (calcFunc-gcd (math-neg a) b))
245 ((Math-looks-negp b)
246 (calcFunc-gcd a (math-neg b)))
247 ((Math-zerop a) b)
248 ((Math-zerop b) a)
249 ((and (Math-ratp a)
250 (Math-ratp b))
251 (math-make-frac (math-gcd (if (eq (car-safe a) 'frac) (nth 1 a) a)
252 (if (eq (car-safe b) 'frac) (nth 1 b) b))
253 (calcFunc-lcm
254 (if (eq (car-safe a) 'frac) (nth 2 a) 1)
255 (if (eq (car-safe b) 'frac) (nth 2 b) 1))))
256 ((not (Math-integerp a))
257 (calc-record-why 'integerp a)
258 (list 'calcFunc-gcd a b))
259 (t
260 (calc-record-why 'integerp b)
bf77c646 261 (list 'calcFunc-gcd a b))))
136211a9
EZ
262
263(defun calcFunc-lcm (a b)
264 (let ((g (calcFunc-gcd a b)))
265 (if (Math-numberp g)
266 (math-div (math-mul a b) g)
bf77c646 267 (list 'calcFunc-lcm a b))))
136211a9
EZ
268
269(defun calcFunc-egcd (a b) ; Knuth section 4.5.2
270 (cond
271 ((not (Math-integerp a))
272 (if (Math-messy-integerp a)
273 (calcFunc-egcd (math-trunc a) b)
274 (calc-record-why 'integerp a)
275 (list 'calcFunc-egcd a b)))
276 ((not (Math-integerp b))
277 (if (Math-messy-integerp b)
278 (calcFunc-egcd a (math-trunc b))
279 (calc-record-why 'integerp b)
280 (list 'calcFunc-egcd a b)))
281 (t
282 (let ((u1 1) (u2 0) (u3 a)
283 (v1 0) (v2 1) (v3 b)
284 t1 t2 q)
285 (while (not (eq v3 0))
286 (setq q (math-idivmod u3 v3)
287 t1 (math-sub u1 (math-mul v1 (car q)))
288 t2 (math-sub u2 (math-mul v2 (car q)))
289 u1 v1 u2 v2 u3 v3
290 v1 t1 v2 t2 v3 (cdr q)))
bf77c646 291 (list 'vec u3 u1 u2)))))
136211a9
EZ
292
293
294;;; Factorial and related functions.
295
296(defun calcFunc-fact (n) ; [I I] [F F] [Public]
297 (let (temp)
298 (cond ((Math-integer-negp n)
299 (if calc-infinite-mode
300 '(var uinf var-uinf)
301 (math-reject-arg n 'range)))
302 ((integerp n)
303 (if (<= n 20)
304 (aref '[1 1 2 6 24 120 720 5040 40320 362880
305 (bigpos 800 628 3) (bigpos 800 916 39)
306 (bigpos 600 1 479) (bigpos 800 20 227 6)
307 (bigpos 200 291 178 87) (bigpos 0 368 674 307 1)
308 (bigpos 0 888 789 922 20) (bigpos 0 96 428 687 355)
309 (bigpos 0 728 705 373 402 6)
310 (bigpos 0 832 408 100 645 121)
311 (bigpos 0 640 176 8 902 432 2)] n)
312 (math-factorial-iter (1- n) 2 1)))
313 ((and (math-messy-integerp n)
314 (Math-lessp n 100))
315 (math-inexact-result)
316 (setq temp (math-trunc n))
317 (if (>= temp 0)
318 (if (<= temp 20)
319 (math-float (calcFunc-fact temp))
320 (math-with-extra-prec 1
321 (math-factorial-iter (1- temp) 2 '(float 1 0))))
322 (math-reject-arg n 'range)))
323 ((math-numberp n)
324 (let* ((q (math-quarter-integer n))
325 (tn (and q (Math-lessp n 1000) (Math-lessp -1000 n)
326 (1+ (math-floor n)))))
327 (cond ((and tn (= q 2)
328 (or calc-symbolic-mode (< (math-abs tn) 20)))
329 (let ((q (if (< tn 0)
330 (math-div
331 (math-pow -2 (- tn))
332 (math-double-factorial-iter (* -2 tn) 3 1 2))
a1506d29 333 (math-div
136211a9
EZ
334 (math-double-factorial-iter (* 2 tn) 3 1 2)
335 (math-pow 2 tn)))))
336 (math-mul q (if calc-symbolic-mode
337 (list 'calcFunc-sqrt '(var pi var-pi))
338 (math-sqrt-pi)))))
339 ((and tn (>= tn 0) (< tn 20)
340 (memq q '(1 3)))
341 (math-inexact-result)
342 (math-div
343 (math-mul (math-double-factorial-iter (* 4 tn) q 1 4)
344 (if (= q 1) (math-gamma-1q) (math-gamma-3q)))
345 (math-pow 4 tn)))
346 (t
347 (math-inexact-result)
348 (math-with-extra-prec 3
349 (math-gammap1-raw (math-float n)))))))
350 ((equal n '(var inf var-inf)) n)
351 (t (calc-record-why 'numberp n)
bf77c646 352 (list 'calcFunc-fact n)))))
136211a9
EZ
353
354(math-defcache math-gamma-1q nil
355 (math-with-extra-prec 3
356 (math-gammap1-raw '(float -75 -2))))
357
358(math-defcache math-gamma-3q nil
359 (math-with-extra-prec 3
360 (math-gammap1-raw '(float -25 -2))))
361
362(defun math-factorial-iter (count n f)
363 (if (= (% n 5) 1)
364 (math-working (format "factorial(%d)" (1- n)) f))
365 (if (> count 0)
366 (math-factorial-iter (1- count) (1+ n) (math-mul n f))
bf77c646 367 f))
136211a9
EZ
368
369(defun calcFunc-dfact (n) ; [I I] [F F] [Public]
370 (cond ((Math-integer-negp n)
371 (if (math-oddp n)
372 (if (eq n -1)
373 1
374 (math-div (if (eq (math-mod n 4) 3) 1 -1)
375 (calcFunc-dfact (math-sub -2 n))))
376 (list 'calcFunc-dfact n)))
377 ((Math-zerop n) 1)
378 ((integerp n) (math-double-factorial-iter n (+ 2 (% n 2)) 1 2))
379 ((math-messy-integerp n)
380 (let ((temp (math-trunc n)))
381 (math-inexact-result)
382 (if (natnump temp)
383 (if (Math-lessp temp 200)
384 (math-with-extra-prec 1
385 (math-double-factorial-iter temp (+ 2 (% temp 2))
386 '(float 1 0) 2))
387 (let* ((half (math-div2 temp))
388 (even (math-mul (math-pow 2 half)
389 (calcFunc-fact (math-float half)))))
390 (if (math-evenp temp)
391 even
392 (math-div (calcFunc-fact n) even))))
8d7498c1 393 (list 'calcFunc-dfact n))))
136211a9
EZ
394 ((equal n '(var inf var-inf)) n)
395 (t (calc-record-why 'natnump n)
bf77c646 396 (list 'calcFunc-dfact n))))
136211a9
EZ
397
398(defun math-double-factorial-iter (max n f step)
399 (if (< (% n 12) step)
400 (math-working (format "dfact(%d)" (- n step)) f))
401 (if (<= n max)
402 (math-double-factorial-iter max (+ n step) (math-mul n f) step)
bf77c646 403 f))
136211a9
EZ
404
405(defun calcFunc-perm (n m) ; [I I I] [F F F] [Public]
406 (cond ((and (integerp n) (integerp m) (<= m n) (>= m 0))
407 (math-factorial-iter m (1+ (- n m)) 1))
408 ((or (not (math-num-integerp n))
409 (and (math-messy-integerp n) (Math-lessp 100 n))
410 (not (math-num-integerp m))
411 (and (math-messy-integerp m) (Math-lessp 100 m)))
412 (or (math-realp n) (equal n '(var inf var-inf))
413 (math-reject-arg n 'realp))
414 (or (math-realp m) (equal m '(var inf var-inf))
415 (math-reject-arg m 'realp))
416 (and (math-num-integerp n) (math-negp n) (math-reject-arg n 'range))
417 (and (math-num-integerp m) (math-negp m) (math-reject-arg m 'range))
418 (math-div (calcFunc-fact n) (calcFunc-fact (math-sub n m))))
419 (t
420 (let ((tn (math-trunc n))
421 (tm (math-trunc m)))
422 (math-inexact-result)
423 (or (integerp tn) (math-reject-arg tn 'fixnump))
424 (or (integerp tm) (math-reject-arg tm 'fixnump))
425 (or (and (<= tm tn) (>= tm 0)) (math-reject-arg tm 'range))
426 (math-with-extra-prec 1
bf77c646 427 (math-factorial-iter tm (1+ (- tn tm)) '(float 1 0)))))))
136211a9
EZ
428
429(defun calcFunc-choose (n m) ; [I I I] [F F F] [Public]
430 (cond ((and (integerp n) (integerp m) (<= m n) (>= m 0))
431 (if (> m (/ n 2))
432 (math-choose-iter (- n m) n 1 1)
433 (math-choose-iter m n 1 1)))
434 ((not (math-realp n))
435 (math-reject-arg n 'realp))
436 ((not (math-realp m))
437 (math-reject-arg m 'realp))
438 ((not (math-num-integerp m))
439 (if (and (math-num-integerp n) (math-negp n))
440 (list 'calcFunc-choose n m)
441 (math-div (calcFunc-fact (math-float n))
442 (math-mul (calcFunc-fact m)
443 (calcFunc-fact (math-sub n m))))))
444 ((math-negp m) 0)
445 ((math-negp n)
446 (let ((val (calcFunc-choose (math-add (math-add n m) -1) m)))
447 (if (math-evenp (math-trunc m))
448 val
449 (math-neg val))))
450 ((and (math-num-integerp n)
451 (Math-lessp n m))
452 0)
453 (t
454 (math-inexact-result)
455 (let ((tm (math-trunc m)))
456 (or (integerp tm) (math-reject-arg tm 'fixnump))
457 (if (> tm 100)
458 (math-div (calcFunc-fact (math-float n))
459 (math-mul (calcFunc-fact (math-float m))
460 (calcFunc-fact (math-float
461 (math-sub n m)))))
462 (math-with-extra-prec 1
bf77c646 463 (math-choose-float-iter tm n 1 1)))))))
136211a9
EZ
464
465(defun math-choose-iter (m n i c)
466 (if (and (= (% i 5) 1) (> i 5))
467 (math-working (format "choose(%d)" (1- i)) c))
468 (if (<= i m)
469 (math-choose-iter m (1- n) (1+ i)
470 (math-quotient (math-mul c n) i))
bf77c646 471 c))
136211a9
EZ
472
473(defun math-choose-float-iter (count n i c)
474 (if (= (% i 5) 1)
475 (math-working (format "choose(%d)" (1- i)) c))
476 (if (> count 0)
477 (math-choose-float-iter (1- count) (math-sub n 1) (1+ i)
478 (math-div (math-mul c n) i))
bf77c646 479 c))
136211a9
EZ
480
481
482;;; Stirling numbers.
483
484(defun calcFunc-stir1 (n m)
bf77c646 485 (math-stirling-number n m 1))
136211a9
EZ
486
487(defun calcFunc-stir2 (n m)
bf77c646 488 (math-stirling-number n m 0))
136211a9 489
3132f345 490(defvar math-stirling-cache (vector [[1]] [[1]]))
8d7498c1
JB
491
492;; The variable math-stirling-local-cache is local to
493;; math-stirling-number, but is used by math-stirling-1
494;; and math-stirling-2, which are called by math-stirling-number.
495(defvar math-stirling-local-cache)
496
136211a9
EZ
497(defun math-stirling-number (n m k)
498 (or (math-num-natnump n) (math-reject-arg n 'natnump))
499 (or (math-num-natnump m) (math-reject-arg m 'natnump))
500 (if (consp n) (setq n (math-trunc n)))
501 (or (integerp n) (math-reject-arg n 'fixnump))
502 (if (consp m) (setq m (math-trunc m)))
503 (or (integerp m) (math-reject-arg m 'fixnump))
504 (if (< n m)
505 0
8d7498c1
JB
506 (let ((math-stirling-local-cache (aref math-stirling-cache k)))
507 (while (<= (length math-stirling-local-cache) n)
508 (let ((i (1- (length math-stirling-local-cache)))
136211a9 509 row)
8d7498c1
JB
510 (setq math-stirling-local-cache
511 (vconcat math-stirling-local-cache
512 (make-vector (length math-stirling-local-cache) nil)))
513 (aset math-stirling-cache k math-stirling-local-cache)
514 (while (< (setq i (1+ i)) (length math-stirling-local-cache))
515 (aset math-stirling-local-cache i (setq row (make-vector (1+ i) nil)))
136211a9
EZ
516 (aset row 0 0)
517 (aset row i 1))))
518 (if (= k 1)
519 (math-stirling-1 n m)
bf77c646 520 (math-stirling-2 n m)))))
136211a9
EZ
521
522(defun math-stirling-1 (n m)
8d7498c1
JB
523 (or (aref (aref math-stirling-local-cache n) m)
524 (aset (aref math-stirling-local-cache n) m
136211a9 525 (math-add (math-stirling-1 (1- n) (1- m))
bf77c646 526 (math-mul (- 1 n) (math-stirling-1 (1- n) m))))))
136211a9
EZ
527
528(defun math-stirling-2 (n m)
8d7498c1
JB
529 (or (aref (aref math-stirling-local-cache n) m)
530 (aset (aref math-stirling-local-cache n) m
136211a9 531 (math-add (math-stirling-2 (1- n) (1- m))
bf77c646 532 (math-mul m (math-stirling-2 (1- n) m))))))
136211a9 533
3132f345
CW
534(defvar math-random-table nil)
535(defvar math-last-RandSeed nil)
536(defvar math-random-ptr1 nil)
537(defvar math-random-ptr2 nil)
538(defvar math-random-shift nil)
136211a9
EZ
539
540;;; Produce a random 10-bit integer, with (random) if no seed provided,
541;;; or else with Numerical Recipes algorithm ran3 / Knuth 3.2.2-A.
8d7498c1
JB
542
543(defvar var-RandSeed nil)
544(defvar math-random-cache nil)
545(defvar math-gaussian-cache nil)
546
136211a9 547(defun math-init-random-base ()
8d7498c1 548 (if var-RandSeed
136211a9
EZ
549 (if (eq (car-safe var-RandSeed) 'vec)
550 nil
551 (if (Math-integerp var-RandSeed)
552 (let* ((seed (math-sub 161803 var-RandSeed))
553 (mj (1+ (math-mod seed '(bigpos 0 0 1))))
554 (mk (1+ (math-mod (math-quotient seed '(bigpos 0 0 1))
555 '(bigpos 0 0 1))))
556 (i 0))
557 (setq math-random-table (cons 'vec (make-list 55 mj)))
558 (while (<= (setq i (1+ i)) 54)
559 (let* ((ii (% (* i 21) 55))
560 (p (nthcdr ii math-random-table)))
561 (setcar p mk)
562 (setq mk (- mj mk)
563 mj (car p)))))
564 (math-reject-arg var-RandSeed "*RandSeed must be an integer"))
565 (setq var-RandSeed (list 'vec var-RandSeed)
566 math-random-ptr1 math-random-table
567 math-random-cache nil
568 math-random-ptr2 (nthcdr 31 math-random-table))
569 (let ((i 200))
570 (while (> (setq i (1- i)) 0)
571 (math-random-base))))
572 (random t)
573 (setq var-RandSeed nil
574 math-random-cache nil
136211a9
EZ
575 math-random-shift -4) ; assume RAND_MAX >= 16383
576 ;; This exercises the random number generator and also helps
577 ;; deduce a better value for RAND_MAX.
8d7498c1
JB
578 (let ((i 0))
579 (while (< (setq i (1+ i)) 30)
580 (if (> (lsh (math-abs (random)) math-random-shift) 4095)
581 (setq math-random-shift (1- math-random-shift))))))
136211a9 582 (setq math-last-RandSeed var-RandSeed
bf77c646 583 math-gaussian-cache nil))
136211a9
EZ
584
585(defun math-random-base ()
586 (if var-RandSeed
587 (progn
588 (setq math-random-ptr1 (or (cdr math-random-ptr1)
589 (cdr math-random-table))
590 math-random-ptr2 (or (cdr math-random-ptr2)
591 (cdr math-random-table)))
592 (logand (lsh (setcar math-random-ptr1
593 (logand (- (car math-random-ptr1)
594 (car math-random-ptr2)) 524287))
595 -6) 1023))
bf77c646 596 (logand (lsh (random) math-random-shift) 1023)))
136211a9
EZ
597
598
599;;; Produce a random digit in the range 0..999.
600;;; Avoid various pitfalls that may lurk in the built-in (random) function!
601;;; Shuffling algorithm from Numerical Recipes, section 7.1.
602(defun math-random-digit ()
8d7498c1
JB
603 (let (i math-random-last)
604 (or (eq var-RandSeed math-last-RandSeed)
136211a9
EZ
605 (math-init-random-base))
606 (or math-random-cache
607 (progn
608 (setq math-random-last (math-random-base)
609 math-random-cache (make-vector 13 nil)
610 i -1)
611 (while (< (setq i (1+ i)) 13)
612 (aset math-random-cache i (math-random-base)))))
613 (while (progn
614 (setq i (/ math-random-last 79) ; 0 <= i < 13
615 math-random-last (aref math-random-cache i))
616 (aset math-random-cache i (math-random-base))
617 (>= math-random-last 1000)))
bf77c646 618 math-random-last))
136211a9
EZ
619
620;;; Produce an N-digit random integer.
621(defun math-random-digits (n)
622 (cond ((<= n 6)
623 (math-scale-right (+ (* (math-random-digit) 1000) (math-random-digit))
624 (- 6 n)))
625 (t (let* ((slop (% (- 900003 n) 3))
626 (i (/ (+ n slop) 3))
627 (digs nil))
628 (while (> i 0)
629 (setq digs (cons (math-random-digit) digs)
630 i (1- i)))
631 (math-normalize (math-scale-right (cons 'bigpos digs)
bf77c646 632 slop))))))
136211a9
EZ
633
634;;; Produce a uniformly-distributed random float 0 <= N < 1.
635(defun math-random-float ()
636 (math-make-float (math-random-digits calc-internal-prec)
bf77c646 637 (- calc-internal-prec)))
136211a9
EZ
638
639;;; Produce a Gaussian-distributed random float with mean=0, sigma=1.
640(defun math-gaussian-float ()
641 (math-with-extra-prec 2
642 (if (and math-gaussian-cache
643 (= (car math-gaussian-cache) calc-internal-prec))
644 (prog1
645 (cdr math-gaussian-cache)
646 (setq math-gaussian-cache nil))
647 (let* ((v1 (math-add (math-mul (math-random-float) 2) -1))
648 (v2 (math-add (math-mul (math-random-float) 2) -1))
649 (r (math-add (math-sqr v1) (math-sqr v2))))
650 (while (or (not (Math-lessp r 1)) (math-zerop r))
651 (setq v1 (math-add (math-mul (math-random-float) 2) -1)
652 v2 (math-add (math-mul (math-random-float) 2) -1)
653 r (math-add (math-sqr v1) (math-sqr v2))))
654 (let ((fac (math-sqrt (math-mul (math-div (calcFunc-ln r) r) -2))))
655 (setq math-gaussian-cache (cons calc-internal-prec
656 (math-mul v1 fac)))
bf77c646 657 (math-mul v2 fac))))))
136211a9
EZ
658
659;;; Produce a random integer or real 0 <= N < MAX.
660(defun calcFunc-random (max)
661 (cond ((Math-zerop max)
662 (math-gaussian-float))
663 ((Math-integerp max)
664 (let* ((digs (math-numdigs max))
665 (r (math-random-digits (+ digs 3))))
666 (math-mod r max)))
667 ((Math-realp max)
668 (math-mul (math-random-float) max))
669 ((and (eq (car max) 'intv) (math-constp max)
670 (Math-lessp (nth 2 max) (nth 3 max)))
671 (if (math-floatp max)
672 (let ((val (math-add (math-mul (math-random-float)
673 (math-sub (nth 3 max) (nth 2 max)))
674 (nth 2 max))))
675 (if (or (and (memq (nth 1 max) '(0 1)) ; almost not worth
676 (Math-equal val (nth 2 max))) ; checking!
677 (and (memq (nth 1 max) '(0 2))
678 (Math-equal val (nth 3 max))))
679 (calcFunc-random max)
680 val))
681 (let ((lo (if (memq (nth 1 max) '(0 1))
682 (math-add (nth 2 max) 1) (nth 2 max)))
683 (hi (if (memq (nth 1 max) '(1 3))
684 (math-add (nth 3 max) 1) (nth 3 max))))
685 (if (Math-lessp lo hi)
686 (math-add (calcFunc-random (math-sub hi lo)) lo)
687 (math-reject-arg max "*Empty interval")))))
688 ((eq (car max) 'vec)
689 (if (cdr max)
690 (nth (1+ (calcFunc-random (1- (length max)))) max)
691 (math-reject-arg max "*Empty list")))
692 ((and (eq (car max) 'sdev) (math-constp max) (Math-realp (nth 1 max)))
693 (math-add (math-mul (math-gaussian-float) (nth 2 max)) (nth 1 max)))
bf77c646 694 (t (math-reject-arg max 'realp))))
136211a9
EZ
695
696;;; Choose N objects at random from the set MAX without duplicates.
697(defun calcFunc-shuffle (n &optional max)
698 (or max (setq max n n -1))
699 (or (and (Math-num-integerp n)
700 (or (natnump (setq n (math-trunc n))) (eq n -1)))
701 (math-reject-arg n 'integerp))
702 (cond ((or (math-zerop max)
703 (math-floatp max)
704 (eq (car-safe max) 'sdev))
705 (if (< n 0)
706 (math-reject-arg n 'natnump)
707 (math-simple-shuffle n max)))
708 ((and (<= n 1) (>= n 0))
709 (math-simple-shuffle n max))
710 ((and (eq (car-safe max) 'intv) (math-constp max))
711 (let ((num (math-add (math-sub (nth 3 max) (nth 2 max))
712 (cdr (assq (nth 1 max)
713 '((0 . -1) (1 . 0)
714 (2 . 0) (3 . 1))))))
715 (min (math-add (nth 2 max) (if (memq (nth 1 max) '(0 1))
716 1 0))))
717 (if (< n 0) (setq n num))
718 (or (math-posp num) (math-reject-arg max 'range))
719 (and (Math-lessp num n) (math-reject-arg n 'range))
720 (if (Math-lessp n (math-quotient num 3))
721 (math-simple-shuffle n max)
722 (if (> (* n 4) (* num 3))
723 (math-add (math-sub min 1)
724 (math-shuffle-list n num (calcFunc-index num)))
725 (let ((tot 0)
726 (m 0)
727 (vec nil))
728 (while (< m n)
729 (if (< (calcFunc-random (- num tot)) (- n m))
730 (setq vec (cons (math-add min tot) vec)
731 m (1+ m)))
732 (setq tot (1+ tot)))
733 (math-shuffle-list n n (cons 'vec vec)))))))
734 ((eq (car-safe max) 'vec)
735 (let ((size (1- (length max))))
736 (if (< n 0) (setq n size))
737 (if (and (> n (/ size 2)) (<= n size))
738 (math-shuffle-list n size (copy-sequence max))
739 (let* ((vals (calcFunc-shuffle
740 n (list 'intv 3 1 (1- (length max)))))
741 (p vals))
742 (while (setq p (cdr p))
743 (setcar p (nth (car p) max)))
744 vals))))
745 ((math-integerp max)
746 (if (math-posp max)
747 (calcFunc-shuffle n (list 'intv 2 0 max))
748 (calcFunc-shuffle n (list 'intv 1 max 0))))
bf77c646 749 (t (math-reject-arg max 'realp))))
136211a9
EZ
750
751(defun math-simple-shuffle (n max)
752 (let ((vec nil)
753 val)
754 (while (>= (setq n (1- n)) 0)
755 (while (math-member (setq val (calcFunc-random max)) vec))
756 (setq vec (cons val vec)))
bf77c646 757 (cons 'vec vec)))
136211a9
EZ
758
759(defun math-shuffle-list (n size vec)
760 (let ((j size)
761 k temp
762 (p vec))
763 (while (cdr (setq p (cdr p)))
764 (setq k (calcFunc-random j)
765 j (1- j)
766 temp (nth k p))
767 (setcar (nthcdr k p) (car p))
768 (setcar p temp))
bf77c646 769 (cons 'vec (nthcdr (- size n -1) vec))))
136211a9
EZ
770
771(defun math-member (x list)
772 (while (and list (not (equal x (car list))))
773 (setq list (cdr list)))
bf77c646 774 list)
136211a9
EZ
775
776
777;;; Check if the integer N is prime. [X I]
778;;; Return (nil) if non-prime,
779;;; (nil N) if non-prime with known factor N,
780;;; (nil unknown) if non-prime with no known factors,
781;;; (t) if prime,
782;;; (maybe N P) if probably prime (after N iters with probability P%)
8d7498c1
JB
783(defvar math-prime-test-cache '(-1))
784
785(defvar math-prime-test-cache-k)
786(defvar math-prime-test-cache-q)
787(defvar math-prime-test-cache-nm1)
788
136211a9
EZ
789(defun math-prime-test (n iters)
790 (if (and (Math-vectorp n) (cdr n))
791 (setq n (nth (1- (length n)) n)))
792 (if (Math-messy-integerp n)
793 (setq n (math-trunc n)))
794 (let ((res))
795 (while (> iters 0)
796 (setq res
797 (cond ((and (integerp n) (<= n 5003))
798 (list (= (math-next-small-prime n) n)))
799 ((not (Math-integerp n))
800 (error "Argument must be an integer"))
801 ((Math-integer-negp n)
802 '(nil))
803 ((Math-natnum-lessp n '(bigpos 0 0 8))
804 (setq n (math-fixnum n))
805 (let ((i -1) v)
806 (while (and (> (% n (setq v (aref math-primes-table
807 (setq i (1+ i)))))
808 0)
809 (< (* v v) n)))
810 (if (= (% n v) 0)
811 (list nil v)
812 '(t))))
813 ((not (equal n (car math-prime-test-cache)))
814 (cond ((= (% (nth 1 n) 2) 0) '(nil 2))
815 ((= (% (nth 1 n) 5) 0) '(nil 5))
816 (t (let ((dig (cdr n)) (sum 0))
817 (while dig
818 (if (cdr dig)
819 (setq sum (% (+ (+ sum (car dig))
820 (* (nth 1 dig) 1000))
821 111111)
822 dig (cdr (cdr dig)))
823 (setq sum (% (+ sum (car dig)) 111111)
824 dig nil)))
825 (cond ((= (% sum 3) 0) '(nil 3))
826 ((= (% sum 7) 0) '(nil 7))
827 ((= (% sum 11) 0) '(nil 11))
828 ((= (% sum 13) 0) '(nil 13))
829 ((= (% sum 37) 0) '(nil 37))
830 (t
831 (setq math-prime-test-cache-k 1
832 math-prime-test-cache-q
833 (math-div2 n)
834 math-prime-test-cache-nm1
835 (math-add n -1))
836 (while (math-evenp
837 math-prime-test-cache-q)
838 (setq math-prime-test-cache-k
839 (1+ math-prime-test-cache-k)
840 math-prime-test-cache-q
841 (math-div2
842 math-prime-test-cache-q)))
843 (setq iters (1+ iters))
844 (list 'maybe
845 0
846 (math-sub
847 100
848 (math-div
849 '(float 232 0)
850 (math-numdigs n))))))))))
851 ((not (eq (car (nth 1 math-prime-test-cache)) 'maybe))
852 (nth 1 math-prime-test-cache))
853 (t ; Fermat step
854 (let* ((x (math-add (calcFunc-random (math-add n -2)) 2))
855 (y (math-pow-mod x math-prime-test-cache-q n))
856 (j 0))
857 (while (and (not (eq y 1))
858 (not (equal y math-prime-test-cache-nm1))
859 (< (setq j (1+ j)) math-prime-test-cache-k))
860 (setq y (math-mod (math-mul y y) n)))
861 (if (or (equal y math-prime-test-cache-nm1)
862 (and (eq y 1) (eq j 0)))
863 (list 'maybe
864 (1+ (nth 1 (nth 1 math-prime-test-cache)))
865 (math-mul (nth 2 (nth 1 math-prime-test-cache))
866 '(float 25 -2)))
867 '(nil unknown))))))
868 (setq math-prime-test-cache (list n res)
869 iters (if (eq (car res) 'maybe)
870 (1- iters)
871 0)))
bf77c646 872 res))
136211a9
EZ
873
874(defun calcFunc-prime (n &optional iters)
875 (or (math-num-integerp n) (math-reject-arg n 'integerp))
876 (or (not iters) (math-num-integerp iters) (math-reject-arg iters 'integerp))
877 (if (car (math-prime-test (math-trunc n) (math-trunc (or iters 1))))
878 1
bf77c646 879 0))
136211a9
EZ
880
881;;; Theory: summing base-10^6 digits modulo 111111 is "casting out 999999s".
882;;; Initial probability that N is prime is 1/ln(N) = log10(e)/log10(N).
883;;; After culling [2,3,5,7,11,13,37], probability of primality is 5.36 x more.
884;;; Initial reported probability of non-primality is thus 100% - this.
885;;; Each Fermat step multiplies this probability by 25%.
886;;; The Fermat step is algorithm P from Knuth section 4.5.4.
887
888
889(defun calcFunc-prfac (n)
890 (setq math-prime-factors-finished t)
891 (if (Math-messy-integerp n)
892 (setq n (math-trunc n)))
893 (if (Math-natnump n)
894 (if (Math-natnum-lessp 2 n)
895 (let (factors res p (i 0))
896 (while (and (not (eq n 1))
897 (< i (length math-primes-table)))
898 (setq p (aref math-primes-table i))
899 (while (eq (cdr (setq res (cond ((eq n p) (cons 1 0))
900 ((eq n 1) (cons 0 1))
901 ((consp n) (math-idivmod n p))
902 (t (cons (/ n p) (% n p))))))
903 0)
904 (math-working "factor" p)
905 (setq factors (nconc factors (list p))
906 n (car res)))
907 (or (eq n 1)
908 (Math-natnum-lessp p (car res))
909 (setq factors (nconc factors (list n))
910 n 1))
911 (setq i (1+ i)))
912 (or (setq math-prime-factors-finished (eq n 1))
913 (setq factors (nconc factors (list n))))
914 (cons 'vec factors))
915 (list 'vec n))
916 (if (Math-integerp n)
917 (if (eq n -1)
918 (list 'vec n)
919 (cons 'vec (cons -1 (cdr (calcFunc-prfac (math-neg n))))))
920 (calc-record-why 'integerp n)
bf77c646 921 (list 'calcFunc-prfac n))))
136211a9
EZ
922
923(defun calcFunc-totient (n)
924 (if (Math-messy-integerp n)
925 (setq n (math-trunc n)))
926 (if (Math-natnump n)
927 (if (Math-natnum-lessp n 2)
928 (if (Math-negp n)
929 (calcFunc-totient (math-abs n))
930 n)
931 (let ((factors (cdr (calcFunc-prfac n)))
932 p)
933 (if math-prime-factors-finished
934 (progn
935 (while factors
936 (setq p (car factors)
937 n (math-mul (math-div n p) (math-add p -1)))
938 (while (equal p (car factors))
939 (setq factors (cdr factors))))
940 n)
941 (calc-record-why "*Number too big to factor" n)
942 (list 'calcFunc-totient n))))
943 (calc-record-why 'natnump n)
bf77c646 944 (list 'calcFunc-totient n)))
136211a9
EZ
945
946(defun calcFunc-moebius (n)
947 (if (Math-messy-integerp n)
948 (setq n (math-trunc n)))
949 (if (and (Math-natnump n) (not (eq n 0)))
950 (if (Math-natnum-lessp n 2)
951 (if (Math-negp n)
952 (calcFunc-moebius (math-abs n))
953 1)
954 (let ((factors (cdr (calcFunc-prfac n)))
955 (mu 1))
956 (if math-prime-factors-finished
957 (progn
958 (while factors
959 (setq mu (if (equal (car factors) (nth 1 factors))
960 0 (math-neg mu))
961 factors (cdr factors)))
962 mu)
963 (calc-record-why "Number too big to factor" n)
964 (list 'calcFunc-moebius n))))
965 (calc-record-why 'posintp n)
bf77c646 966 (list 'calcFunc-moebius n)))
136211a9
EZ
967
968
969(defun calcFunc-nextprime (n &optional iters)
970 (if (Math-integerp n)
971 (if (Math-integer-negp n)
972 2
973 (if (and (integerp n) (< n 5003))
974 (math-next-small-prime (1+ n))
975 (if (math-evenp n)
976 (setq n (math-add n -1)))
977 (let (res)
978 (while (not (car (setq res (math-prime-test
979 (setq n (math-add n 2))
980 (or iters 1))))))
981 (if (and calc-verbose-nextprime
982 (eq (car res) 'maybe))
983 (calc-report-prime-test res)))
984 n))
985 (if (Math-realp n)
986 (calcFunc-nextprime (math-trunc n) iters)
bf77c646 987 (math-reject-arg n 'integerp))))
136211a9
EZ
988
989(defun calcFunc-prevprime (n &optional iters)
990 (if (Math-integerp n)
991 (if (Math-lessp n 4)
992 2
993 (if (math-evenp n)
994 (setq n (math-add n 1)))
995 (let (res)
996 (while (not (car (setq res (math-prime-test
997 (setq n (math-add n -2))
998 (or iters 1))))))
999 (if (and calc-verbose-nextprime
1000 (eq (car res) 'maybe))
1001 (calc-report-prime-test res)))
1002 n)
1003 (if (Math-realp n)
1004 (calcFunc-prevprime (math-ceiling n) iters)
bf77c646 1005 (math-reject-arg n 'integerp))))
136211a9
EZ
1006
1007(defun math-next-small-prime (n)
1008 (if (and (integerp n) (> n 2))
1009 (let ((lo -1)
1010 (hi (length math-primes-table))
1011 mid)
1012 (while (> (- hi lo) 1)
1013 (if (> n (aref math-primes-table
1014 (setq mid (ash (+ lo hi) -1))))
1015 (setq lo mid)
1016 (setq hi mid)))
1017 (aref math-primes-table hi))
bf77c646 1018 2))
136211a9 1019
43f34ccc 1020(provide 'calc-comb)
136211a9 1021
ab5796a9 1022;;; arch-tag: 1d75ee9b-0815-42bd-a321-bb3dc001cc02
bf77c646 1023;;; calc-comb.el ends here