Eliminate spurious redefinition of derivabbrev in Ctensor, fix documentation of diagm...
[maxima/cygwin.git] / src / asum.lisp
blobfcfc11a0eb9368ff342d978343e60fb3bfc6f26e
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 asum)
15 (load-macsyma-macros rzmac)
17 (declare-top (special opers *a *n $factlim sum msump *i *opers-list opers-list $ratsimpexpons makef $factorial_expand))
19 (loop for (x y) on '(%cot %tan %csc %sin %sec %cos %coth %tanh %csch %sinh %sech %cosh)
20 by #'cddr do (putprop x y 'recip) (putprop y x 'recip))
22 (defmvar $zeta%pi t)
24 ;; polynomial predicates and other such things
26 (defun poly? (exp var)
27 (cond ((or (atom exp) (free exp var)))
28 ((member (caar exp) '(mtimes mplus) :test #'eq)
29 (do ((exp (cdr exp) (cdr exp)))
30 ((null exp) t)
31 (and (null (poly? (car exp) var)) (return nil))))
32 ((and (eq (caar exp) 'mexpt)
33 (integerp (caddr exp))
34 (> (caddr exp) 0))
35 (poly? (cadr exp) var))))
37 (defun smono (x var)
38 (smonogen x var t))
40 (defun smonop (x var)
41 (smonogen x var nil))
43 (defun smonogen (x var fl) ; fl indicates whether to return *a *n
44 (cond ((free x var) (and fl (setq *n 0 *a x)) t)
45 ((atom x) (and fl (setq *n (setq *a 1))) t)
46 ((and (listp (car x))
47 (eq (caar x) 'mtimes))
48 (do ((x (cdr x) (cdr x))
49 (a '(1)) (n '(0)))
50 ((null x)
51 (and fl (setq *n (addn n nil) *a (muln a nil))) t)
52 (let (*a *n)
53 (if (smonogen (car x) var fl)
54 (and fl (setq a (cons *a a) n (cons *n n)))
55 (return nil)))))
56 ((and (listp (car x))
57 (eq (caar x) 'mexpt))
58 (cond ((and (free (caddr x) var) (eq (cadr x) var))
59 (and fl (setq *n (caddr x) *a 1)) t)))))
61 ;; factorial stuff
63 (setq $factlim 100000 ; set to a big integer which will work (not -1)
64 makef nil)
66 (defmfun $genfact (&rest l)
67 (cons '(%genfact) l))
69 (defun gfact (n %m i)
70 (cond ((minusp %m) (improper-arg-err %m '$genfact))
71 ((= %m 0) 1)
72 (t (prog (ans)
73 (setq ans n)
74 a (if (= %m 1) (return ans))
75 (setq n (m- n i) %m (1- %m) ans (m* ans n))
76 (go a)))))
78 ;; From Richard Fateman's paper, "Comments on Factorial Programs",
79 ;; http://www.cs.berkeley.edu/~fateman/papers/factorial.pdf
81 ;; k(n,m) = n*(n-m)*(n-2*m)*...
83 ;; (k n 1) is n!
85 ;; This is much faster (3-4 times) than the original factorial
86 ;; function.
88 (defun k (n m)
89 (if (<= n m)
91 (* (k n (* 2 m))
92 (k (- n m) (* 2 m)))))
94 (defun factorial (n)
95 (if (zerop n)
97 (k n 1)))
99 ;;; Factorial has mirror symmetry
101 (defprop mfactorial t commutes-with-conjugate)
103 (defmfun simpfact (x y z)
104 (oneargcheck x)
105 (setq y (simpcheck (cadr x) z))
106 (cond ((and (mnump y)
107 (eq ($sign y) '$neg)
108 (zerop1 (sub (simplify (list '(%truncate) y)) y)))
109 ;; Negative integer or a real representation of a negative integer.
110 (merror (intl:gettext "factorial: factorial of negative integer ~:M not defined.") y))
111 ((or (floatp y)
112 ($bfloatp y)
113 (and (not (integerp y))
114 (not (ratnump y))
115 (or (and (complex-number-p y 'float-or-rational-p)
116 (or $numer
117 (floatp ($realpart y))
118 (floatp ($imagpart y))))
119 (and (complex-number-p y 'bigfloat-or-number-p)
120 (or $numer
121 ($bfloatp ($realpart y))
122 ($bfloatp ($imagpart y))))))
123 (and (not makef) (ratnump y) (equal (caddr y) 2)))
124 ;; Numerically evaluate for real or complex argument in float or
125 ;; bigfloat precision using the Gamma function
126 (simplify (list '(%gamma) (add 1 y))))
127 ((eq y '$inf) '$inf)
128 ((and $factorial_expand
129 (mplusp y)
130 (integerp (cadr y)))
131 ;; factorial(n+m) and m integer. Expand.
132 (let ((m (cadr y))
133 (n (simplify (cons '(mplus) (cddr y)))))
134 (cond ((>= m 0)
135 (mul
136 (simplify (list '($pochhammer) (add n 1) m))
137 (simplify (list '(mfactorial) n))))
138 ((< m 0)
139 (setq m (- m))
140 (div
141 (mul (power -1 m) (simplify (list '(mfactorial) n)))
142 ;; We factor to get the ordering (n-1)*(n-2)*...
143 ($factor
144 (simplify (list '($pochhammer) (mul -1 n) m))))))))
145 ((or (not (fixnump y)) (not (> y -1)))
146 (eqtest (list '(mfactorial) y) x))
147 ((or (minusp $factlim) (not (> y $factlim)))
148 (factorial y))
149 (t (eqtest (list '(mfactorial) y) x))))
151 (defun makegamma1 (e)
152 (cond ((atom e) e)
153 ((eq (caar e) 'mfactorial)
154 (list '(%gamma) (list '(mplus) 1 (makegamma1 (cadr e)))))
156 ;; Begin code copied from orthopoly/orthopoly-init.lisp
157 ;; Do pochhammer(x,n) ==> gamma(x+n)/gamma(x).
159 ((eq (caar e) '$pochhammer)
160 (let ((x (makegamma1 (nth 1 e)))
161 (n (makegamma1 (nth 2 e))))
162 (div (take '(%gamma) (add x n)) (take '(%gamma) x))))
164 ;; (gamma(x/z+1)*z^floor(y))/gamma(x/z-floor(y)+1)
166 ((eq (caar e) '%genfact)
167 (let ((x (makegamma1 (nth 1 e)))
168 (y (makegamma1 (nth 2 e)))
169 (z (makegamma1 (nth 3 e))))
170 (setq y (take '($floor) y))
171 (div
172 (mul
173 (take '(%gamma) (add (div x z) 1))
174 (power z y))
175 (take '(%gamma) (sub (add (div x z) 1) y)))))
176 ;; End code copied from orthopoly/orthopoly-init.lisp
178 ;; Double factorial
180 ((eq (caar e) '%factorial_double)
181 (let ((x (makegamma1 (nth 1 e))))
182 (mul
183 (power
184 (div 2 '$%pi)
185 (mul
186 (div 1 4)
187 (sub 1 (simplify (list '(%cos) (mul '$%pi x))))))
188 (power 2 (div x 2))
189 (simplify (list '(%gamma) (add 1 (div x 2)))))))
191 ((eq (caar e) '%elliptic_kc)
192 ;; Complete elliptic integral of the first kind
193 (cond ((alike1 (cadr e) '((rat simp) 1 2))
194 ;; K(1/2) = gamma(1/4)/4/sqrt(pi)
195 '((mtimes simp) ((rat simp) 1 4)
196 ((mexpt simp) $%pi ((rat simp) -1 2))
197 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2)))
198 ((or (alike1 (cadr e)
199 '((mtimes simp) ((rat simp) 1 4)
200 ((mplus simp) 2
201 ((mexpt simp) 3 ((rat simp) 1 2)))))
202 (alike1 (cadr e)
203 '((mplus simp) ((rat simp) 1 2)
204 ((mtimes simp) ((rat simp) 1 4)
205 ((mexpt simp) 3 ((rat simp) 1 2)))))
206 (alike1 (cadr e)
207 ;; 1/(8-4*sqrt(3))
208 '((mexpt simp)
209 ((mplus simp) 8
210 ((mtimes simp) -4
211 ((mexpt simp) 3 ((rat simp) 1 2))))
212 -1)))
213 ;; K((2+sqrt(3)/4))
214 '((mtimes simp) ((rat simp) 1 4)
215 ((mexpt simp) 3 ((rat simp) 1 4))
216 ((mexpt simp) $%pi ((rat simp) -1 2))
217 ((%gamma simp) ((rat simp) 1 6))
218 ((%gamma simp) ((rat simp) 1 3))))
219 ((or (alike1 (cadr e)
220 ;; (2-sqrt(3))/4
221 '((mtimes simp) ((rat simp) 1 4)
222 ((mplus simp) 2
223 ((mtimes simp) -1
224 ((mexpt simp) 3 ((rat simp) 1 2))))))
225 (alike1 (cadr e)
226 ;; 1/2-sqrt(3)/4
227 '((mplus simp) ((rat simp) 1 2)
228 ((mtimes simp) ((rat simp) -1 4)
229 ((mexpt simp) 3 ((rat simp) 1 2)))))
230 (alike (cadr e)
231 ;; 1/(4*sqrt(3)+8)
232 '((mexpt simp)
233 ((mplus simp) 8
234 ((mtimes simp) 4
235 ((mexpt simp) 3 ((rat simp) 1 2))))
236 -1)))
237 ;; K((2-sqrt(3))/4)
238 '((mtimes simp) ((rat simp) 1 4)
239 ((mexpt simp) 3 ((rat simp) -1 4))
240 ((mexpt simp) $%pi ((rat simp) -1 2))
241 ((%gamma simp) ((rat simp) 1 6))
242 ((%gamma simp) ((rat simp) 1 3))))
243 ((or
244 ;; (3-2*sqrt(2))/(3+2*sqrt(2))
245 (alike1 (cadr e)
246 '((mtimes simp)
247 ((mplus simp) 3
248 ((mtimes simp) -2
249 ((mexpt simp) 2 ((rat simp) 1 2))))
250 ((mexpt simp)
251 ((mplus simp) 3
252 ((mtimes simp) 2
253 ((mexpt simp) 2 ((rat simp) 1 2)))) -1)))
254 ;; 17 - 12*sqrt(2)
255 (alike1 (cadr e)
256 '((mplus simp) 17
257 ((mtimes simp) -12
258 ((mexpt simp) 2 ((rat simp) 1 2)))))
259 ;; (2*SQRT(2) - 3)/(2*SQRT(2) + 3)
260 (alike1 (cadr e)
261 '((mtimes simp) -1
262 ((mplus simp) -3
263 ((mtimes simp) 2
264 ((mexpt simp) 2 ((rat simp) 1 2))))
265 ((mexpt simp)
266 ((mplus simp) 3
267 ((mtimes simp) 2
268 ((mexpt simp) 2 ((rat simp) 1 2))))
269 -1))))
270 '((mtimes simp) ((rat simp) 1 8)
271 ((mexpt simp) 2 ((rat simp) -1 2))
272 ((mplus simp) 1 ((mexpt simp) 2 ((rat simp) 1 2)))
273 ((mexpt simp) $%pi ((rat simp) -1 2))
274 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2)))
276 ;; Give up
277 e)))
278 ((eq (caar e) '%elliptic_ec)
279 ;; Complete elliptic integral of the second kind
280 (cond ((alike1 (cadr e) '((rat simp) 1 2))
281 ;; 2*E(1/2) - K(1/2) = 2*%pi^(3/2)*gamma(1/4)^(-2)
282 '((mplus simp)
283 ((mtimes simp) ((mexpt simp) $%pi ((rat simp) 3 2))
284 ((mexpt simp)
285 ((%gamma simp irreducible) ((rat simp) 1 4)) -2))
286 ((mtimes simp) ((rat simp) 1 8)
287 ((mexpt simp) $%pi ((rat simp) -1 2))
288 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2))))
289 ((or (alike1 (cadr e)
290 '((mtimes simp) ((rat simp) 1 4)
291 ((mplus simp) 2
292 ((mtimes simp) -1
293 ((mexpt simp) 3 ((rat simp) 1 2))))))
294 (alike1 (cadr e)
295 '((mplus simp) ((rat simp) 1 2)
296 ((mtimes simp) ((rat simp) -1 4)
297 ((mexpt simp) 3 ((rat simp) 1 2))))))
298 ;; E((2-sqrt(3))/4)
300 ;; %pi/4/sqrt(3) = K*(E-(sqrt(3)+1)/2/sqrt(3)*K)
301 '((mplus simp)
302 ((mtimes simp) ((mexpt simp) 3 ((rat simp) -1 4))
303 ((mexpt simp) $%pi ((rat simp) 3 2))
304 ((mexpt simp) ((%gamma simp) ((rat simp) 1 6)) -1)
305 ((mexpt simp) ((%gamma simp) ((rat simp) 1 3)) -1))
306 ((mtimes simp) ((rat simp) 1 8)
307 ((mexpt simp) 3 ((rat simp) -3 4))
308 ((mexpt simp) $%pi ((rat simp) -1 2))
309 ((%gamma simp) ((rat simp) 1 6))
310 ((%gamma simp) ((rat simp) 1 3)))
311 ((mtimes simp) ((rat simp) 1 8)
312 ((mexpt simp) 3 ((rat simp) -1 4))
313 ((mexpt simp) $%pi ((rat simp) -1 2))
314 ((%gamma simp) ((rat simp) 1 6))
315 ((%gamma simp) ((rat simp) 1 3)))))
316 ((or (alike1 (cadr e)
317 '((mtimes simp) ((rat simp) 1 4)
318 ((mplus simp) 2
319 ((mexpt simp) 3 ((rat simp) 1 2)))))
320 (alike1 (cadr e)
321 '((mplus simp) ((rat simp) 1 2)
322 ((mtimes simp) ((rat simp) 1 4)
323 ((mexpt simp) 3 ((rat simp) 1 2))))))
324 ;; E((2+sqrt(3))/4)
326 ;; %pi*sqrt(3)/4 = K1*(E1-(sqrt(3)-1)/2/sqrt(3)*K1)
327 '((mplus simp)
328 ((mtimes simp) 3 ((mexpt simp) 3 ((rat simp) -3 4))
329 ((mexpt simp) $%pi ((rat simp) 3 2))
330 ((mexpt simp) ((%gamma simp) ((rat simp) 1 6)) -1)
331 ((mexpt simp) ((%gamma simp) ((rat simp) 1 3)) -1))
332 ((mtimes simp) ((rat simp) 3 8)
333 ((mexpt simp) 3 ((rat simp) -3 4))
334 ((mexpt simp) $%pi ((rat simp) -1 2))
335 ((%gamma simp) ((rat simp) 1 6))
336 ((%gamma simp) ((rat simp) 1 3)))
337 ((mtimes simp) ((rat simp) -1 8)
338 ((mexpt simp) 3 ((rat simp) -1 4))
339 ((mexpt simp) $%pi ((rat simp) -1 2))
340 ((%gamma simp) ((rat simp) 1 6))
341 ((%gamma simp) ((rat simp) 1 3)))))
343 e)))
344 (t (recur-apply #'makegamma1 e))))
346 (defmfun simpgfact (x vestigial z)
347 (declare (ignore vestigial))
348 (if (not (= (length x) 4)) (wna-err '$genfact))
349 (setq z (mapcar #'(lambda (q) (simpcheck q z)) (cdr x)))
350 (let ((a (car z)) (b (take '($floor) (cadr z))) (c (caddr z)))
351 (cond ((and (fixnump a)
352 (fixnump b)
353 (fixnump c))
354 (if (and (> a -1)
355 (> b -1)
356 (or (<= c a) (= b 0))
357 (<= b (/ a c)))
358 (gfact a b c)
359 (merror (intl:gettext "genfact: generalized factorial not defined for given arguments."))))
360 (t (eqtest (list '(%genfact) a
361 (if (and (not (atom b))
362 (eq (caar b) '$floor))
363 (cadr b)
366 x)))))
368 ;; sum begins
370 (defmvar $cauchysum nil
371 "When multiplying together sums with INF as their upper limit,
372 causes the Cauchy product to be used rather than the usual product.
373 In the Cauchy product the index of the inner summation is a function of
374 the index of the outer one rather than varying independently."
375 modified-commands '$sum)
377 (defmvar $gensumnum 0
378 "The numeric suffix used to generate the next variable of
379 summation. If it is set to FALSE then the index will consist only of
380 GENINDEX with no numeric suffix."
381 modified-commands '$sum
382 setting-predicate #'(lambda (x) (or (null x) (integerp x))))
384 (defmvar $genindex '$i
385 "The alphabetic prefix used to generate the next variable of
386 summation when necessary."
387 modified-commands '$sum
388 setting-predicate #'symbolp)
390 (defmvar $zerobern t)
391 (defmvar $simpsum nil)
392 (defmvar $simpproduct nil)
394 (defvar *infsumsimp t)
396 ;; These variables should be initialized where they belong.
398 (setq $wtlevel nil $cflength 1
399 $weightlevels '((mlist)) *trunclist nil $taylordepth 3
400 $maxtaydiff 4 $verbose nil $psexpand nil ps-bmt-disrep t
401 silent-taylor-flag nil)
403 (defmacro sum-arg (sum)
404 `(cadr ,sum))
406 (defmacro sum-index (sum)
407 `(caddr ,sum))
409 (defmacro sum-lower (sum)
410 `(cadddr ,sum))
412 (defmacro sum-upper (sum)
413 `(cadr (cdddr ,sum)))
415 (defmspec $sum (l)
416 (setq l (cdr l))
417 (if (= (length l) 4)
418 (dosum (car l) (cadr l) (meval (caddr l)) (meval (cadddr l)) t :evaluate-summand t)
419 (wna-err '$sum)))
421 (defmspec $lsum (l)
422 (setq l (cdr l))
423 (or (= (length l) 3) (wna-err '$lsum))
424 (let ((form (car l))
425 (ind (cadr l))
426 (lis (meval (caddr l)))
427 (ans 0))
428 (or (symbolp ind) (merror (intl:gettext "lsum: second argument must be a variable; found ~M") ind))
429 (cond (($listp lis)
430 (loop for v in (cdr lis)
431 with lind = (cons ind nil)
432 for w = (cons v nil)
434 (setq ans (add* ans (mbinding (lind w) (meval form)))))
435 ans)
436 (t `((%lsum) ,form ,ind ,lis)))))
438 (defmfun simpsum (x y z)
439 (let (($ratsimpexpons t))
440 (setq y (simplifya (sum-arg x) z)))
441 (simpsum1 y (sum-index x) (simplifya (sum-lower x) z)
442 (simplifya (sum-upper x) z)))
444 ; This function was SIMPSUM1 until the sum/product code was revised Nov 2005.
445 ; The revised code punts back to this function since this code knows
446 ; some simplifications not handled by the revised code. -- Robert Dodier
448 (defun simpsum1-save (exp i lo hi)
449 (cond ((not (symbolp i)) (merror (intl:gettext "sum: index must be a symbol; found ~M") i))
450 ((equal lo hi) (mbinding ((list i) (list hi)) (meval exp)))
451 ((and (atom exp)
452 (not (eq exp i))
453 (getl '%sum '($outative $linear)))
454 (freesum exp lo hi 1))
455 ((null $simpsum) (list (get '%sum 'msimpind) exp i lo hi))
456 ((and (or (eq lo '$minf)
457 (alike1 lo '((mtimes simp) -1 $inf)))
458 (equal hi '$inf))
459 (let ((pos-part (simpsum2 exp i 0 '$inf))
460 (neg-part (simpsum2 (maxima-substitute (m- i) i exp) i 1 '$inf)))
461 (cond
462 ((or (eq neg-part '$und)
463 (eq pos-part '$und))
464 '$und)
465 ((eq pos-part '$inf)
466 (if (eq neg-part '$minf) '$und '$inf))
467 ((eq pos-part '$minf)
468 (if (eq neg-part '$inf) '$und '$minf))
469 ((or (eq neg-part '$inf) (eq neg-part '$minf))
470 neg-part)
471 (t (m+ neg-part pos-part)))))
472 ((or (eq lo '$minf)
473 (alike1 lo '((mtimes simp) -1 '$inf)))
474 (simpsum2 (maxima-substitute (m- i) i exp) i (m- hi) '$inf))
475 (t (simpsum2 exp i lo hi))))
477 ;; DOSUM, MEVALSUMARG, DO%SUM -- general principles
479 ;; - evaluate the summand/productand
480 ;; - substitute a gensym for the index variable and make assertions (via assume) about the gensym index
481 ;; - return 0/1 for empty sum/product. sumhack/prodhack are ignored
482 ;; - distribute sum/product over mbags when listarith = true
484 (defun dosum (expr ind low hi sump &key (evaluate-summand t))
485 (setq low (ratdisrep low) hi (ratdisrep hi)) ;; UGH, GAG WITH ME A SPOON
486 (if (not (symbolp ind))
487 (merror (intl:gettext "~:M: index must be a symbol; found ~M") (if sump '$sum '$product) ind))
488 (unwind-protect
489 (prog (u *i lind l*i *hl)
490 (setq lind (cons ind nil))
491 (cond
492 ((not (fixnump (setq *hl (mfuncall '$floor (m- hi low)))))
493 (if evaluate-summand (setq expr (mevalsumarg expr ind low hi)))
494 (return (cons (if sump '(%sum) '(%product))
495 (list expr ind low hi))))
496 ((signp l *hl)
497 (return (if sump 0 1))))
498 (setq *i low l*i (list *i) u (if sump 0 1))
499 lo (setq u
500 (if sump
501 (add u (resimplify (let* ((foo (mbinding (lind l*i) (meval expr)))
502 (bar (subst-if-not-freeof *i ind foo)))
503 bar)))
504 (mul u (resimplify (let* ((foo (mbinding (lind l*i) (meval expr)))
505 (bar (subst-if-not-freeof *i ind foo)))
506 bar)))))
507 (when (zerop *hl) (return u))
508 (setq *hl (1- *hl))
509 (setq *i (car (rplaca l*i (m+ *i 1))))
510 (go lo))))
512 (defun subst-if-not-freeof (x y expr)
513 (if ($freeof y expr)
514 expr
515 (if (atom expr)
517 (let* ((args (cdr expr))
518 (L (eval `(mapcar (lambda (a) (subst-if-not-freeof ',x ',y a)) ',args))))
519 (cons (car expr) L)))))
521 (defun mevalsumarg (expr ind low hi)
522 (if (let (($prederror nil))
523 (eq (mevalp `((mlessp) ,hi ,low)) t))
526 (let ((gensym-ind (gensym)))
527 (if (apparently-integer low)
528 (meval `(($declare) ,gensym-ind $integer)))
529 (assume (list '(mgeqp) gensym-ind low))
530 (if (not (eq hi '$inf))
531 (assume (list '(mgeqp) hi gensym-ind)))
532 (let ((msump t) (foo) (summand))
533 (setq summand
534 (if (and (not (atom expr)) (get (caar expr) 'mevalsumarg-macro))
535 (funcall (get (caar expr) 'mevalsumarg-macro) expr)
536 expr))
537 (let (($simp nil))
538 (setq summand ($substitute gensym-ind ind summand)))
539 (setq foo (mbinding ((list gensym-ind) (list gensym-ind))
540 (resimplify (meval summand))))
541 ;; At this point we do not switch off simplification to preserve
542 ;; the achieved simplification of the summand (DK 02/2010).
543 (let (($simp t))
544 (setq foo ($substitute ind gensym-ind foo)))
545 (if (not (eq hi '$inf))
546 (forget (list '(mgeqp) hi gensym-ind)))
547 (forget (list '(mgeqp) gensym-ind low))
548 (if (apparently-integer low)
549 (meval `(($remove) ,gensym-ind $integer)))
550 foo)))
552 (defun apparently-integer (x)
553 (or ($integerp x) ($featurep x '$integer)))
555 (defun do%sum (l op)
556 (if (not (= (length l) 4)) (wna-err op))
557 (let ((ind (cadr l)))
558 (if (mquotep ind) (setq ind (cadr ind)))
559 (if (not (symbolp ind))
560 (merror (intl:gettext "~:M: index must be a symbol; found ~M") op ind))
561 (let ((low (caddr l))
562 (hi (cadddr l)))
563 (list (mevalsumarg (car l) ind low hi)
564 ind (meval (caddr l)) (meval (cadddr l))))))
566 (defun simpsum1 (e k lo hi)
567 (let ((fact1) (fact2) (acc 0) (n) (sgn) ($prederror nil) (i (gensym)) (ex))
568 (setq lo ($ratdisrep lo))
569 (setq hi ($ratdisrep hi))
571 (setq n ($limit (add 1 (sub hi lo))))
572 (setq sgn ($sign n))
574 (setq fact1 `((mgeqp) ,i ,lo))
575 (setq fact2 `((mgeqp) ,hi ,i))
577 (if (not (eq t (csign lo))) (mfuncall '$assume fact1))
578 (if (not (eq t (csign hi))) (mfuncall '$assume fact2))
580 (setq ex (subst i k e))
581 (setq ex (subst i k ex))
583 (setq acc
584 (cond ((and (eq n '$inf) ($freeof i ex))
585 (setq sgn (csign ex))
586 (cond ((eq sgn '$pos) '$inf)
587 ((eq sgn '$neg) '$minf)
588 ((eq sgn '$zero) 0)
589 (t `((%sum simp) ,ex ,i ,lo ,hi))))
591 ((and (mbagp e) $listarith)
592 (simplifya
593 `((,(caar e)) ,@(mapcar #'(lambda (s) (mfuncall '$sum s k lo hi)) (margs e))) t))
595 ((or (eq sgn '$neg) (eq sgn '$zero) (eq sgn '$nz)) 0)
597 ((like ex 0) 0)
599 (($freeof i ex) (mult n ex))
601 ((and (integerp n) (eq sgn '$pos) $simpsum)
602 (unwind-protect
603 (dotimes (j n acc)
604 (setq acc (add acc (resimplify (subst (add j lo) i ex)))))
605 (mfuncall '$forget fact1)
606 (mfuncall '$forget fact2)))
608 (setq ex (subst '%sum '$sum ex))
609 `((%sum simp) ,(subst k i ex) ,k ,lo ,hi))))
611 (setq acc (subst k i acc))
613 ;; If expression is still a summation,
614 ;; punt to previous simplification code.
616 (if (and $simpsum (op-equalp acc '$sum '%sum))
617 (let* ((args (cdr acc)) (e (first args)) (i (second args)) (i0 (third args)) (i1 (fourth args)))
618 (setq acc (simpsum1-save e i i0 i1))))
620 (mfuncall '$forget fact1)
621 (mfuncall '$forget fact2)
623 acc))
625 (defun simpprod1 (e k lo hi)
626 (let ((fact1) (fact2) (acc 1) (n) (sgn) ($prederror nil) (i (gensym)) (ex) (ex-mag) (realp))
628 (setq lo ($ratdisrep lo))
629 (setq hi ($ratdisrep hi))
630 (setq n ($limit (add 1 (sub hi lo))))
631 (setq sgn ($sign n))
632 (setq fact1 `((mgeqp) ,i ,lo))
633 (setq fact2 `((mgeqp) ,hi ,i))
635 (if (not (eq t (csign lo))) (mfuncall '$assume fact1))
636 (if (not (eq t (csign hi))) (mfuncall '$assume fact2))
638 (setq ex (subst i k e))
639 (setq ex (subst i k ex))
641 (setq acc
642 (cond
643 ((like ex 1) 1)
645 ((and (eq n '$inf) ($freeof i ex))
646 (setq ex-mag (mfuncall '$cabs ex))
647 (setq realp (mfuncall '$imagpart ex))
648 (setq realp (mevalp `((mequal) 0 ,realp)))
650 (cond ((eq t (mevalp `((mlessp) ,ex-mag 1))) 0)
651 ((and (eq realp t) (eq t (mevalp `((mgreaterp) ,ex 1)))) '$inf)
652 ((eq t (mevalp `((mgreaterp) ,ex-mag 1))) '$infinity)
653 ((eq t (mevalp `((mequal) 1 ,ex-mag))) '$und)
654 (t `((%product) ,e ,k ,lo ,hi))))
656 ((or (eq sgn '$neg) (eq sgn '$zero) (eq sgn '$nz))
659 ((and (mbagp e) $listarith)
660 (simplifya
661 `((,(caar e)) ,@(mapcar #'(lambda (s) (mfuncall '$product s k lo hi)) (margs e))) t))
663 (($freeof i ex) (power ex n))
665 ((and (integerp n) (eq sgn '$pos) $simpproduct)
666 (unwind-protect
667 (dotimes (j n acc)
668 (setq acc (mult acc (resimplify (subst (add j lo) i ex)))))
670 (mfuncall '$forget fact1)
671 (mfuncall '$forget fact2)))
674 (setq ex (subst '%product '$product ex))
675 `((%product simp) ,(subst k i ex) ,k ,lo ,hi))))
677 ;; Hmm, this is curious... don't call existing product simplifications
678 ;; if index range is infinite -- what's up with that??
680 (if (and $simpproduct (op-equalp acc '$product '%product) (not (like n '$inf)))
681 (let* ((args (cdr acc)) (e (first args)) (i (second args)) (i0 (third args)) (i1 (fourth args)))
682 (setq acc (simpprod1-save e i i0 i1))))
684 (setq acc (subst k i acc))
685 (setq acc (subst '%product '$product acc))
687 (mfuncall '$forget fact1)
688 (mfuncall '$forget fact2)
690 acc))
692 ; This function was SIMPPROD1 until the sum/product code was revised Nov 2005.
693 ; The revised code punts back to this function since this code knows
694 ; some simplifications not handled by the revised code. -- Robert Dodier
696 (defun simpprod1-save (exp i lo hi)
697 (let (u)
698 (cond ((not (symbolp i)) (merror (intl:gettext "product: index must be a symbol; found ~M") i))
699 ((alike1 lo hi)
700 (let ((valist (list i)))
701 (mbinding (valist (list hi))
702 (meval exp))))
703 ((eq ($sign (setq u (m- hi lo))) '$neg)
704 (cond ((eq ($sign (add2 u 1)) '$zero) 1)
705 (t (merror (intl:gettext "product: lower bound ~M greater than upper bound ~M") lo hi))))
706 ((atom exp)
707 (cond ((null (eq exp i))
708 (power* exp (list '(mplus) hi 1 (list '(mtimes) -1 lo))))
709 ((let ((lot (asksign lo)))
710 (cond ((equal lot '$zero) 0)
711 ((eq lot '$positive)
712 (m// (list '(mfactorial) hi)
713 (list '(mfactorial) (list '(mplus) lo -1))))
714 ((m* (list '(mfactorial)
715 (list '(mabs) lo))
716 (cond ((member (asksign hi) '($zero $positive) :test #'eq)
718 (t (prog2 0
719 (m^ -1 (m+ hi lo 1))
720 (setq hi (list '(mabs) hi)))))
721 (list '(mfactorial) hi))))))))
722 ((list '(%product simp) exp i lo hi)))))
725 ;; multiplication of sums
727 (defun gensumindex ()
728 (intern (format nil "~S~D" $genindex (incf $gensumnum))))
730 (defun sumtimes (x y)
731 (cond ((null x) y)
732 ((null y) x)
733 ((or (safe-zerop x) (safe-zerop y)) 0)
734 ((or (atom x) (not (eq (caar x) '%sum))) (sumultin x y))
735 ((or (atom y) (not (eq (caar y) '%sum))) (sumultin y x))
736 (t (let (u v i j)
737 (if (great (sum-arg x) (sum-arg y)) (setq u y v x) (setq u x v y))
738 (setq i (let ((ind (gensumindex)))
739 (setq u (subst ind (sum-index u) u)) ind))
740 (setq j (let ((ind (gensumindex)))
741 (setq v (subst ind (sum-index v) v)) ind))
742 (if (and $cauchysum (eq (sum-upper u) '$inf)
743 (eq (sum-upper v) '$inf))
744 (list '(%sum)
745 (list '(%sum)
746 (sumtimes (maxima-substitute j i (sum-arg u))
747 (maxima-substitute (m- i j) j (sum-arg v)))
748 j (sum-lower u) (m- i (sum-lower v)))
749 i (m+ (sum-lower u) (sum-lower v)) '$inf)
750 (list '(%sum)
751 (list '(%sum) (sumtimes (sum-arg u) (sum-arg v))
752 j (sum-lower v) (sum-upper v))
753 i (sum-lower u) (sum-upper u)))))))
755 (defun sumultin (x s) ; Multiplies x into a sum adjusting indices.
756 (cond ((or (atom s) (not (eq (caar s) '%sum))) (m* x s))
757 ((free x (sum-index s))
758 (list* (car s) (sumultin x (sum-arg s)) (cddr s)))
759 (t (let ((ind (gensumindex)))
760 (list* (car s)
761 (sumultin x (subst ind (sum-index s) (sum-arg s)))
763 (cdddr s))))))
765 ;; addition of sums
767 (defun sumpls (sum out)
768 (prog (l)
769 (if (null out) (return (cons sum nil)))
770 (setq out (setq l (cons nil out)))
771 a (if (null (cdr out)) (return (cons sum (cdr l))))
772 (and (not (atom (cadr out)))
773 (consp (caadr out))
774 (eq (caar (cadr out)) '%sum)
775 (alike1 (sum-arg (cadr out)) (sum-arg sum))
776 (alike1 (sum-index (cadr out)) (sum-index sum))
777 (cond ((onediff (sum-upper (cadr out)) (sum-lower sum))
778 (setq sum (list (car sum)
779 (sum-arg sum)
780 (sum-index sum)
781 (sum-lower (cadr out))
782 (sum-upper sum)))
783 (rplacd out (cddr out))
784 (go a))
785 ((onediff (sum-upper sum) (sum-lower (cadr out)))
786 (setq sum (list (car sum)
787 (sum-arg sum)
788 (sum-index sum)
789 (sum-lower sum)
790 (sum-upper (cadr out))))
791 (rplacd out (cddr out))
792 (go a))))
793 (setq out (cdr out))
794 (go a)))
796 (defun onediff (x y)
797 (equal 1 (m- y x)))
799 (defun freesum (e b a q)
800 (m* e q (m- (m+ a 1) b)))
802 ;; linear operator stuff
804 (defparameter *opers-list '(($linear . linearize1)))
805 (defparameter opers (list '$linear))
807 (defun oper-apply (e z)
808 (cond ((null opers-list)
809 (let ((w (get (caar e) 'operators)))
810 (if w (funcall w e 1 z) (simpargs e z))))
811 ((get (caar e) (caar opers-list))
812 (let ((opers-list (cdr opers-list))
813 (fun (cdar opers-list)))
814 (funcall fun e z)))
815 (t (let ((opers-list (cdr opers-list)))
816 (oper-apply e z)))))
818 (defun linearize1 (e z) ; z = t means args already simplified.
819 (linearize2 (cons (car e) (mapcar #'(lambda (q) (simpcheck q z)) (cdr e)))
820 nil))
822 (defun opident (op)
823 (cond ((eq op 'mplus) 0)
824 ((eq op 'mtimes) 1)))
826 (defun rem-const (e) ;removes constantp stuff
827 (do ((l (cdr e) (cdr l))
828 (a (list (opident (caar e))))
829 (b (list (opident (caar e)))))
830 ((null l)
831 (cons (simplifya (cons (list (caar e)) a) nil)
832 (simplifya (cons (list (caar e)) b) nil)))
833 (if ($constantp (car l))
834 (setq a (cons (car l) a))
835 (setq b (cons (car l) b)))))
837 (defun linearize2 (e times)
838 (cond ((linearconst e))
839 ((atom (cadr e)) (oper-apply e t))
840 ((eq (caar (cadr e)) 'mplus)
841 (addn (mapcar #'(lambda (q)
842 (linearize2 (list* (car e) q (cddr e)) nil))
843 (cdr (cadr e)))
845 ((and (eq (caar (cadr e)) 'mtimes) (null times))
846 (let ((z (if (and (cddr e)
847 (or (atom (caddr e))
848 ($subvarp (caddr e))))
849 (partition (cadr e) (caddr e) 1)
850 (rem-const (cadr e))))
851 (w))
852 (setq w (linearize2 (list* (car e)
853 (simplifya (cdr z) t)
854 (cddr e))
856 (linearize3 w e (car z))))
857 (t (oper-apply e t))))
859 (defun linearconst (e)
860 (if (or (mnump (cadr e))
861 (constant (cadr e))
862 (and (cddr e)
863 (or (atom (caddr e)) (member 'array (cdar (caddr e)) :test #'eq))
864 (free (cadr e) (caddr e))))
865 (if (or (zerop1 (cadr e))
866 (and (member (caar e) '(%sum %integrate) :test #'eq)
867 (= (length e) 5)
868 (or (eq (cadddr e) '$minf)
869 (member (car (cddddr e)) '($inf $infinity) :test #'eq))
870 (eq ($asksign (cadr e)) '$zero)))
872 (let ((w (oper-apply (list* (car e) 1 (cddr e)) t)))
873 (linearize3 w e (cadr e))))))
875 (defun linearize3 (w e x)
876 (let (w1)
877 (if (and (member w '($inf $minf $infinity) :test #'eq) (safe-zerop x))
878 (merror (intl:gettext "LINEARIZE3: undefined form 0*inf: ~M") e))
879 (setq w (mul2 (simplifya x t) w))
880 (cond ((or (atom w) (getl (caar w) '($outative $linear))) (setq w1 1))
881 ((eq (caar w) 'mtimes)
882 (setq w1 (cons '(mtimes) nil))
883 (do ((w2 (cdr w) (cdr w2)))
884 ((null w2) (setq w1 (nreverse w1)))
885 (if (or (atom (car w2))
886 (not (getl (caaar w2) '($outative $linear))))
887 (setq w1 (cons (car w2) w1)))))
888 (t (setq w1 w)))
889 (if (and (not (atom w1)) (or (among '$inf w1) (among '$minf w1)))
890 (infsimp w)
891 w)))
893 (setq opers (cons '$additive opers)
894 *opers-list (cons '($additive . additive) *opers-list))
896 (defun rem-opers-p (p)
897 (cond ((eq (caar opers-list) p)
898 (setq opers-list (cdr p)))
899 ((do ((l opers-list (cdr l)))
900 ((null l))
901 (if (eq (caar (cdr l)) p)
902 (return (rplacd l (cddr l))))))))
904 (defun additive (e z)
905 (cond ((get (caar e) '$outative) ; Really a linearize!
906 (setq opers-list (copy-list opers-list))
907 (rem-opers-p '$outative)
908 (linearize1 e z))
909 ((mplusp (cadr e))
910 (addn (mapcar #'(lambda (q)
911 (let ((opers-list *opers-list))
912 (oper-apply (list* (car e) q (cddr e)) z)))
913 (cdr (cadr e)))
915 (t (oper-apply e z))))
917 (setq opers (cons '$multiplicative opers)
918 *opers-list (cons '($multiplicative . multiplicative) *opers-list))
920 (defun multiplicative (e z)
921 (cond ((mtimesp (cadr e))
922 (muln (mapcar #'(lambda (q)
923 (let ((opers-list *opers-list))
924 (oper-apply (list* (car e) q (cddr e)) z)))
925 (cdr (cadr e)))
927 (t (oper-apply e z))))
929 (setq opers (cons '$outative opers)
930 *opers-list (cons '($outative . outative) *opers-list))
932 (defun outative (e z)
933 (setq e (cons (car e) (mapcar #'(lambda (q) (simpcheck q z)) (cdr e))))
934 (cond ((get (caar e) '$additive)
935 (setq opers-list (copy-list opers-list ))
936 (rem-opers-p '$additive)
937 (linearize1 e t))
938 ((linearconst e))
939 ((mtimesp (cadr e))
940 (let ((u (if (and (cddr e)
941 (or (atom (caddr e))
942 ($subvarp (caddr e))))
943 (partition (cadr e) (caddr e) 1)
944 (rem-const (cadr e))))
945 (w))
946 (setq w (oper-apply (list* (car e)
947 (simplifya (cdr u) t)
948 (cddr e))
950 (linearize3 w e (car u))))
951 (t (oper-apply e t))))
953 (defprop %sum t $outative)
954 (defprop %sum t opers)
955 (defprop %integrate t $outative)
956 (defprop %integrate t opers)
957 (defprop %limit t $outative)
958 (defprop %limit t opers)
960 (setq opers (cons '$evenfun opers)
961 *opers-list (cons '($evenfun . evenfun) *opers-list))
963 (setq opers (cons '$oddfun opers)
964 *opers-list (cons '($oddfun . oddfun) *opers-list))
966 (defmfun evenfun (e z)
967 (if (or (null (cdr e)) (cddr e))
968 (merror (intl:gettext "Function declared 'even' takes exactly one argument; found ~M") e))
969 (let ((arg (simpcheck (cadr e) z)))
970 (oper-apply (list (car e) (if (mminusp arg) (neg arg) arg)) t)))
972 (defmfun oddfun (e z)
973 (if (or (null (cdr e)) (cddr e))
974 (merror (intl:gettext "Function declared 'odd' takes exactly one argument; found ~M") e))
975 (let ((arg (simpcheck (cadr e) z)))
976 (if (mminusp arg) (neg (oper-apply (list (car e) (neg arg)) t))
977 (oper-apply (list (car e) arg) t))))
979 (setq opers (cons '$commutative opers)
980 *opers-list (cons '($commutative . commutative1) *opers-list))
982 (setq opers (cons '$symmetric opers)
983 *opers-list (cons '($symmetric . commutative1) *opers-list))
985 (defmfun commutative1 (e z)
986 (oper-apply (cons (car e)
987 (reverse
988 (sort (mapcar #'(lambda (q) (simpcheck q z))
989 (cdr e))
990 'great)))
993 (setq opers (cons '$antisymmetric opers)
994 *opers-list (cons '($antisymmetric . antisym) *opers-list))
996 (declare-top (special antisym-sign))
998 (defmfun antisym (e z)
999 (let ((l (mapcar #'(lambda (q) (simpcheck q z)) (cdr e))))
1000 (let (antisym-sign) (if (or (not (eq (caar e) 'mnctimes)) (freel l 'mnctimes))
1001 (setq l (bbsort1 l)))
1002 (cond ((equal l 0) 0)
1003 ((prog1 (null antisym-sign) (setq e (oper-apply (cons (car e) l) t)))
1005 (t (neg e))))))
1007 (defun bbsort1 (l)
1008 (prog (sl sl1)
1009 (if (or (null l) (null (cdr l))) (return l))
1010 (setq antisym-sign nil sl (list nil (car l)))
1011 loop (setq l (cdr l))
1012 (if (null l) (return (nreverse (cdr sl))))
1013 (setq sl1 sl)
1014 loop1(cond ((null (cdr sl1)) (rplacd sl1 (cons (car l) nil)))
1015 ((alike1 (car l) (cadr sl1)) (return 0))
1016 ((great (car l) (cadr sl1)) (rplacd sl1 (cons (car l) (cdr sl1))))
1017 (t (setq antisym-sign (not antisym-sign) sl1 (cdr sl1)) (go loop1)))
1018 (go loop)))
1020 (setq opers (cons '$nary opers)
1021 *opers-list (cons '($nary . nary1) *opers-list))
1023 (defun nary1 (e z)
1024 (oper-apply (nary2 e z) z))
1026 (defun nary2 (e z)
1028 ((l (cdr e) (cdr l)) (ans) (some-change))
1030 ((null l)
1031 (if some-change
1032 (nary2 (cons (car e) (nreverse ans)) z)
1033 (simpargs e z)))
1035 (setq
1036 ans (if (and (not (atom (car l))) (eq (caaar l) (caar e)))
1037 (progn
1038 (setq some-change t)
1039 (nconc (reverse (cdar l)) ans))
1040 (cons (car l) ans)))))
1042 (setq opers (cons '$lassociative opers)
1043 *opers-list (cons '($lassociative . lassociative) *opers-list))
1045 (defmfun lassociative (e z)
1046 (let*
1047 ((ans0 (oper-apply (cons (car e) (total-nary e)) z))
1048 (ans (cdr ans0)))
1049 (cond ((or (null (cddr ans)) (not (eq (caar ans0) (caar e)))) ans0)
1050 ((do ((newans (list (car e) (car ans) (cadr ans))
1051 (list (car e) newans (car ans)))
1052 (ans (cddr ans) (cdr ans)))
1053 ((null ans) newans))))))
1055 (setq opers (cons '$rassociative opers)
1056 *opers-list (cons '($rassociative . rassociative) *opers-list))
1058 (defmfun rassociative (e z)
1059 (let*
1060 ((ans0 (oper-apply (cons (car e) (total-nary e)) z))
1061 (ans (cdr ans0)))
1062 (cond ((or (null (cddr ans)) (not (eq (caar ans0) (caar e)))) ans0)
1063 (t (setq ans (nreverse ans))
1064 (do ((newans (list (car e) (cadr ans) (car ans))
1065 (list (car e) (car ans) newans))
1066 (ans (cddr ans) (cdr ans)))
1067 ((null ans) newans))))))
1069 (defmfun total-nary (e)
1070 (do ((l (cdr e) (cdr l)) (ans))
1071 ((null l) (nreverse ans))
1072 (setq ans (if (and (not (atom (car l))) (eq (caaar l) (caar e)))
1073 (nconc (reverse (total-nary (car l))) ans)
1074 (cons (car l) ans)))))
1076 (defparameter $opproperties (cons '(mlist simp) (reverse opers)))