1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
))
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
)))
31 (and (null (poly?
(car exp
) var
)) (return nil
))))
32 ((and (eq (caar exp
) 'mexpt
)
33 (integerp (caddr exp
))
35 (poly?
(cadr exp
) var
))))
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
)
47 (eq (caar x
) 'mtimes
))
48 (do ((x (cdr x
) (cdr x
))
51 (and fl
(setq *n
(addn n nil
) *a
(muln a nil
))) t
)
53 (if (smonogen (car x
) var fl
)
54 (and fl
(setq a
(cons *a a
) n
(cons *n n
)))
58 (cond ((and (free (caddr x
) var
) (eq (cadr x
) var
))
59 (and fl
(setq *n
(caddr x
) *a
1)) t
)))))
63 (setq $factlim
100000 ; set to a big integer which will work (not -1)
66 (defmfun $genfact
(&rest l
)
70 (cond ((minusp %m
) (improper-arg-err %m
'$genfact
))
74 a
(if (= %m
1) (return ans
))
75 (setq n
(m- n i
) %m
(1- %m
) ans
(m* ans n
))
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)*...
85 ;; This is much faster (3-4 times) than the original factorial
92 (k (- n m
) (* 2 m
)))))
99 ;;; Factorial has mirror symmetry
101 (defprop mfactorial t commutes-with-conjugate
)
103 (defmfun simpfact
(x y z
)
105 (setq y
(simpcheck (cadr x
) z
))
106 (cond ((and (mnump y
)
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
))
113 (and (not (integerp y
))
115 (or (and (complex-number-p y
'float-or-rational-p
)
117 (floatp ($realpart y
))
118 (floatp ($imagpart y
))))
119 (and (complex-number-p y
'bigfloat-or-number-p
)
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
))))
128 ((and $factorial_expand
131 ;; factorial(n+m) and m integer. Expand.
133 (n (simplify (cons '(mplus) (cddr y
)))))
136 (simplify (list '($pochhammer
) (add n
1) m
))
137 (simplify (list '(mfactorial) n
))))
141 (mul (power -
1 m
) (simplify (list '(mfactorial) n
)))
142 ;; We factor to get the ordering (n-1)*(n-2)*...
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
)))
149 (t (eqtest (list '(mfactorial) y
) x
))))
151 (defun makegamma1 (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
))
173 (take '(%gamma
) (add (div x z
) 1))
175 (take '(%gamma
) (sub (add (div x z
) 1) y
)))))
176 ;; End code copied from orthopoly/orthopoly-init.lisp
180 ((eq (caar e
) '%factorial_double
)
181 (let ((x (makegamma1 (nth 1 e
))))
187 (sub 1 (simplify (list '(%cos
) (mul '$%pi x
))))))
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)
201 ((mexpt simp
) 3 ((rat simp
) 1 2)))))
203 '((mplus simp
) ((rat simp
) 1 2)
204 ((mtimes simp
) ((rat simp
) 1 4)
205 ((mexpt simp
) 3 ((rat simp
) 1 2)))))
211 ((mexpt simp
) 3 ((rat simp
) 1 2))))
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
)
221 '((mtimes simp
) ((rat simp
) 1 4)
224 ((mexpt simp
) 3 ((rat simp
) 1 2))))))
227 '((mplus simp
) ((rat simp
) 1 2)
228 ((mtimes simp
) ((rat simp
) -
1 4)
229 ((mexpt simp
) 3 ((rat simp
) 1 2)))))
235 ((mexpt simp
) 3 ((rat simp
) 1 2))))
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))))
244 ;; (3-2*sqrt(2))/(3+2*sqrt(2))
249 ((mexpt simp
) 2 ((rat simp
) 1 2))))
253 ((mexpt simp
) 2 ((rat simp
) 1 2)))) -
1)))
258 ((mexpt simp
) 2 ((rat simp
) 1 2)))))
259 ;; (2*SQRT(2) - 3)/(2*SQRT(2) + 3)
264 ((mexpt simp
) 2 ((rat simp
) 1 2))))
268 ((mexpt simp
) 2 ((rat simp
) 1 2))))
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)))
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)
283 ((mtimes simp
) ((mexpt simp
) $%pi
((rat simp
) 3 2))
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)
293 ((mexpt simp
) 3 ((rat simp
) 1 2))))))
295 '((mplus simp
) ((rat simp
) 1 2)
296 ((mtimes simp
) ((rat simp
) -
1 4)
297 ((mexpt simp
) 3 ((rat simp
) 1 2))))))
300 ;; %pi/4/sqrt(3) = K*(E-(sqrt(3)+1)/2/sqrt(3)*K)
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)
319 ((mexpt simp
) 3 ((rat simp
) 1 2)))))
321 '((mplus simp
) ((rat simp
) 1 2)
322 ((mtimes simp
) ((rat simp
) 1 4)
323 ((mexpt simp
) 3 ((rat simp
) 1 2))))))
326 ;; %pi*sqrt(3)/4 = K1*(E1-(sqrt(3)-1)/2/sqrt(3)*K1)
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)))))
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
)
356 (or (<= c a
) (= b
0))
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
))
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)
406 (defmacro sum-index
(sum)
409 (defmacro sum-lower
(sum)
412 (defmacro sum-upper
(sum)
413 `(cadr (cdddr ,sum
)))
418 (dosum (car l
) (cadr l
) (meval (caddr l
)) (meval (cadddr l
)) t
:evaluate-summand t
)
423 (or (= (length l
) 3) (wna-err '$lsum
))
426 (lis (meval (caddr l
)))
428 (or (symbolp ind
) (merror (intl:gettext
"lsum: second argument must be a variable; found ~M") ind
))
430 (loop for v in
(cdr lis
)
431 with lind
= (cons ind nil
)
434 (setq ans
(add* ans
(mbinding (lind w
) (meval form
)))))
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
)))
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
)))
459 (let ((pos-part (simpsum2 exp i
0 '$inf
))
460 (neg-part (simpsum2 (maxima-substitute (m- i
) i exp
) i
1 '$inf
)))
462 ((or (eq neg-part
'$und
)
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
))
471 (t (m+ neg-part pos-part
)))))
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
))
489 (prog (u *i lind l
*i
*hl
)
490 (setq lind
(cons ind nil
))
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
))))
497 (return (if sump
0 1))))
498 (setq *i low l
*i
(list *i
) u
(if sump
0 1))
501 (add u
(resimplify (let* ((foo (mbinding (lind l
*i
) (meval expr
)))
502 (bar (subst-if-not-freeof *i ind foo
)))
504 (mul u
(resimplify (let* ((foo (mbinding (lind l
*i
) (meval expr
)))
505 (bar (subst-if-not-freeof *i ind foo
)))
507 (when (zerop *hl
) (return u
))
509 (setq *i
(car (rplaca l
*i
(m+ *i
1))))
512 (defun subst-if-not-freeof (x y 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))
534 (if (and (not (atom expr
)) (get (caar expr
) 'mevalsumarg-macro
))
535 (funcall (get (caar expr
) 'mevalsumarg-macro
) expr
)
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).
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
)))
552 (defun apparently-integer (x)
553 (or ($integerp x
) ($featurep x
'$integer
)))
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
))
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
))))
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
))
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
)
589 (t `((%sum simp
) ,ex
,i
,lo
,hi
))))
591 ((and (mbagp e
) $listarith
)
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)
599 (($freeof i ex
) (mult n ex
))
601 ((and (integerp n
) (eq sgn
'$pos
) $simpsum
)
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
)
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
))))
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
))
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
)
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
)
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
)
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
)
698 (cond ((not (symbolp i
)) (merror (intl:gettext
"product: index must be a symbol; found ~M") i
))
700 (let ((valist (list i
)))
701 (mbinding (valist (list hi
))
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
))))
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)
712 (m// (list '(mfactorial) hi
)
713 (list '(mfactorial) (list '(mplus) lo -
1))))
714 ((m* (list '(mfactorial)
716 (cond ((member (asksign hi
) '($zero $positive
) :test
#'eq
)
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
)
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
))
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
))
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
)
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)))
761 (sumultin x
(subst ind
(sum-index s
) (sum-arg s
)))
767 (defun sumpls (sum out
)
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
)))
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
)
781 (sum-lower (cadr out
))
783 (rplacd out
(cddr out
))
785 ((onediff (sum-upper sum
) (sum-lower (cadr out
)))
786 (setq sum
(list (car sum
)
790 (sum-upper (cadr out
))))
791 (rplacd out
(cddr out
))
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
)))
815 (t (let ((opers-list (cdr opers-list
)))
818 (defun linearize1 (e z
) ; z = t means args already simplified.
819 (linearize2 (cons (car e
) (mapcar #'(lambda (q) (simpcheck q z
)) (cdr e
)))
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
)))))
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
))
845 ((and (eq (caar (cadr e
)) 'mtimes
) (null times
))
846 (let ((z (if (and (cddr e
)
848 ($subvarp
(caddr e
))))
849 (partition (cadr e
) (caddr e
) 1)
850 (rem-const (cadr e
))))
852 (setq w
(linearize2 (list* (car e
)
853 (simplifya (cdr z
) t
)
856 (linearize3 w e
(car z
))))
857 (t (oper-apply e t
))))
859 (defun linearconst (e)
860 (if (or (mnump (cadr 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
)
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
)
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
)))))
889 (if (and (not (atom w1
)) (or (among '$inf w1
) (among '$minf w1
)))
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
)))
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
)
910 (addn (mapcar #'(lambda (q)
911 (let ((opers-list *opers-list
))
912 (oper-apply (list* (car e
) q
(cddr e
)) z
)))
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
)))
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
)
940 (let ((u (if (and (cddr e
)
942 ($subvarp
(caddr e
))))
943 (partition (cadr e
) (caddr e
) 1)
944 (rem-const (cadr e
))))
946 (setq w
(oper-apply (list* (car e
)
947 (simplifya (cdr u
) t
)
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
)
988 (sort (mapcar #'(lambda (q) (simpcheck q z
))
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
)))
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
))))
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
)))
1020 (setq opers
(cons '$nary opers
)
1021 *opers-list
(cons '($nary . nary1
) *opers-list
))
1024 (oper-apply (nary2 e z
) z
))
1028 ((l (cdr e
) (cdr l
)) (ans) (some-change))
1032 (nary2 (cons (car e
) (nreverse ans
)) z
)
1036 ans
(if (and (not (atom (car l
))) (eq (caaar l
) (caar e
)))
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
)
1047 ((ans0 (oper-apply (cons (car e
) (total-nary e
)) z
))
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
)
1060 ((ans0 (oper-apply (cons (car e
) (total-nary e
)) z
))
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
)))