Eliminate spurious redefinition of derivabbrev in Ctensor, fix documentation of diagm...
[maxima/cygwin.git] / src / combin.lisp
blob6e94ab116be1db10d7b8998afb5af00885fff5ad
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
13 (macsyma-module combin)
15 (declare-top (special *mfactl *factlist donel nn* dn* *ans* *var*
16 $zerobern *n $cflength *a* $prevfib $next_lucas
17 *infsumsimp *times *plus sum usum makef
18 varlist genvar $sumsplitfact $ratfac $simpsum
19 $prederror $listarith
20 $ratprint $zeta%pi $bftorat))
22 (load-macsyma-macros mhayat rzmac ratmac)
24 ;; minfactorial and factcomb stuff
26 (defmfun $makefact (e)
27 (let ((makef t)) (if (atom e) e (simplify (makefact1 e)))))
29 (defun makefact1 (e)
30 (cond ((atom e) e)
31 ((eq (caar e) '%binomial)
32 (subst (makefact1 (cadr e)) 'x
33 (subst (makefact1 (caddr e)) 'y
34 '((mtimes) ((mfactorial) x)
35 ((mexpt) ((mfactorial) y) -1)
36 ((mexpt) ((mfactorial) ((mplus) x ((mtimes) -1 y)))
37 -1)))))
38 ((eq (caar e) '%gamma)
39 (list '(mfactorial) (list '(mplus) -1 (makefact1 (cadr e)))))
40 ((eq (caar e) '$beta)
41 (makefact1 (subst (cadr e) 'x
42 (subst (caddr e) 'y
43 '((mtimes) ((%gamma) x)
44 ((%gamma) y)
45 ((mexpt) ((%gamma) ((mplus) x y)) -1))))))
46 (t (recur-apply #'makefact1 e))))
48 (defmfun $makegamma (e)
49 (if (atom e) e (simplify (makegamma1 ($makefact e)))))
51 (defmfun $minfactorial (e)
52 (let (*mfactl *factlist)
53 (if (specrepp e) (setq e (specdisrep e)))
54 (getfact e)
55 (mapl #'evfac1 *factlist)
56 (setq e (evfact e))))
58 (defun evfact (e)
59 (cond ((atom e) e)
60 ((eq (caar e) 'mfactorial)
61 ;; Replace factorial with simplified expression from *factlist.
62 (simplifya (cdr (assoc (cadr e) *factlist :test #'equal)) nil))
63 ((member (caar e) '(%sum %derivative %integrate %product) :test #'eq)
64 (cons (list (caar e)) (cons (evfact (cadr e)) (cddr e))))
65 (t (recur-apply #'evfact e))))
67 (defun adfactl (e l)
68 (let (n)
69 (cond ((null l) (push (list e) *mfactl))
70 ((numberp (setq n ($ratsimp `((mplus) ,e ((mtimes) -1 ,(caar l))))))
71 (cond ((plusp n)
72 (rplacd (car l) (cons e (cdar l))))
73 ((rplaca l (cons e (car l))))))
74 ((adfactl e (cdr l))))))
76 (defun getfact (e)
77 (cond ((atom e) nil)
78 ((eq (caar e) 'mfactorial)
79 (and (null (member (cadr e) *factlist :test #'equal))
80 (prog2
81 (push (cadr e) *factlist)
82 (adfactl (cadr e) *mfactl))))
83 ((member (caar e) '(%sum %derivative %integrate %product) :test #'eq)
84 (getfact (cadr e)))
85 ((mapc #'getfact (cdr e)))))
87 (defun evfac1 (e)
88 (do ((al *mfactl (cdr al)))
89 ((member (car e) (car al) :test #'equal)
90 (rplaca e
91 (cons (car e)
92 (list '(mtimes)
93 (gfact (car e)
94 ($ratsimp (list '(mplus) (car e)
95 (list '(mtimes) -1 (caar al)))) 1)
96 (list '(mfactorial) (caar al))))))))
98 (defmfun $factcomb (e)
99 (let ((varlist varlist ) genvar $ratfac (ratrep (and (not (atom e)) (eq (caar e) 'mrat))))
100 (and ratrep (setq e (ratdisrep e)))
101 (setq e (factcomb e)
102 e (cond ((atom e) e)
103 (t (simplify (cons (list (caar e))
104 (mapcar #'factcomb1 (cdr e)))))))
105 (or $sumsplitfact (setq e ($minfactorial e)))
106 (if ratrep (ratf e) e)))
108 (defun factcomb1 (e)
109 (cond ((free e 'mfactorial) e)
110 ((member (caar e) '(mplus mtimes mexpt) :test #'eq)
111 (cons (list (caar e)) (mapcar #'factcomb1 (cdr e))))
112 (t (setq e (factcomb e))
113 (if (atom e)
115 (cons (list (caar e)) (mapcar #'factcomb1 (cdr e)))))))
117 (defun factcomb (e)
118 (cond ((atom e) e)
119 ((free e 'mfactorial) e)
120 ((member (caar e) '(mplus mtimes) :test #'eq)
121 (factpluscomb (factcombplus e)))
122 ((eq (caar e) 'mexpt)
123 (simpexpt (list '(mexpt) (factcomb (cadr e))
124 (factcomb (caddr e)))
125 1 nil))
126 ((eq (caar e) 'mrat)
127 (factrat e))
128 (t (cons (car e) (mapcar #'factcomb (cdr e))))))
130 (defun factrat (e)
131 (let (nn* dn*)
132 (setq e (factqsnt ($ratdisrep (cons (car e) (cons (cadr e) 1)))
133 ($ratdisrep (cons (car e) (cons (cddr e) 1)))))
134 (numden e)
135 (div* (factpluscomb nn*) (factpluscomb dn*))))
137 (defun factqsnt (num den)
138 (if (equal num 0) 0
139 (let (nn* dn* (e (factpluscomb (div* den num))))
140 (numden e)
141 (factpluscomb (div* dn* nn*)))))
143 (defun factcombplus (e)
144 (let (nn* dn*)
145 (do ((l1 (nplus e) (cdr l1))
146 (l2))
147 ((null l1)
148 (simplus (cons '(mplus)
149 (mapcar #'(lambda (q) (factqsnt (car q) (cdr q))) l2))
150 1 nil))
151 (numden (car l1))
152 (do ((l3 l2 (cdr l3))
153 (l4))
154 ((null l3) (setq l2 (nconc l2 (list (cons nn* dn*)))))
155 (setq l4 (car l3))
156 (cond ((not (free ($gcd dn* (cdr l4)) 'mfactorial))
157 (numden (list '(mplus) (div* nn* dn*)
158 (div* (car l4) (cdr l4))))
159 (setq l2 (delete l4 l2 :count 1 :test #'eq))))))))
161 (defun factpluscomb (e)
162 (prog (donel fact indl tt)
163 tag (setq e (factexpand e)
164 fact (getfactorial e))
165 (or fact (return e))
166 (setq indl (mapcar #'(lambda (q) (factplusdep q fact))
167 (nplus e))
168 tt (factpowerselect indl (nplus e) fact)
169 e (cond ((cdr tt)
170 (cons '(mplus) (mapcar #'(lambda (q) (factplus2 q fact))
171 tt)))
172 (t (factplus2 (car tt) fact))))
173 (go tag)))
175 (defun nplus (e)
176 (if (eq (caar e) 'mplus)
177 (cdr e)
178 (list e)))
180 (defun factexpand (e)
181 (cond ((atom e) e)
182 ((eq (caar e) 'mplus)
183 (simplus (cons '(mplus) (mapcar #'factexpand (cdr e)))
184 1 nil))
185 ((free e 'mfactorial) e)
186 (t ($expand e))))
188 (defun getfactorial (e)
189 (cond ((atom e) nil)
190 ((member (caar e) '(mplus mtimes) :test #'eq)
191 (do ((e (cdr e) (cdr e))
192 (a))
193 ((null e) nil)
194 (setq a (getfactorial (car e)))
195 (and a (return a))))
196 ((eq (caar e) 'mexpt)
197 (getfactorial (cadr e)))
198 ((eq (caar e) 'mfactorial)
199 (and (null (memalike (cadr e) donel))
200 (list '(mfactorial)
201 (car (setq donel (cons (cadr e) donel))))))))
203 (defun factplusdep (e fact)
204 (cond ((alike1 e fact) 1)
205 ((atom e) nil)
206 ((eq (caar e) 'mtimes)
207 (do ((l (cdr e) (cdr l))
208 (e) (out))
209 ((null l) nil)
210 (setq e (car l))
211 (and (setq out (factplusdep e fact))
212 (return out))))
213 ((eq (caar e) 'mexpt)
214 (let ((fto (factplusdep (cadr e) fact)))
215 (and fto (simptimes (list '(mtimes) fto
216 (caddr e)) 1 t))))
217 ((eq (caar e) 'mplus)
218 (same (mapcar #'(lambda (q) (factplusdep q fact))
219 (cdr e))))))
221 (defun same (l)
222 (do ((ca (car l))
223 (cd (cdr l) (cdr cd))
224 (cad))
225 ((null cd) ca)
226 (setq cad (car cd))
227 (or (alike1 ca cad)
228 (return nil))))
230 (defun factpowerselect (indl e fact)
231 (let (l fl)
232 (do ((i indl (cdr i))
233 (j e (cdr j))
234 (expt) (exp))
235 ((null i) l)
236 (setq expt (car i)
237 exp (cond (expt
238 (setq exp ($divide (car j) `((mexpt) ,fact ,expt)))
239 ;; (car j) need not involve fact^expt since
240 ;; fact^expt may be the gcd of the num and denom
241 ;; of (car j) and $divide will cancel this out.
242 (if (not (equal (cadr exp) 0))
243 (cadr exp)
244 (progn
245 (setq expt '())
246 (caddr exp))))
247 (t (car j))))
248 (cond ((null l) (setq l (list (list expt exp))))
249 ((setq fl (assolike expt l))
250 (nconc fl (list exp)))
251 (t (nconc l (list (list expt exp))))))))
253 (defun factplus2 (l fact)
254 (let ((expt (car l)))
255 (cond (expt (factplus0 (cond ((cddr l) (rplaca l '(mplus)))
256 (t (cadr l)))
257 expt (cadr fact)))
258 (t (rplaca l '(mplus))))))
260 (defun factplus0 (r e fact)
261 (do ((i -1 (1- i))
262 (fpn fact (list '(mplus) fact i))
263 (j -1) (exp) (rfpn) (div))
264 (nil)
265 (setq rfpn (simpexpt (list '(mexpt) fpn -1) 1 nil))
266 (setq div (dypheyed r (simpexpt (list '(mexpt) rfpn e) 1 nil)))
267 (cond ((or (null (or $sumsplitfact (equal (cadr div) 0)))
268 (equal (car div) 0))
269 (return (simplus (cons '(mplus) (mapcar
270 #'(lambda (q)
271 (incf j)
272 (list '(mtimes) q (list '(mexpt)
273 (list '(mfactorial) (list '(mplus) fpn j)) e)))
274 (factplus1 (cons r exp) e fpn)))
275 1 nil)))
276 (t (setq r (car div))
277 (setq exp (cons (cadr div) exp))))))
279 (defun factplus1 (exp e fact)
280 (do ((l exp (cdr l))
281 (i 2 (1+ i))
282 (fpn (list '(mplus) fact 1) (list '(mplus) fact i))
283 (div))
284 ((null l) exp)
285 (setq div (dypheyed (car l) (list '(mexpt) fpn e)))
286 (and (or $sumsplitfact (equal (cadr div) 0))
287 (null (equal (car div) 0))
288 (rplaca l (cadr div))
289 (rplacd l (cons (cond ((cadr l)
290 (simplus (list '(mplus) (car div) (cadr l))
291 1 nil))
293 (setq donel
294 (cons (simplus fpn 1 nil) donel))
295 (car div)))
296 (cddr l))))))
298 (defun dypheyed (r f)
299 (let (r1 p1 p2)
300 (newvar r)
301 (setq r1 (ratf f)
302 p1 (pdegreevector (cadr r1))
303 p2 (pdegreevector (cddr r1)))
304 (do ((i p1 (cdr i))
305 (j p2 (cdr j))
306 (k (caddar r1) (cdr k)))
307 ((null k) (kansel r (cadr r1) (cddr r1)))
308 (cond ((> (car i) (car j))
309 (return (cdr ($divide r f (car k)))))))))
311 (defun kansel (r n d)
312 (let (r1 p1 p2)
313 (setq r1 (ratf r)
314 p1 (testdivide (cadr r1) n)
315 p2 (testdivide (cddr r1) d))
316 (if (and p1 p2)
317 (cons (rdis (cons p1 p2)) '(0))
318 (cons '0 (list r)))))
320 ;; euler and bernoulli stuff
322 (defvar *bn* (make-array 17 :adjustable t :element-type 'integer
323 :initial-contents '(0 -1 1 -1 5. -691. 7. -3617. 43867. -174611. 854513.
324 -236364091. 8553103. -23749461029. 8615841276005.
325 -7709321041217. 2577687858367.)))
327 (defvar *bd* (make-array 17 :adjustable t :element-type 'integer
328 :initial-contents '(0 30. 42. 30. 66. 2730. 6. 510. 798. 330. 138. 2730.
329 6. 870. 14322. 510. 6.)))
331 (defvar *eu* (make-array 11 :adjustable t :element-type 'integer
332 :initial-contents '(-1 5. -61. 1385. -50521. 2702765. -199360981. 19391512145.
333 -2404879675441. 370371188237525. -69348874393137901.)))
335 (putprop '*eu* 11 'lim)
336 (putprop 'bern 16 'lim)
338 (defmfun $euler (s)
339 (setq s
340 (let ((%n 0) $float)
341 (cond ((or (not (fixnump s)) (< s 0)) (list '($euler) s))
342 ((zerop (setq %n s)) 1)
343 ($zerobern
344 (cond ((oddp %n) 0)
345 ((null (> (ash %n -1) (get '*eu* 'lim)))
346 (aref *eu* (1- (ash %n -1))))
347 ((eq $zerobern '%$/#&)
348 (euler %n))
349 ((setq *eu* (adjust-array *eu* (1+ (ash %n -1))))
350 (euler %n))))
351 ((<= %n (get '*eu* 'lim))
352 (aref *eu* (1- %n)))
353 ((setq *eu* (adjust-array *eu* (1+ %n)))
354 (euler (* 2 %n))))))
355 (simplify s))
357 (defun nxtbincoef (m nom)
358 (truncate (* nom (- *a* m)) m))
360 (defun euler (%a*)
361 (prog (nom %k e fl $zerobern *a*)
362 (setq nom 1 %k %a* fl nil e 0 $zerobern '%$/#& *a* (1+ %a*))
363 a (cond ((zerop %k)
364 (setq e (- e))
365 (setf (aref *eu* (1- (ash %a* -1))) e)
366 (putprop '*eu* (ash %a* -1) 'lim)
367 (return e)))
368 (setq nom (nxtbincoef (1+ (- %a* %k)) nom) %k (1- %k))
369 (cond ((setq fl (null fl))
370 (go a)))
371 (incf e (* nom ($euler %k)))
372 (go a)))
374 (defmfun simpeuler (x vestigial z)
375 (declare (ignore vestigial))
376 (oneargcheck x)
377 (let ((u (simpcheck (cadr x) z)))
378 (if (and (fixnump u) (>= u 0))
379 ($euler u)
380 (eqtest (list '($euler) u) x))))
382 (defmfun $bern (s)
383 (setq s
384 (let ((%n 0) $float)
385 (cond ((or (not (fixnump s)) (< s 0)) (list '($bern) s))
386 ((= (setq %n s) 0) 1)
387 ((= %n 1) '((rat) -1 2))
388 ((= %n 2) '((rat) 1 6))
389 ($zerobern
390 (cond ((oddp %n) 0)
391 ((null (> (setq %n (1- (ash %n -1))) (get 'bern 'lim)))
392 (list '(rat) (aref *bn* %n) (aref *bd* %n)))
393 ((eq $zerobern '$/#&) (bern (* 2 (1+ %n))))
395 (setq *bn* (adjust-array *bn* (setq %n (1+ %n))))
396 (setq *bd* (adjust-array *bd* %n))
397 (bern (* 2 %n)))))
398 ((null (> %n (get 'bern 'lim)))
399 (list '(rat) (aref *bn* (- %n 2)) (aref *bd* (- %n 2))))
401 (setq *bn* (adjust-array *bn* (1+ %n)))
402 (setq *bd* (adjust-array *bd* (1+ %n)))
403 (bern (* 2 (1- %n)))))))
404 (simplify s))
406 (defun bern (%a*)
407 (prog (nom %k bb a b $zerobern l *a*)
408 (setq %k 0
409 l (1- %a*)
410 %a* (1+ %a*)
411 nom 1
412 $zerobern '$/#&
415 *a* (1+ %a*))
416 a (cond ((= %k l)
417 (setq bb (*red a (* -1 b %a*)))
418 (putprop 'bern (setq %a* (1- (ash %a* -1))) 'lim)
419 (setf (aref *bn* %a*) (cadr bb))
420 (setf (aref *bd* %a*) (caddr bb))
421 (return bb)))
422 (incf %k)
423 (setq a (+ (* b (setq nom (nxtbincoef %k nom))
424 (num1 (setq bb ($bern %k))))
425 (* a (denom1 bb))))
426 (setq b (* b (denom1 bb)))
427 (setq a (*red a b) b (denom1 a) a (num1 a))
428 (go a)))
430 (defmfun simpbern (x vestigial z)
431 (declare (ignore vestigial))
432 (oneargcheck x)
433 (let ((u (simpcheck (cadr x) z)))
434 (if (and (fixnump u) (not (< u 0)))
435 ($bern u)
436 (eqtest (list '($bern) u) x))))
438 ;;; ----------------------------------------------------------------------------
439 ;;; Bernoulli polynomials
441 ;;; The following explicit formula is directly implemented:
443 ;;; n
444 ;;; ====
445 ;;; \ n - k
446 ;;; B (x) = > b binomial(n, k) x
447 ;;; n / k
448 ;;; ====
449 ;;; k = 0
451 ;;; The coeffizients b[k] are the Bernoulli numbers. The algorithm does not
452 ;;; skip over Beroulli numbers, which are zero. We have to ensure that
453 ;;; $zerobern is bound to true.
454 ;;; ----------------------------------------------------------------------------
456 (defun $bernpoly (x s)
457 (let ((%n 0) ($zerobern t))
458 (cond ((not (fixnump s)) (list '($bernpoly) x s))
459 ((> (setq %n s) -1)
460 (do ((sum (cons (if (and (= %n 0) (zerop1 x))
461 (add 1 x)
462 (power x %n))
463 nil)
464 (cons (mul (binocomp %n %k)
465 ($bern %k)
466 (if (and (= %n %k) (zerop1 x))
467 (add 1 x)
468 (power x (- %n %k))))
469 sum))
470 (%k 1 (1+ %k)))
471 ((> %k %n) (addn sum t))))
472 (t (list '($bernpoly) x %n)))))
474 ;;; ----------------------------------------------------------------------------
475 ;;; Euler polynomials
477 ;;; The following explicit formula is directly implemented:
479 ;;; n 1 n - k
480 ;;; ==== E binomial(n, k) (x - -)
481 ;;; \ k 2
482 ;;; E (x) = > ------------------------------
483 ;;; n / k
484 ;;; ==== 2
485 ;;; k = 0
487 ;;; The coeffizients E[k] are the Euler numbers.
488 ;;; ----------------------------------------------------------------------------
490 (defun $eulerpoly (x s)
491 (let ((n 0) ($zerobern t) (y 0))
492 (cond ((not (fixnump s)) (list '($eulerpoly) x s))
493 ((> (setq n s) -1)
494 (do ((sum (cons (if (and (zerop1 (setq y (sub x (div 1 2))))
495 (= n 0))
496 (add 1 y)
497 (power y n))
498 nil)
499 (cons (mul (binocomp n k)
500 ($euler k)
501 (power 2 (mul -1 k))
502 (if (and (zerop1 (setq y (sub x (div 1 2))))
503 (= n k))
504 (add 1 y)
505 (power y (- n k))))
506 sum))
507 (k 1 (1+ k)))
508 ((> k n) ($expand (addn sum t)))))
509 (t (list '($eulerpoly) x n)))))
511 ;; zeta and fibonacci stuff
513 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
515 ;;; Implementation of the Riemann Zeta function as a simplifying function
517 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
519 (defun $zeta (z)
520 (simplify (list '(%zeta) z)))
522 ;;; Set properties to give full support to the parser and display
524 (defprop $zeta %zeta alias)
525 (defprop $zeta %zeta verb)
527 (defprop %zeta $zeta reversealias)
528 (defprop %zeta $zeta noun)
530 ;;; The Riemann Zeta function is a simplifying function
532 (defprop %zeta simp-zeta operators)
534 ;;; The Riemann Zeta function has mirror symmetry
536 (defprop %zeta t commutes-with-conjugate)
538 ;;; The Riemann Zeta function distributes over lists, matrices, and equations
540 (defprop %zeta (mlist $matrix mequal) distribute_over)
542 ;;; We support a simplim%function. The function is looked up in simplimit and
543 ;;; handles specific values of the function.
545 (defprop %zeta simplim%zeta simplim%function)
547 (defun simplim%zeta (expr var val)
548 ;; Look for the limit of the argument
549 (let* ((arg (limit (cadr expr) var val 'think))
550 (dir (limit (add (cadr expr) (neg arg)) var val 'think)))
551 (cond
552 ;; Handle an argument 1 at this place
553 ((onep1 arg)
554 (cond ((eq dir '$zeroa)
555 '$inf)
556 ((eq dir '$zerob)
557 '$minf)
558 (t '$infinity)))
560 ;; All other cases are handled by the simplifier of the function.
561 (simplify (list '(%zeta) arg))))))
563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
565 (defun simp-zeta (expr z simpflag)
566 (oneargcheck expr)
567 (setq z (simpcheck (cadr expr) simpflag))
568 (cond
570 ;; Check for special values
571 ((eq z '$inf) 1)
572 ((alike1 z '((mtimes) -1 $minf)) 1)
573 ((zerop1 z)
574 (cond (($bfloatp z) ($bfloat '((rat) -1 2)))
575 ((floatp z) -0.5)
576 (t '((rat simp) -1 2))))
577 ((onep1 z)
578 (simp-domain-error (intl:gettext "zeta: zeta(~:M) is undefined.") z))
580 ;; Check for numerical evaluation
581 ((or (bigfloat-numerical-eval-p z)
582 (complex-bigfloat-numerical-eval-p z)
583 (float-numerical-eval-p z)
584 (complex-float-numerical-eval-p z))
585 (to (float-zeta z)))
586 ;; Check for transformations and argument simplifications
587 ((integerp z)
588 (cond
589 ((oddp z)
590 (cond ((> z 1)
591 (eqtest (list '(%zeta) z) expr))
592 ((setq z (sub 1 z))
593 (mul -1 (div ($bern z) z)))))
594 ((minusp z) 0)
595 ((not $zeta%pi) (eqtest (list '(%zeta) z) expr))
596 (t (let ($numer $float)
597 (mul (power '$%pi z)
598 (mul (div (power 2 (1- z))
599 (take '(mfactorial) z))
600 (take '(mabs) ($bern z))))))))
602 (eqtest (list '(%zeta) z) expr))))
604 ;; See http://numbers.computation.free.fr/Constants/constants.html
605 ;; and, in particular,
606 ;; http://numbers.computation.free.fr/Constants/Miscellaneous/zetaevaluations.pdf.
607 ;; We use the algorithm from Proposition 2:
609 ;; zeta(s) = 1/(1-2^(1-s)) *
610 ;; (sum((-1)^(k-1)/k^s,k,1,n) +
611 ;; 1/2^n*sum((-1)^(k-1)*e(k-n)/k^s,k,n+1,2*n))
612 ;; + g(n,s)
614 ;; where e(k) = sum(binomial(n,j), j, k, n). Writing s = sigma + %i*t, when
615 ;; sigma is positive you get an error estimate of
617 ;; |g(n,s)| <= 1/8^n * h(s)
619 ;; where
621 ;; h(s) = ((1 + abs (t / sigma)) exp (abs (t) * %pi / 2)) / abs (1 - 2^(1 - s))
623 ;; We need to figure out how many terms are required to make |g(n,s)|
624 ;; sufficiently small. The answer is
626 ;; n = (log h(s) - log (eps)) / log (8)
628 ;; and
630 ;; log (h (s)) = (%pi/2 * abs (t)) + log (1 + t/sigma) - log (abs (1 - 2^(1 - s)))
632 ;; Notice that this bound is a bit rubbish when sigma is near zero. In that
633 ;; case, use the expansion zeta(s) = -1/2-1/2*log(2*pi)*s.
634 (defun float-zeta (s)
635 ;; If s is a rational (real or complex), convert to a float. This
636 ;; is needed so we can compute a sensible epsilon value. (What is
637 ;; the epsilon value for an exact rational?)
638 (setf s (bigfloat:to s))
639 (typecase s
640 (rational
641 (setf s (float s)))
642 ((complex rational)
643 (setf s (coerce s '(complex flonum)))))
645 (let ((sigma (bigfloat:realpart s)))
646 (cond
647 ;; abs(s)^2 < epsilon, use the expansion zeta(s) = -1/2-1/2*log(2*%pi)*s
648 ((bigfloat:< (bigfloat:abs (bigfloat:* s s)) (bigfloat:epsilon s))
649 (bigfloat:+ -1/2
650 (bigfloat:* -1/2
651 (bigfloat:log (bigfloat:* 2 (bigfloat:%pi s)))
652 s)))
654 ;; Reflection formula
655 ((bigfloat:minusp sigma)
656 (bigfloat:* (bigfloat:expt 2 s)
657 (bigfloat:expt (bigfloat:%pi s)
658 (bigfloat:- s 1))
659 (bigfloat:sin (bigfloat:* (bigfloat:/ (bigfloat:%pi s)
662 (bigfloat:to ($gamma (to (bigfloat:- 1 s))))
663 (float-zeta (bigfloat:- 1 s))))
665 ;; The general formula from above. Call the imaginary part "tau" rather
666 ;; than the "t" above, because that isn't a CL keyword...
668 (let* ((tau (bigfloat:imagpart s))
669 (logh
670 (bigfloat:-
671 (if (bigfloat:zerop tau) 0
672 (bigfloat:+
673 (bigfloat:* 1.6 (bigfloat:abs tau))
674 (bigfloat:log (bigfloat:1+
675 (bigfloat:abs
676 (bigfloat:/ tau sigma))))))
677 (bigfloat:log
678 (bigfloat:abs
679 (bigfloat:- 1 (bigfloat:expt 2 (bigfloat:- 1 s)))))))
681 (logeps (bigfloat:log (bigfloat:epsilon s)))
683 (n (max (bigfloat:ceiling
684 (bigfloat:/ (bigfloat:- logh logeps) (bigfloat:log 8)))
687 (sum1 0)
688 (sum2 0))
689 (flet ((binsum (k n)
690 ;; sum(binomial(n,j), j, k, n) = sum(binomial(n,j), j, n, k)
691 (let ((sum 0)
692 (term 1))
693 (loop for j from n downto k
695 (progn
696 (incf sum term)
697 (setf term (/ (* term j) (+ n 1 (- j))))))
698 sum)))
699 ;; (format t "n = ~D terms~%" n)
700 ;; sum1 = sum((-1)^(k-1)/k^s,k,1,n)
701 ;; sum2 = sum((-1)^(k-1)/e(n,k-n)/k^s, k, n+1, 2*n)
702 ;; = (-1)^n*sum((-1)^(m-1)*e(n,m)/(n+k)^s, k, 1, n)
703 (loop for k from 1 to n
704 for d = (bigfloat:expt k s)
705 do (progn
706 (bigfloat:incf sum1 (bigfloat:/ (cl:expt -1 (- k 1)) d))
707 (bigfloat:incf sum2 (bigfloat:/ (* (cl:expt -1 (- k 1))
708 (binsum k n))
709 (bigfloat:expt (+ k n) s))))
710 finally (return (values sum1 sum2)))
711 (when (oddp n)
712 (setq sum2 (bigfloat:- sum2)))
713 (bigfloat:/ (bigfloat:+ sum1
714 (bigfloat:/ sum2 (bigfloat:expt 2 n)))
715 (bigfloat:- 1 (bigfloat:expt 2 (bigfloat:- 1 s))))))))))
717 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
719 (defmfun $fib (n)
720 (cond ((fixnump n) (ffib n))
721 (t (setq $prevfib `(($fib) ,(add2* n -1)))
722 `(($fib) ,n))))
724 (defun ffib (%n)
725 (declare (fixnum %n))
726 (cond ((= %n -1)
727 (setq $prevfib -1)
729 ((zerop %n)
730 (setq $prevfib 1)
733 (let* ((f2 (ffib (ash (logandc2 %n 1) -1))) ; f2 = fib(n/2) or fib((n-1)/2)
734 (x (+ f2 $prevfib))
735 (y (* $prevfib $prevfib))
736 (z (* f2 f2)))
737 (setq f2 (- (* x x) y)
738 $prevfib (+ y z))
739 (when (oddp %n)
740 (psetq $prevfib f2
741 f2 (+ f2 $prevfib)))
742 f2))))
744 (defmfun $lucas (n)
745 (cond
746 ((fixnump n) (lucas n))
747 (t (setq $next_lucas `(($lucas) ,(add2* n 1)))
748 `(($lucas) ,n) )))
750 (defun lucas (n)
751 (declare (fixnum n))
752 (let ((w 2) (x 2) (y 1) u v (sign (signum n))) (declare (fixnum sign))
753 (setq n (abs n))
754 (do ((i (1- (integer-length n)) (1- i)))
755 ((< i 0))
756 (declare (fixnum i))
757 (setq u (* x x) v (* y y))
758 (if (logbitp i n)
759 (setq y (+ v w) x (+ y (- u) w) w -2)
760 (setq x (- u w) y (+ v w (- x)) w 2) ))
761 (cond
762 ((or (= 1 sign) (not (logbitp 0 n)))
763 (setq $next_lucas y)
766 (setq $next_lucas (neg y))
767 (neg x) ))))
769 ;; continued fraction stuff
771 (defmfun $cfdisrep (a)
772 (cond ((not ($listp a))
773 (merror (intl:gettext "cfdisrep: argument must be a list; found ~M") a))
774 ((null (cddr a)) (cadr a))
775 ((equal (cadr a) 0)
776 (list '(mexpt) (cfdisrep1 (cddr a)) -1))
777 ((cfdisrep1 (cdr a)))))
779 (defun cfdisrep1 (a)
780 (cond ((cdr a)
781 (list '(mplus simp cf) (car a)
782 (prog2 (setq a (cfdisrep1 (cdr a)))
783 (cond ((integerp a) (list '(rat simp) 1 a))
784 (t (list '(mexpt simp) a -1))))))
785 ((car a))))
787 (defun cfmak (a)
788 (setq a (meval a))
789 (cond ((integerp a) (list a))
790 ((eq (caar a) 'mlist) (cdr a))
791 ((eq (caar a) 'rat) (ratcf (cadr a) (caddr a)))
792 ((merror (intl:gettext "cf: continued fractions must be lists or integers; found ~M") a))))
794 (defun makcf (a)
795 (cond ((null (cdr a)) (car a))
796 ((cons '(mlist simp cf) a))))
798 ;;; Translation properties for $CF defined in MAXSRC;TRANS5 >
800 (defmspec $cf (a)
801 (cfratsimp (let ($listarith)
802 (cfeval (meval (fexprcheck a))))))
804 ;; Definition of cfratsimp as given in SF bug report # 620928.
805 (defun cfratsimp (a)
806 (cond ((atom a) a)
807 ((member 'cf (car a) :test #'eq) a)
808 (t (cons '(mlist cf simp)
809 (apply 'find-cf (cf-back-recurrence (cdr a)))))))
811 ; Code to expand nth degree roots of integers into continued fraction
812 ; approximations. E.g. cf(2^(1/3))
813 ; Courtesy of Andrei Zorine (feniy@mail.nnov.ru) 2005/05/07
815 (defun cfnroot(b)
816 (let ((ans (list '(mlist xf))) ent ($algebraic $true))
817 (dotimes (i $cflength (nreverse ans))
818 (setq ent (meval `(($floor) ,b))
819 ans (cons ent ans)
820 b ($ratsimp (m// (m- b ent)))))))
822 (defun cfeval (a)
823 (let (temp $ratprint)
824 (cond ((integerp a) (list '(mlist cf) a))
825 ((floatp a)
826 (let ((a (maxima-rationalize a)))
827 (cons '(mlist cf) (ratcf (car a) (cdr a)))))
828 (($bfloatp a)
829 (let (($bftorat t))
830 (setq a (bigfloat2rat a))
831 (cons '(mlist cf) (ratcf (car a) (cdr a)))))
832 ((atom a)
833 (merror (intl:gettext "cf: ~:M is not a continued fraction.") a))
834 ((eq (caar a) 'rat)
835 (cons '(mlist cf) (ratcf (cadr a) (caddr a))))
836 ((eq (caar a) 'mlist)
837 (cfratsimp a))
838 ;;the following doesn't work for non standard form
839 ;; (cfplus a '((mlist) 0)))
840 ((and (mtimesp a) (cddr a) (null (cdddr a))
841 (fixnump (cadr a))
842 (mexptp (caddr a))
843 (fixnump (cadr (caddr a)))
844 (alike1 (caddr (caddr a)) '((rat) 1 2)))
845 (cfsqrt (cfeval (* (expt (cadr a) 2) (cadr (caddr a))))))
846 ((eq (caar a) 'mexpt)
847 (cond ((alike1 (caddr a) '((rat) 1 2))
848 (cfsqrt (cfeval (cadr a))))
849 ((integerp (m* 2 (caddr a))) ; a^(n/2) was sqrt(a^n)
850 (cfsqrt (cfeval (cfexpt (cadr a) (m* 2 (caddr a))))))
851 ((integerp (cadr a)) (cfnroot a)) ; <=== new case x
852 ((cfexpt (cfeval (cadr a)) (caddr a)))))
853 ((setq temp (assoc (caar a) '((mplus . cfplus) (mtimes . cftimes) (mquotient . cfquot)
854 (mdifference . cfdiff) (mminus . cfminus)) :test #'eq))
855 (cf (cfeval (cadr a)) (cddr a) (cdr temp)))
856 ((eq (caar a) 'mrat)
857 (cfeval ($ratdisrep a)))
858 (t (merror (intl:gettext "cf: ~:M is not a continued fraction.") a)))))
860 (defun cf (a l fun)
861 (cond ((null l) a)
862 ((cf (funcall fun a (meval (list '($cf) (car l)))) (cdr l) fun))))
864 (defun cfplus (a b)
865 (setq a (cfmak a) b (cfmak b))
866 (makcf (cffun '(0 1 1 0) '(0 0 0 1) a b)))
868 (defun cftimes (a b)
869 (setq a (cfmak a) b (cfmak b))
870 (makcf (cffun '(1 0 0 0) '(0 0 0 1) a b)))
872 (defun cfdiff (a b)
873 (setq a (cfmak a) b (cfmak b))
874 (makcf (cffun '(0 1 -1 0) '(0 0 0 1) a b)))
876 (defun cfmin (a)
877 (setq a (cfmak a))
878 (makcf (cffun '(0 0 -1 0) '(0 0 0 1) a '(0))))
880 (defun cfquot (a b)
881 (setq a (cfmak a) b (cfmak b))
882 (makcf (cffun '(0 1 0 0) '(0 0 1 0) a b)))
884 (defun cfexpt (b e)
885 (setq b (cfmak b))
886 (cond ((null (integerp e))
887 (merror (intl:gettext "cf: can't raise continued fraction to non-integral power ~M") e))
888 ((let ((n (abs e)))
889 (do ((n (ash n -1) (ash n -1))
890 (s (cond ((oddp n) b)
891 (t '(1)))))
892 ((zerop n)
893 (makcf
894 (cond ((signp g e)
896 ((cffun '(0 0 0 1) '(0 1 0 0) b '(1))))))
897 (setq b (cffun '(1 0 0 0) '(0 0 0 1) b b))
898 (and (oddp n)
899 (setq s (cffun '(1 0 0 0) '(0 0 0 1) s b))))))))
902 (defun conf1 (f g a b &aux (den (conf2 g a b)))
903 (cond ((zerop den)
904 (* (signum (conf2 f a b )) ; (/ most-positive-fixnum (^ 2 4))
905 #.(expt 2 31)))
906 (t (truncate (conf2 f a b) den))))
908 (defun conf2 (n a b) ;2*(abn_0+an_1+bn_2+n_3)
909 (* 2 (+ (* (car n) a b)
910 (* (cadr n) a)
911 (* (caddr n) b)
912 (cadddr n))))
914 ;;(cffun '(0 1 1 0) '(0 0 0 1) '(1 2) '(1 1 1 2)) gets error
915 ;;should give (3 10)
917 (defun cf-convergents-p-q (cf &optional (n (length cf)) &aux pp qq)
918 "returns two lists such that pp_i/qq_i is the quotient of the first i terms
919 of cf"
920 (case (length cf)
921 (0 1)
922 (1 cf(list 1))
924 (setq pp (list (1+ (* (first cf) (second cf))) (car cf)))
925 (setq qq (list (second cf) 1))
926 (show pp qq)
927 (setq cf (cddr cf))
928 (loop for i from 2 to n
929 while cf
931 (push (+ (* (car cf) (car pp))
932 (second pp)) pp)
933 (push (+ (* (car cf) (car qq))
934 (second qq)) qq)
935 (setq cf (cdr cf))
936 finally (return (list (reverse pp) (reverse qq)))))))
939 (defun find-cf1 (p q so-far)
940 (multiple-value-bind (quot rem) (truncate p q)
941 (cond ((< rem 0) (incf rem q) (incf quot -1))
942 ((zerop rem) (return-from find-cf1 (cons quot so-far))))
943 (setq so-far (cons quot so-far))
944 (find-cf1 q rem so-far)))
946 (defun find-cf (p q)
947 "returns the continued fraction for p and q integers, q not zero"
948 (cond ((zerop q) (maxima-error "find-cf: quotient by zero"))
949 ((< q 0) (setq p (- p)) (setq q (- q))))
950 (nreverse (find-cf1 p q ())))
952 (defun cf-back-recurrence (cf &aux tem (num-gg 0)(den-gg 1))
953 "converts CF (a continued fraction list) to a list of numerator
954 denominator using recurrence from end
955 and not calculating intermediate quotients.
956 The numerator and denom are relatively
957 prime"
958 (loop for v in (reverse cf)
959 do (setq tem (* den-gg v))
960 (setq tem (+ tem num-gg))
961 (setq num-gg den-gg)
962 (setq den-gg tem)
963 finally
964 (return
965 (cond ((and (<= den-gg 0) (< num-gg 0))
966 (list (- den-gg) (- num-gg)))
967 (t(list den-gg num-gg))))))
969 (declare-top (unspecial w))
971 ;;(cffun '(0 1 1 0) '(0 0 0 1) '(1 2) '(1 1 1 2)) gets error
972 ;;should give (3 10)
974 (defun cffun (f g a b)
975 (prog (c v w)
976 (declare (special v))
977 a (and (zerop (cadddr g))
978 (zerop (caddr g))
979 (zerop (cadr g))
980 (zerop (car g))
981 (return (reverse c)))
982 (and (equal (setq w (conf1 f g (car a) (1+ (car b))))
983 (setq v (conf1 f g (car a) (car b))))
984 (equal (conf1 f g (1+ (car a)) (car b)) v)
985 (equal (conf1 f g (1+ (car a)) (1+ (car b))) v)
986 (setq g (mapcar #'(lambda (a b)
987 (declare (special v))
988 (- a (* v b)))
989 f (setq f g)))
990 (setq c (cons v c))
991 (go a))
992 (cond ((< (abs (- (conf1 f g (1+ (car a)) (car b)) v))
993 (abs (- w v)))
994 (cond ((setq v (cdr b))
995 (setq f (conf6 f b))
996 (setq g (conf6 g b))
997 (setq b v))
998 (t (setq f (conf7 f b)) (setq g (conf7 g b)))))
1000 (cond ((setq v (cdr a))
1001 (setq f (conf4 f a))
1002 (setq g (conf4 g a))
1003 (setq a v))
1004 (t (setq f (conf5 f a)) (setq g (conf5 g a))))))
1005 (go a)))
1007 (defun conf4 (n a) ;n_0*a_0+n_2,n_1*a_0+n_3,n_0,n_1
1008 (list (+ (* (car n) (car a)) (caddr n))
1009 (+ (* (cadr n) (car a)) (cadddr n))
1010 (car n)
1011 (cadr n)))
1013 (defun conf5 (n a) ;0,0, n_0*a_0,n_2
1014 (list 0 0
1015 (+ (* (car n) (car a)) (caddr n))
1016 (+ (* (cadr n) (car a)) (cadddr n))))
1018 (defun conf6 (n b)
1019 (list (+ (* (car n) (car b)) (cadr n))
1020 (car n)
1021 (+ (* (caddr n) (car b)) (cadddr n))
1022 (caddr n)))
1024 (defun conf7 (n b)
1025 (list 0 (+ (* (car n) (car b)) (cadr n))
1026 0 (+ (* (caddr n) (car b)) (cadddr n))))
1028 (defun cfsqrt (n)
1029 (cond ((cddr n) ;A non integer
1030 (merror (intl:gettext "cf: argument of sqrt must be an integer; found ~M") n))
1031 ((setq n (cadr n))))
1032 (setq n (sqcont n))
1033 (cond ((= $cflength 1)
1034 (cons '(mlist simp) n))
1035 ((do ((i 2 (1+ i))
1036 (a (copy-tree (cdr n))))
1037 ((> i $cflength) (cons '(mlist simp) n))
1038 (setq n (nconc n (copy-tree a)))))))
1040 (defmfun $qunit (n)
1041 (let ((isqrtn ($isqrt n)))
1042 (when (or (not (integerp n))
1043 (minusp n)
1044 (= (* isqrtn isqrtn) n))
1045 (merror
1046 (intl:gettext "qunit: Argument must be a positive non quadratic integer.")))
1047 (let ((l (sqcont n)))
1048 (list '(mplus) (pelso1 l 0 1)
1049 (list '(mtimes)
1050 (list '(mexpt) n '((rat) 1 2))
1051 (pelso1 l 1 0))))))
1053 (defun pelso1 (l a b)
1054 (do ((i l (cdr i))) (nil)
1055 (and (null (cdr i)) (return b))
1056 (setq b (+ a (* (car i) (setq a b))))))
1058 (defun sqcont (n)
1059 (prog (q q1 q2 m m1 a0 a l)
1060 (setq a0 ($isqrt n) a (list a0) q2 1 m1 a0
1061 q1 (- n (* m1 m1)) l (* 2 a0))
1062 a (setq a (cons (truncate (+ m1 a0) q1) a))
1063 (cond ((equal (car a) l)
1064 (return (nreverse a))))
1065 (setq m (- (* (car a) q1) m1)
1066 q (+ q2 (* (car a) (- m1 m)))
1067 q2 q1 q1 q m1 m)
1068 (go a)))
1070 (defun ratcf (x y)
1071 (prog (a b)
1072 a (cond ((equal y 1) (return (nreverse (cons x a))))
1073 ((minusp x)
1074 (setq b (+ y (rem x y))
1075 a (cons (1- (truncate x y)) a)
1076 x y y b))
1077 ((> y x)
1078 (setq a (cons 0 a))
1079 (setq b x x y y b))
1080 ((equal x y) (return (nreverse (cons 1 a))))
1081 ((setq b (rem x y))
1082 (setq a (cons (truncate x y) a) x y y b)))
1083 (go a)))
1085 (defmfun $cfexpand (x)
1086 (cond ((null ($listp x)) x)
1087 ((cons '($matrix) (cfexpand (cdr x))))))
1089 (defun cfexpand (ll)
1090 (do ((p1 0 p2)
1091 (p2 1 (simplify (list '(mplus) (list '(mtimes) (car l) p2) p1)))
1092 (q1 1 q2)
1093 (q2 0 (simplify (list '(mplus) (list '(mtimes) (car l) q2) q1)))
1094 (l ll (cdr l)))
1095 ((null l) (list (list '(mlist) p2 p1) (list '(mlist) q2 q1)))))
1097 ;; Summation stuff
1099 (defun adsum (e)
1100 (push (simplify e) sum))
1102 (defun adusum (e)
1103 (push (simplify e) usum))
1105 (defmfun simpsum2 (exp i lo hi)
1106 (prog (*plus *times $simpsum u)
1107 (setq *plus (list 0) *times 1)
1108 (when (or (and (eq hi '$inf) (eq lo '$minf))
1109 (equal 0 (m+ hi lo)))
1110 (setq $simpsum t lo 0)
1111 (setq *plus (cons (m* -1 *times (maxima-substitute 0 i exp)) *plus))
1112 (setq exp (m+ exp (maxima-substitute (m- i) i exp))))
1113 (cond ((eq ($sign (setq u (m- hi lo))) '$neg)
1114 (if (equal u -1)
1115 (return 0)
1116 (merror (intl:gettext "sum: lower bound ~M greater than upper bound ~M") lo hi)))
1117 ((free exp i)
1118 (return (m+l (cons (freesum exp lo hi *times) *plus))))
1120 ((setq exp (sumsum exp i lo hi))
1121 (setq exp (m* *times (dosum (cadr exp) (caddr exp)
1122 (cadddr exp) (cadr (cdddr exp)) t :evaluate-summand nil))))
1123 (t (return (m+l *plus))))
1124 (return (m+l (cons exp *plus)))))
1126 (defun sumsum (e *var* lo hi)
1127 (let (sum usum)
1128 (cond ((eq hi '$inf)
1129 (cond (*infsumsimp (isum e lo))
1130 ((setq usum (list e)))))
1131 ((finite-sum e 1 lo hi)))
1132 (cond ((eq sum nil)
1133 (return-from sumsum (list '(%sum) e *var* lo hi))))
1134 (setq *plus
1135 (nconc (mapcar
1136 #'(lambda (q) (simptimes (list '(mtimes) *times q) 1 nil))
1137 sum)
1138 *plus))
1139 (and usum (setq usum (list '(%sum) (simplus (cons '(plus) usum) 1 t) *var* lo hi)))))
1141 (defun finite-sum (e y lo hi)
1142 (cond ((null e))
1143 ((free e *var*)
1144 (adsum (m* y e (m+ hi 1 (m- lo)))))
1145 ((poly? e *var*)
1146 (adsum (m* y (fpolysum e lo hi))))
1147 ((eq (caar e) '%binomial) (fbino e y lo hi))
1148 ((eq (caar e) 'mplus)
1149 (mapc #'(lambda (q) (finite-sum q y lo hi)) (cdr e)))
1150 ((and (or (mtimesp e) (mexptp e) (mplusp e))
1151 (fsgeo e y lo hi)))
1153 (adusum e)
1154 nil)))
1156 (defun isum-giveup (e)
1157 (cond ((atom e) nil)
1158 ((eq (caar e) 'mexpt)
1159 (not (or (free (cadr e) *var*)
1160 (ratp (caddr e) *var*))))
1161 ((member (caar e) '(mplus mtimes) :test #'eq)
1162 (some #'identity (mapcar #'isum-giveup (cdr e))))
1163 (t)))
1165 (defun isum (e lo)
1166 (cond ((isum-giveup e)
1167 (setq sum nil usum (list e)))
1168 ((eq (catch 'isumout (isum1 e lo)) 'divergent)
1169 (merror (intl:gettext "sum: sum is divergent.")))))
1171 (defun isum1 (e lo)
1172 (cond ((free e *var*)
1173 (unless (eq (asksign e) '$zero)
1174 (throw 'isumout 'divergent)))
1175 ((ratp e *var*)
1176 (adsum (ipolysum e lo)))
1177 ((eq (caar e) 'mplus)
1178 (mapc #'(lambda (x) (isum1 x lo)) (cdr e)))
1179 ( (isgeo e lo))
1180 ((adusum e))))
1182 (defun ipolysum (e lo)
1183 (ipoly1 ($expand e) lo))
1185 (defun ipoly1 (e lo)
1186 (cond ((smono e *var*)
1187 (ipoly2 *a *n lo (asksign (simplify (list '(mplus) *n 1)))))
1188 ((mplusp e)
1189 (cons '(mplus) (mapcar #'(lambda (x) (ipoly1 x lo)) (cdr e))))
1190 (t (adusum e)
1191 0)))
1193 (defun ipoly2 (a n lo sign)
1194 (cond ((member (asksign lo) '($zero $negative) :test #'eq)
1195 (throw 'isumout 'divergent)))
1196 (and (null (equal lo 1))
1197 (let ((sign sign) ($simpsum t))
1198 (adsum `((%sum)
1199 ((mtimes) ,a -1 ((mexpt) ,*var* ,n))
1200 ,*var* 1 ((mplus) -1 ,lo)))))
1201 (cond ((eq sign '$negative)
1202 (list '(mtimes) a ($zeta (meval (list '(mtimes) -1 n)))))
1203 ((throw 'isumout 'divergent))))
1205 (defun fsgeo (e y lo hi)
1206 (let ((r ($ratsimp (div* (maxima-substitute (list '(mplus) *var* 1) *var* e) e))))
1207 (cond ((equal r 1)
1208 (adsum
1209 (list '(mtimes)
1210 (list '(mplus) 1 hi (list '(mtimes) -1 lo))
1211 (maxima-substitute lo *var* e))))
1212 ((free r *var*)
1213 (adsum
1214 (list '(mtimes) y
1215 (maxima-substitute 0 *var* e)
1216 (list '(mplus)
1217 (list '(mexpt) r (list '(mplus) hi 1))
1218 (list '(mtimes) -1 (list '(mexpt) r lo)))
1219 (list '(mexpt) (list '(mplus) r -1) -1)))))))
1221 (defun isgeo (e lo)
1222 (let ((r ($ratsimp (div* (maxima-substitute (list '(mplus) *var* 1) *var* e) e))))
1223 (and (free r *var*)
1224 (isgeo1 (maxima-substitute lo *var* e)
1225 r (asksign (simplify (list '(mplus) (list '(mabs) r) -1)))))))
1227 (defun isgeo1 (a r sign)
1228 (cond ((eq sign '$positive)
1229 (throw 'isumout 'divergent))
1230 ((eq sign '$zero)
1231 (throw 'isumout 'divergent))
1232 ((eq sign '$negative)
1233 (adsum (list '(mtimes) a
1234 (list '(mexpt) (list '(mplus) 1 (list '(mtimes) -1 r)) -1))))))
1237 ;; Sums of polynomials using
1238 ;; bernpoly(x+1, n) - bernpoly(x, n) = n*x^(n-1)
1239 ;; which implies
1240 ;; sum(k^n, k, A, B) = 1/(n+1)*(bernpoly(B+1, n+1) - bernpoly(A, n+1))
1242 ;; fpoly1 returns 1/(n+1)*(bernpoly(foo+1, n+1) - bernpoly(0, n+1)) for each power
1243 ;; in the polynomial e
1245 (defun fpolysum (e lo hi) ;returns *ans*
1246 (let ((a (fpoly1 (setq e ($expand ($ratdisrep ($rat e *var*)))) lo))
1247 ($prederror))
1248 (cond ((null a) 0)
1249 ((member lo '(0 1))
1250 (maxima-substitute hi 'foo a))
1252 (list '(mplus) (maxima-substitute hi 'foo a)
1253 (list '(mtimes) -1 (maxima-substitute (list '(mplus) lo -1) 'foo a)))))))
1255 (defun fpoly1 (e lo)
1256 (cond ((smono e *var*)
1257 (fpoly2 *a *n e lo))
1258 ((eq (caar e) 'mplus)
1259 (cons '(mplus) (mapcar #'(lambda (x) (fpoly1 x lo)) (cdr e))))
1260 (t (adusum e) 0)))
1262 (defun fpoly2 (a n e lo)
1263 (cond ((null (and (integerp n) (> n -1))) (adusum e) 0)
1264 ((equal n 0)
1265 (m* (cond ((signp e lo)
1266 (m1+ 'foo))
1267 (t 'foo))
1269 (($ratsimp
1270 (m* a (list '(rat) 1 (1+ n))
1271 (m- ($bernpoly (m+ 'foo 1) (1+ n))
1272 ($bern (1+ n))))))))
1274 ;; fbino can do these sums:
1275 ;; a) sum(binomial(n,k),k,0,n) -> 2^n
1276 ;; b) sum(binomial(n-k,k,k,0,n) -> fib(n+1)
1277 ;; c) sum(binomial(n,2k),k,0,n) -> 2^(n-1)
1278 ;; d) sum(binomial(a+k,b),k,l,h) -> binomial(h+a+1,b+1) - binomial(l+a,b+1)
1279 (defun fbino (e y lo hi)
1280 ;; e=binomial(n,d)
1281 (prog (n d l h)
1282 ;; check that n and d are linear in *var*
1283 (when (null (setq n (m2 (cadr e) (list 'n 'linear* *var*))))
1284 (return (adusum e)))
1285 (setq n (cdr (assoc 'n n :test #'eq)))
1286 (when (null (setq d (m2 (caddr e) (list 'd 'linear* *var*))))
1287 (return (adusum e)))
1288 (setq d (cdr (assoc 'd d :test #'eq)))
1290 ;; binomial(a+b*k,c+b*k) -> binomial(a+b*k, a-c)
1291 (when (equal (cdr n) (cdr d))
1292 (setq d (cons (m- (car n) (car d)) 0)))
1294 (cond
1295 ;; substitute k with -k in sum(binomial(a+b*k, c-d*k))
1296 ;; and sum(binomial(a-b*k,c))
1297 ((and (numberp (cdr d))
1298 (or (minusp (cdr d))
1299 (and (zerop (cdr d))
1300 (numberp (cdr n))
1301 (minusp (cdr n)))))
1302 (rplacd d (- (cdr d)))
1303 (rplacd n (- (cdr n)))
1304 (setq l (m- hi)
1305 h (m- lo)))
1306 (t (setq l lo h hi)))
1308 (cond
1310 ;; sum(binomial(a+k,c),k,l,h)
1311 ((and (equal 0 (cdr d)) (equal 1 (cdr n)))
1312 (adsum (m* y (m- (list '(%binomial) (m+ h (car n) 1) (m+ (car d) 1))
1313 (list '(%binomial) (m+ l (car n)) (m+ (car d) 1))))))
1315 ;; sum(binomial(n,k),k,0,n)=2^n
1316 ((and (equal 1 (cdr d)) (equal 0 (cdr n)))
1317 ;; sum(binomial(n,k+c),k,l,h)=sum(binomial(n,k+c+l),k,0,h-l)
1318 (let ((h1 (m- h l))
1319 (c (m+ (car d) l)))
1320 (if (and (integerp (m- (car n) h1))
1321 (integerp c))
1322 (progn
1323 (adsum (m* y (m^ 2 (car n))))
1324 (when (member (asksign (m- (m+ h1 c) (car n))) '($zero $negative) :test #'eq)
1325 (adsum (m* -1 y (dosum (list '(%binomial) (car n) *var*)
1326 *var* (m+ h1 c 1) (car n) t :evaluate-summand nil))))
1327 (when (> c 0)
1328 (adsum (m* -1 y (dosum (list '(%binomial) (car n) *var*)
1329 *var* 0 (m- c 1) t :evaluate-summand nil)))))
1330 (adusum e))))
1332 ;; sum(binomial(b-k,k),k,0,floor(b/2))=fib(b+1)
1333 ((and (equal -1 (cdr n)) (equal 1 (cdr d)))
1334 ;; sum(binomial(a-k,b+k),k,l,h)=sum(binomial(a+b-k,k),k,l+b,h+b)
1335 (let ((h1 (m+ h (car d)))
1336 (l1 (m+ l (car d)))
1337 (a1 (m+ (car n) (car d))))
1338 ;; sum(binomial(a1-k,k),k,0,floor(a1/2))=fib(a1+1)
1339 ;; we only do sums with h>floor(a1/2)
1340 (if (and (integerp l1)
1341 (member (asksign (m- h1 (m// a1 2))) '($zero $positive) :test #'eq))
1342 (progn
1343 (adsum (m* y ($fib (m+ a1 1))))
1344 (when (> l1 0)
1345 (adsum (m* -1 y (dosum (list '(%binomial) (m- a1 *var*) *var*)
1346 *var* 0 (m- l1 1) t :evaluate-summand nil)))))
1347 (adusum e))))
1349 ;; sum(binomial(n,2*k),k,0,floor(n/2))=2^(n-1)
1350 ;; sum(binomial(n,2*k+1),k,0,floor((n-1)/2))=2^(n-1)
1351 ((and (equal 0 (cdr n)) (equal 2 (cdr d)))
1352 ;; sum(binomial(a,2*k+b),k,l,h)=sum(binomial(a,2*k),k,l+b/2,h+b/2), b even
1353 ;; sum(binomial(a,2*k+b),k,l,h)=sum(binomial(a,2*k+1),k,l+(b-1)/2,h+(b-1)/2), b odd
1354 (let ((a (car n))
1355 (r1 (if (oddp (car d)) 1 0))
1356 (l1 (if (oddp (car d))
1357 (m+ l (truncate (1- (car d)) 2))
1358 (m+ l (truncate (car d) 2)))))
1359 (when (and (integerp l1)
1360 (member (asksign (m- a hi)) '($zero $positive) :test #'eq))
1361 (adsum (m* y (m^ 2 (m- a 1))))
1362 (when (> l1 0)
1363 (adsum (m* -1 y (dosum (list '(%binomial) a (m+ *var* *var* r1))
1364 *var* 0 (m- l1 1) t :evaluate-summand nil)))))))
1366 ;; other sums we can't do
1368 (adusum e)))))
1370 ;; product routines
1372 (defmspec $product (l)
1373 (setq l (cdr l))
1374 (cond ((not (= (length l) 4)) (merror (intl:gettext "product: expected exactly four arguments.")))
1375 ((dosum (car l) (cadr l) (meval (caddr l)) (meval (cadddr l)) nil :evaluate-summand t))))
1377 (declare-top (special $ratsimpexpons))
1379 ;; Is this guy actually looking at the value of its middle arg?
1381 (defun simpprod (x y z)
1382 (let (($ratsimpexpons t))
1383 (cond ((equal y 1)
1384 (setq y (simplifya (cadr x) z)))
1385 ((setq y (simptimes (list '(mexpt) (cadr x) y) 1 z)))))
1386 (simpprod1 y (caddr x)
1387 (simplifya (cadddr x) z)
1388 (simplifya (cadr (cdddr x)) z)))
1390 (defmfun $taytorat (e)
1391 (cond ((mbagp e) (cons (car e) (mapcar #'$taytorat (cdr e))))
1392 ((or (atom e) (not (member 'trunc (cdar e) :test #'eq))) (ratf e))
1393 ((catch 'srrat (srrat e)))
1394 (t (ratf ($ratdisrep e)))))
1396 (defun srrat (e)
1397 (cons (list 'mrat 'simp (caddar e) (cadddr (car e)))
1398 (srrat2 (cdr e))))
1400 (defun srrat2 (e)
1401 (if (pscoefp e) e (srrat3 (terms e) (gvar e))))
1403 (defun srrat3 (l *var*)
1404 (cond ((null l) '(0 . 1))
1405 ((null (=1 (cdr (le l))))
1406 (throw 'srrat nil))
1407 ((null (n-term l))
1408 (rattimes (cons (list *var* (car (le l)) 1) 1)
1409 (srrat2 (lc l))
1411 ((ratplus
1412 (rattimes (cons (list *var* (car (le l)) 1) 1)
1413 (srrat2 (lc l))
1415 (srrat3 (n-term l) *var*)))))
1418 (declare-top (special $props *i))
1420 (defmspec $deftaylor (l)
1421 (prog (fun series param op ops)
1422 a (when (null (setq l (cdr l))) (return (cons '(mlist) ops)))
1423 (setq fun (meval (car l)) series (meval (cadr l)) l (cdr l) param () )
1424 (when (or (atom fun)
1425 (if (eq (caar fun) 'mqapply)
1426 (or (cdddr fun) ; must be one parameter
1427 (null (cddr fun)) ; must have exactly one
1428 (do ((subs (cdadr fun) (cdr subs)))
1429 ((null subs)
1430 (setq op (caaadr fun))
1431 (when (cddr fun)
1432 (setq param (caddr fun)))
1433 '())
1434 (unless (atom (car subs)) (return 't))))
1435 (progn
1436 (setq op (caar fun))
1437 (when (cdr fun) (setq param (cadr fun)))
1438 (or (and (oldget op 'op) (not (eq op 'mfactorial)))
1439 (not (atom (cadr fun)))
1440 (not (= (length fun) 2))))))
1441 (merror (intl:gettext "deftaylor: don't know how to handle this function: ~M") fun))
1442 (when (oldget op 'sp2)
1443 (mtell (intl:gettext "deftaylor: redefining ~:M.~%") op))
1444 (when param (setq series (subst 'sp2var param series)))
1445 (setq series (subsum '*index series))
1446 (putprop op series 'sp2)
1447 (when (eq (caar fun) 'mqapply)
1448 (putprop op (cdadr fun) 'sp2subs))
1449 (add2lnc op $props)
1450 (push op ops)
1451 (go a)))
1453 (defun subsum (*i e) (susum1 e))
1455 (defun susum1 (e)
1456 (cond ((atom e) e)
1457 ((eq (caar e) '%sum)
1458 (if (null (smonop (cadr e) 'sp2var))
1459 (merror (intl:gettext "deftaylor: argument must be a power series at 0."))
1460 (subst *i (caddr e) e)))
1461 (t (recur-apply #'susum1 e))))
1463 (declare-top (special varlist genvar $factorflag $ratfac *p* *var* *l* *x*))
1465 (defmfun $polydecomp (e v)
1466 (let ((varlist (list v))
1467 (genvar nil)
1468 *var* p den $factorflag $ratfac)
1469 (setq p (cdr (ratf (ratdisrep e)))
1470 *var* (cdr (ratf v)))
1471 (cond ((or (null (cdr *var*))
1472 (null (equal (cdar *var*) '(1 1))))
1473 (merror (intl:gettext "polydecomp: second argument must be an atom; found ~M") v))
1474 (t (setq *var* (caar *var*))))
1475 (cond ((or (pcoefp (cdr p))
1476 (null (eq (cadr p) *var*)))
1477 (setq den (cdr p)
1478 p (car p)))
1479 (t (merror (intl:gettext "polydecomp: cannot apply 'polydecomp' to a rational function."))))
1480 (cons '(mlist)
1481 (cond ((or (pcoefp p)
1482 (null (eq (car p) *var*)))
1483 (list (rdis (cons p den))))
1484 (t (setq p (pdecomp p *var*))
1485 (do ((l
1486 (setq p (mapcar #'(lambda (q) (cons q 1)) p))
1487 (cdr l))
1488 (a))
1489 ((null l)
1490 (cons (rdis (cons (caar p)
1491 (ptimes (cdar p) den)))
1492 (mapcar #'rdis (cdr p))))
1493 (cond ((setq a (pdecpow (car l) *var*))
1494 (rplaca l (car a))
1495 (cond ((cdr l)
1496 (rplacd l
1497 (cons (ratplus
1498 (rattimes
1499 (cadr l)
1500 (cons (ptterm (cdaadr a) 1)
1501 (cdadr a))
1503 (cons
1504 (ptterm (cdaadr a) 0)
1505 (cdadr a)))
1506 (cddr l))))
1507 ((equal (cadr a)
1508 (cons (list *var* 1 1) 1)))
1509 (t (rplacd l (list (cadr a)))))))))))))
1512 ;;; POLYDECOMP is like $POLYDECOMP except it takes a poly in *POLY* format (as
1513 ;;; defined in SOLVE) (numerator of a RAT form) and returns a list of
1514 ;;; headerless rat forms. In otherwords, it is $POLYDECOMP minus type checking
1515 ;;; and conversions to/from general representation which SOLVE doesn't
1516 ;;; want/need on a general basis.
1517 ;;; It is used in the SOLVE package and as such it should have an autoload
1518 ;;; property
1520 (defun polydecomp (p *var*)
1521 (let ($factorflag $ratfac)
1522 (cond ((or (pcoefp p)
1523 (null (eq (car p) *var*)))
1524 (cons p nil))
1525 (t (setq p (pdecomp p *var*))
1526 (do ((l (setq p (mapcar #'(lambda (q) (cons q 1)) p))
1527 (cdr l))
1528 (a))
1529 ((null l)
1530 (cons (cons (caar p)
1531 (cdar p))
1532 (cdr p)))
1533 (cond ((setq a (pdecpow (car l) *var*))
1534 (rplaca l (car a))
1535 (cond ((cdr l)
1536 (rplacd l
1537 (cons (ratplus
1538 (rattimes
1539 (cadr l)
1540 (cons (ptterm (cdaadr a) 1)
1541 (cdadr a))
1543 (cons
1544 (ptterm (cdaadr a) 0)
1545 (cdadr a)))
1546 (cddr l))))
1547 ((equal (cadr a)
1548 (cons (list *var* 1 1) 1)))
1549 (t (rplacd l (list (cadr a))))))))))))
1553 (defun pdecred (f h *var*) ;f = g(h(*var*))
1554 (cond ((or (pcoefp h) (null (eq (car h) *var*))
1555 (equal (cadr h) 1)
1556 (null (zerop (rem (cadr f) (cadr h))))
1557 (and (null (pzerop (caadr (setq f (pdivide f h)))))
1558 (equal (cdadr f) 1)))
1559 nil)
1560 (t (do ((q (pdivide (caar f) h) (pdivide (caar q) h))
1561 (i 1 (1+ i))
1562 (*ans*))
1563 ((pzerop (caar q))
1564 (cond ((and (equal (cdadr q) 1)
1565 (or (pcoefp (caadr q))
1566 (null (eq (caar (cadr q)) *var*))))
1567 (psimp *var* (cons i (cons (caadr q) *ans*))))))
1568 (cond ((and (equal (cdadr q) 1)
1569 (or (pcoefp (caadr q))
1570 (null (eq (caar (cadr q)) *var*))))
1571 (and (null (pzerop (caadr q)))
1572 (setq *ans* (cons i (cons (caadr q) *ans*)))))
1573 (t (return nil)))))))
1575 (defun pdecomp (p *var*)
1576 (let ((c (ptterm (cdr p) 0))
1577 (a) (*x* (list *var* 1 1)))
1578 (cons (pcplus c (car (setq a (pdecomp* (pdifference p c)))))
1579 (cdr a))))
1581 (defun pdecomp* (*p*)
1582 (let ((a)
1583 (l (pdecgdfrm (pfactor (pquotient *p* *x*)))))
1584 (cond ((or (pdecprimep (cadr *p*))
1585 (null (setq a (pdecomp1 *x* l))))
1586 (list *p*))
1587 (t (append (pdecomp* (car a)) (cdr a))))))
1589 (defun pdecomp1 (prod l)
1590 (cond ((null l)
1591 (and (null (equal (cadr prod) (cadr *p*)))
1592 (setq l (pdecred *p* prod *var*))
1593 (list l prod)))
1594 ((pdecomp1 prod (cdr l)))
1595 (t (pdecomp1 (ptimes (car l) prod) (cdr l)))))
1597 (defun pdecgdfrm (l) ;Get list of divisors
1598 (do ((l (copy-list l ))
1599 (ll (list (car l))
1600 (cons (car l) ll)))
1601 (nil)
1602 (rplaca (cdr l) (1- (cadr l)))
1603 (cond ((signp e (cadr l))
1604 (setq l (cddr l))))
1605 (cond ((null l) (return ll)))))
1607 (defun pdecprimep (x)
1608 (setq x (cfactorw x))
1609 (and (null (cddr x)) (equal (cadr x) 1)))
1611 (defun pdecpow (p *var*)
1612 (setq p (car p))
1613 (let ((p1 (pderivative p *var*))
1614 p2 p1p p1c a lin p2p)
1615 (setq p1p (oldcontent p1)
1616 p1c (car p1p) p1p (cadr p1p))
1617 (setq p2 (pderivative p1 *var*))
1618 (setq p2p (cadr (oldcontent p2)))
1619 (and (setq lin (testdivide p1p p2p))
1620 (null (pcoefp lin))
1621 (eq (car lin) *var*)
1622 (list (ratplus
1623 (rattimes (cons (list *var* (cadr p) 1) 1)
1624 (setq a (ratreduce p1c
1625 (ptimes (cadr p)
1626 (caddr lin))))
1628 (ratdif (cons p 1)
1629 (rattimes a (cons (pexpt lin (cadr p)) 1)
1630 t)))
1631 (cons lin 1)))))
1633 (declare-top (unspecial *mfactl *factlist donel nn* dn*
1634 *var* *ans* *n *a*
1635 *infsumsimp *times *plus sum usum makef))