1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (macsyma-module sumcon
)
15 (defmfun $sumcontract
(e) ; e is assumed to be simplified
18 (do ((x (cdr e
) (cdr x
)) (sums) (notsums) (car-x))
19 ((null x
) (cond ((null sums
)
20 (subst0 (cons '(mplus)
23 (t (setq sums
(sumcontract1 sums
))
24 (addn (cons sums notsums
) t
))))
27 (setq notsums
(cons car-x notsums
)))
28 ((eq (caar car-x
) '%sum
)
29 (setq sums
(cons (cons ($sumcontract
(cadr car-x
))
32 (t (setq notsums
(cons car-x notsums
))))))
33 (t (recur-apply #'$sumcontract e
))))
35 (defmfun $intosum
(e) ; e is assumed to be simplified
38 ((eq (caar e
) 'mtimes
) ;puts outside product inside
39 (do ((x (cdr e
) (cdr x
)) (sum) (notsum))
40 ((null x
) (cond ((null sum
)
41 (subst0 (cons '(mtimes)
46 (if (free (cons nil notsum
) (caddr sum
))
48 (get-free-index (cons nil
(cons sum notsum
))))))
49 (setq sum
(subst new-index
(caddr sum
) sum
))
50 (rplaca (cdr sum
) (muln (cons (cadr sum
) notsum
) t
))
51 (rplacd (car sum
) nil
)
55 (setq notsum
(cons (car x
) notsum
)))
57 (setq sum
(if (null sum
)
59 (muln (list sum
(car x
)) t
))))
60 (t (setq notsum
(cons ($sumcontract
(car x
))
62 (t (recur-apply #'$intosum e
)))))
64 (defun sumcontract1 (sums)
65 (addn (sumcontract2 nil sums
) t
))
67 (defun sumcontract2 (result left
)
70 (let ((x (sumcombine1 (car left
) (cdr left
))))
71 (sumcontract2 (append (car x
) result
) (cdr x
)))))
73 (defun sumcombine1 (pattern llist
)
74 (do ((sum pattern
) (non-sums nil
)
75 (un-matched-sums nil
) (try-this-one)
76 (llist llist
(cdr llist
)))
77 ((null llist
) (cons (cons (simplify (cons '(%sum
) sum
))
80 (setq try-this-one
(car llist
))
81 (cond ((and (numberp (sub* (caddr sum
) (caddr try-this-one
)))
82 (numberp (sub* (cadddr sum
) (cadddr try-this-one
))))
83 (let ((x (sumcombine2 try-this-one sum
)))
85 non-sums
(cons (cdr x
) non-sums
))))
86 (t (setq un-matched-sums
(cons try-this-one un-matched-sums
))))))
88 (defun sumcombine2 (sum1 sum2
)
89 (let* ((e1 (car sum1
))
97 (newl (simplify `(($max
) ,l1
,l2
)))
98 (newh (simplify `(($min
) ,h1
,h2
)))
99 (newi (cond ((eq i1 i2
) i1
)
102 (t (get-free-index (list nil i1 i2 e1 e2 l1 l2 h1 h2
)))))
105 (setq e1
(subst newi i1 e1
))
106 (setq e2
(subst newi i2 e2
))
107 (setq new-sum
(list '(%sum
) (add2 e1 e2
) newi newl newh
))
112 (list newi newi newi newi
)
113 (list l1
(add2 newh
1) l2
(add2 newh
1))
114 (list (sub* newl
1) h1
(sub* newl
1) h2
)
117 (cons new-sum extracted
)))
119 (defun get-free-index (llist &optional i
)
120 (or (do ((try-list (cdr $niceindicespref
) (cdr try-list
)))
122 (if (or (free llist
(car try-list
))
123 (eq i
(car try-list
)))
124 (return (car try-list
))))
125 (do ((n 0 (1+ n
)) (try))
127 (setq try
(intern (format nil
"~a~d" (cadr $niceindicespref
) n
)))
128 (if (free llist try
) (return try
)))))
130 (defmfun $bashindices
(e) ; e is assumed to be simplified
133 (let (($genindex
'$j
)
134 (e (recur-apply #'$bashindices e
)))
136 ((member (caar e
) '(%sum %product
) :test
#'eq
)
137 (sumconsimp (subst (gensumindex) (caddr e
) e
)))
140 (defmfun $niceindices
(e)
143 (let ((e (recur-apply #'$niceindices e
)))
145 ((member (caar e
) '(%sum %product
) :test
#'eq
)
146 (sumconsimp (subst (get-free-index e
(caddr e
)) (caddr e
) e
)))
149 (defun sumconsimp (e)
150 (if (and (not (atom e
)) (member (caar e
) '(%sum %product
) :test
#'eq
))
151 (list* (car e
) (sumconsimp (cadr e
)) (cddr e
))