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