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