Forgot to load lapack in a few examples
[maxima.git] / share / contrib / unit / unit-functions.lisp
blob5924089e0fbeb62a291112c825a5c0b95afebc7f
1 ;; Definitions to avoid undefined variable warnings
2 (defvar allbutl nil)
3 (defvar dcount 0)
4 (defvar greatorder nil)
5 (defvar lessorder nil)
7 ;; Redefining toplevel-macsyma-eval for post_eval_functions
8 ;; Define the finaleval list
9 (defmvar $pre_eval_functions `((mlist))
10 nil
11 :setting-predicate #'(lambda (val)
12 (and ($listp val)
13 (every 'symbolp (margs val)))))
15 (defmvar $post_eval_functions `((mlist))
16 nil
17 :setting-predicate #'(lambda (val)
18 (and ($listp val)
19 (every 'symbolp (margs val)))))
21 (defun toplevel-macsyma-eval (x)
22 ;; Functional definition of toplevel-macsyma-eval
23 (dolist (fi (margs $pre_eval_functions) x)
24 (setq x (mfuncall fi x)))
25 (setq x (meval* x))
26 (dolist (fi (margs $post_eval_functions) x)
27 (setq x (mfuncall fi x))))
30 (defmfun kill1 (x)
31 ;; Redefine kill1 in order to be able to properly reset post_eval_functions
32 ;; with kill(all) and kill(post_eval_functions)
33 (funcall
34 #'(lambda (z)
35 (cond ((and allbutl (member x allbutl :test #'eq)))
36 ((eq (setq x (getopr x)) '$labels)
37 (dolist (u (cdr $labels))
38 (cond ((and allbutl (member u allbutl :test #'eq))
39 (setq z (nconc z (ncons u))))
40 (t (makunbound u) (remprop u 'time)
41 (remprop u 'nodisp))))
42 (setq $labels (cons '(mlist simp) z) $linenum 0 dcount 0))
43 ((member x '($values $arrays $aliases $rules $props
44 $let_rule_packages) :test #'eq)
45 (mapc #'kill1 (cdr (symbol-value x))))
46 ((member x '($functions $macros $gradefs $dependencies $structures) :test #'eq)
47 (mapc #'(lambda (y) (kill1 (caar y))) (cdr (symbol-value x))))
48 ((eq x '$myoptions))
49 ((eq x '$tellrats) (setq tellratlist nil))
50 ((eq x '$ratweights) (setq *ratweights nil
51 $ratweights '((mlist simp))))
52 ((eq x '$features)
53 (cond ((not (equal (cdr $features) featurel))
54 (setq $features (cons '(mlist simp)
55 (copy-list featurel ))))))
56 ((eq x '$pre_eval_functions) (setq $pre_eval_functions '((mlist)) ))
57 ((eq x '$post_eval_functions) (setq $post_eval_functions '((mlist)) ))
58 ((or (eq x t) (eq x '$all))
59 (setq $pre_eval_functions '((mlist)))
60 (setq $post_eval_functions '((mlist)))
61 (mapc #'kill1 (cdr $infolists))
62 (setq $ratvars '((mlist simp)) varlist nil genvar nil
63 checkfactors nil greatorder nil lessorder nil $gensumnum 0
64 *ratweights nil $ratweights
65 '((mlist simp))
66 tellratlist nil $dontfactor '((mlist)) $setcheck nil)
67 (killallcontexts))
68 ((setq z (assoc x '(($inlabels . $inchar) ($outlabels . $outchar)
69 ($linelabels . $linechar)) :test #'eq))
70 (mapc #'(lambda (y) (remvalue y '$kill))
71 (getlabels* (eval (cdr z)) nil)))
72 ((and (fixnump x) (not (< x 0))) (remlabels x))
73 ((atom x) (kill1-atom x))
74 ((and (eq (caar x) 'mlist) (fixnump (cadr x))
75 (or (and (null (cddr x))
76 (setq x (append x (ncons (cadr x)))))
77 (and (fixnump (caddr x)) (not (> (cadr x) (caddr x))))))
78 (let (($linenum (caddr x))) (remlabels (f- (caddr x) (cadr x)))))
79 ((setq z (mgetl (caar x) '(hashar array))) (remarrelem z x))
80 ((and ($subvarp x)
81 (boundp (caar x))
82 (hash-table-p (setq z (symbol-value (caar x)))))
83 ; Evaluate the subscripts (as is done in ARRSTORE)
84 (let ((indices (mevalargs (cdr x))))
85 (if (gethash 'dim1 z)
86 (remhash (car indices) z)
87 (remhash indices z))))
88 ((eq (caar x) '$@) (mrecord-kill x))
89 ((and (eq (caar x) '$allbut)
90 (not (dolist (u (cdr x))
91 (if (not (symbolp u)) (return t)))))
92 (let ((allbutl (cdr x))) (kill1 t)))
93 (t (improper-arg-err x '$kill))))
94 nil))
96 ;; Define a couple variables specific to this code
97 (defmvar $allunitslist `((mlist)))
98 (defmvar $unitformatresults t)
100 ;; Ensure that units formatting isn't invoked by letsimp.
101 ;; Unless unitformatresults is bound to NIL, letsimp runs into
102 ;; an infinite recursion via NFORMAT.
104 (let ((builtin-letsimp (get '$letsimp 'mfexpr*)))
105 (defmspec $letsimp (e)
106 (let (($unitformatresults nil))
107 (funcall builtin-letsimp e))))
109 ;; Code to enable correct display of multiplication by units via nformat
110 (defun notunitfree (form)
111 ;;returns t if expression contains units, nil otherwise
112 (cond ((atom form) ($member form $allunitslist))
113 ((null (car form)) nil)
114 ((atom (car form))(or ($member (car form) $allunitslist)
115 (notunitfree (cdr form))))
116 (t (or (notunitfree (cdr (car form)))
117 (notunitfree (cdr form))))))
120 ;; Code to optionally group units by common unit
122 (defun unitmember (form list1)
123 (cond ((equal (car list1) nil) nil)
124 ((equal form (car list1)) t)
125 (t (unitmember form (cdr list1)))))
127 (defun groupbyaddlisp (form)
128 (cond ((or (not(notunitfree (car form))) (not(notunitfree (cadr form))))
129 form)
130 ((and (null (cddr form))(equal (meval (list '(mplus simp) (caddr (car form)) (list '(mtimes simp) -1 (caddr (cadr form))))) 0))
131 (list (list '(mtimes) (meval (list '(mplus simp) (cadr (car form)) (cadr (cadr form)))) (caddr (car form)))))
132 ((null (cddr form)) form)
133 ((equal (meval (list '(mplus simp) (caddr (car form)) (list '(mtimes simp) -1 (caddr (cadr form))))) 0)
134 (groupbyaddlisp (cons (list '(mtimes) (meval (list '(mplus simp) (cadr (car form)) (cadr (cadr form)))) (caddr (car form)))
135 (cddr form))))
136 (t (cons (list '(mtimes) (cadr (car form)) (caddr (car form))) (groupbyaddlisp (cdr form))))))
138 (defun groupadd (form)
139 (cond ((and (not (atom form)) (notunitfree form) (equal (caar form) 'mplus))
140 (let ((temp1 (groupbyaddlisp (cdr (nformat form)))))
141 (cond ((or (not (equal (cdr temp1) nil)) (and (atom (cadr temp1)) (not (equal (cadr temp1) nil))))
142 (cons '(mplus) temp1))
143 ((equal (cdr temp1) nil) (car temp1)))))
144 (t form)))
146 (defun onlyunits (form)
147 ;;returns t if expression contains only units, nil otherwise
148 (cond ((null (car form)) t)
149 ((atom (car form))(and ($member (car form) $allunitslist)
150 (onlyunits (cdr form))))
151 (t (and (onlyunits (list (car (cdr (car form)))))
152 (onlyunits (cdr form))))))
154 (defun getunits (form)
155 ;;returns a list containing all unit terms
156 (cond ((null (cadr form)) '())
157 ((atom (cadr form))
158 (if ($member (cadr form) $allunitslist)
159 (cons (cadr form)
160 (getunits (cons (car form) (cdr (cdr form)))))
161 (getunits (cons (car form) (cdr (cdr form))))))
163 (if ($member (cadr (cadr form)) $allunitslist)
164 (cons (cadr form)
165 (getunits (cons (car form) (cdr (cdr form)))))
166 (getunits (cons (car form) (cdr (cdr form))))))))
168 (defun nonunits (form)
169 ;;returns a list containing all non-unit terms
170 (cond ((null (cadr form)) '())
171 ((atom (cadr form))
172 (if (not($member (cadr form) $allunitslist))
173 (cons (cadr form)
174 (nonunits (cons (car form) (cdr (cdr form)))))
175 (nonunits (cons (car form) (cdr (cdr form))))))
177 (if (not($member (cadr (cadr form)) $allunitslist))
178 (cons (cadr form)
179 (nonunits (cons (car form) (cdr (cdr form)))))
180 (nonunits (cons (car form) (cdr (cdr form))))))))
182 (defun unitmtimeswrapper (form)
183 (cond ((and (notunitfree form) (not(onlyunits (cdr form)))
184 (not (equal '(-1) (nonunits form))) (equal $unitformatresults t))
185 (list '(mtimes)
186 (cons '(mtimes simp) (nonunits form))
187 (cons '(mtimes simp) (getunits form))))
188 ((onlyunits (cdr form)) (form-mtimes form))
189 (t (form-mtimes form))))
192 (defmfun nformat (form)
193 (cond ((atom form)
194 (cond ((and (numberp form) (minusp form)) (list '(mminus) (- form)))
195 ((eq t form) (if in-p t '$true))
196 ((eq nil form) (if in-p nil '$false))
197 (t form)))
198 ((atom (car form))
199 form)
200 ((eq 'rat (caar form))
201 (cond ((minusp (cadr form))
202 (list '(mminus) (list '(rat) (- (cadr form)) (caddr form))))
203 (t (cons '(rat) (cdr form)))))
204 ((eq 'mmacroexpanded (caar form)) (nformat (caddr form)))
205 ((null (cdar form)) form)
206 ((eq 'mplus (caar form)) (form-mplus form))
207 ((eq 'mtimes (caar form)) (unitmtimeswrapper form))
208 ((eq 'mexpt (caar form)) (form-mexpt form))
209 ((eq 'mrat (caar form)) (form-mrat form))
210 ((eq 'mpois (caar form)) (nformat ($outofpois form)))
211 ((eq 'bigfloat (caar form))
212 (if (minusp (cadr form))
213 (list '(mminus) (list (car form) (- (cadr form)) (caddr form)))
214 (cons (car form) (cdr form))))
215 (t form)))