Rename tab to datatab in numeric/interpol.mac
[maxima.git] / src / asum.lisp
blob3bbb88aed2dbb9740821dcfeb0b0946f161bb01d
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 *a sum *i))
19 (defmvar $opproperties
20 (list* '(mlist simp)
21 ;; This list was obtained by using an existing version of
22 ;; maxima and printing out the value. It can also be
23 ;; obtained by examining the code below to see what is
24 ;; placed on the OPER variable.
25 '($linear $additive $multiplicative $outative $evenfun $oddfun
26 $commutative $symmetric $antisymmetric $nary $lassociative $rassociative))
27 "List of the special operator properties recognized by the Maxima simplifier."
28 ;; Don't reset this. (This was originally a defparameter which
29 ;; wouldn't get reset.)
30 no-reset
31 ;; We probably don't want the user to modify this except via
32 ;; define_opproperty.
33 :properties ((assign 'neverset)))
35 (loop for (x y) on '(%cot %tan %csc %sin %sec %cos %coth %tanh %csch %sinh %sech %cosh)
36 by #'cddr do (putprop x y 'recip) (putprop y x 'recip))
38 ;; polynomial predicates and other such things
40 (defun poly? (exp var)
41 (cond ((or (atom exp) (free exp var)))
42 ((member (caar exp) '(mtimes mplus) :test #'eq)
43 (do ((exp (cdr exp) (cdr exp)))
44 ((null exp) t)
45 (and (null (poly? (car exp) var)) (return nil))))
46 ((and (eq (caar exp) 'mexpt)
47 (integerp (caddr exp))
48 (> (caddr exp) 0))
49 (poly? (cadr exp) var))))
51 (defun smono (x var)
52 (smonogen x var t))
54 (defun smonop (x var)
55 (smonogen x var nil))
57 (defun smonogen (x var fl) ; fl indicates whether to return *a *n
58 (cond ((free x var) (and fl (setq *n 0 *a x)) t)
59 ((atom x) (and fl (setq *n (setq *a 1))) t)
60 ((and (listp (car x))
61 (eq (caar x) 'mtimes))
62 (do ((x (cdr x) (cdr x))
63 (a '(1)) (n '(0)))
64 ((null x)
65 (and fl (setq *n (addn n nil) *a (muln a nil))) t)
66 (let (*a *n)
67 (if (smonogen (car x) var fl)
68 (and fl (setq a (cons *a a) n (cons *n n)))
69 (return nil)))))
70 ((and (listp (car x))
71 (eq (caar x) 'mexpt))
72 (cond ((and (free (caddr x) var) (eq (cadr x) var))
73 (and fl (setq *n (caddr x) *a 1)) t)))))
75 ;; factorial stuff
77 (defun gfact (n %m i)
78 (cond ((minusp %m) (improper-arg-err %m '$genfact))
79 ((= %m 0) 1)
80 (t (prog (ans)
81 (setq ans n)
82 a (if (= %m 1) (return ans))
83 (setq n (m- n i) %m (1- %m) ans (m* ans n))
84 (go a)))))
86 ;; From Richard Fateman's paper, "Comments on Factorial Programs",
87 ;; http://www.cs.berkeley.edu/~fateman/papers/factorial.pdf
89 ;; k(n,m) = n*(n-m)*(n-2*m)*...
91 ;; (k n 1) is n!
93 ;; This is much faster (3-4 times) than the original factorial
94 ;; function.
96 (defun factorial (n)
97 (labels ((k (n m)
98 (if (<= n m)
100 (* (k n (* 2 m))
101 (k (- n m) (* 2 m))))))
102 (if (zerop n)
104 (k n 1))))
106 ;;; Factorial has mirror symmetry
108 (defprop mfactorial t commutes-with-conjugate)
110 (defun simpfact (x y z)
111 (oneargcheck x)
112 (setq y (simpcheck (cadr x) z))
113 (cond ((and (mnump y)
114 (eq ($sign y) '$neg)
115 (zerop1 (sub (simplify (list '(%truncate) y)) y)))
116 ;; Negative integer or a real representation of a negative integer.
117 (merror (intl:gettext "factorial: factorial of negative integer ~:M not defined.") y))
118 ((or (floatp y)
119 ($bfloatp y)
120 (and (not (integerp y))
121 (not (ratnump y))
122 (or (and (complex-number-p y 'float-or-rational-p)
123 (or $numer
124 (floatp ($realpart y))
125 (floatp ($imagpart y))))
126 (and (complex-number-p y 'bigfloat-or-number-p)
127 (or $numer
128 ($bfloatp ($realpart y))
129 ($bfloatp ($imagpart y))))))
130 (and (not makef) (ratnump y) (equal (caddr y) 2)))
131 ;; Numerically evaluate for real or complex argument in float or
132 ;; bigfloat precision using the Gamma function
133 (simplify (list '(%gamma) (add 1 y))))
134 ((eq y '$inf) '$inf)
135 ((and $factorial_expand
136 (mplusp y)
137 (integerp (cadr y)))
138 ;; factorial(n+m) and m integer. Expand.
139 (let ((m (cadr y))
140 (n (simplify (cons '(mplus) (cddr y)))))
141 (cond ((>= m 0)
142 (mul
143 (simplify (list '($pochhammer) (add n 1) m))
144 (simplify (list '(mfactorial) n))))
145 ((< m 0)
146 (setq m (- m))
147 (div
148 (mul (power -1 m) (simplify (list '(mfactorial) n)))
149 ;; We factor to get the ordering (n-1)*(n-2)*...
150 ($factor
151 (simplify (list '($pochhammer) (mul -1 n) m))))))))
152 ((or (not (fixnump y)) (not (> y -1)))
153 (eqtest (list '(mfactorial) y) x))
154 ((or (minusp $factlim) (not (> y $factlim)))
155 (factorial y))
156 (t (eqtest (list '(mfactorial) y) x))))
158 (defun makegamma1 (e)
159 (cond ((atom e) e)
160 ((eq (caar e) 'mfactorial)
161 (list '(%gamma) (list '(mplus) 1 (makegamma1 (cadr e)))))
163 ;; Begin code copied from orthopoly/orthopoly-init.lisp
164 ;; Do pochhammer(x,n) ==> gamma(x+n)/gamma(x),
165 ;; but not if x is a negative integer or zero.
167 ((eq (caar e) '$pochhammer)
168 (let ((x (makegamma1 (nth 1 e)))
169 (n (makegamma1 (nth 2 e))))
170 (if (and (integerp x) (<= x 0))
172 (div (take '(%gamma) (add x n)) (take '(%gamma) x)))))
174 ;; (gamma(x/z+1)*z^floor(y))/gamma(x/z-floor(y)+1)
176 ((eq (caar e) '%genfact)
177 (let ((x (makegamma1 (nth 1 e)))
178 (y (makegamma1 (nth 2 e)))
179 (z (makegamma1 (nth 3 e))))
180 (setq y (take '($floor) y))
181 (div
182 (mul
183 (take '(%gamma) (add (div x z) 1))
184 (power z y))
185 (take '(%gamma) (sub (add (div x z) 1) y)))))
186 ;; End code copied from orthopoly/orthopoly-init.lisp
188 ;; Double factorial
190 ((eq (caar e) '%double_factorial)
191 (let ((x (makegamma1 (nth 1 e))))
192 (mul
193 (power
194 (div 2 '$%pi)
195 (mul
196 (div 1 4)
197 (sub 1 (simplify (list '(%cos) (mul '$%pi x))))))
198 (power 2 (div x 2))
199 (simplify (list '(%gamma) (add 1 (div x 2)))))))
201 ((eq (caar e) '%elliptic_kc)
202 ;; Complete elliptic integral of the first kind
203 (cond ((alike1 (cadr e) '((rat simp) 1 2))
204 ;; K(1/2) = gamma(1/4)/4/sqrt(pi)
205 '((mtimes simp) ((rat simp) 1 4)
206 ((mexpt simp) $%pi ((rat simp) -1 2))
207 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2)))
208 ((or (alike1 (cadr e)
209 '((mtimes simp) ((rat simp) 1 4)
210 ((mplus simp) 2
211 ((mexpt simp) 3 ((rat simp) 1 2)))))
212 (alike1 (cadr e)
213 '((mplus simp) ((rat simp) 1 2)
214 ((mtimes simp) ((rat simp) 1 4)
215 ((mexpt simp) 3 ((rat simp) 1 2)))))
216 (alike1 (cadr e)
217 ;; 1/(8-4*sqrt(3))
218 '((mexpt simp)
219 ((mplus simp) 8
220 ((mtimes simp) -4
221 ((mexpt simp) 3 ((rat simp) 1 2))))
222 -1)))
223 ;; K((2+sqrt(3)/4))
224 '((mtimes simp) ((rat simp) 1 4)
225 ((mexpt simp) 3 ((rat simp) 1 4))
226 ((mexpt simp) $%pi ((rat simp) -1 2))
227 ((%gamma simp) ((rat simp) 1 6))
228 ((%gamma simp) ((rat simp) 1 3))))
229 ((or (alike1 (cadr e)
230 ;; (2-sqrt(3))/4
231 '((mtimes simp) ((rat simp) 1 4)
232 ((mplus simp) 2
233 ((mtimes simp) -1
234 ((mexpt simp) 3 ((rat simp) 1 2))))))
235 (alike1 (cadr e)
236 ;; 1/2-sqrt(3)/4
237 '((mplus simp) ((rat simp) 1 2)
238 ((mtimes simp) ((rat simp) -1 4)
239 ((mexpt simp) 3 ((rat simp) 1 2)))))
240 (alike (cadr e)
241 ;; 1/(4*sqrt(3)+8)
242 '((mexpt simp)
243 ((mplus simp) 8
244 ((mtimes simp) 4
245 ((mexpt simp) 3 ((rat simp) 1 2))))
246 -1)))
247 ;; K((2-sqrt(3))/4)
248 '((mtimes simp) ((rat simp) 1 4)
249 ((mexpt simp) 3 ((rat simp) -1 4))
250 ((mexpt simp) $%pi ((rat simp) -1 2))
251 ((%gamma simp) ((rat simp) 1 6))
252 ((%gamma simp) ((rat simp) 1 3))))
253 ((or
254 ;; (3-2*sqrt(2))/(3+2*sqrt(2))
255 (alike1 (cadr e)
256 '((mtimes simp)
257 ((mplus simp) 3
258 ((mtimes simp) -2
259 ((mexpt simp) 2 ((rat simp) 1 2))))
260 ((mexpt simp)
261 ((mplus simp) 3
262 ((mtimes simp) 2
263 ((mexpt simp) 2 ((rat simp) 1 2)))) -1)))
264 ;; 17 - 12*sqrt(2)
265 (alike1 (cadr e)
266 '((mplus simp) 17
267 ((mtimes simp) -12
268 ((mexpt simp) 2 ((rat simp) 1 2)))))
269 ;; (2*SQRT(2) - 3)/(2*SQRT(2) + 3)
270 (alike1 (cadr e)
271 '((mtimes simp) -1
272 ((mplus simp) -3
273 ((mtimes simp) 2
274 ((mexpt simp) 2 ((rat simp) 1 2))))
275 ((mexpt simp)
276 ((mplus simp) 3
277 ((mtimes simp) 2
278 ((mexpt simp) 2 ((rat simp) 1 2))))
279 -1))))
280 '((mtimes simp) ((rat simp) 1 8)
281 ((mexpt simp) 2 ((rat simp) -1 2))
282 ((mplus simp) 1 ((mexpt simp) 2 ((rat simp) 1 2)))
283 ((mexpt simp) $%pi ((rat simp) -1 2))
284 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2)))
286 ;; Give up
287 e)))
288 ((eq (caar e) '%elliptic_ec)
289 ;; Complete elliptic integral of the second kind
290 (cond ((alike1 (cadr e) '((rat simp) 1 2))
291 ;; 2*E(1/2) - K(1/2) = 2*%pi^(3/2)*gamma(1/4)^(-2)
292 '((mplus simp)
293 ((mtimes simp) ((mexpt simp) $%pi ((rat simp) 3 2))
294 ((mexpt simp)
295 ((%gamma simp irreducible) ((rat simp) 1 4)) -2))
296 ((mtimes simp) ((rat simp) 1 8)
297 ((mexpt simp) $%pi ((rat simp) -1 2))
298 ((mexpt simp) ((%gamma simp) ((rat simp) 1 4)) 2))))
299 ((or (alike1 (cadr e)
300 '((mtimes simp) ((rat simp) 1 4)
301 ((mplus simp) 2
302 ((mtimes simp) -1
303 ((mexpt simp) 3 ((rat simp) 1 2))))))
304 (alike1 (cadr e)
305 '((mplus simp) ((rat simp) 1 2)
306 ((mtimes simp) ((rat simp) -1 4)
307 ((mexpt simp) 3 ((rat simp) 1 2))))))
308 ;; E((2-sqrt(3))/4)
310 ;; %pi/4/sqrt(3) = K*(E-(sqrt(3)+1)/2/sqrt(3)*K)
311 '((mplus simp)
312 ((mtimes simp) ((mexpt simp) 3 ((rat simp) -1 4))
313 ((mexpt simp) $%pi ((rat simp) 3 2))
314 ((mexpt simp) ((%gamma simp) ((rat simp) 1 6)) -1)
315 ((mexpt simp) ((%gamma simp) ((rat simp) 1 3)) -1))
316 ((mtimes simp) ((rat simp) 1 8)
317 ((mexpt simp) 3 ((rat simp) -3 4))
318 ((mexpt simp) $%pi ((rat simp) -1 2))
319 ((%gamma simp) ((rat simp) 1 6))
320 ((%gamma simp) ((rat simp) 1 3)))
321 ((mtimes simp) ((rat simp) 1 8)
322 ((mexpt simp) 3 ((rat simp) -1 4))
323 ((mexpt simp) $%pi ((rat simp) -1 2))
324 ((%gamma simp) ((rat simp) 1 6))
325 ((%gamma simp) ((rat simp) 1 3)))))
326 ((or (alike1 (cadr e)
327 '((mtimes simp) ((rat simp) 1 4)
328 ((mplus simp) 2
329 ((mexpt simp) 3 ((rat simp) 1 2)))))
330 (alike1 (cadr e)
331 '((mplus simp) ((rat simp) 1 2)
332 ((mtimes simp) ((rat simp) 1 4)
333 ((mexpt simp) 3 ((rat simp) 1 2))))))
334 ;; E((2+sqrt(3))/4)
336 ;; %pi*sqrt(3)/4 = K1*(E1-(sqrt(3)-1)/2/sqrt(3)*K1)
337 '((mplus simp)
338 ((mtimes simp) 3 ((mexpt simp) 3 ((rat simp) -3 4))
339 ((mexpt simp) $%pi ((rat simp) 3 2))
340 ((mexpt simp) ((%gamma simp) ((rat simp) 1 6)) -1)
341 ((mexpt simp) ((%gamma simp) ((rat simp) 1 3)) -1))
342 ((mtimes simp) ((rat simp) 3 8)
343 ((mexpt simp) 3 ((rat simp) -3 4))
344 ((mexpt simp) $%pi ((rat simp) -1 2))
345 ((%gamma simp) ((rat simp) 1 6))
346 ((%gamma simp) ((rat simp) 1 3)))
347 ((mtimes simp) ((rat simp) -1 8)
348 ((mexpt simp) 3 ((rat simp) -1 4))
349 ((mexpt simp) $%pi ((rat simp) -1 2))
350 ((%gamma simp) ((rat simp) 1 6))
351 ((%gamma simp) ((rat simp) 1 3)))))
353 e)))
354 (t (recur-apply #'makegamma1 e))))
356 (def-simplifier genfact (x y z)
357 (let ((a x)
358 (b (take '($floor) y))
359 (c z))
360 (cond ((and (fixnump a)
361 (fixnump b)
362 (fixnump c))
363 (if (and (> a -1)
364 (> b -1)
365 (or (<= c a) (= b 0))
366 (<= b (/ a c)))
367 (gfact a b c)
368 (merror (intl:gettext "genfact: generalized factorial not defined for given arguments."))))
370 ;; Give up, we want to return a result with args that are
371 ;; different from the original. In particular, we want the
372 ;; floor of y if y was real number. Otherwise, we leave
373 ;; it.
374 (give-up a
375 (if (and (not (atom b))
376 (eq (caar b) '$floor))
377 (cadr b)
379 c)))))
381 ;; sum begins
383 ;; These variables should be initialized where they belong.
385 ;; FIXME: maxtaydiff and *trunclist don't appear to used anywhere.
386 (defmvar $maxtaydiff 4)
387 (defvar *trunclist nil)
389 (defmacro sum-arg (sum)
390 `(cadr ,sum))
392 (defmacro sum-index (sum)
393 `(caddr ,sum))
395 (defmacro sum-lower (sum)
396 `(cadddr ,sum))
398 (defmacro sum-upper (sum)
399 `(cadr (cdddr ,sum)))
401 (defmspec $sum (l)
402 (arg-count-check 4 l)
403 (setq l (cdr l))
404 (dosum (car l) (cadr l) (meval (caddr l)) (meval (cadddr l)) t :evaluate-summand t))
407 (defmspec $lsum (l)
408 (arg-count-check 3 l)
409 (setq l (cdr l))
410 ;;(or (= (length l) 3) (wna-err '$lsum))
411 (let ((form (car l))
412 (ind (cadr l))
413 (lis (meval (caddr l)))
414 (ans 0))
415 (or (symbolp ind) (merror (intl:gettext "lsum: second argument must be a variable; found ~M") ind))
416 (cond (($listp lis)
417 (loop for v in (cdr lis)
418 with lind = (cons ind nil)
419 for w = (cons v nil)
421 (setq ans (add* ans (mbinding (lind w) (meval form)))))
422 ans)
423 (t `((%lsum) ,form ,ind ,lis)))))
425 (defun simpsum (x y z)
426 (let (($ratsimpexpons t))
427 (setq y (simplifya (sum-arg x) z)))
428 (simpsum1 y (sum-index x) (simplifya (sum-lower x) z)
429 (simplifya (sum-upper x) z)))
431 ; This function was SIMPSUM1 until the sum/product code was revised Nov 2005.
432 ; The revised code punts back to this function since this code knows
433 ; some simplifications not handled by the revised code. -- Robert Dodier
435 (defun simpsum1-save (exp i lo hi)
436 (cond ((not (symbolp i)) (merror (intl:gettext "sum: index must be a symbol; found ~M") i))
437 ((equal lo hi) (mbinding ((list i) (list hi)) (meval exp)))
438 ((and (atom exp)
439 (not (eq exp i))
440 (getl '%sum '($outative $linear)))
441 (freesum exp lo hi 1))
442 ((null $simpsum) (list (get '%sum 'msimpind) exp i lo hi))
443 ((and (or (eq lo '$minf)
444 (alike1 lo '((mtimes simp) -1 $inf)))
445 (equal hi '$inf))
446 (let ((pos-part (simpsum2 exp i 0 '$inf))
447 (neg-part (simpsum2 (maxima-substitute (m- i) i exp) i 1 '$inf)))
448 (cond
449 ((or (eq neg-part '$und)
450 (eq pos-part '$und))
451 '$und)
452 ((eq pos-part '$inf)
453 (if (eq neg-part '$minf) '$und '$inf))
454 ((eq pos-part '$minf)
455 (if (eq neg-part '$inf) '$und '$minf))
456 ((or (eq neg-part '$inf) (eq neg-part '$minf))
457 neg-part)
458 (t (m+ neg-part pos-part)))))
459 ((or (eq lo '$minf)
460 (alike1 lo '((mtimes simp) -1 '$inf)))
461 (simpsum2 (maxima-substitute (m- i) i exp) i (m- hi) '$inf))
462 (t (simpsum2 exp i lo hi))))
464 ;; DOSUM, MEVALSUMARG, DO%SUM -- general principles
466 ;; - evaluate the summand/productand
467 ;; - substitute a gensym for the index variable and make assertions (via assume) about the gensym index
468 ;; - return 0/1 for empty sum/product. sumhack/prodhack are ignored
469 ;; - distribute sum/product over mbags when listarith = true
471 (defun dosum (expr ind low hi sump &key (evaluate-summand t))
472 (setq low (ratdisrep low) hi (ratdisrep hi)) ;; UGH, GAG WITH ME A SPOON
473 (if (not (symbolp ind))
474 (merror (intl:gettext "~:M: index must be a symbol; found ~M") (if sump '$sum '$product) ind))
475 (unwind-protect
476 (prog (u *i lind l*i *hl)
477 (setq lind (cons ind nil))
478 (cond
479 ((not (fixnump (setq *hl (mfuncall '$floor (m- hi low)))))
480 (if evaluate-summand (setq expr (mevalsumarg expr ind low hi)))
481 (return (cons (if sump '(%sum) '(%product))
482 (list expr ind low hi))))
483 ((signp l *hl)
484 (return (if sump 0 1))))
485 (setq *i low l*i (list *i) u (if sump 0 1))
486 lo (setq u
487 (if sump
488 (add u (resimplify (let* ((foo (mbinding (lind l*i) (meval expr)))
489 (bar (subst-if-not-freeof *i ind foo)))
490 bar)))
491 (mul u (resimplify (let* ((foo (mbinding (lind l*i) (meval expr)))
492 (bar (subst-if-not-freeof *i ind foo)))
493 bar)))))
494 (when (zerop *hl) (return u))
495 (setq *hl (1- *hl))
496 (setq *i (car (rplaca l*i (m+ *i 1))))
497 (go lo))))
499 (defun subst-if-not-freeof (x y expr)
500 (if ($freeof y expr)
501 ;; suppressing substitution here avoids substituting for
502 ;; local variables recognize by freeof, e.g., formal argument of lambda.
503 expr
504 (let ($simp) (maxima-substitute x y expr))))
506 (defun mevalsumarg (expr ind low hi)
507 (if (let (($prederror nil))
508 (eq (mevalp `((mlessp) ,hi ,low)) t))
511 (let ((gensym-ind (gensym)))
512 (if (apparently-integer low)
513 (meval `(($declare) ,gensym-ind $integer)))
514 (assume (list '(mgeqp) gensym-ind low))
515 (if (not (eq hi '$inf))
516 (assume (list '(mgeqp) hi gensym-ind)))
517 (let ((msump t) (foo) (summand))
518 (setq summand
519 (if (and (not (atom expr)) (get (caar expr) 'mevalsumarg-macro))
520 (funcall (get (caar expr) 'mevalsumarg-macro) expr)
521 expr))
522 (let (($simp nil))
523 (setq summand ($substitute gensym-ind ind summand)))
524 (setq foo (mbinding ((list gensym-ind) (list gensym-ind))
525 (resimplify (meval summand))))
526 ;; At this point we do not switch off simplification to preserve
527 ;; the achieved simplification of the summand (DK 02/2010).
528 (let (($simp t))
529 (setq foo ($substitute ind gensym-ind foo)))
530 (if (not (eq hi '$inf))
531 (forget (list '(mgeqp) hi gensym-ind)))
532 (forget (list '(mgeqp) gensym-ind low))
533 (if (apparently-integer low)
534 (meval `(($remove) ,gensym-ind $integer)))
535 foo)))
537 (defun apparently-integer (x)
538 (or ($integerp x) ($featurep x '$integer)))
540 (defun do%sum (l op)
541 (if (not (= (length l) 4)) (wna-err op))
542 (let ((ind (cadr l)))
543 (if (mquotep ind) (setq ind (cadr ind)))
544 (if (not (symbolp ind))
545 (merror (intl:gettext "~:M: index must be a symbol; found ~M") op ind))
546 (let ((low (caddr l))
547 (hi (cadddr l)))
548 (list (mevalsumarg (car l) ind low hi)
549 ind (meval (caddr l)) (meval (cadddr l))))))
551 (defun simpsum1 (e k lo hi)
552 (with-new-context (context)
553 (let ((acc 0) (n) (sgn) ($prederror nil) (i (gensym)) (ex))
554 (setq lo ($ratdisrep lo))
555 (setq hi ($ratdisrep hi))
557 (setq n ($limit (add 1 (sub hi lo))))
558 (setq sgn ($sign n))
560 (if (not (eq t (csign lo))) (mfuncall '$assume `((mgeqp) ,i ,lo)))
561 (if (not (eq t (csign hi))) (mfuncall '$assume `((mgeqp) ,hi ,i)))
563 (setq ex (subst i k e))
564 (setq ex (subst i k ex))
566 (setq acc
567 (cond ((and (eq n '$inf) ($freeof i ex))
568 (setq sgn (csign ex))
569 (cond ((eq sgn '$pos) '$inf)
570 ((eq sgn '$neg) '$minf)
571 ((eq sgn '$zero) 0)
572 (t `((%sum simp) ,ex ,i ,lo ,hi))))
574 ((and (mbagp e) $listarith)
575 (simplifya
576 `((,(caar e)) ,@(mapcar #'(lambda (s) (mfuncall '$sum s k lo hi)) (margs e))) t))
578 ((or (eq sgn '$neg) (eq sgn '$zero) (eq sgn '$nz)) 0)
580 ((like ex 0) 0)
582 (($freeof i ex) (mult n ex))
584 ((and (integerp n) (eq sgn '$pos) $simpsum)
585 (dotimes (j n acc)
586 (setq acc (add acc (resimplify (subst (add j lo) i ex))))))
589 (setq ex (subst '%sum '$sum ex))
590 `((%sum simp) ,(subst k i ex) ,k ,lo ,hi))))
592 (setq acc (subst k i acc))
594 ;; If expression is still a summation,
595 ;; punt to previous simplification code.
597 (if (and $simpsum (op-equalp acc '$sum '%sum))
598 (let* ((args (cdr acc)) (e (first args)) (i (second args)) (i0 (third args)) (i1 (fourth args)))
599 (setq acc (simpsum1-save e i i0 i1))))
601 acc)))
603 (defun simpprod1 (e k lo hi)
604 (with-new-context (context)
605 (let ((acc 1) (n) (sgn) ($prederror nil) (i (gensym)) (ex) (ex-mag) (realp))
607 (setq lo ($ratdisrep lo))
608 (setq hi ($ratdisrep hi))
609 (setq n ($limit (add 1 (sub hi lo))))
610 (setq sgn ($sign n))
612 (if (not (eq t (csign lo))) (mfuncall '$assume `((mgeqp) ,i ,lo)))
613 (if (not (eq t (csign hi))) (mfuncall '$assume `((mgeqp) ,hi ,i)))
615 (setq ex (subst i k e))
616 (setq ex (subst i k ex))
618 (setq acc
619 (cond
620 ((like ex 1) 1)
622 ((and (eq n '$inf) ($freeof i ex))
623 (setq ex-mag (mfuncall '$cabs ex))
624 (setq realp (mfuncall '$imagpart ex))
625 (setq realp (mevalp `((mequal) 0 ,realp)))
627 (cond ((eq t (mevalp `((mlessp) ,ex-mag 1))) 0)
628 ((and (eq realp t) (eq t (mevalp `((mgreaterp) ,ex 1)))) '$inf)
629 ((eq t (mevalp `((mgreaterp) ,ex-mag 1))) '$infinity)
630 ((eq t (mevalp `((mequal) 1 ,ex-mag))) '$und)
631 (t `((%product) ,e ,k ,lo ,hi))))
633 ((or (eq sgn '$neg) (eq sgn '$zero) (eq sgn '$nz))
636 ((and (mbagp e) $listarith)
637 (simplifya
638 `((,(caar e)) ,@(mapcar #'(lambda (s) (mfuncall '$product s k lo hi)) (margs e))) t))
640 (($freeof i ex) (power ex n))
642 ((and (integerp n) (eq sgn '$pos) $simpproduct)
643 (dotimes (j n acc)
644 (setq acc (mult acc (resimplify (subst (add j lo) i ex))))))
647 (setq ex (subst '%product '$product ex))
648 `((%product simp) ,(subst k i ex) ,k ,lo ,hi))))
650 ;; Hmm, this is curious... don't call existing product simplifications
651 ;; if index range is infinite -- what's up with that??
653 (if (and $simpproduct (op-equalp acc '$product '%product) (not (like n '$inf)))
654 (let* ((args (cdr acc)) (e (first args)) (i (second args)) (i0 (third args)) (i1 (fourth args)))
655 (setq acc (simpprod1-save e i i0 i1))))
657 (setq acc (subst k i acc))
658 (setq acc (subst '%product '$product acc))
660 acc)))
662 ; This function was SIMPPROD1 until the sum/product code was revised Nov 2005.
663 ; The revised code punts back to this function since this code knows
664 ; some simplifications not handled by the revised code. -- Robert Dodier
666 (defun simpprod1-save (exp i lo hi)
667 (let (u)
668 (cond ((not (symbolp i)) (merror (intl:gettext "product: index must be a symbol; found ~M") i))
669 ((alike1 lo hi)
670 (let ((valist (list i)))
671 (mbinding (valist (list hi))
672 (meval exp))))
673 ((eq ($sign (setq u (m- hi lo))) '$neg)
674 (cond ((eq ($sign (add2 u 1)) '$zero) 1)
675 (t (merror (intl:gettext "product: lower bound ~M greater than upper bound ~M") lo hi))))
676 ((atom exp)
677 (cond ((null (eq exp i))
678 (power* exp (list '(mplus) hi 1 (list '(mtimes) -1 lo))))
679 ((let ((lot (asksign lo)))
680 (cond ((equal lot '$zero) 0)
681 ((eq lot '$positive)
682 (m// (list '(mfactorial) hi)
683 (list '(mfactorial) (list '(mplus) lo -1))))
684 ((m* (list '(mfactorial)
685 (list '(mabs) lo))
686 (cond ((member (asksign hi) '($zero $positive) :test #'eq)
688 (t (prog1
689 (m^ -1 (m+ hi lo 1))
690 (setq hi (list '(mabs) hi)))))
691 (list '(mfactorial) hi))))))))
692 ((list '(%product simp) exp i lo hi)))))
695 ;; multiplication of sums
697 (defun gensumindex ()
698 (if $gensumnum
699 (intern (format nil "~S~D" $genindex (incf $gensumnum)))
700 (intern (format nil "~S" $genindex))))
702 (defun sumtimes (x y)
703 (cond ((null x) y)
704 ((null y) x)
705 ((or (safe-zerop x) (safe-zerop y)) 0)
706 ((or (atom x) (not (eq (caar x) '%sum))) (sumultin x y))
707 ((or (atom y) (not (eq (caar y) '%sum))) (sumultin y x))
708 (t (let (u v i j)
709 (if (great (sum-arg x) (sum-arg y)) (setq u y v x) (setq u x v y))
710 (setq i (let ((ind (gensumindex)))
711 (setq u (subst ind (sum-index u) u)) ind))
712 (setq j (let ((ind (gensumindex)))
713 (setq v (subst ind (sum-index v) v)) ind))
714 (if (and $cauchysum (eq (sum-upper u) '$inf)
715 (eq (sum-upper v) '$inf))
716 (list '(%sum)
717 (list '(%sum)
718 (sumtimes (maxima-substitute j i (sum-arg u))
719 (maxima-substitute (m- i j) j (sum-arg v)))
720 j (sum-lower u) (m- i (sum-lower v)))
721 i (m+ (sum-lower u) (sum-lower v)) '$inf)
722 (list '(%sum)
723 (list '(%sum) (sumtimes (sum-arg u) (sum-arg v))
724 j (sum-lower v) (sum-upper v))
725 i (sum-lower u) (sum-upper u)))))))
727 (defun sumultin (x s) ; Multiplies x into a sum adjusting indices.
728 (cond ((or (atom s) (not (eq (caar s) '%sum))) (m* x s))
729 ((free x (sum-index s))
730 (list* (car s) (sumultin x (sum-arg s)) (cddr s)))
731 (t (let ((ind (gensumindex)))
732 (list* (car s)
733 (sumultin x (subst ind (sum-index s) (sum-arg s)))
735 (cdddr s))))))
737 ;; addition of sums
739 (defun sumpls (sum out)
740 (prog (l)
741 (if (null out) (return (cons sum nil)))
742 (setq out (setq l (cons nil out)))
743 a (if (null (cdr out)) (return (cons sum (cdr l))))
744 (and (not (atom (cadr out)))
745 (consp (caadr out))
746 (eq (caar (cadr out)) '%sum)
747 (alike1 (sum-arg (cadr out)) (sum-arg sum))
748 (alike1 (sum-index (cadr out)) (sum-index sum))
749 (cond ((onediff (sum-upper (cadr out)) (sum-lower sum))
750 (setq sum (list (car sum)
751 (sum-arg sum)
752 (sum-index sum)
753 (sum-lower (cadr out))
754 (sum-upper sum)))
755 (rplacd out (cddr out))
756 (go a))
757 ((onediff (sum-upper sum) (sum-lower (cadr out)))
758 (setq sum (list (car sum)
759 (sum-arg sum)
760 (sum-index sum)
761 (sum-lower sum)
762 (sum-upper (cadr out))))
763 (rplacd out (cddr out))
764 (go a))))
765 (setq out (cdr out))
766 (go a)))
768 (defun onediff (x y)
769 (equal 1 (m- y x)))
771 (defun freesum (e b a q)
772 (m* e q (m- (m+ a 1) b)))
774 ;; linear operator stuff
776 (defun oper-apply (e z)
777 (cond ((null opers-list)
778 (let ((w (get (caar e) 'operators)))
779 (if w (funcall w e 1 z) (simpargs e z))))
780 ((get (caar e) (caar opers-list))
781 (let ((opers-list (cdr opers-list))
782 (fun (cdar opers-list)))
783 (funcall fun e z)))
784 (t (let ((opers-list (cdr opers-list)))
785 (oper-apply e z)))))
787 ;; Define an operator simplification, the same as antisymmetric, commutative, linear, etc.
788 ;; Here OP = operator name, FN = function of 1 argument to carry out operator-specific simplification.
789 ;; 1. push operator name onto OPERS
790 ;; 2. update $OPPROPERTIES
791 ;; 3. push operator name and glue code onto *OPERS-LIST
792 ;; 4. declare operator name as a feature, so declare(..., <op>) is recognized
794 (defmfun $define_opproperty (op fn)
795 (unless (symbolp op)
796 (merror "define_opproperty: first argument must be a symbol; found: ~M" op))
797 (unless (or (symbolp fn) (and (consp fn) (eq (caar fn) 'lambda)))
798 (merror "define_opproperty: second argument must be a symbol or lambda expression; found: ~M" fn))
799 (push op opers)
800 (setq $opproperties (cons '(mlist simp) (reverse opers)))
801 (let
802 ((fn-glue (coerce (if (symbolp fn)
803 `(lambda (e z)
804 (declare (ignorable z))
805 (if (or (fboundp ',fn) (mget ',fn 'mexpr))
806 (let ((e1 (let ($simp) (mfuncall ',fn e))))
807 (if ($mapatom e1) e1 (oper-apply e1 nil)))
808 (list '(,fn) (let ((*opers-list (cdr *opers-list))) (oper-apply e z)))))
809 `(lambda (e z)
810 (declare (ignore z))
811 (let ((e1 (let ($simp) (mfuncall ',fn e))))
812 (if ($mapatom e1) e1 (oper-apply e1 nil)))))
813 'function)))
814 (push `(,op . ,fn-glue) *opers-list))
815 (mfuncall '$declare op '$feature))
817 (defun linearize1 (e z) ; z = t means args already simplified.
818 (linearize2 (cons (car e) (mapcar #'(lambda (q) (simpcheck q z)) (cdr e)))
819 nil))
821 (defun opident (op)
822 (cond ((eq op 'mplus) 0)
823 ((eq op 'mtimes) 1)))
825 (defun rem-const (e) ;removes constantp stuff
826 (do ((l (cdr e) (cdr l))
827 (a (list (opident (caar e))))
828 (b (list (opident (caar e)))))
829 ((null l)
830 (cons (simplifya (cons (list (caar e)) a) nil)
831 (simplifya (cons (list (caar e)) b) nil)))
832 (if ($constantp (car l))
833 (setq a (cons (car l) a))
834 (setq b (cons (car l) b)))))
836 (defun linearize2 (e times)
837 (cond ((linearconst e))
838 ((atom (cadr e)) (oper-apply e t))
839 ((eq (caar (cadr e)) 'mplus)
840 (addn (mapcar #'(lambda (q)
841 (linearize2 (list* (car e) q (cddr e)) nil))
842 (cdr (cadr e)))
844 ((and (eq (caar (cadr e)) 'mtimes) (null times))
845 (let ((z (if (and (cddr e)
846 (or (atom (caddr e))
847 ($subvarp (caddr e))))
848 (partition (cadr e) (caddr e) 1)
849 (rem-const (cadr e))))
850 (w))
851 (setq w (linearize2 (list* (car e)
852 (simplifya (cdr z) t)
853 (cddr e))
855 (linearize3 w e (car z))))
856 (t (oper-apply e t))))
858 (defun linearconst (e)
859 (if (or (mnump (cadr e))
860 (constant (cadr e))
861 (and (cddr e)
862 (or (atom (caddr e)) (member 'array (cdar (caddr e)) :test #'eq))
863 (free (cadr e) (caddr e))))
864 (if (or (zerop1 (cadr e))
865 (and (member (caar e) '(%sum %integrate) :test #'eq)
866 (= (length e) 5)
867 (or (eq (cadddr e) '$minf)
868 (member (car (cddddr e)) '($inf $infinity) :test #'eq))
869 (eq ($asksign (cadr e)) '$zero)))
871 (let ((w (oper-apply (list* (car e) 1 (cddr e)) t)))
872 (linearize3 w e (cadr e))))))
874 (defun linearize3 (w e x)
875 (let (w1)
876 (if (and (member w '($inf $minf $infinity) :test #'eq) (safe-zerop x))
877 (merror (intl:gettext "LINEARIZE3: undefined form 0*inf: ~M") e))
878 (setq w (mul2 (simplifya x t) w))
879 (cond ((or (atom w) (getl (caar w) '($outative $linear))) (setq w1 1))
880 ((eq (caar w) 'mtimes)
881 (setq w1 (cons '(mtimes) nil))
882 (do ((w2 (cdr w) (cdr w2)))
883 ((null w2) (setq w1 (nreverse w1)))
884 (if (or (atom (car w2))
885 (not (getl (caaar w2) '($outative $linear))))
886 (setq w1 (cons (car w2) w1)))))
887 (t (setq w1 w)))
888 (if (and (not (atom w1)) (or (among '$inf w1) (among '$minf w1)))
889 (infsimp w)
890 w)))
892 (setq opers (cons '$additive opers)
893 *opers-list (cons '($additive . additive) *opers-list))
895 (defun rem-opers-p (p)
896 (cond ((eq (caar opers-list) p)
897 (setq opers-list (cdr p)))
898 ((do ((l opers-list (cdr l)))
899 ((null l))
900 (if (eq (caar (cdr l)) p)
901 (return (rplacd l (cddr l))))))))
903 (defun additive (e z)
904 (cond ((get (caar e) '$outative) ; Really a linearize!
905 (setq opers-list (copy-list opers-list))
906 (rem-opers-p '$outative)
907 (linearize1 e z))
908 ((mplusp (cadr e))
909 (addn (mapcar #'(lambda (q)
910 (let ((opers-list *opers-list))
911 (oper-apply (list* (car e) q (cddr e)) z)))
912 (cdr (cadr e)))
914 (t (oper-apply e z))))
916 (setq opers (cons '$multiplicative opers)
917 *opers-list (cons '($multiplicative . multiplicative) *opers-list))
919 (defun multiplicative (e z)
920 (cond ((mtimesp (cadr e))
921 (muln (mapcar #'(lambda (q)
922 (let ((opers-list *opers-list))
923 (oper-apply (list* (car e) q (cddr e)) z)))
924 (cdr (cadr e)))
926 (t (oper-apply e z))))
928 (setq opers (cons '$outative opers)
929 *opers-list (cons '($outative . outative) *opers-list))
931 (defun outative (e z)
932 (setq e (cons (car e) (mapcar #'(lambda (q) (simpcheck q z)) (cdr e))))
933 (cond ((get (caar e) '$additive)
934 (setq opers-list (copy-list opers-list ))
935 (rem-opers-p '$additive)
936 (linearize1 e t))
937 ((linearconst e))
938 ((mtimesp (cadr e))
939 (let ((u (if (and (cddr e)
940 (or (atom (caddr e))
941 ($subvarp (caddr e))))
942 (partition (cadr e) (caddr e) 1)
943 (rem-const (cadr e))))
944 (w))
945 (setq w (oper-apply (list* (car e)
946 (simplifya (cdr u) t)
947 (cddr e))
949 (linearize3 w e (car u))))
950 (t (oper-apply e t))))
952 (defprop %sum t $outative)
953 (defprop %sum t opers)
954 (defprop %integrate t $outative)
955 (defprop %integrate t opers)
956 (defprop %limit t $outative)
957 (defprop %limit t opers)
959 (setq opers (cons '$evenfun opers)
960 *opers-list (cons '($evenfun . evenfun) *opers-list))
962 (setq opers (cons '$oddfun opers)
963 *opers-list (cons '($oddfun . oddfun) *opers-list))
965 (defun evenfun (e z)
966 (if (or (null (cdr e)) (cddr e))
967 (merror (intl:gettext "Function declared 'even' takes exactly one argument; found ~M") e))
968 (let ((arg (simpcheck (cadr e) z)))
969 (oper-apply (list (car e) (if (mminusp arg) (neg arg) arg)) t)))
971 (defun oddfun (e z)
972 (if (or (null (cdr e)) (cddr e))
973 (merror (intl:gettext "Function declared 'odd' takes exactly one argument; found ~M") e))
974 (let ((arg (simpcheck (cadr e) z)))
975 (if (mminusp arg) (neg (oper-apply (list (car e) (neg arg)) t))
976 (oper-apply (list (car e) arg) t))))
978 (setq opers (cons '$commutative opers)
979 *opers-list (cons '($commutative . commutative1) *opers-list))
981 (setq opers (cons '$symmetric opers)
982 *opers-list (cons '($symmetric . commutative1) *opers-list))
984 (defun commutative1 (e z)
985 (oper-apply (cons (car e)
986 (reverse
987 (sort (mapcar #'(lambda (q) (simpcheck q z))
988 (cdr e))
989 'great)))
992 (setq opers (cons '$antisymmetric opers)
993 *opers-list (cons '($antisymmetric . antisym) *opers-list))
995 (defun antisym (e z)
996 (when (and $dotscrules (mnctimesp e))
997 (let ($dotexptsimp)
998 (setq e (simpnct e 1 nil))))
999 (if ($atom e) e (antisym1 e z)))
1001 (defun antisym1 (e z)
1002 (let ((antisym-sign nil)
1003 (l (mapcar #'(lambda (q) (simpcheck q z)) (cdr e))))
1004 (when (or (not (eq (caar e) 'mnctimes)) (freel l 'mnctimes))
1005 (multiple-value-setq (l antisym-sign) (bbsort1 l)))
1006 (cond ((equal l 0) 0)
1007 ((prog1
1008 (null antisym-sign)
1009 (setq e (oper-apply (cons (car e) l) t)))
1011 (t (neg e)))))
1013 (defun bbsort1 (l)
1014 (prog (sl sl1 antisym-sign)
1015 (if (or (null l) (null (cdr l))) (return (values l antisym-sign))
1016 (setq sl (list nil (car l))))
1017 loop (setq l (cdr l))
1018 (if (null l) (return (values (nreverse (cdr sl)) antisym-sign)))
1019 (setq sl1 sl)
1020 loop1(cond ((null (cdr sl1)) (rplacd sl1 (cons (car l) nil)))
1021 ((alike1 (car l) (cadr sl1)) (return (values 0 nil)))
1022 ((great (car l) (cadr sl1)) (rplacd sl1 (cons (car l) (cdr sl1))))
1023 (t (setq antisym-sign (not antisym-sign) sl1 (cdr sl1)) (go loop1)))
1024 (go loop)))
1026 (setq opers (cons '$nary opers)
1027 *opers-list (cons '($nary . nary1) *opers-list))
1029 (defun nary1 (e z)
1030 (oper-apply (nary2 e z) z))
1032 (defun nary2 (e z)
1034 ((l (cdr e) (cdr l)) (ans) (some-change))
1036 ((null l)
1037 (if some-change
1038 (nary2 (cons (car e) (nreverse ans)) z)
1039 (simpargs e z)))
1041 (setq
1042 ans (if (and (not (atom (car l))) (eq (caaar l) (caar e)))
1043 (progn
1044 (setq some-change t)
1045 (nconc (reverse (cdar l)) ans))
1046 (cons (car l) ans)))))
1048 (setq opers (cons '$lassociative opers)
1049 *opers-list (cons '($lassociative . lassociative) *opers-list))
1051 (defun lassociative (e z)
1052 (let*
1053 ((ans0 (oper-apply (cons (car e) (total-nary e)) z))
1054 (ans (if (consp ans0) (cdr ans0))))
1055 (cond ((or (null (cddr ans)) (not (eq (caar ans0) (caar e)))) ans0)
1056 ((do ((newans (list (car e) (car ans) (cadr ans))
1057 (list (car e) newans (car ans)))
1058 (ans (cddr ans) (cdr ans)))
1059 ((null ans) newans))))))
1061 (setq opers (cons '$rassociative opers)
1062 *opers-list (cons '($rassociative . rassociative) *opers-list))
1064 (defun rassociative (e z)
1065 (let*
1066 ((ans0 (oper-apply (cons (car e) (total-nary e)) z))
1067 (ans (if (consp ans0) (cdr ans0))))
1068 (cond ((or (null (cddr ans)) (not (eq (caar ans0) (caar e)))) ans0)
1069 (t (setq ans (nreverse ans))
1070 (do ((newans (list (car e) (cadr ans) (car ans))
1071 (list (car e) (car ans) newans))
1072 (ans (cddr ans) (cdr ans)))
1073 ((null ans) newans))))))
1075 (defun total-nary (e)
1076 (do ((l (cdr e) (cdr l)) (ans))
1077 ((null l) (nreverse ans))
1078 (setq ans (if (and (not (atom (car l))) (eq (caaar l) (caar e)))
1079 (nconc (reverse (total-nary (car l))) ans)
1080 (cons (car l) ans)))))