Add tests to rtest_translator that involve if, is and maybe
[maxima.git] / src / asum.lisp
blob19244f45c3c96b9ef5a46bb2fc5ad8f1c011269c
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 (defmvar $factlim 100000) ; set to a big integer which will work (not -1)
64 (defvar 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 factorial (n)
89 (labels ((k (n m)
90 (if (<= n m)
92 (* (k n (* 2 m))
93 (k (- n m) (* 2 m))))))
94 (if (zerop n)
96 (k n 1))))
98 ;;; Factorial has mirror symmetry
100 (defprop mfactorial t commutes-with-conjugate)
102 (defun simpfact (x y z)
103 (oneargcheck x)
104 (setq y (simpcheck (cadr x) z))
105 (cond ((and (mnump y)
106 (eq ($sign y) '$neg)
107 (zerop1 (sub (simplify (list '(%truncate) y)) y)))
108 ;; Negative integer or a real representation of a negative integer.
109 (merror (intl:gettext "factorial: factorial of negative integer ~:M not defined.") y))
110 ((or (floatp y)
111 ($bfloatp y)
112 (and (not (integerp y))
113 (not (ratnump y))
114 (or (and (complex-number-p y 'float-or-rational-p)
115 (or $numer
116 (floatp ($realpart y))
117 (floatp ($imagpart y))))
118 (and (complex-number-p y 'bigfloat-or-number-p)
119 (or $numer
120 ($bfloatp ($realpart y))
121 ($bfloatp ($imagpart y))))))
122 (and (not makef) (ratnump y) (equal (caddr y) 2)))
123 ;; Numerically evaluate for real or complex argument in float or
124 ;; bigfloat precision using the Gamma function
125 (simplify (list '(%gamma) (add 1 y))))
126 ((eq y '$inf) '$inf)
127 ((and $factorial_expand
128 (mplusp y)
129 (integerp (cadr y)))
130 ;; factorial(n+m) and m integer. Expand.
131 (let ((m (cadr y))
132 (n (simplify (cons '(mplus) (cddr y)))))
133 (cond ((>= m 0)
134 (mul
135 (simplify (list '($pochhammer) (add n 1) m))
136 (simplify (list '(mfactorial) n))))
137 ((< m 0)
138 (setq m (- m))
139 (div
140 (mul (power -1 m) (simplify (list '(mfactorial) n)))
141 ;; We factor to get the ordering (n-1)*(n-2)*...
142 ($factor
143 (simplify (list '($pochhammer) (mul -1 n) m))))))))
144 ((or (not (fixnump y)) (not (> y -1)))
145 (eqtest (list '(mfactorial) y) x))
146 ((or (minusp $factlim) (not (> y $factlim)))
147 (factorial y))
148 (t (eqtest (list '(mfactorial) y) x))))
150 (defun makegamma1 (e)
151 (cond ((atom e) e)
152 ((eq (caar e) 'mfactorial)
153 (list '(%gamma) (list '(mplus) 1 (makegamma1 (cadr e)))))
155 ;; Begin code copied from orthopoly/orthopoly-init.lisp
156 ;; Do pochhammer(x,n) ==> gamma(x+n)/gamma(x).
158 ((eq (caar e) '$pochhammer)
159 (let ((x (makegamma1 (nth 1 e)))
160 (n (makegamma1 (nth 2 e))))
161 (div (take '(%gamma) (add x n)) (take '(%gamma) x))))
163 ;; (gamma(x/z+1)*z^floor(y))/gamma(x/z-floor(y)+1)
165 ((eq (caar e) '%genfact)
166 (let ((x (makegamma1 (nth 1 e)))
167 (y (makegamma1 (nth 2 e)))
168 (z (makegamma1 (nth 3 e))))
169 (setq y (take '($floor) y))
170 (div
171 (mul
172 (take '(%gamma) (add (div x z) 1))
173 (power z y))
174 (take '(%gamma) (sub (add (div x z) 1) y)))))
175 ;; End code copied from orthopoly/orthopoly-init.lisp
177 ;; Double factorial
179 ((eq (caar e) '%double_factorial)
180 (let ((x (makegamma1 (nth 1 e))))
181 (mul
182 (power
183 (div 2 '$%pi)
184 (mul
185 (div 1 4)
186 (sub 1 (simplify (list '(%cos) (mul '$%pi x))))))
187 (power 2 (div x 2))
188 (simplify (list '(%gamma) (add 1 (div x 2)))))))
190 ((eq (caar e) '%elliptic_kc)
191 ;; Complete elliptic integral of the first kind
192 (cond ((alike1 (cadr e) '((rat simp) 1 2))
193 ;; K(1/2) = gamma(1/4)/4/sqrt(pi)
194 '((mtimes simp) ((rat simp) 1 4)
195 ((mexpt simp) $%pi ((rat simp) -1 2))
196 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2)))
197 ((or (alike1 (cadr e)
198 '((mtimes simp) ((rat simp) 1 4)
199 ((mplus simp) 2
200 ((mexpt simp) 3 ((rat simp) 1 2)))))
201 (alike1 (cadr e)
202 '((mplus simp) ((rat simp) 1 2)
203 ((mtimes simp) ((rat simp) 1 4)
204 ((mexpt simp) 3 ((rat simp) 1 2)))))
205 (alike1 (cadr e)
206 ;; 1/(8-4*sqrt(3))
207 '((mexpt simp)
208 ((mplus simp) 8
209 ((mtimes simp) -4
210 ((mexpt simp) 3 ((rat simp) 1 2))))
211 -1)))
212 ;; K((2+sqrt(3)/4))
213 '((mtimes simp) ((rat simp) 1 4)
214 ((mexpt simp) 3 ((rat simp) 1 4))
215 ((mexpt simp) $%pi ((rat simp) -1 2))
216 ((%gamma simp) ((rat simp) 1 6))
217 ((%gamma simp) ((rat simp) 1 3))))
218 ((or (alike1 (cadr e)
219 ;; (2-sqrt(3))/4
220 '((mtimes simp) ((rat simp) 1 4)
221 ((mplus simp) 2
222 ((mtimes simp) -1
223 ((mexpt simp) 3 ((rat simp) 1 2))))))
224 (alike1 (cadr e)
225 ;; 1/2-sqrt(3)/4
226 '((mplus simp) ((rat simp) 1 2)
227 ((mtimes simp) ((rat simp) -1 4)
228 ((mexpt simp) 3 ((rat simp) 1 2)))))
229 (alike (cadr e)
230 ;; 1/(4*sqrt(3)+8)
231 '((mexpt simp)
232 ((mplus simp) 8
233 ((mtimes simp) 4
234 ((mexpt simp) 3 ((rat simp) 1 2))))
235 -1)))
236 ;; K((2-sqrt(3))/4)
237 '((mtimes simp) ((rat simp) 1 4)
238 ((mexpt simp) 3 ((rat simp) -1 4))
239 ((mexpt simp) $%pi ((rat simp) -1 2))
240 ((%gamma simp) ((rat simp) 1 6))
241 ((%gamma simp) ((rat simp) 1 3))))
242 ((or
243 ;; (3-2*sqrt(2))/(3+2*sqrt(2))
244 (alike1 (cadr e)
245 '((mtimes simp)
246 ((mplus simp) 3
247 ((mtimes simp) -2
248 ((mexpt simp) 2 ((rat simp) 1 2))))
249 ((mexpt simp)
250 ((mplus simp) 3
251 ((mtimes simp) 2
252 ((mexpt simp) 2 ((rat simp) 1 2)))) -1)))
253 ;; 17 - 12*sqrt(2)
254 (alike1 (cadr e)
255 '((mplus simp) 17
256 ((mtimes simp) -12
257 ((mexpt simp) 2 ((rat simp) 1 2)))))
258 ;; (2*SQRT(2) - 3)/(2*SQRT(2) + 3)
259 (alike1 (cadr e)
260 '((mtimes simp) -1
261 ((mplus simp) -3
262 ((mtimes simp) 2
263 ((mexpt simp) 2 ((rat simp) 1 2))))
264 ((mexpt simp)
265 ((mplus simp) 3
266 ((mtimes simp) 2
267 ((mexpt simp) 2 ((rat simp) 1 2))))
268 -1))))
269 '((mtimes simp) ((rat simp) 1 8)
270 ((mexpt simp) 2 ((rat simp) -1 2))
271 ((mplus simp) 1 ((mexpt simp) 2 ((rat simp) 1 2)))
272 ((mexpt simp) $%pi ((rat simp) -1 2))
273 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2)))
275 ;; Give up
276 e)))
277 ((eq (caar e) '%elliptic_ec)
278 ;; Complete elliptic integral of the second kind
279 (cond ((alike1 (cadr e) '((rat simp) 1 2))
280 ;; 2*E(1/2) - K(1/2) = 2*%pi^(3/2)*gamma(1/4)^(-2)
281 '((mplus simp)
282 ((mtimes simp) ((mexpt simp) $%pi ((rat simp) 3 2))
283 ((mexpt simp)
284 ((%gamma simp irreducible) ((rat simp) 1 4)) -2))
285 ((mtimes simp) ((rat simp) 1 8)
286 ((mexpt simp) $%pi ((rat simp) -1 2))
287 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2))))
288 ((or (alike1 (cadr e)
289 '((mtimes simp) ((rat simp) 1 4)
290 ((mplus simp) 2
291 ((mtimes simp) -1
292 ((mexpt simp) 3 ((rat simp) 1 2))))))
293 (alike1 (cadr e)
294 '((mplus simp) ((rat simp) 1 2)
295 ((mtimes simp) ((rat simp) -1 4)
296 ((mexpt simp) 3 ((rat simp) 1 2))))))
297 ;; E((2-sqrt(3))/4)
299 ;; %pi/4/sqrt(3) = K*(E-(sqrt(3)+1)/2/sqrt(3)*K)
300 '((mplus simp)
301 ((mtimes simp) ((mexpt simp) 3 ((rat simp) -1 4))
302 ((mexpt simp) $%pi ((rat simp) 3 2))
303 ((mexpt simp) ((%gamma simp) ((rat simp) 1 6)) -1)
304 ((mexpt simp) ((%gamma simp) ((rat simp) 1 3)) -1))
305 ((mtimes simp) ((rat simp) 1 8)
306 ((mexpt simp) 3 ((rat simp) -3 4))
307 ((mexpt simp) $%pi ((rat simp) -1 2))
308 ((%gamma simp) ((rat simp) 1 6))
309 ((%gamma simp) ((rat simp) 1 3)))
310 ((mtimes simp) ((rat simp) 1 8)
311 ((mexpt simp) 3 ((rat simp) -1 4))
312 ((mexpt simp) $%pi ((rat simp) -1 2))
313 ((%gamma simp) ((rat simp) 1 6))
314 ((%gamma simp) ((rat simp) 1 3)))))
315 ((or (alike1 (cadr e)
316 '((mtimes simp) ((rat simp) 1 4)
317 ((mplus simp) 2
318 ((mexpt simp) 3 ((rat simp) 1 2)))))
319 (alike1 (cadr e)
320 '((mplus simp) ((rat simp) 1 2)
321 ((mtimes simp) ((rat simp) 1 4)
322 ((mexpt simp) 3 ((rat simp) 1 2))))))
323 ;; E((2+sqrt(3))/4)
325 ;; %pi*sqrt(3)/4 = K1*(E1-(sqrt(3)-1)/2/sqrt(3)*K1)
326 '((mplus simp)
327 ((mtimes simp) 3 ((mexpt simp) 3 ((rat simp) -3 4))
328 ((mexpt simp) $%pi ((rat simp) 3 2))
329 ((mexpt simp) ((%gamma simp) ((rat simp) 1 6)) -1)
330 ((mexpt simp) ((%gamma simp) ((rat simp) 1 3)) -1))
331 ((mtimes simp) ((rat simp) 3 8)
332 ((mexpt simp) 3 ((rat simp) -3 4))
333 ((mexpt simp) $%pi ((rat simp) -1 2))
334 ((%gamma simp) ((rat simp) 1 6))
335 ((%gamma simp) ((rat simp) 1 3)))
336 ((mtimes simp) ((rat simp) -1 8)
337 ((mexpt simp) 3 ((rat simp) -1 4))
338 ((mexpt simp) $%pi ((rat simp) -1 2))
339 ((%gamma simp) ((rat simp) 1 6))
340 ((%gamma simp) ((rat simp) 1 3)))))
342 e)))
343 (t (recur-apply #'makegamma1 e))))
345 (defun simpgfact (x vestigial z)
346 (declare (ignore vestigial))
347 (arg-count-check 3 x)
348 (setq z (mapcar #'(lambda (q) (simpcheck q z)) (cdr x)))
349 (let ((a (car z)) (b (take '($floor) (cadr z))) (c (caddr z)))
350 (cond ((and (fixnump a)
351 (fixnump b)
352 (fixnump c))
353 (if (and (> a -1)
354 (> b -1)
355 (or (<= c a) (= b 0))
356 (<= b (/ a c)))
357 (gfact a b c)
358 (merror (intl:gettext "genfact: generalized factorial not defined for given arguments."))))
359 (t (eqtest (list '(%genfact) a
360 (if (and (not (atom b))
361 (eq (caar b) '$floor))
362 (cadr b)
365 x)))))
367 ;; sum begins
369 (defmvar $cauchysum nil
370 "When multiplying together sums with INF as their upper limit,
371 causes the Cauchy product to be used rather than the usual product.
372 In the Cauchy product the index of the inner summation is a function of
373 the index of the outer one rather than varying independently."
374 modified-commands '$sum)
376 (defmvar $gensumnum 0
377 "The numeric suffix used to generate the next variable of
378 summation. If it is set to FALSE then the index will consist only of
379 GENINDEX with no numeric suffix."
380 modified-commands '$sum
381 setting-predicate #'(lambda (x) (or (null x) (integerp x))))
383 (defmvar $genindex '$i
384 "The alphabetic prefix used to generate the next variable of
385 summation when necessary."
386 modified-commands '$sum
387 setting-predicate #'symbolp)
389 (defmvar $zerobern t)
390 (defmvar $simpsum nil)
391 (defmvar $simpproduct nil)
393 (defvar *infsumsimp t)
395 ;; These variables should be initialized where they belong.
397 (defmvar $cflength 1)
398 (defmvar $taylordepth 3)
399 (defmvar $maxtaydiff 4)
400 (defmvar $verbose nil)
401 (defvar *trunclist nil)
402 (defvar ps-bmt-disrep t)
403 (defvar silent-taylor-flag nil)
405 (defmacro sum-arg (sum)
406 `(cadr ,sum))
408 (defmacro sum-index (sum)
409 `(caddr ,sum))
411 (defmacro sum-lower (sum)
412 `(cadddr ,sum))
414 (defmacro sum-upper (sum)
415 `(cadr (cdddr ,sum)))
417 (defmspec $sum (l)
418 (arg-count-check 4 l)
419 (setq l (cdr l))
420 (dosum (car l) (cadr l) (meval (caddr l)) (meval (cadddr l)) t :evaluate-summand t))
423 (defmspec $lsum (l)
424 (arg-count-check 3 l)
425 (setq l (cdr l))
426 ;;(or (= (length l) 3) (wna-err '$lsum))
427 (let ((form (car l))
428 (ind (cadr l))
429 (lis (meval (caddr l)))
430 (ans 0))
431 (or (symbolp ind) (merror (intl:gettext "lsum: second argument must be a variable; found ~M") ind))
432 (cond (($listp lis)
433 (loop for v in (cdr lis)
434 with lind = (cons ind nil)
435 for w = (cons v nil)
437 (setq ans (add* ans (mbinding (lind w) (meval form)))))
438 ans)
439 (t `((%lsum) ,form ,ind ,lis)))))
441 (defun simpsum (x y z)
442 (let (($ratsimpexpons t))
443 (setq y (simplifya (sum-arg x) z)))
444 (simpsum1 y (sum-index x) (simplifya (sum-lower x) z)
445 (simplifya (sum-upper x) z)))
447 ; This function was SIMPSUM1 until the sum/product code was revised Nov 2005.
448 ; The revised code punts back to this function since this code knows
449 ; some simplifications not handled by the revised code. -- Robert Dodier
451 (defun simpsum1-save (exp i lo hi)
452 (cond ((not (symbolp i)) (merror (intl:gettext "sum: index must be a symbol; found ~M") i))
453 ((equal lo hi) (mbinding ((list i) (list hi)) (meval exp)))
454 ((and (atom exp)
455 (not (eq exp i))
456 (getl '%sum '($outative $linear)))
457 (freesum exp lo hi 1))
458 ((null $simpsum) (list (get '%sum 'msimpind) exp i lo hi))
459 ((and (or (eq lo '$minf)
460 (alike1 lo '((mtimes simp) -1 $inf)))
461 (equal hi '$inf))
462 (let ((pos-part (simpsum2 exp i 0 '$inf))
463 (neg-part (simpsum2 (maxima-substitute (m- i) i exp) i 1 '$inf)))
464 (cond
465 ((or (eq neg-part '$und)
466 (eq pos-part '$und))
467 '$und)
468 ((eq pos-part '$inf)
469 (if (eq neg-part '$minf) '$und '$inf))
470 ((eq pos-part '$minf)
471 (if (eq neg-part '$inf) '$und '$minf))
472 ((or (eq neg-part '$inf) (eq neg-part '$minf))
473 neg-part)
474 (t (m+ neg-part pos-part)))))
475 ((or (eq lo '$minf)
476 (alike1 lo '((mtimes simp) -1 '$inf)))
477 (simpsum2 (maxima-substitute (m- i) i exp) i (m- hi) '$inf))
478 (t (simpsum2 exp i lo hi))))
480 ;; DOSUM, MEVALSUMARG, DO%SUM -- general principles
482 ;; - evaluate the summand/productand
483 ;; - substitute a gensym for the index variable and make assertions (via assume) about the gensym index
484 ;; - return 0/1 for empty sum/product. sumhack/prodhack are ignored
485 ;; - distribute sum/product over mbags when listarith = true
487 (defun dosum (expr ind low hi sump &key (evaluate-summand t))
488 (setq low (ratdisrep low) hi (ratdisrep hi)) ;; UGH, GAG WITH ME A SPOON
489 (if (not (symbolp ind))
490 (merror (intl:gettext "~:M: index must be a symbol; found ~M") (if sump '$sum '$product) ind))
491 (unwind-protect
492 (prog (u *i lind l*i *hl)
493 (setq lind (cons ind nil))
494 (cond
495 ((not (fixnump (setq *hl (mfuncall '$floor (m- hi low)))))
496 (if evaluate-summand (setq expr (mevalsumarg expr ind low hi)))
497 (return (cons (if sump '(%sum) '(%product))
498 (list expr ind low hi))))
499 ((signp l *hl)
500 (return (if sump 0 1))))
501 (setq *i low l*i (list *i) u (if sump 0 1))
502 lo (setq u
503 (if sump
504 (add u (resimplify (let* ((foo (mbinding (lind l*i) (meval expr)))
505 (bar (subst-if-not-freeof *i ind foo)))
506 bar)))
507 (mul u (resimplify (let* ((foo (mbinding (lind l*i) (meval expr)))
508 (bar (subst-if-not-freeof *i ind foo)))
509 bar)))))
510 (when (zerop *hl) (return u))
511 (setq *hl (1- *hl))
512 (setq *i (car (rplaca l*i (m+ *i 1))))
513 (go lo))))
515 (defun subst-if-not-freeof (x y expr)
516 (if ($freeof y expr)
517 ;; suppressing substitution here avoids substituting for
518 ;; local variables recognize by freeof, e.g., formal argument of lambda.
519 expr
520 (let ($simp) (maxima-substitute x y expr))))
522 (defun mevalsumarg (expr ind low hi)
523 (if (let (($prederror nil))
524 (eq (mevalp `((mlessp) ,hi ,low)) t))
527 (let ((gensym-ind (gensym)))
528 (if (apparently-integer low)
529 (meval `(($declare) ,gensym-ind $integer)))
530 (assume (list '(mgeqp) gensym-ind low))
531 (if (not (eq hi '$inf))
532 (assume (list '(mgeqp) hi gensym-ind)))
533 (let ((msump t) (foo) (summand))
534 (setq summand
535 (if (and (not (atom expr)) (get (caar expr) 'mevalsumarg-macro))
536 (funcall (get (caar expr) 'mevalsumarg-macro) expr)
537 expr))
538 (let (($simp nil))
539 (setq summand ($substitute gensym-ind ind summand)))
540 (setq foo (mbinding ((list gensym-ind) (list gensym-ind))
541 (resimplify (meval summand))))
542 ;; At this point we do not switch off simplification to preserve
543 ;; the achieved simplification of the summand (DK 02/2010).
544 (let (($simp t))
545 (setq foo ($substitute ind gensym-ind foo)))
546 (if (not (eq hi '$inf))
547 (forget (list '(mgeqp) hi gensym-ind)))
548 (forget (list '(mgeqp) gensym-ind low))
549 (if (apparently-integer low)
550 (meval `(($remove) ,gensym-ind $integer)))
551 foo)))
553 (defun apparently-integer (x)
554 (or ($integerp x) ($featurep x '$integer)))
556 (defun do%sum (l op)
557 (if (not (= (length l) 4)) (wna-err op))
558 (let ((ind (cadr l)))
559 (if (mquotep ind) (setq ind (cadr ind)))
560 (if (not (symbolp ind))
561 (merror (intl:gettext "~:M: index must be a symbol; found ~M") op ind))
562 (let ((low (caddr l))
563 (hi (cadddr l)))
564 (list (mevalsumarg (car l) ind low hi)
565 ind (meval (caddr l)) (meval (cadddr l))))))
567 (defun simpsum1 (e k lo hi)
568 (with-new-context (context)
569 (let ((acc 0) (n) (sgn) ($prederror nil) (i (gensym)) (ex))
570 (setq lo ($ratdisrep lo))
571 (setq hi ($ratdisrep hi))
573 (setq n ($limit (add 1 (sub hi lo))))
574 (setq sgn ($sign n))
576 (if (not (eq t (csign lo))) (mfuncall '$assume `((mgeqp) ,i ,lo)))
577 (if (not (eq t (csign hi))) (mfuncall '$assume `((mgeqp) ,hi ,i)))
579 (setq ex (subst i k e))
580 (setq ex (subst i k ex))
582 (setq acc
583 (cond ((and (eq n '$inf) ($freeof i ex))
584 (setq sgn (csign ex))
585 (cond ((eq sgn '$pos) '$inf)
586 ((eq sgn '$neg) '$minf)
587 ((eq sgn '$zero) 0)
588 (t `((%sum simp) ,ex ,i ,lo ,hi))))
590 ((and (mbagp e) $listarith)
591 (simplifya
592 `((,(caar e)) ,@(mapcar #'(lambda (s) (mfuncall '$sum s k lo hi)) (margs e))) t))
594 ((or (eq sgn '$neg) (eq sgn '$zero) (eq sgn '$nz)) 0)
596 ((like ex 0) 0)
598 (($freeof i ex) (mult n ex))
600 ((and (integerp n) (eq sgn '$pos) $simpsum)
601 (dotimes (j n acc)
602 (setq acc (add acc (resimplify (subst (add j lo) i ex))))))
605 (setq ex (subst '%sum '$sum ex))
606 `((%sum simp) ,(subst k i ex) ,k ,lo ,hi))))
608 (setq acc (subst k i acc))
610 ;; If expression is still a summation,
611 ;; punt to previous simplification code.
613 (if (and $simpsum (op-equalp acc '$sum '%sum))
614 (let* ((args (cdr acc)) (e (first args)) (i (second args)) (i0 (third args)) (i1 (fourth args)))
615 (setq acc (simpsum1-save e i i0 i1))))
617 acc)))
619 (defun simpprod1 (e k lo hi)
620 (with-new-context (context)
621 (let ((acc 1) (n) (sgn) ($prederror nil) (i (gensym)) (ex) (ex-mag) (realp))
623 (setq lo ($ratdisrep lo))
624 (setq hi ($ratdisrep hi))
625 (setq n ($limit (add 1 (sub hi lo))))
626 (setq sgn ($sign n))
628 (if (not (eq t (csign lo))) (mfuncall '$assume `((mgeqp) ,i ,lo)))
629 (if (not (eq t (csign hi))) (mfuncall '$assume `((mgeqp) ,hi ,i)))
631 (setq ex (subst i k e))
632 (setq ex (subst i k ex))
634 (setq acc
635 (cond
636 ((like ex 1) 1)
638 ((and (eq n '$inf) ($freeof i ex))
639 (setq ex-mag (mfuncall '$cabs ex))
640 (setq realp (mfuncall '$imagpart ex))
641 (setq realp (mevalp `((mequal) 0 ,realp)))
643 (cond ((eq t (mevalp `((mlessp) ,ex-mag 1))) 0)
644 ((and (eq realp t) (eq t (mevalp `((mgreaterp) ,ex 1)))) '$inf)
645 ((eq t (mevalp `((mgreaterp) ,ex-mag 1))) '$infinity)
646 ((eq t (mevalp `((mequal) 1 ,ex-mag))) '$und)
647 (t `((%product) ,e ,k ,lo ,hi))))
649 ((or (eq sgn '$neg) (eq sgn '$zero) (eq sgn '$nz))
652 ((and (mbagp e) $listarith)
653 (simplifya
654 `((,(caar e)) ,@(mapcar #'(lambda (s) (mfuncall '$product s k lo hi)) (margs e))) t))
656 (($freeof i ex) (power ex n))
658 ((and (integerp n) (eq sgn '$pos) $simpproduct)
659 (dotimes (j n acc)
660 (setq acc (mult acc (resimplify (subst (add j lo) i ex))))))
663 (setq ex (subst '%product '$product ex))
664 `((%product simp) ,(subst k i ex) ,k ,lo ,hi))))
666 ;; Hmm, this is curious... don't call existing product simplifications
667 ;; if index range is infinite -- what's up with that??
669 (if (and $simpproduct (op-equalp acc '$product '%product) (not (like n '$inf)))
670 (let* ((args (cdr acc)) (e (first args)) (i (second args)) (i0 (third args)) (i1 (fourth args)))
671 (setq acc (simpprod1-save e i i0 i1))))
673 (setq acc (subst k i acc))
674 (setq acc (subst '%product '$product acc))
676 acc)))
678 ; This function was SIMPPROD1 until the sum/product code was revised Nov 2005.
679 ; The revised code punts back to this function since this code knows
680 ; some simplifications not handled by the revised code. -- Robert Dodier
682 (defun simpprod1-save (exp i lo hi)
683 (let (u)
684 (cond ((not (symbolp i)) (merror (intl:gettext "product: index must be a symbol; found ~M") i))
685 ((alike1 lo hi)
686 (let ((valist (list i)))
687 (mbinding (valist (list hi))
688 (meval exp))))
689 ((eq ($sign (setq u (m- hi lo))) '$neg)
690 (cond ((eq ($sign (add2 u 1)) '$zero) 1)
691 (t (merror (intl:gettext "product: lower bound ~M greater than upper bound ~M") lo hi))))
692 ((atom exp)
693 (cond ((null (eq exp i))
694 (power* exp (list '(mplus) hi 1 (list '(mtimes) -1 lo))))
695 ((let ((lot (asksign lo)))
696 (cond ((equal lot '$zero) 0)
697 ((eq lot '$positive)
698 (m// (list '(mfactorial) hi)
699 (list '(mfactorial) (list '(mplus) lo -1))))
700 ((m* (list '(mfactorial)
701 (list '(mabs) lo))
702 (cond ((member (asksign hi) '($zero $positive) :test #'eq)
704 (t (prog1
705 (m^ -1 (m+ hi lo 1))
706 (setq hi (list '(mabs) hi)))))
707 (list '(mfactorial) hi))))))))
708 ((list '(%product simp) exp i lo hi)))))
711 ;; multiplication of sums
713 (defun gensumindex ()
714 (intern (format nil "~S~D" $genindex (incf $gensumnum))))
716 (defun sumtimes (x y)
717 (cond ((null x) y)
718 ((null y) x)
719 ((or (safe-zerop x) (safe-zerop y)) 0)
720 ((or (atom x) (not (eq (caar x) '%sum))) (sumultin x y))
721 ((or (atom y) (not (eq (caar y) '%sum))) (sumultin y x))
722 (t (let (u v i j)
723 (if (great (sum-arg x) (sum-arg y)) (setq u y v x) (setq u x v y))
724 (setq i (let ((ind (gensumindex)))
725 (setq u (subst ind (sum-index u) u)) ind))
726 (setq j (let ((ind (gensumindex)))
727 (setq v (subst ind (sum-index v) v)) ind))
728 (if (and $cauchysum (eq (sum-upper u) '$inf)
729 (eq (sum-upper v) '$inf))
730 (list '(%sum)
731 (list '(%sum)
732 (sumtimes (maxima-substitute j i (sum-arg u))
733 (maxima-substitute (m- i j) j (sum-arg v)))
734 j (sum-lower u) (m- i (sum-lower v)))
735 i (m+ (sum-lower u) (sum-lower v)) '$inf)
736 (list '(%sum)
737 (list '(%sum) (sumtimes (sum-arg u) (sum-arg v))
738 j (sum-lower v) (sum-upper v))
739 i (sum-lower u) (sum-upper u)))))))
741 (defun sumultin (x s) ; Multiplies x into a sum adjusting indices.
742 (cond ((or (atom s) (not (eq (caar s) '%sum))) (m* x s))
743 ((free x (sum-index s))
744 (list* (car s) (sumultin x (sum-arg s)) (cddr s)))
745 (t (let ((ind (gensumindex)))
746 (list* (car s)
747 (sumultin x (subst ind (sum-index s) (sum-arg s)))
749 (cdddr s))))))
751 ;; addition of sums
753 (defun sumpls (sum out)
754 (prog (l)
755 (if (null out) (return (cons sum nil)))
756 (setq out (setq l (cons nil out)))
757 a (if (null (cdr out)) (return (cons sum (cdr l))))
758 (and (not (atom (cadr out)))
759 (consp (caadr out))
760 (eq (caar (cadr out)) '%sum)
761 (alike1 (sum-arg (cadr out)) (sum-arg sum))
762 (alike1 (sum-index (cadr out)) (sum-index sum))
763 (cond ((onediff (sum-upper (cadr out)) (sum-lower sum))
764 (setq sum (list (car sum)
765 (sum-arg sum)
766 (sum-index sum)
767 (sum-lower (cadr out))
768 (sum-upper sum)))
769 (rplacd out (cddr out))
770 (go a))
771 ((onediff (sum-upper sum) (sum-lower (cadr out)))
772 (setq sum (list (car sum)
773 (sum-arg sum)
774 (sum-index sum)
775 (sum-lower sum)
776 (sum-upper (cadr out))))
777 (rplacd out (cddr out))
778 (go a))))
779 (setq out (cdr out))
780 (go a)))
782 (defun onediff (x y)
783 (equal 1 (m- y x)))
785 (defun freesum (e b a q)
786 (m* e q (m- (m+ a 1) b)))
788 ;; linear operator stuff
790 (defparameter *opers-list '(($linear . linearize1)))
791 (defparameter opers (list '$linear))
793 (defun oper-apply (e z)
794 (cond ((null opers-list)
795 (let ((w (get (caar e) 'operators)))
796 (if w (funcall w e 1 z) (simpargs e z))))
797 ((get (caar e) (caar opers-list))
798 (let ((opers-list (cdr opers-list))
799 (fun (cdar opers-list)))
800 (funcall fun e z)))
801 (t (let ((opers-list (cdr opers-list)))
802 (oper-apply e z)))))
804 ;; Define an operator simplification, the same as antisymmetric, commutative, linear, etc.
805 ;; Here OP = operator name, FN = function of 1 argument to carry out operator-specific simplification.
806 ;; 1. push operator name onto OPERS
807 ;; 2. update $OPPROPERTIES
808 ;; 3. push operator name and glue code onto *OPERS-LIST
809 ;; 4. declare operator name as a feature, so declare(..., <op>) is recognized
811 (defmfun $define_opproperty (op fn)
812 (unless (symbolp op)
813 (merror "define_opproperty: first argument must be a symbol; found: ~M" op))
814 (unless (or (symbolp fn) (and (consp fn) (eq (caar fn) 'lambda)))
815 (merror "define_opproperty: second argument must be a symbol or lambda expression; found: ~M" fn))
816 (push op opers)
817 (setq $opproperties (cons '(mlist simp) (reverse opers)))
818 (let
819 ((fn-glue (coerce (if (symbolp fn)
820 `(lambda (e z)
821 (declare (ignorable z))
822 (if (or (fboundp ',fn) (mget ',fn 'mexpr))
823 (let ((e1 (let ($simp) (mfuncall ',fn e))))
824 (if ($mapatom e1) e1 (oper-apply e1 nil)))
825 (list '(,fn) (let ((*opers-list (cdr *opers-list))) (oper-apply e z)))))
826 `(lambda (e z)
827 (declare (ignore z))
828 (let ((e1 (let ($simp) (mfuncall ',fn e))))
829 (if ($mapatom e1) e1 (oper-apply e1 nil)))))
830 'function)))
831 (push `(,op . ,fn-glue) *opers-list))
832 (mfuncall '$declare op '$feature))
834 (defun linearize1 (e z) ; z = t means args already simplified.
835 (linearize2 (cons (car e) (mapcar #'(lambda (q) (simpcheck q z)) (cdr e)))
836 nil))
838 (defun opident (op)
839 (cond ((eq op 'mplus) 0)
840 ((eq op 'mtimes) 1)))
842 (defun rem-const (e) ;removes constantp stuff
843 (do ((l (cdr e) (cdr l))
844 (a (list (opident (caar e))))
845 (b (list (opident (caar e)))))
846 ((null l)
847 (cons (simplifya (cons (list (caar e)) a) nil)
848 (simplifya (cons (list (caar e)) b) nil)))
849 (if ($constantp (car l))
850 (setq a (cons (car l) a))
851 (setq b (cons (car l) b)))))
853 (defun linearize2 (e times)
854 (cond ((linearconst e))
855 ((atom (cadr e)) (oper-apply e t))
856 ((eq (caar (cadr e)) 'mplus)
857 (addn (mapcar #'(lambda (q)
858 (linearize2 (list* (car e) q (cddr e)) nil))
859 (cdr (cadr e)))
861 ((and (eq (caar (cadr e)) 'mtimes) (null times))
862 (let ((z (if (and (cddr e)
863 (or (atom (caddr e))
864 ($subvarp (caddr e))))
865 (partition (cadr e) (caddr e) 1)
866 (rem-const (cadr e))))
867 (w))
868 (setq w (linearize2 (list* (car e)
869 (simplifya (cdr z) t)
870 (cddr e))
872 (linearize3 w e (car z))))
873 (t (oper-apply e t))))
875 (defun linearconst (e)
876 (if (or (mnump (cadr e))
877 (constant (cadr e))
878 (and (cddr e)
879 (or (atom (caddr e)) (member 'array (cdar (caddr e)) :test #'eq))
880 (free (cadr e) (caddr e))))
881 (if (or (zerop1 (cadr e))
882 (and (member (caar e) '(%sum %integrate) :test #'eq)
883 (= (length e) 5)
884 (or (eq (cadddr e) '$minf)
885 (member (car (cddddr e)) '($inf $infinity) :test #'eq))
886 (eq ($asksign (cadr e)) '$zero)))
888 (let ((w (oper-apply (list* (car e) 1 (cddr e)) t)))
889 (linearize3 w e (cadr e))))))
891 (defun linearize3 (w e x)
892 (let (w1)
893 (if (and (member w '($inf $minf $infinity) :test #'eq) (safe-zerop x))
894 (merror (intl:gettext "LINEARIZE3: undefined form 0*inf: ~M") e))
895 (setq w (mul2 (simplifya x t) w))
896 (cond ((or (atom w) (getl (caar w) '($outative $linear))) (setq w1 1))
897 ((eq (caar w) 'mtimes)
898 (setq w1 (cons '(mtimes) nil))
899 (do ((w2 (cdr w) (cdr w2)))
900 ((null w2) (setq w1 (nreverse w1)))
901 (if (or (atom (car w2))
902 (not (getl (caaar w2) '($outative $linear))))
903 (setq w1 (cons (car w2) w1)))))
904 (t (setq w1 w)))
905 (if (and (not (atom w1)) (or (among '$inf w1) (among '$minf w1)))
906 (infsimp w)
907 w)))
909 (setq opers (cons '$additive opers)
910 *opers-list (cons '($additive . additive) *opers-list))
912 (defun rem-opers-p (p)
913 (cond ((eq (caar opers-list) p)
914 (setq opers-list (cdr p)))
915 ((do ((l opers-list (cdr l)))
916 ((null l))
917 (if (eq (caar (cdr l)) p)
918 (return (rplacd l (cddr l))))))))
920 (defun additive (e z)
921 (cond ((get (caar e) '$outative) ; Really a linearize!
922 (setq opers-list (copy-list opers-list))
923 (rem-opers-p '$outative)
924 (linearize1 e z))
925 ((mplusp (cadr e))
926 (addn (mapcar #'(lambda (q)
927 (let ((opers-list *opers-list))
928 (oper-apply (list* (car e) q (cddr e)) z)))
929 (cdr (cadr e)))
931 (t (oper-apply e z))))
933 (setq opers (cons '$multiplicative opers)
934 *opers-list (cons '($multiplicative . multiplicative) *opers-list))
936 (defun multiplicative (e z)
937 (cond ((mtimesp (cadr e))
938 (muln (mapcar #'(lambda (q)
939 (let ((opers-list *opers-list))
940 (oper-apply (list* (car e) q (cddr e)) z)))
941 (cdr (cadr e)))
943 (t (oper-apply e z))))
945 (setq opers (cons '$outative opers)
946 *opers-list (cons '($outative . outative) *opers-list))
948 (defun outative (e z)
949 (setq e (cons (car e) (mapcar #'(lambda (q) (simpcheck q z)) (cdr e))))
950 (cond ((get (caar e) '$additive)
951 (setq opers-list (copy-list opers-list ))
952 (rem-opers-p '$additive)
953 (linearize1 e t))
954 ((linearconst e))
955 ((mtimesp (cadr e))
956 (let ((u (if (and (cddr e)
957 (or (atom (caddr e))
958 ($subvarp (caddr e))))
959 (partition (cadr e) (caddr e) 1)
960 (rem-const (cadr e))))
961 (w))
962 (setq w (oper-apply (list* (car e)
963 (simplifya (cdr u) t)
964 (cddr e))
966 (linearize3 w e (car u))))
967 (t (oper-apply e t))))
969 (defprop %sum t $outative)
970 (defprop %sum t opers)
971 (defprop %integrate t $outative)
972 (defprop %integrate t opers)
973 (defprop %limit t $outative)
974 (defprop %limit t opers)
976 (setq opers (cons '$evenfun opers)
977 *opers-list (cons '($evenfun . evenfun) *opers-list))
979 (setq opers (cons '$oddfun opers)
980 *opers-list (cons '($oddfun . oddfun) *opers-list))
982 (defun evenfun (e z)
983 (if (or (null (cdr e)) (cddr e))
984 (merror (intl:gettext "Function declared 'even' takes exactly one argument; found ~M") e))
985 (let ((arg (simpcheck (cadr e) z)))
986 (oper-apply (list (car e) (if (mminusp arg) (neg arg) arg)) t)))
988 (defun oddfun (e z)
989 (if (or (null (cdr e)) (cddr e))
990 (merror (intl:gettext "Function declared 'odd' takes exactly one argument; found ~M") e))
991 (let ((arg (simpcheck (cadr e) z)))
992 (if (mminusp arg) (neg (oper-apply (list (car e) (neg arg)) t))
993 (oper-apply (list (car e) arg) t))))
995 (setq opers (cons '$commutative opers)
996 *opers-list (cons '($commutative . commutative1) *opers-list))
998 (setq opers (cons '$symmetric opers)
999 *opers-list (cons '($symmetric . commutative1) *opers-list))
1001 (defun commutative1 (e z)
1002 (oper-apply (cons (car e)
1003 (reverse
1004 (sort (mapcar #'(lambda (q) (simpcheck q z))
1005 (cdr e))
1006 'great)))
1009 (setq opers (cons '$antisymmetric opers)
1010 *opers-list (cons '($antisymmetric . antisym) *opers-list))
1012 (defun antisym (e z)
1013 (when (and $dotscrules (mnctimesp e))
1014 (let ($dotexptsimp)
1015 (setq e (simpnct e 1 nil))))
1016 (if ($atom e) e (antisym1 e z)))
1018 (defun antisym1 (e z)
1019 (let ((antisym-sign nil)
1020 (l (mapcar #'(lambda (q) (simpcheck q z)) (cdr e))))
1021 (when (or (not (eq (caar e) 'mnctimes)) (freel l 'mnctimes))
1022 (multiple-value-setq (l antisym-sign) (bbsort1 l)))
1023 (cond ((equal l 0) 0)
1024 ((prog1
1025 (null antisym-sign)
1026 (setq e (oper-apply (cons (car e) l) t)))
1028 (t (neg e)))))
1030 (defun bbsort1 (l)
1031 (prog (sl sl1 antisym-sign)
1032 (if (or (null l) (null (cdr l))) (return (values l antisym-sign))
1033 (setq sl (list nil (car l))))
1034 loop (setq l (cdr l))
1035 (if (null l) (return (values (nreverse (cdr sl)) antisym-sign)))
1036 (setq sl1 sl)
1037 loop1(cond ((null (cdr sl1)) (rplacd sl1 (cons (car l) nil)))
1038 ((alike1 (car l) (cadr sl1)) (return (values 0 nil)))
1039 ((great (car l) (cadr sl1)) (rplacd sl1 (cons (car l) (cdr sl1))))
1040 (t (setq antisym-sign (not antisym-sign) sl1 (cdr sl1)) (go loop1)))
1041 (go loop)))
1043 (setq opers (cons '$nary opers)
1044 *opers-list (cons '($nary . nary1) *opers-list))
1046 (defun nary1 (e z)
1047 (oper-apply (nary2 e z) z))
1049 (defun nary2 (e z)
1051 ((l (cdr e) (cdr l)) (ans) (some-change))
1053 ((null l)
1054 (if some-change
1055 (nary2 (cons (car e) (nreverse ans)) z)
1056 (simpargs e z)))
1058 (setq
1059 ans (if (and (not (atom (car l))) (eq (caaar l) (caar e)))
1060 (progn
1061 (setq some-change t)
1062 (nconc (reverse (cdar l)) ans))
1063 (cons (car l) ans)))))
1065 (setq opers (cons '$lassociative opers)
1066 *opers-list (cons '($lassociative . lassociative) *opers-list))
1068 (defun lassociative (e z)
1069 (let*
1070 ((ans0 (oper-apply (cons (car e) (total-nary e)) z))
1071 (ans (if (consp ans0) (cdr ans0))))
1072 (cond ((or (null (cddr ans)) (not (eq (caar ans0) (caar e)))) ans0)
1073 ((do ((newans (list (car e) (car ans) (cadr ans))
1074 (list (car e) newans (car ans)))
1075 (ans (cddr ans) (cdr ans)))
1076 ((null ans) newans))))))
1078 (setq opers (cons '$rassociative opers)
1079 *opers-list (cons '($rassociative . rassociative) *opers-list))
1081 (defun rassociative (e z)
1082 (let*
1083 ((ans0 (oper-apply (cons (car e) (total-nary e)) z))
1084 (ans (if (consp ans0) (cdr ans0))))
1085 (cond ((or (null (cddr ans)) (not (eq (caar ans0) (caar e)))) ans0)
1086 (t (setq ans (nreverse ans))
1087 (do ((newans (list (car e) (cadr ans) (car ans))
1088 (list (car e) (car ans) newans))
1089 (ans (cddr ans) (cdr ans)))
1090 ((null ans) newans))))))
1092 (defun total-nary (e)
1093 (do ((l (cdr e) (cdr l)) (ans))
1094 ((null l) (nreverse ans))
1095 (setq ans (if (and (not (atom (car l))) (eq (caaar l) (caar e)))
1096 (nconc (reverse (total-nary (car l))) ans)
1097 (cons (car l) ans)))))
1099 (defparameter $opproperties (cons '(mlist simp) (reverse opers)))