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 (defmvar $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
93 (k (- n m
) (* 2 m
))))))
98 ;;; Factorial has mirror symmetry
100 (defprop mfactorial t commutes-with-conjugate
)
102 (defun simpfact (x y z
)
104 (setq y
(simpcheck (cadr x
) z
))
105 (cond ((and (mnump y
)
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
))
112 (and (not (integerp y
))
114 (or (and (complex-number-p y
'float-or-rational-p
)
116 (floatp ($realpart y
))
117 (floatp ($imagpart y
))))
118 (and (complex-number-p y
'bigfloat-or-number-p
)
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
))))
127 ((and $factorial_expand
130 ;; factorial(n+m) and m integer. Expand.
132 (n (simplify (cons '(mplus) (cddr y
)))))
135 (simplify (list '($pochhammer
) (add n
1) m
))
136 (simplify (list '(mfactorial) n
))))
140 (mul (power -
1 m
) (simplify (list '(mfactorial) n
)))
141 ;; We factor to get the ordering (n-1)*(n-2)*...
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
)))
148 (t (eqtest (list '(mfactorial) y
) x
))))
150 (defun makegamma1 (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
))
172 (take '(%gamma
) (add (div x z
) 1))
174 (take '(%gamma
) (sub (add (div x z
) 1) y
)))))
175 ;; End code copied from orthopoly/orthopoly-init.lisp
179 ((eq (caar e
) '%double_factorial
)
180 (let ((x (makegamma1 (nth 1 e
))))
186 (sub 1 (simplify (list '(%cos
) (mul '$%pi x
))))))
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)
200 ((mexpt simp
) 3 ((rat simp
) 1 2)))))
202 '((mplus simp
) ((rat simp
) 1 2)
203 ((mtimes simp
) ((rat simp
) 1 4)
204 ((mexpt simp
) 3 ((rat simp
) 1 2)))))
210 ((mexpt simp
) 3 ((rat simp
) 1 2))))
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
)
220 '((mtimes simp
) ((rat simp
) 1 4)
223 ((mexpt simp
) 3 ((rat simp
) 1 2))))))
226 '((mplus simp
) ((rat simp
) 1 2)
227 ((mtimes simp
) ((rat simp
) -
1 4)
228 ((mexpt simp
) 3 ((rat simp
) 1 2)))))
234 ((mexpt simp
) 3 ((rat simp
) 1 2))))
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))))
243 ;; (3-2*sqrt(2))/(3+2*sqrt(2))
248 ((mexpt simp
) 2 ((rat simp
) 1 2))))
252 ((mexpt simp
) 2 ((rat simp
) 1 2)))) -
1)))
257 ((mexpt simp
) 2 ((rat simp
) 1 2)))))
258 ;; (2*SQRT(2) - 3)/(2*SQRT(2) + 3)
263 ((mexpt simp
) 2 ((rat simp
) 1 2))))
267 ((mexpt simp
) 2 ((rat simp
) 1 2))))
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)))
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)
282 ((mtimes simp
) ((mexpt simp
) $%pi
((rat simp
) 3 2))
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)
292 ((mexpt simp
) 3 ((rat simp
) 1 2))))))
294 '((mplus simp
) ((rat simp
) 1 2)
295 ((mtimes simp
) ((rat simp
) -
1 4)
296 ((mexpt simp
) 3 ((rat simp
) 1 2))))))
299 ;; %pi/4/sqrt(3) = K*(E-(sqrt(3)+1)/2/sqrt(3)*K)
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)
318 ((mexpt simp
) 3 ((rat simp
) 1 2)))))
320 '((mplus simp
) ((rat simp
) 1 2)
321 ((mtimes simp
) ((rat simp
) 1 4)
322 ((mexpt simp
) 3 ((rat simp
) 1 2))))))
325 ;; %pi*sqrt(3)/4 = K1*(E1-(sqrt(3)-1)/2/sqrt(3)*K1)
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)))))
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
)
355 (or (<= c a
) (= b
0))
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
))
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)
408 (defmacro sum-index
(sum)
411 (defmacro sum-lower
(sum)
414 (defmacro sum-upper
(sum)
415 `(cadr (cdddr ,sum
)))
418 (arg-count-check 4 l
)
420 (dosum (car l
) (cadr l
) (meval (caddr l
)) (meval (cadddr l
)) t
:evaluate-summand t
))
424 (arg-count-check 3 l
)
426 ;;(or (= (length l) 3) (wna-err '$lsum))
429 (lis (meval (caddr l
)))
431 (or (symbolp ind
) (merror (intl:gettext
"lsum: second argument must be a variable; found ~M") ind
))
433 (loop for v in
(cdr lis
)
434 with lind
= (cons ind nil
)
437 (setq ans
(add* ans
(mbinding (lind w
) (meval form
)))))
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
)))
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
)))
462 (let ((pos-part (simpsum2 exp i
0 '$inf
))
463 (neg-part (simpsum2 (maxima-substitute (m- i
) i exp
) i
1 '$inf
)))
465 ((or (eq neg-part
'$und
)
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
))
474 (t (m+ neg-part pos-part
)))))
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
))
492 (prog (u *i lind l
*i
*hl
)
493 (setq lind
(cons ind nil
))
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
))))
500 (return (if sump
0 1))))
501 (setq *i low l
*i
(list *i
) u
(if sump
0 1))
504 (add u
(resimplify (let* ((foo (mbinding (lind l
*i
) (meval expr
)))
505 (bar (subst-if-not-freeof *i ind foo
)))
507 (mul u
(resimplify (let* ((foo (mbinding (lind l
*i
) (meval expr
)))
508 (bar (subst-if-not-freeof *i ind foo
)))
510 (when (zerop *hl
) (return u
))
512 (setq *i
(car (rplaca l
*i
(m+ *i
1))))
515 (defun subst-if-not-freeof (x y expr
)
517 ;; suppressing substitution here avoids substituting for
518 ;; local variables recognize by freeof, e.g., formal argument of lambda.
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))
535 (if (and (not (atom expr
)) (get (caar expr
) 'mevalsumarg-macro
))
536 (funcall (get (caar expr
) 'mevalsumarg-macro
) expr
)
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).
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
)))
553 (defun apparently-integer (x)
554 (or ($integerp x
) ($featurep x
'$integer
)))
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
))
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
))))
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
))
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
)
588 (t `((%sum simp
) ,ex
,i
,lo
,hi
))))
590 ((and (mbagp e
) $listarith
)
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)
598 (($freeof i ex
) (mult n ex
))
600 ((and (integerp n
) (eq sgn
'$pos
) $simpsum
)
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
))))
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
))))
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
))
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
)
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
)
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
))
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
)
684 (cond ((not (symbolp i
)) (merror (intl:gettext
"product: index must be a symbol; found ~M") i
))
686 (let ((valist (list i
)))
687 (mbinding (valist (list hi
))
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
))))
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)
698 (m// (list '(mfactorial) hi
)
699 (list '(mfactorial) (list '(mplus) lo -
1))))
700 ((m* (list '(mfactorial)
702 (cond ((member (asksign hi
) '($zero $positive
) :test
#'eq
)
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
)
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
))
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
))
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
)
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)))
747 (sumultin x
(subst ind
(sum-index s
) (sum-arg s
)))
753 (defun sumpls (sum out
)
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
)))
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
)
767 (sum-lower (cadr out
))
769 (rplacd out
(cddr out
))
771 ((onediff (sum-upper sum
) (sum-lower (cadr out
)))
772 (setq sum
(list (car sum
)
776 (sum-upper (cadr out
))))
777 (rplacd out
(cddr out
))
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
)))
801 (t (let ((opers-list (cdr opers-list
)))
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
)
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
))
817 (setq $opproperties
(cons '(mlist simp
) (reverse opers
)))
819 ((fn-glue (coerce (if (symbolp fn
)
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
)))))
828 (let ((e1 (let ($simp
) (mfuncall ',fn e
))))
829 (if ($mapatom e1
) e1
(oper-apply e1 nil
)))))
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
)))
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
)))))
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
))
861 ((and (eq (caar (cadr e
)) 'mtimes
) (null times
))
862 (let ((z (if (and (cddr e
)
864 ($subvarp
(caddr e
))))
865 (partition (cadr e
) (caddr e
) 1)
866 (rem-const (cadr e
))))
868 (setq w
(linearize2 (list* (car e
)
869 (simplifya (cdr z
) t
)
872 (linearize3 w e
(car z
))))
873 (t (oper-apply e t
))))
875 (defun linearconst (e)
876 (if (or (mnump (cadr 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
)
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
)
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
)))))
905 (if (and (not (atom w1
)) (or (among '$inf w1
) (among '$minf w1
)))
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
)))
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
)
926 (addn (mapcar #'(lambda (q)
927 (let ((opers-list *opers-list
))
928 (oper-apply (list* (car e
) q
(cddr e
)) z
)))
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
)))
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
)
956 (let ((u (if (and (cddr e
)
958 ($subvarp
(caddr e
))))
959 (partition (cadr e
) (caddr e
) 1)
960 (rem-const (cadr e
))))
962 (setq w
(oper-apply (list* (car e
)
963 (simplifya (cdr u
) t
)
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
))
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
)))
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
)
1004 (sort (mapcar #'(lambda (q) (simpcheck q z
))
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
))
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)
1026 (setq e
(oper-apply (cons (car e
) l
) t
)))
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
)))
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
)))
1043 (setq opers
(cons '$nary opers
)
1044 *opers-list
(cons '($nary . nary1
) *opers-list
))
1047 (oper-apply (nary2 e z
) z
))
1051 ((l (cdr e
) (cdr l
)) (ans) (some-change))
1055 (nary2 (cons (car e
) (nreverse ans
)) z
)
1059 ans
(if (and (not (atom (car l
))) (eq (caaar l
) (caar e
)))
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
)
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
)
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
)))