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