1;;; -*-  Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3;;;     The data in this file contains enhancments.                    ;;;;;
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
11(in-package :maxima)
12
13(macsyma-module nisimp)
14
15;;;programs for the LET LETSIMP LETRULES and REMLET commands
16;;;these programs use the names LETSIMPTREE and LETRULES on the
17;;;property list of atoms
18;;;except for the top level programs all program names have the prefix NIS
19
20(declare-top (special nistree nisrules nisflag $ratexpand varlist $ratfac))
21
22(defmvar $letvarsimp nil)
23
24(defmvar $letrat nil)
25
26(defmvar $default_let_rule_package '$default_let_rule_package
27  "The name of the default rule package used by `let' and `letsimp'")
28
29(putprop '$default_let_rule_package 'let-rule-setter 'assign)
30
31(defmvar $current_let_rule_package '$default_let_rule_package
32  "The name of the current rule package used by `let' and `letsimp'")
33
34(putprop '$current_let_rule_package 'let-rule-setter 'assign)
35
36(defmvar $let_rule_packages '((mlist) $default_let_rule_package)
37  "The names of the various let rule simplification packages")
38
39(putprop '$let_rule_packages 'let-rule-setter 'assign)
40
41(setq nisrules nil nistree nil)
42
43(defun let-rule-setter (var val)
44  (cond ((eq var '$default_let_rule_package)
45	 (merror (intl:gettext "assignment: cannot assign to default_let_rule_package.")))
46	((and (eq var '$current_let_rule_package)
47	      (not (memalike val (cdr $let_rule_packages))))
48	 (merror (intl:gettext "assignment: ~M is not a rule package.") val))
49	((eq var '$let_rule_packages)
50	 (merror (intl:gettext "assignment: cannot assign to let_rule_packages.~%assignment: call 'let' to create let rule packages.")))))
51
52(defmspec $let (l) (setq l (cdr l))
53	  (if (null (cdr l)) (wna-err '$let))
54	  ;;LET([PATTERN,REPL,PRED,ARG1,...,ARGN],NAME)
55	  (prog (pattern pat replacement treename text $ratfac)
56	     ;;LET(PATTERN,REPL,PRED,ARG1,...,ARGN)
57	     (cond ((atom (car l))
58		    (setq treename $current_let_rule_package))
59		   ((eq 'mlist (caaar l))
60		    (setq treename (cadr l))
61		    (if (not (symbolp treename))
62			(improper-arg-err treename '$let))
63		    (setq l (cdar l)))
64		   (t (setq treename $current_let_rule_package)))
65	     (let ((nistree (mget treename 'letsimptree))
66		   (nisrules (mget treename 'letrules)))
67	       (setq pat (strip-lineinfo (meval (car l))))
68	       (setq replacement (cdr l))
69	       (setq pattern (cond ((atom pat) (list pat))
70				   ((eq (caar pat) 'mtimes)
71				    (cdr pat))
72				   (t (list pat))))
73	       (setq nistree (nislet nistree pattern replacement))
74	       (cond (treename (mputprop treename
75					 nistree
76					 'letsimptree)
77			       (add2lnc treename $let_rule_packages)))
78	       (nonsymchk (caddr l) '$let)
79	       (setq text
80		     (append (list '(mtext) pat '| --> | )
81			     (cond ((cddr l)
82				    (list (cadr l)
83					  '#.(intern (format nil " ~A " 'where))
84					  (cons (list (caddr l))
85						(cdddr l))))
86				   (t replacement))))
87	       (setq nisrules (append (list text) nisrules))
88	       (cond (treename (mputprop treename
89					 nisrules
90					 'letrules)))
91	       (return text))))
92
93(defun nislet (tree list function)
94  (prog (permlist)
95     (setq permlist (nispermutations list))
96     step (cond ((eq nil permlist) (return tree)))
97     (setq tree (nistreebuilder tree (car permlist) function))
98     (setq permlist (cdr permlist))
99     (go step)))
100
101(defun nispermutations (llist)
102  (cond
103    ((null (cdr llist)) (list llist))
104    (t
105     (prog (permlist a)
106      step (setq permlist
107		 (append
108		  (nisaddon (car llist)
109			    (nispermutations (append a (cdr llist))))
110		  permlist))
111      (if (null (cdr llist)) (return permlist))
112      (push (car llist) a)
113      (setq llist (cdr llist))
114      (go step)))))
115
116(defun nisaddon (x llist)
117  (if llist (cons (cons x (car llist)) (nisaddon x (cdr llist)))))
118
119(defun nistreebuilder (tree perm function)
120  (cond ((null perm) (cons (list function) tree))
121	((null tree)
122	 (list (cons (car perm)
123		     (nistreebuilder nil (cdr perm) function))))
124	((equal (car perm) (caar tree))
125	 (nisswcar tree
126		   (nisswcdr (car tree)
127			     (nistreebuilder (cdar tree)
128					     (cdr perm)
129					     function))))
130	(t (nisswcdr tree
131		     (nistreebuilder (cdr tree)
132				     perm
133				     function)))))
134
135(defun nisswcar (x y)
136  (cons y (cdr x)))
137
138(defun nisswcdr (x y)
139  (cons (car x) y))
140
141(defmspec $remlet (x)
142  (setq x (cdr x))
143  ;; REMLET(PROD,NAME) REMLET(PROD) REMLET() REMLET(FALSE,NAME)
144  (prog (pattern text treename)
145     (cond ((cddr x) (wna-err '$remlet))
146	   ((null (cdr x)) (setq treename $current_let_rule_package))
147	   (t (setq treename (cadr x))
148	      (if (not (symbolp treename))
149		  (improper-arg-err treename '$remlet))))
150     (setq pattern (strip-lineinfo (meval (car x))))
151     (when (or (not pattern) (eq '$all pattern))
152       (setq nisrules nil nistree nil)
153       (unless (eq treename '$default_let_rule_package)
154	 (setq $let_rule_packages (delete treename $let_rule_packages :count 1 :test #'eq)))
155       (go a))
156     (setq nistree (mget treename 'letsimptree))
157     (if (setq text (nisremlet pattern)) (return text))
158     (if nistree
159	 (setq nisrules
160	       (nistreelister (mget treename 'letrules) pattern))
161	 (setq nisrules nil))
162     a    (mputprop treename nistree 'letsimptree)
163     (mputprop treename nisrules 'letrules)
164     (return '$done)))
165
166(defun nistreelister (llist pattern)
167  (prog (x)
168   a    (if (alike1 pattern (cadar llist)) (return (append x (cdr llist))))
169   (setq x (append x (list (car llist))) llist (cdr llist))
170   (go a)))
171
172(defun nisremlet (pat)
173  (prog (llist permlist x)
174     (setq llist (if (mtimesp pat) (cdr pat) (ncons pat)))
175     (setq nisflag t x nistree)
176     (setq permlist (nispermutations llist))
177     step (when (null permlist) (setq nistree x) (return nil))
178     (setq x (nistreetrimmer (car permlist) x))
179     (if (null nisflag) (merror (intl:gettext "remlet: no rule found: ~M") pat))
180     (setq permlist (cdr permlist))
181     (go step)))
182
183(defun nistreetrimmer (perm tree)
184  (cond ((null perm)
185	 (cond ((null tree) (setq nisflag nil))
186	       ((null (cdar tree))
187		(setq nisflag (caar tree)) (cdr tree))
188	       (t (nisswcdr tree (nistreetrimmer nil (cdr tree))))))
189	((null tree) (setq nisflag nil))
190	((equal (car perm) (caar tree))
191	 (prog (x)
192	    (setq x (nistreetrimmer (cdr perm) (cdar tree)))
193	    (if (null x) (return (cdr tree)))
194	    (return (nisswcar tree (nisswcdr (car tree) x)))))
195	(t (nisswcdr tree (nistreetrimmer perm (cdr tree))))))
196
197(defmspec $letrules (name)
198  (setq name (cdr name))		;LETRULES(NAME)
199  (let ((treename (if name (car name) $current_let_rule_package)))
200    (if (not (symbolp treename)) (improper-arg-err treename '$letrules))
201    (setq nistree (mget treename 'letsimptree)
202	  nisrules (mget treename 'letrules))
203    (apply #'$disp nisrules)))
204
205(defmspec $letsimp (form)		;letsimp(expr,tree1,...,treen)
206  (setq form (cdr form))
207  (let* ((expr (strip-lineinfo (meval (pop form))))
208	 (sw ($ratp expr))
209	 $ratfac)
210    (progv (unless sw '(varlist genvar))
211	(unless sw (list varlist genvar))
212      (when (and sw (member 'trunc (cdar expr) :test #'eq))
213	(setq expr ($taytorat expr)))
214      (dolist (rulepackage (or form (list $current_let_rule_package))
215	       (if sw (ratf expr) expr))
216	(unless (symbolp rulepackage)
217	  (improper-arg-err rulepackage '$letsimp))
218	(when (setq nistree (mget rulepackage 'letsimptree))
219	  ;; Whereas nisletsimp returns an expression in general
220	  ;; representation, the original expr might be in CRE form.
221	  ;; Regardless, we use ratf to make sure varlist and genvar
222	  ;; know of expr's kernels.
223	  (setq expr (nisletsimp (if (atom expr)
224				     expr
225				     (ratf expr)))))))))
226
227(defun nisletsimp (e)
228  (let (x)
229    (cond ((mnump e) e)
230	  ((or (and (atom e) (setq x (ncons e)))
231	       (and (eq (caar e) 'mtimes) (setq x (cdr e))))
232	   (setq x (nisnewlist x))
233	   (if x (nisletsimp ($ratexpand (cons '(mtimes) x))) e))
234	  ((member (caar e) '(mplus mequal mlist $matrix) :test #'eq)
235	   (cons (if (eq (caar e) 'mplus) '(mplus) (car e))
236		 (mapcar #'nisletsimp (cdr e))))
237	  ((or (eq (caar e) 'mrat)
238	       (and (eq (caar e) 'mquotient) (setq e (ratf e))))
239	   (nisletsimprat e))
240	  (t ;; A kernel (= product of 1 element)
241	   (setq x (nisnewlist (ncons e)))
242	   (if x (nisletsimp ($ratexpand (cons '(mtimes) x))) e)))))
243
244(defun nisletsimprat (e)
245  (let ((num (cadr e)) (denom (cddr e)) $ratexpand)
246    (if $letvarsimp (setq varlist (mapcar #'nisletsimp varlist)))
247    (let (($ratexpand t))
248      (setq num (nisletsimp (pdis num)) denom (nisletsimp (pdis denom))))
249    (setq e (list '(mquotient) num denom))
250    (if $letrat (nisletsimp ($ratexpand e)) e)))
251
252(defun nisnewlist (llist)
253  (let ((x (nissearch llist nistree nil))) (if x (nisreplace llist x))))
254
255(defun nissearch (x y z)
256  (cond ((null y) nil)
257	((nisinnernull y) (nisfix (nisinnernull y) z))
258	((null x) nil)
259	(t (prog (xx yy path bind)
260	      (setq yy y)
261	      a    (setq xx x)
262	      b    (cond ((and (setq bind (nismatch (car xx)
263						    (caar yy)
264						    z))
265			       (setq path
266				     (nissearch (cdr xx)
267						(cdar yy)
268						(cdr bind))))
269			  (return (cons (car bind) path))))
270	      (setq xx (cdr xx))
271	      (cond (xx (go b)))
272	      (setq yy (cdr yy))
273	      (cond ((null yy) (return nil)))
274	      (go a)))))
275
276(defun nisinnernull (x)
277  (cond ((null x) nil)
278	((null (cdar x)) (caar x))
279	(t (nisinnernull (cdr x)))))
280
281(defun nisfix (funperd argasslist)
282  (prog (function args bindings perd flag)
283     (if (not argasslist) (return (car funperd)))
284     (setq argasslist (nisnumberpicker argasslist))
285     (setq args (maplist 'caar argasslist))
286     (setq bindings (maplist 'cdar argasslist))
287     (mbinding (args bindings)
288	       (setq function (car funperd))
289	       (if (setq perd (cdr funperd))
290		   (if (not (meval perd)) (setq flag t)))
291	       (if (null flag) (setq function (meval function))))
292     (return (if flag nil (list function)))))
293
294(defun nisnumberpicker (x)
295  (cond ((null x) nil)
296        ((or (not (symbolp (caar x)))
297             (kindp (caar x) '$constant))
298         ;; Skip over numbers and constants
299         (nisnumberpicker (cdr x)))
300        (t (nisswcdr x (nisnumberpicker (cdr x))))))
301
302(defun nismatch (a b c)
303  (prog (x y newexpt)
304     (setq x (nisextract a))
305     (setq y (nisextract b))
306     (cond
307       ((cadr y)
308	(cond ((and (equal (car x) (car y))
309		    (setq newexpt (nisexpocheck (cddr x)
310						(cddr y)
311						c))
312		    (setq c (nisargschecker (cadr x)
313					    (cadr y)
314					    c)))
315	       (cond ((equal '(rat) (car newexpt))
316		      (return (cons (cons a (nisbuild x newexpt))
317				    c)))
318		     (t (return (cons (cons a '(dummy 0 (0 0)))
319				      newexpt)))))
320	      (t (return nil)))))
321     (cond ((and (setq c (nisargmatch (niskernel a) (car y) c))
322		 (setq newexpt (nisexpocheck (cddr x)
323					     (cddr y)
324					     c)))
325	    (cond ((equal '(rat) (car newexpt))
326		   (return (cons (cons a (nisbuild x newexpt))
327				 c)))
328		  (t (return (cons (cons a '(dummy 0 (0 0)))
329				   newexpt))))))
330     (return nil)))
331
332(defun niskernel (a)
333  (if (mexptp a) (cadr a) a))
334
335(defun nisextract (x)
336  (cond ((or (atom x) (eq (caar x) 'rat))
337	 (cons x (cons nil 1)))
338	((eq 'mexpt (caar x))
339	 (cond ((atom (cadr x))
340		(cons (cadr x) (cons nil (caddr x))))
341	       (t (cons (if (member 'array (cdaadr x) :test #'eq)
342			    (list (caaadr x) 'array)
343			    (caaadr x))
344			(cons (cdadr x) (caddr x))))))
345	(t (cons (if (member 'array (cdar x) :test #'eq)
346		     (list (caar x) 'array)
347		     (caar x))
348		 (cons (cdr x) 1)))))
349
350(defun nisargschecker (listargs treeargs argasslist)
351  (prog (c)
352     (cond ((and listargs treeargs) (go check))
353	   ((or listargs treeargs) (return nil))
354	   (t (return argasslist)))
355     check(setq c (nisargmatch (car listargs)
356			       (car treeargs)
357			       argasslist))
358     (cond (c (return (nisargschecker (cdr listargs)
359				      (cdr treeargs)
360				      c)))
361	   (t (return nil)))))
362
363(defun nisexpocheck (listpower treepower argasslist)
364  (prog (p q r s a b xx)
365     (cond ((atom treepower)
366	    (cond ((numberp treepower)
367		   (prog2 (setq r treepower s 1) (go math)))
368		  (t (return (nisargmatch listpower
369					  treepower
370					  argasslist))))))
371     (setq r (cadr treepower) s (caddr treepower))
372     (if (not (numberp s)) (return nil))
373     math (cond ((numberp listpower) (setq p listpower q 1))
374		((atom listpower) (return nil))
375		((eq 'rat (caar listpower))
376		 (setq p (cadr listpower) q (caddr listpower)))
377		(t (return nil)))
378     (setq xx (* (* q s) (- (* p s) (* q r))))
379     (setq a (< (* r s) 0))
380     (setq b (< xx 0))
381     (cond ((or (not (or a b)) (and a (or b (equal 0 xx))))
382	    (return (list '(rat) xx (* q s)))))
383     (return nil)))
384
385(defun nisargmatch (x y c)
386  (prog (w)
387     (setq w c)
388     up   (if (null w) (go down))
389     (cond ((eq (caar w) y)
390	    (cond ((alike1 (cdar w) x) (return c))
391		  (t (return nil)))))
392     (setq w (cdr w))
393     (go up)
394     down (setq w (mget y 'matchdeclare))
395     (cond ((null w) (if (equal x y) (go out) (return nil)))
396	   ((member (car w) '($true t) :test #'eq) (go out))
397	   ((and (atom (car w))
398		 (meval (cons (ncons (car w))
399			      (append (cdr w) (list x)))))
400	    (go out))
401	   ((and (not (atom (car w)))
402		 (not (atom (caar w)))
403		 (atom (caaar w))
404         ; If we arrive here, (CAR W) is a Maxima expression like ((FOO) ...)
405         ; If (CAR W) is a Maxima lambda expression, evaluate it via MFUNCALL.
406         ; Otherwise, append X and call MEVAL.
407         ; Note that "otherwise" includes Maxima lambda expressions with missing arguments;
408         ; in that case the expression is ((MQAPPLY) ((LAMBDA) ...)) and MEVAL is the way to go.
409		 (if (eq (caaar w) 'lambda)
410           (mfuncall (car w) x)
411           (meval (append (car w) (list x)))))
412	    (go out))
413	   (t (return nil)))
414     out  (return (cons (cons y x) c))))
415
416(defun nisbuild (x newexpt)
417  (list '(mexpt)
418	(if (cadr x)
419	    (cons (if (symbolp (car x)) (ncons (car x)) (car x))
420		  (cadr x))
421	    (car x))
422	newexpt))
423
424(defun nisreplace (llist asslist)
425  (cond ((eq (cdr asslist) nil) (cons (car asslist) llist))
426	((equal (car llist) (caar asslist))
427	 (cond ((equal 0 (cadar (cdddar asslist)))
428		(nisreplace (cdr llist) (cdr asslist)))
429	       (t (cons (cdar asslist)
430			(nisreplace (cdr llist) (cdr asslist))))))
431	(t (cons (car llist) (nisreplace (cdr llist) asslist)))))
432