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