3
;; University of Nebraska at Kearney (aka UNK)
7
;; The user of this code assumes all risk for its use. It has no warranty.
8
;; If you don't know the meaning of "no warranty," don't use this code. :)
10
;; Installation and usage: Put flatten.lisp in a directory that
11
;; Maxima can find. (Maxima can find files in directories described
12
;; in the list file_search_lisp.) To use flatten, begin by loading it.
14
;; (C1) load("flatten.lisp")$
15
;; (C2) flatten([x=7,[y+x=0,z+1=0], [[x-y=2]]]);
16
;; (D2) [x = 7, y + x = 0, z + 1 = 0, x - y = 2]
17
;; (C3) m : matrix([a,b],[c,d])$
18
;; (C4) flatten(args(m));
21
;; Flatten is somewhat difficult to define -- essentially it evaluates an
22
;; expression as if its main operator had been declared nary; however, there
23
;; is a difference. We have
25
;; (C1) load("flatten.lisp");
27
;; (C2) flatten(f(g(f(f(x)))));
29
;; (C3) declare(f,nary);
35
;; Unlike declaring the main operator of an expression to be nary, flatten
36
;; doesn't recurse into other function arguments.
38
;; This is supposed to be a clone of Macsyma's flatten function.
39
;; Unlike the Macyma version, this version
40
;; (a) handles CRE expressions,
41
;; (b) doesn't try to flatten expressions of the form a^(b^c) -- Macsyma's
42
;; flatten gives an error about a "wrong number of arguments to "^"."
43
;; (c) doesn't try to flatten expressions of the form a=(b=c).
45
;; There are other functions other than ^ and = that we shouldn't try
46
;; to flatten -- Bessel functions, etc.
49
($put '$flatten 1 '$version)
51
;; Return the operator and argument of the expression e.
53
(defun get-op-and-arg (e)
55
(cond ((or ($atom e) ($subvarp e))
58
((and (consp (nth 0 e)) ($subvarp (nth 1 e)))
59
(setq op `(,(nth 0 e) ,(nth 1 e)))
67
(setq e (ratdisrep e))
68
(cond ((or ($atom e) ($subvarp e)(or (member ($inpart e 0) (list '&^ '&=))))
71
(let ((op (multiple-value-list (get-op-and-arg e))))
74
(setq e (mapcar #'(lambda (x) (flatten-op x op)) e))
75
(setq e (reduce #'append e))
76
(cond ((and (consp (car op)) (eq (caar op) 'mqapply))
81
(defun flatten-op (e op)
83
(setq e-op (multiple-value-list (get-op-and-arg e)))
84
(setq e-arg (cadr e-op))
85
(setq e-op (car e-op))
86
(cond ((equal e-op op)
87
(mapcan #'(lambda (x) (flatten-op x op)) e-arg))
b'\\ No newline at end of file'