Rename *ll* and *ul* to ll and ul in strictly-in-interval
[maxima.git] / src / matrun.lisp
blob4455451944079be336c4ca6d65f63a7e71014b0f
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 matrun)
15 ;;; TRANSLATION properties for the FSUBRs in this file
16 ;;; can be found in MAXSRC;TRANS5 >. Be sure to check on those
17 ;;; if any semantic changes are made.
19 ;; $MAXAPPLYDEPTH is the maximum depth within an expression to which
20 ;; APPLYi will delve. If $MAXAPPLYDEPTH is 0, it is applied only to
21 ;; top level.
22 (defmvar $maxapplydepth 10000.)
24 ;; If $MAXAPPLYHEIGHT is 0, only atoms are affected by $APPLYB1 and
25 ;; $APPLYB2.
26 (defmvar $maxapplyheight 10000.)
28 (defmvar matchreverse nil)
30 (defmspec $disprule (l) (setq l (cdr l))
31 (if (and (eq (car l) '$all) (null (cdr l)))
32 (disprule1 (cdr $rules))
33 (disprule1 l)))
35 (defun disprule1 (l)
36 `((mlist simp) ,@(loop for r in l collect (cadr ($ldisp (consrule r))))))
38 (defun consrule (x)
39 (let ((rule (mget x '$rule)))
40 (if rule (list '(msetq simp) x (cons '(marrow simp) (cdr rule)))
41 (merror (intl:gettext "disprule: ~:M is not a user rule.") x))))
43 (defmfun $remrule (op rule)
44 (prog (rules)
45 (setq op (getopr op))
46 (cond ((not (eq rule '$all))
47 (removerule op rule) (return (getop op)))
48 ((null (setq rules (mget op 'oldrules)))
49 (merror (intl:gettext "remrule: no rules known for operator ~:@M") op)))
50 next (cond ((or (null rules) (null (cdr rules)))
51 (mputprop op 1 'rulenum) (return (getop op)))
52 (t (removerule op (car rules))
53 (setq rules (cdr rules)) (go next)))))
55 (defun removerule (op rule)
56 (cond ((member rule *builtin-$rules* :test #'eq)
57 (mget op 'oldrules))
59 (prog
60 (oldrules old othrulename othrule)
61 (setq oldrules (mget op 'oldrules))
62 (cond ((or (null rule) (null (setq oldrules (member rule oldrules :test #'equal))))
63 (merror (intl:gettext "remrule: no such rule: ~:M") rule))
64 ((null (car (setq oldrules (cdr oldrules))))
65 (setq oldrules (cdr oldrules))
66 (setq othrulename nil)
67 (setq othrule #'(lambda (a bb c) (declare (ignore bb)) (simpargs a c))))
68 (t (setq othrulename (car oldrules))
69 (setq othrule (cadr (getl (car oldrules) '(expr subr))))))
70 (putprop-or-remprop rule othrule 'expr)
71 (setq old (cdr (member rule (reverse (mget op 'oldrules)) :test #'equal)))
72 (if old (putprop-or-remprop (car old)
73 (subst othrulename rule (get (car old) 'expr))
74 'expr))
75 (if (boundp rule) (makunbound rule))
76 (mremprop rule '$rule)
77 (mremprop rule '$ruletype)
78 (mremprop rule 'ruleof)
79 (remprop rule 'expr)
80 (setq $rules (delete rule $rules :count 1 :test #'eq))
81 (putprop-or-remprop rule othrulename 'expr)
82 (if (eq (get op 'operators) rule)
83 (putprop-or-remprop op othrulename 'operators))
84 (let ((l (delete rule (mget op 'oldrules) :test #'eq)))
85 (if (equal l '(nil)) (mremprop op 'oldrules) (mputprop op l 'oldrules))
86 (return l))))))
88 (defun putprop-or-remprop (x y z)
89 (if y
90 (putprop x y z)
91 (remprop x z)))
93 (defun findbe (e)
94 (cond ((equal e 1) '(1 . 0))
95 ((equal e 0) '(0 . 1))
96 ((atom e) (cons e 1))
97 ((eq (caar e) 'mexpt) (cons (cadr e) (caddr e)))
98 (t (cons e 1))))
100 (defun findfun (e p c)
101 (prog ()
102 (cond ((and (null (atom e)) (eq (caar e) p)) (return e))
103 ((or (atom e) (not (eq (caar e) c))) (matcherr))
104 ((and (null matchreverse) (member c '(mplus mtimes) :test #'eq))
105 (setq e (reverse (cdr e))) (go b)))
106 a (setq e (cdr e))
107 b (cond ((null e) (matcherr))
108 ((and (not (atom (car e))) (eq (caaar e) p)) (return (car e))))
109 (go a)))
111 (defun findexpon (e1 base* c)
112 (prog (e)
113 (setq e e1)
114 (cond ((and (mexptp e) (alike1 base* (cadr e)))
115 (return (caddr e)))
116 ((or (atom e) (not (eq (caar e) c))) (go c))
117 ((and (null matchreverse) (member c '(mplus mtimes) :test #'eq))
118 (setq e (reverse (cdr e))) (go b)))
119 a (setq e (cdr e))
120 b (cond ((null e) (go c))
121 ((and (mexptp (car e)) (alike1 base* (cadar e)))
122 (return (caddar e))))
123 (go a)
124 c (cond ((or (and (not (atom e1)) (member c '(mplus mtimes) :test #'eq)
125 (eq c (caar e1)) (memalike base* e1))
126 (alike1 e1 base*)
127 (and (not (atom base*)) (eq c (caar base*))))
128 (return 1))
129 ((eq c 'mexpt) (matcherr))
130 (t (return 0)))))
132 (defun findbase (e expon c)
133 (prog ()
134 (cond ((equal expon 0)
135 (if (and (eq c 'mexpt) (not (equal 1 e))) (matcherr))
136 (return 1))
137 ((equal expon 1) (return e))
138 ((and (numberp expon) (> expon 0) (equal e 0))
139 (return 0))
140 ((and (mexptp e) (alike1 expon (caddr e)))
141 (return (cadr e)))
142 ((or (atom e) (not (eq (caar e) c))) (matcherr))
143 ((and (null matchreverse) (member c '(mplus mtimes) :test #'eq))
144 (setq e (reverse (cdr e))) (go b)))
145 a (setq e (cdr e))
146 b (cond ((null e)
147 (return (if (and (realp expon) (minusp expon)) 1 0)))
148 ((and (mexptp (car e)) (alike1 expon (caddar e)))
149 (return (cadar e))))
150 (go a)))
152 (defun part+ (e p preds)
153 (if (and (consp e) (eq (caar e) 'mplus))
154 (part+-mplus e p preds)
155 (part+-not-mplus e p preds)))
157 (defun part+-not-mplus (e p preds)
158 (part+-mplus (list '(mplus) 0 e) p preds))
160 (defun part+-mplus (e p preds)
161 (prog (flag saved val)
162 (if (not (mplusp e)) (matcherr))
163 (cond ((> (length p) (length preds))
164 (setq p (reverse p))
165 (setq p (nthkdr p (- (length p) (length preds))))
166 (setq p (nreverse p))))
167 (setq e (copy-tree e)) ; PREVIOUSLY: (setq e ($ratexpand e))
168 (setq e (cdr e))
169 a (cond ((null p) (cond ((null e) (return t)) (t (matcherr))))
170 ((and (cdr preds) (member (car (caddar preds)) '(msetq setq) :test #'eq))
171 (cond (flag (merror (intl:gettext "PART+: two or more pattern variables match anything.")))
172 (t (setq flag t p (reverse p) preds (reverse preds))
173 (go a))))
174 ((not (atom (car p)))
175 (prog (mye)
176 (setq mye e)
177 loop (cond ((null mye) (matcherr)))
178 (setq val (catch 'match (mcall (car preds) (car mye))))
179 (cond ((null val)
180 (setq mye (cdr mye)) (go loop))
181 (t (return (setq e (delete (car mye) e :count 1 :test #'equal))))))
182 (go b))
183 (t (mset (car p) 0)))
184 (setq saved 0)
185 (mapc
186 #'(lambda (z)
187 (cond ((null (setq val (catch 'match (mcall (car preds) z)))) nil)
188 (t (setq saved (add2* saved val))
189 (setq e (delete z e :count 1 :test #'equal)))))
191 (cond ((and (equal saved 0)
192 (null (setq val (catch 'match (mcall (car preds) 0)))))
193 (matcherr)))
194 (mset (car p) saved)
195 b (setq preds (cdr preds) p (cdr p))
196 (go a)))
198 (defun part* (e p preds)
199 (if (and (consp e) (eq (caar e) 'mtimes))
200 (part*-mtimes e p preds)
201 (part*-not-mtimes e p preds)))
203 (defun part*-not-mtimes (e p preds)
204 (part*-mtimes (list '(mtimes) 1 e) p preds))
206 (defun part*-mtimes (e p preds)
207 (prog (flag saved val)
208 (if (not (mtimesp e)) (matcherr))
209 (cond ((> (length p) (length preds))
210 (setq p (reverse p))
211 (setq p (nthkdr p (- (length p) (length preds))))
212 (setq p (nreverse p))))
213 (setq e (copy-tree e)) ; PREVIOUSLY: (setq e ($factor e))
214 (setq e (cdr e))
215 a (cond ((null p) (cond ((null e) (return t)) (t (matcherr))))
216 ((and (cdr preds) (member (car (caddar preds)) '(msetq setq) :test #'eq))
217 (cond (flag (merror (intl:gettext "PART*: two or more pattern variables match anything.")))
218 (t (setq flag t p (reverse p) preds (reverse preds))
219 (go a))))
220 ((not (atom (car p)))
221 (prog (mye)
222 (setq mye e)
223 loop (cond ((null mye) (matcherr)))
224 (setq val (catch 'match (mcall (car preds) (car mye))))
225 (cond ((null val)
226 (setq mye (cdr mye)) (go loop))
227 (t (return (setq e (delete (car mye) e :count 1 :test #'equal))))))
228 (go b))
229 (t (mset (car p) 1)))
230 (setq saved 1)
231 (mapc
232 #'(lambda (z) (setq val (catch 'match (mcall (car preds) z)))
233 (cond ((null val) nil)
234 (t (setq saved (mul2* saved val))
235 (setq e (delete z e :count 1 :test #'equal)))))
237 (cond ((and (equal saved 1)
238 (null (setq val (catch 'match (mcall (car preds) 1)))))
239 (matcherr)))
240 (mset (car p) saved)
241 b (setq preds (cdr preds) p (cdr p))
242 (go a)))
244 ;;; TRANSLATE property in MAXSRC;TRANS5 >
246 (defmspec $apply1 (l) (setq l (cdr l))
247 (let ((expr (meval (car l))))
248 (mapc #'(lambda (z) (setq expr (apply1 expr z 0))) (cdr l))
249 expr))
251 (defun apply1 (expr *rule depth)
252 (cond
253 ((> depth $maxapplydepth) expr)
255 (prog nil
256 (*rulechk *rule)
257 (setq expr (rule-apply *rule expr))
258 b (cond
259 ((or (atom expr) (mnump expr)) (return expr))
260 ((eq (caar expr) 'mrat)
261 (setq expr (ratdisrep expr)) (go b))
263 (return
264 (simplifya
265 (cons
266 (delsimp (car expr))
267 (mapcar #'(lambda (z) (apply1 z *rule (1+ depth)))
268 (cdr expr)))
269 t))))))))
271 (defmspec $applyb1 (l) (setq l (cdr l))
272 (let ((expr (meval (car l))))
273 (mapc #'(lambda (z) (setq expr (car (apply1hack expr z)))) (cdr l))
274 expr))
276 (defun apply1hack (expr *rule)
277 (prog (pairs max)
278 (*rulechk *rule)
279 (setq max 0)
280 b (cond
281 ((atom expr) (return (cons (multiple-value-bind (ans rule-hit) (mcall *rule expr) (if rule-hit ans expr)) 0)))
282 ((specrepp expr) (setq expr (specdisrep expr)) (go b)))
283 (setq pairs (mapcar #'(lambda (z) (apply1hack z *rule))
284 (cdr expr)))
285 (setq max 0)
286 (mapc #'(lambda (l) (setq max (max max (cdr l)))) pairs)
287 (setq expr (simplifya (cons (delsimp (car expr))
288 (mapcar #'car pairs))
290 (cond ((= max $maxapplyheight) (return (cons expr max))))
291 (setq expr (rule-apply *rule expr))
292 (return (cons expr (1+ max)))))
294 (defun *rulechk (*rule)
295 (if (and (symbolp *rule) (not (fboundp *rule)) (not (mfboundp *rule)))
296 (merror (intl:gettext "apply1: no such rule: ~:M") *rule)))
298 (defun rule-apply (*rule expr)
299 (prog (ans rule-hit)
300 loop (multiple-value-setq (ans rule-hit) (mcall *rule expr))
301 (cond ((and rule-hit (not (alike1 ans expr)))
302 (setq expr ans) (go loop)))
303 (return expr)))
305 (defmspec $apply2 (l) (setq l (cdr l))
306 (let ((rulelist (cdr l))) (apply2 rulelist (meval (car l)) 0)))
308 (defun apply2 (rulelist expr depth)
309 (cond
310 ((> depth $maxapplydepth) expr)
312 (prog (ans ruleptr rule-hit)
313 a (setq ruleptr rulelist)
314 b (cond
315 ((null ruleptr)
316 (cond
317 ((atom expr) (return expr))
318 ((eq (caar expr) 'mrat)
319 (setq expr (ratdisrep expr)) (go b))
321 (return
322 (simplifya
323 (cons
324 (delsimp (car expr))
325 (mapcar #'(lambda (z) (apply2 rulelist z (1+ depth)))
326 (cdr expr)))
327 t))))))
328 (cond ((progn (multiple-value-setq (ans rule-hit) (mcall (car ruleptr) expr)) rule-hit)
329 (setq expr ans)
330 (go a))
331 (t (setq ruleptr (cdr ruleptr)) (go b)))))))
333 (defmspec $applyb2 (l) (setq l (cdr l))
334 (let ((rulelist (cdr l))) (car (apply2hack rulelist (meval (car l))))))
336 (defun apply2hack (rulelist e)
337 (prog (pairs max)
338 (setq max 0)
339 (cond ((atom e) (return (cons (apply2 rulelist e -1) 0)))
340 ((specrepp e) (return (apply2hack rulelist (specdisrep e)))))
341 (setq pairs (mapcar #'(lambda (x) (apply2hack rulelist x)) (cdr e)))
342 (setq max 0)
343 (mapc #'(lambda (l) (setq max (max max (cdr l)))) pairs)
344 (setq e (simplifya (cons (delsimp (car e)) (mapcar #'car pairs)) t))
345 (cond ((= max $maxapplyheight) (return (cons e max)))
346 (t (return (cons (apply2 rulelist e -1) (1+ max)))))))