Rename lapack.texi to lapack.texi.m4
[maxima.git] / src / sumcon.lisp
blob5366e7b467cbeaaed563c106187b17f02e54951f
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 sumcon)
15 (defmfun $sumcontract (e) ; e is assumed to be simplified
16 (cond ((atom e) e)
17 ((eq (caar e) 'mplus)
18 (do ((x (cdr e) (cdr x)) (sums) (notsums) (car-x))
19 ((null x) (cond ((null sums)
20 (subst0 (cons '(mplus)
21 (nreverse notsums))
22 e))
23 (t (setq sums (sumcontract1 sums))
24 (addn (cons sums notsums) t))))
25 (setq car-x (car x))
26 (cond ((atom car-x)
27 (setq notsums (cons car-x notsums)))
28 ((eq (caar car-x) '%sum)
29 (setq sums (cons (cons ($sumcontract (cadr car-x))
30 (cddr car-x))
31 sums)))
32 (t (setq notsums (cons car-x notsums))))))
33 (t (recur-apply #'$sumcontract e))))
35 (defmfun $intosum (e) ; e is assumed to be simplified
36 (let (($sumexpand t))
37 (cond ((atom e) e)
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)
42 (nreverse notsum))
43 e))
44 (t (simpsum
45 (let ((new-index
46 (if (free (cons nil notsum) (caddr sum))
47 (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)
52 sum)
53 1 t))))
54 (cond ((atom (car x))
55 (setq notsum (cons (car x) notsum)))
56 ((eq (caaar x) '%sum)
57 (setq sum (if (null sum)
58 (copy-tree (car x))
59 (muln (list sum (car x)) t))))
60 (t (setq notsum (cons ($sumcontract (car x))
61 notsum))))))
62 (t (recur-apply #'$intosum e)))))
64 (defun sumcontract1 (sums)
65 (addn (sumcontract2 nil sums) t))
67 (defun sumcontract2 (result left)
68 (if (null left)
69 result
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))
78 non-sums)
79 un-matched-sums))
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)))
84 (setq sum (cdar x)
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))
90 (e2 (car sum2))
91 (i1 (cadr sum1))
92 (i2 (cadr sum2))
93 (l1 (caddr sum1))
94 (l2 (caddr sum2))
95 (h1 (cadddr sum1))
96 (h2 (cadddr sum2))
97 (newl (simplify `(($max) ,l1 ,l2)))
98 (newh (simplify `(($min) ,h1 ,h2)))
99 (newi (cond ((eq i1 i2) i1)
100 ((free e1 i2) i2)
101 ((free e2 i1) i1)
102 (t (get-free-index (list nil i1 i2 e1 e2 l1 l2 h1 h2)))))
103 (extracted nil)
104 (new-sum nil))
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))
108 (setq extracted
109 (addn
110 (mapcar #'dosum
111 (list e1 e1 e2 e2)
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)
115 '(t t t t))
117 (cons new-sum extracted)))
119 (defun get-free-index (llist &optional i)
120 (or (do ((try-list (cdr $niceindicespref) (cdr try-list)))
121 ((null 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))
126 (nil)
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
131 (if (atom e)
133 (let (($genindex '$j)
134 (e (recur-apply #'$bashindices e)))
135 (cond ((atom e) e)
136 ((member (caar e) '(%sum %product) :test #'eq)
137 (sumconsimp (subst (gensumindex) (caddr e) e)))
138 (t e)))))
140 (defmfun $niceindices (e)
141 (if (atom e)
143 (let ((e (recur-apply #'$niceindices e)))
144 (cond ((atom e) e)
145 ((member (caar e) '(%sum %product) :test #'eq)
146 (sumconsimp (subst (get-free-index e (caddr e)) (caddr e) e)))
147 (t 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))
152 (resimplify e)))