Updated testsuite with an expected GCL error in to_poly_share
[maxima.git] / src / comm.lisp
blob6fe8e55c089c7ff25a1ee17b21bc6651c8c9cfe3
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 (in-package :maxima)
11 ;; ** (c) Copyright 1982 Massachusetts Institute of Technology **
13 (macsyma-module comm)
15 (declare-top (special *linelabel*))
17 ;; op and opr properties
19 (defvar *opr-table* (make-hash-table :test #'equal))
21 (defun getopr0 (x)
22 (or
23 (and (symbolp x) (get x 'opr))
24 (and (stringp x) (gethash x *opr-table*))))
26 (defun getopr (x)
27 (or (getopr0 x) x))
29 (defun putopr (x y)
30 (or
31 (and (symbolp x) (setf (get x 'opr) y))
32 (and (stringp x) (setf (gethash x *opr-table*) y))))
34 (defun remopr (x)
35 (or
36 (and (symbolp x) (remprop x 'opr))
37 (and (stringp x) (remhash x *opr-table*))))
39 ;; This business about operator properties is terrible --
40 ;; this stuff should be in src/nparse.lisp, and it should be split up
41 ;; for each operator. Extra points for making it declarative.
43 (mapc #'(lambda (x)
44 (putprop (car x) (cadr x) 'op)
45 (putprop (intern (concatenate 'string "%" (symbol-name (car x)))) (cadr x) 'op) ;; nounify not yet available
46 (putopr (cadr x) (car x))
47 (push (cadr x) *mopl*))
49 '((mplus "+") (mminus "-") (mtimes "*") (mexpt "**") (mexpt "^")
50 (mnctimes ".") (rat "/") (mquotient "/") (mncexpt "^^")
51 (mequal "=") (mgreaterp ">") (mlessp "<") (mleqp "<=") (mgeqp ">=")
52 (mnotequal "#") (mand "and") (mor "or") (mnot "not") (msetq ":")
53 (mdefine ":=") (mdefmacro "::=") (mquote "'") (mlist "[")
54 (mset "::") (mfactorial "!") (marrow "-->") (mprogn "(")
55 (mcond "if") (mdo "do") (mdoin "do_in")))
57 (mapc #'(lambda (x) (putprop (car x) (cadr x) 'op))
58 '((mqapply $subvar) (bigfloat $bfloat)))
60 (defmvar $vect_cross nil
61 "If TRUE allows DIFF(X~Y,T) to work where ~ is defined in
62 SHARE;VECT where VECT_CROSS is set to TRUE.")
64 (defmfun $substitute (new old &optional (expr nil three-arg?))
65 (cond (three-arg? (maxima-substitute new old expr))
67 (let ((l new) (z old))
68 (cond ((and ($listp l) ($listp (cadr l)) (null (cddr l)))
69 ($substitute (cadr l) z))
70 ((notloreq l) (improper-arg-err l '$substitute))
71 ((eq (caar l) 'mequal) (maxima-substitute (caddr l) (cadr l) z))
72 (t (do ((l (cdr l) (cdr l)))
73 ((null l) z)
74 (setq z ($substitute (car l) z)))))))))
76 ;; Define an alias $psubst and a reversealias for $psubstitute
77 (defprop $psubst $psubstitute alias)
78 (defprop $psubstitute $psubst reversealias)
80 ;; $psubstitute is similar to $substitute. In distinction from $substitute
81 ;; the function $psubstitute does parallel substitution, if the first argument
82 ;; is a list of equations.
83 (defmfun $psubstitute (old new &optional (expr nil three-arg?))
84 (cond (three-arg? (maxima-substitute old new expr))
86 (let ((l old) (z new))
87 (cond ((and ($listp l)
88 ($listp (cadr l))
89 (null (cddr l)))
90 ;; A nested list.
91 ($psubstitute (cadr l) z))
92 ((and ($listp l)
93 (eq (caar (cadr l)) 'mequal)
94 (null (cddr l)))
95 ;; A list with one equation.
96 ($psubstitute (cadr l) z))
97 ((notloreq l) (improper-arg-err l '$psubstitute))
98 ((eq (caar l) 'mequal)
99 ;; Do a substitution for one equation.
100 (maxima-substitute (caddr l) (cadr l) z))
102 ;; We have a list of equations. We do parallel substitution.
103 (let (gensymbol genlist eqn ($simp nil))
104 ;; At first substitute a gensym for the expressions of
105 ;; the left hand side of the equations.
106 (do ((l (cdr l) (cdr l)))
107 ((null l) z)
108 (setq eqn (car l))
109 (when (not (eq 'mequal (caar eqn)))
110 (improper-arg-err old '$substitute))
111 (setq gensymbol (gensym))
112 ;; Store the gensym and the new expression into a list.
113 (push (cons gensymbol (caddr eqn)) genlist)
114 ;; Substitute a gensym for the old expression.
115 (setq z (maxima-substitute gensymbol (cadr eqn) z)))
116 ;; Substitute the new expressions for the gensyms.
117 (do ((l genlist (cdr l)))
118 ((null l)
119 ;; Resimplify the result.
120 (let (($simp t)) (resimplify z)))
121 (setq z (maxima-substitute (cdar l) (caar l) z))))))))))
123 (defun maxima-substitute (x y z) ; The args to SUBSTITUTE are assumed to be simplified.
124 ;; Prevent replacing dependent variable with constant in derivative
125 (cond ((and (not (atom z)) (eq (caar z) '%derivative) (eq (cadr z) y) (typep x 'number)) z)
127 (let ((in-p t) (substp t))
128 (if (and (mnump y) (= (signum1 y) 1))
129 (let ($sqrtdispflag ($pfeformat t)) (setq z (nformat-all z))))
130 (simplifya
131 (if (atom y)
132 (cond ((equal y -1)
133 (setq y '((mminus) 1)) (subst2 x y (nformat-all z) nil nil)) ;; negxpty and timesp don't matter in this call since (caar y) != 'mexpt
135 (cond ((and (not (symbolp x))
136 (functionp x))
137 (let ((tem (gensym)))
138 (setf (get tem 'operators) 'application-operator)
139 (setf (symbol-function tem) x)
140 (setq x tem))))
141 (subst1 x y z)))
142 (let ((negxpty (if (and (eq (caar y) 'mexpt)
143 (= (signum1 (caddr y)) 1))
144 (mul2 -1 (caddr y))))
145 (timesp (if (eq (caar y) 'mtimes) (setq y (nformat y)))))
146 (subst2 x y z negxpty timesp)))
147 nil)))
150 ;;Remainder of page is update from F302 --gsb
152 (defun subst1 (x y z) ; Y is an atom
153 (cond ((atom z) (if (equal y z) x z))
154 ((specrepp z) (subst1 x y (specdisrep z)))
155 ((eq (caar z) 'bigfloat) z)
156 ((and (eq (caar z) 'rat) (or (equal y (cadr z)) (equal y (caddr z))))
157 (div (subst1 x y (cadr z)) (subst1 x y (caddr z))))
158 ((at-substp z) (subst-except-second-arg x y z))
159 ((and (eq y t) (eq (caar z) 'mcond))
160 (list (cons (caar z) nil) (subst1 x y (cadr z)) (subst1 x y (caddr z))
161 (cadddr z) (subst1 x y (car (cddddr z)))))
162 (t (let ((margs (mapcar #'(lambda (z1) (subst1 x y z1)) (cdr z)))
163 (oprx (getopr x)) (opry (getopr y)))
164 (if (and $opsubst
165 (or (eq opry (caar z))
166 (and (eq (caar z) 'rat) (eq opry 'mquotient))))
167 (if (or (numberp x)
168 (member x '(t nil $%e $%pi $%i) :test #'eq)
169 (and (not (atom x))
170 (not (or (eq (car x) 'lambda)
171 (eq (caar x) 'lambda)))))
172 (if (or (and (member 'array (cdar z) :test #'eq)
173 (or (and (mnump x) $subnumsimp)
174 (and (not (mnump x)) (not (atom x)))))
175 ($subvarp x))
176 (let ((substp 'mqapply))
177 (subst0 (list* '(mqapply) x margs) z))
178 (merror (intl:gettext "subst: cannot substitute ~M for operator ~M in expression ~M") x y z))
179 (subst0 (cons (cons oprx nil) margs) z))
180 (subst0 (cons (cons (caar z) nil) margs) z))))))
182 (defun subst2 (x y z negxpty timesp)
183 (let (newexpt)
184 (cond ((atom z) z)
185 ((specrepp z) (subst2 x y (specdisrep z) negxpty timesp))
186 ((at-substp z) z) ;; IS SUBST-EXCEPT-SECOND-ARG APPROPRIATE HERE ?? !!
187 ((alike1 y z) x)
188 ((and timesp (eq (caar z) 'mtimes) (alike1 y (setq z (nformat z)))) x)
189 ((and (eq (caar y) 'mexpt) (eq (caar z) 'mexpt) (alike1 (cadr y) (cadr z))
190 (setq newexpt (cond ((alike1 negxpty (caddr z)) -1)
191 ($exptsubst (expthack (caddr y) (caddr z))))))
192 (list '(mexpt) x newexpt))
193 ((and $derivsubst (eq (caar y) '%derivative) (eq (caar z) '%derivative)
194 (alike1 (cadr y) (cadr z)))
195 (let ((tail (subst-diff-match (cddr y) (cdr z))))
196 (cond ((null tail) z)
197 (t (cons (cons (caar z) nil) (cons x (cdr tail)))))))
198 (t (recur-apply #'(lambda (z1) (subst2 x y z1 negxpty timesp)) z)))))
200 ;; replace y with x in z, but leave z's second arg unchanged.
201 ;; This is for cases like at(integrate(x, x, a, b), [x=3])
202 ;; where second arg of integrate binds a new variable x,
203 ;; and we do not wish to subst 3 for x inside integrand.
204 (defun subst-except-second-arg (x y z)
205 (cond
206 ((member (caar z) '(%integrate %sum %product %limit %laplace))
207 (append
208 (list (remove 'simp (car z)) ; ensure resimplification after substitution
209 (if (eq y (third z)) ; if (third z) is new var that shadows y
210 (second z) ; leave (second z) unchanged
211 (subst1 x y (second z))) ; otherwise replace y with x in (second z)
212 (third z)) ; never change integration var
213 (mapcar (lambda (z) (subst1 x y z)); do subst in limits of integral
214 (cdddr z))))
215 ((eq (caar z) '%at)
216 ;; similar considerations here, but different structure of expression.
217 (let*
218 ((at-eqn-or-eqns (third z))
219 (at-eqns-list (if (eq (caar at-eqn-or-eqns) 'mlist) (rest at-eqn-or-eqns) (list at-eqn-or-eqns))))
220 (list
221 (remove 'simp (car z)) ;; ensure resimplification after substitution
222 (if (member y (mapcar #'(lambda (e) (second e)) at-eqns-list))
223 (second z)
224 (subst1 x y (second z)))
225 `((mlist) ,@(mapcar #'(lambda (e) (list (first e) (second e) (subst1 x y (third e)))) at-eqns-list)))))
226 ((eq (caar z) '%derivative)
227 ;; and again, with yet a different structure.
228 (let*
229 ((vars-and-degrees (rest (rest z)))
230 (diff-vars (loop for v in vars-and-degrees by #'cddr collect v))
231 (diff-degrees (loop for n in (rest vars-and-degrees) by #'cddr collect n)))
232 (append
233 (list
234 (remove 'simp (car z)) ;; ensure resimplification after substitution
235 (if (member y diff-vars)
236 (second z)
237 (subst1 x y (second z))))
238 (apply #'append (loop for v in diff-vars for n in diff-degrees collect (list v (subst1 x y n)))))))
239 (t z)))
241 (defun subst0 (new old)
242 (cond ((atom new) new)
243 ((alike (cdr new) (cdr old))
244 (cond ((eq (caar new) (caar old)) old)
245 (t (simplifya (cons (cons (caar new) (member 'array (cdar old) :test #'eq)) (cdr old))
246 nil))))
247 ((member 'array (cdar old) :test #'eq)
248 (simplifya (cons (cons (caar new) '(array)) (cdr new)) nil))
249 (t (simplifya new nil))))
251 (defun expthack (y z)
252 (prog (nn* dn* yn yd zn zd qd)
253 (cond ((and (mnump y) (mnump z))
254 (return (if (numberp (setq y (div* z y))) y)))
255 ((atom z) (if (not (mnump y)) (return nil)))
256 ((or (ratnump z) (eq (caar z) 'mplus)) (return nil)))
257 (numden y) ; (CSIMP) sets NN* and DN*
258 (setq yn nn* yd dn*)
259 (numden z)
260 (setq zn nn* zd dn*)
261 (setq qd (cond ((and (equal zd 1) (equal yd 1)) 1)
262 ((prog2 (numden (div* zd yd))
263 (and (equal dn* 1) (equal nn* 1)))
265 ((equal nn* 1) (div* 1 dn*))
266 ((equal dn* 1) nn*)
267 (t (return nil))))
268 (numden (div* zn yn))
269 (if (equal dn* 1) (return (div* nn* qd)))))
271 (defun subst-diff-match (l1 l2)
272 (do ((l l1 (cddr l)) (l2 (copy-list l2)) (failed nil nil))
273 ((null l) l2)
274 (do ((l2 l2 (cddr l2)))
275 ((null (cdr l2)) (setq failed t))
276 (if (alike1 (car l) (cadr l2))
277 (if (and (fixnump (cadr l)) (fixnump (caddr l2)))
278 (cond ((< (cadr l) (caddr l2))
279 (return (rplacd (cdr l2)
280 (cons (- (caddr l2) (cadr l))
281 (cdddr l2)))))
282 ((= (cadr l) (caddr l2))
283 (return (rplacd l2 (cdddr l2))))
284 (t (return (setq failed t))))
285 (return (setq failed t)))))
286 (if failed (return nil))))
288 ;;This probably should be a subst or macro.
289 (defun at-substp (z)
290 (and *atp*
291 (or (member (caar z) '(%derivative %del) :test #'eq)
292 (member (caar z) dummy-variable-operators :test #'eq))))
294 (defun recur-apply (fun e)
295 (cond ((eq (caar e) 'bigfloat) e)
296 ((specrepp e) (funcall fun (specdisrep e)))
297 (t (let ((newargs (mapcar fun (cdr e))))
298 (if (alike newargs (cdr e))
300 (simplifya (cons (cons (caar e) (member 'array (cdar e) :test #'eq)) newargs)
301 nil))))))
303 (defmfun $depends (&rest args)
304 (when (oddp (length args))
305 (merror (intl:gettext "depends: number of arguments must be even.")))
306 (do ((args args (cddr args))
307 (l))
308 ((null args) (i-$dependencies (nreverse l)))
309 (if ($listp (first args))
310 (mapc #'(lambda (e) (push (depends1 e (second args)) l))
311 (cdr (first args)))
312 (push (depends1 (first args) (second args)) l))))
314 (defun depends1 (x y)
315 (nonsymchk x '$depends)
316 (cons (cons x nil) (if ($listp y) (cdr y) (cons y nil))))
318 (defmspec $dependencies (form)
319 (i-$dependencies (cdr form)))
321 (defun i-$dependencies (l &aux res)
322 (dolist (z l)
323 (cond
324 ((atom z)
325 (merror
326 (intl:gettext
327 "depends: argument must be a non-atomic expression; found ~M") z))
329 (do ((zz z (cdr zz))
330 (y nil))
331 ((null zz)
332 (mputprop (caar z) (setq y (reverse y)) 'depends)
333 (setq res (push (cons (ncons (caar z)) y) res))
334 (unless (cdr $dependencies)
335 (setq $dependencies (copy-list '((mlist simp)))))
336 (add2lnc (cons (cons (caar z) nil) y) $dependencies))
337 (cond ((and ($subvarp (cadr zz))
338 (not (member (caar (cadr zz)) y)))
339 (setq y (push (cadr zz) y)))
340 ((not (symbolp (cadr zz)))
341 (merror
342 (intl:gettext "depends: argument must be a symbol; found ~M")
343 (cadr zz)))
344 ((and (cadr zz)
345 (not (member (cadr zz) y)))
346 (setq y (push (cadr zz) y))))))))
347 (cons '(mlist simp) (reverse res)))
349 (defmspec $gradef (l)
350 (setq l (cdr l))
351 (let ((z (car l)) (n 0))
352 (cond ((atom z)
353 (if (not (= (length l) 3)) (merror (intl:gettext "gradef: expected exactly three arguments.")))
354 (mputprop z
355 (cons (cons (cadr l) (meval (caddr l)))
356 (mget z '$atomgrad))
357 '$atomgrad)
358 (i-$dependencies (cons (cons (ncons z)
359 ;; Append existing dependencies
360 (cons (cadr l) (mget z 'depends)))
361 nil))
362 (add2lnc z $props)
364 ((or (mopp1 (caar z)) (member 'array (cdar z) :test #'eq))
365 (merror (intl:gettext "gradef: argument cannot be a built-in operator or subscripted expression; found ~M") z))
366 ((prog2 (setq n (- (length z) (length l))) (minusp n))
367 (wna-err '$gradef))
368 (t (do ((zl (cdr z) (cdr zl))) ((null zl))
369 (if (not (symbolp (car zl)))
370 (merror (intl:gettext "gradef: argument must be a symbol; found ~M") (car zl))))
371 (setq l (nconc (mapcar #'(lambda (x) (remsimp (meval x)))
372 (cdr l))
373 (mapcar #'(lambda (x) (list '(%derivative) z x 1))
374 (nthcdr (- (length z) n) z))))
375 (putprop (caar z)
376 (sublis (mapcar #'cons (cdr z) (mapcar #'stripdollar (cdr z)))
377 (cons (cdr z) l))
378 'grad)
379 (or (cdr $gradefs) (setq $gradefs (copy-list '((mlist simp)))))
380 (add2lnc (cons (cons (caar z) nil) (cdr z)) $gradefs) z))))
382 (defmfun $diff (&rest args)
383 (declare (dynamic-extent args))
384 (let (derivlist)
385 (deriv args)))
387 (defmfun $del (e)
388 (stotaldiff e))
390 (defun deriv (e)
391 (prog (exp z count)
392 (cond ((null e) (wna-err '$diff))
393 ((null (cdr e)) (return (stotaldiff (car e))))
394 ((null (cddr e)) (nconc e '(1))))
395 (setq exp (car e) z (setq e (copy-list e)))
396 loop (if (or (null derivlist) (member (cadr z) derivlist :test #'equal)) (go doit))
397 ; DERIVLIST is set by $EV
398 (setq z (cdr z))
399 loop2(cond ((cdr z) (go loop))
400 ((null (cdr e)) (return exp))
401 (t (go noun)))
402 doit (cond ((nonvarcheck (cadr z) '$diff))
403 ((null (cddr z)) (wna-err '$diff))
404 ((not (fixnump (caddr z))) (go noun))
405 ((minusp (setq count (caddr z)))
406 (merror (intl:gettext "diff: order of derivative must be a nonnegative integer; found ~M") count)))
407 loop1(cond ((zerop count) (rplacd z (cdddr z)) (go loop2))
408 ((equal (setq exp (sdiff exp (cadr z))) 0) (return 0)))
409 (setq count (1- count))
410 (go loop1)
411 noun (return (diff%deriv (cons exp (cdr e))))))
413 (defun chainrule (e x)
414 (let (w)
415 (cond (*islinp*
416 ;; sdiff is called from the function islinear.
417 (if (and (not (atom e))
418 (eq (caar e) '%derivative)
419 (not (freel (cdr e) x)))
420 (diff%deriv (list e x 1))
422 ((atomgrad e x))
423 ((not (setq w (mget (cond ((atom e) e)
424 ((member 'array (cdar e) :test #'eq) (caar e))
425 ((atom (cadr e)) (cadr e))
426 (t (caaadr e)))
427 'depends)))
429 (t (let (derivflag)
430 (addn (mapcar
431 #'(lambda (u)
432 (let ((y (sdiff u x)))
433 (if (equal y 0)
435 (list '(mtimes)
436 (or (atomgrad e u)
437 (list '(%derivative) e u 1))
438 y))))
440 nil))))))
442 (defun atomgrad (e x)
443 (let (y)
444 (and (atom e) (setq y (mget e '$atomgrad)) (assolike x y))))
446 (defun depends (e x &aux l)
447 (setq e (specrepcheck e))
448 (cond ((alike1 e x) t)
449 ((mnump e) nil)
450 ((and (symbolp e) (setq l (mget e 'depends)))
451 ;; Go recursively through the list of dependencies.
452 ;; This code detects indirect dependencies like a(x) and x(t).
453 (dependsl l x))
454 ((atom e) nil)
455 (t (or (depends (caar e) x)
456 (dependsl (cdr e) x)))))
458 (defun dependsl (l x)
459 (dolist (u l)
460 (if (depends u x) (return t))))
462 (defun sdiff (e x) ; The args to SDIFF are assumed to be simplified.
463 ;; Remove a special representation from the variable of differentiation
464 (setq x (specrepcheck x))
465 (cond ((alike1 e x) 1)
466 ((mnump e) 0)
467 ((or (atom e) (member 'array (cdar e) :test #'eq)) (chainrule e x))
468 ((eq (caar e) 'mrat) (ratdx e x))
469 ((eq (caar e) 'mpois) ($poisdiff e x)) ; Poisson series
470 ((eq (caar e) 'mplus) (addn (sdiffmap (cdr e) x) t))
471 ((mbagp e) (cons (car e) (sdiffmap (cdr e) x)))
472 ((member (caar e) '(%sum %product) :test #'eq) (diffsumprod e x))
473 ((eq (caar e) '%at) (diff-%at e x))
474 ((not (depends e x)) 0)
475 ((eq (caar e) 'mtimes) (addn (sdifftimes (cdr e) x) t))
476 ((eq (caar e) 'mexpt) (diffexpt e x))
477 ((eq (caar e) 'mnctimes)
478 (let (($dotdistrib t))
479 (add2 (ncmuln (cons (sdiff (cadr e) x) (cddr e)) t)
480 (ncmul2 (cadr e) (sdiff (cons '(mnctimes) (cddr e)) x)))))
481 ((and $vect_cross (eq (caar e) '|$~|))
482 (add2* `((|$~|) ,(cadr e) ,(sdiff (caddr e) x))
483 `((|$~|) ,(sdiff (cadr e) x) ,(caddr e))))
484 ((eq (caar e) 'mncexpt) (diffncexpt e x))
485 ((member (caar e) '(%log %plog) :test #'eq)
486 (sdiffgrad (cond ((and (not (atom (cadr e))) (eq (caaadr e) 'mabs))
487 (cons (car e) (cdadr e)))
488 (t e))
490 ((eq (caar e) '%derivative)
491 (cond ((or (atom (cadr e)) (member 'array (cdaadr e) :test #'eq)) (chainrule e x))
492 ((freel (cddr e) x) (diff%deriv (cons (sdiff (cadr e) x) (cddr e))))
493 (t (diff%deriv (list e x 1)))))
494 ((member (caar e) '(%binomial $beta) :test #'eq)
495 (let ((efact ($makefact e)))
496 (mul2 (factor (sdiff efact x)) (div e efact))))
497 ((eq (caar e) '%integrate) (diffint e x))
498 ((eq (caar e) '%laplace) (difflaplace e x))
499 ((eq (caar e) '%at) (diff-%at e x))
500 ; This rule is not correct. We cut it out.
501 ; ((member (caar e) '(%realpart %imagpart) :test #'eq)
502 ; (list (cons (caar e) nil) (sdiff (cadr e) x)))
503 ((and (eq (caar e) 'mqapply)
504 (eq (caaadr e) '$%f))
505 ;; Handle %f, hypergeometric function
507 ;; The derivative of %f[p,q]([a1,...,ap],[b1,...,bq],z) is
509 ;; a1*a2*...*ap/(b1*b2*...*bq)
510 ;; *%f[p,q]([a1+1,a2+1,...,ap+1],[b1+1,b2+1,...,bq+1],z)
511 (let* ((arg1 (cdr (third e)))
512 (arg2 (cdr (fourth e)))
513 (v (fifth e)))
514 (mul (sdiff v x)
515 (div (mull arg1) (mull arg2))
516 `((mqapply) (($%f array) ,(length arg1) ,(length arg2))
517 ((mlist) ,@(incr1 arg1))
518 ((mlist) ,@(incr1 arg2))
519 ,v))))
520 (t (sdiffgrad e x))))
522 (defun sdiffgrad (e x)
523 (let ((fun (caar e)) grad args result)
524 (cond ((and (eq fun 'mqapply) (zl-get (caaadr e) 'grad))
525 ;; Change the array function f[n](x) to f(n,x), call sdiffgrad again.
526 (setq result
527 (sdiffgrad (cons (cons (caaadr e) nil)
528 (append (cdadr e) (cddr e)))
530 ;; If noun form for f(n,x), adjust the noun form for f[n](x)
531 (if (isinop result '%derivative)
532 (if (not (depends e x))
534 (diff%deriv (list e x 1)))
535 result))
537 ;; extension for pdiff.
538 ((and (get '$pderivop 'operators) (funcall 'sdiffgrad-pdiff e x)))
540 ;; two line extension for hypergeometric.
541 ((and (equal fun '%hypergeometric) (get '%hypergeometric 'operators))
542 (funcall 'diff-hypergeometric (second e) (third e) (fourth e) x))
544 ((or (eq fun 'mqapply) (null (setq grad (zl-get fun 'grad))))
545 (if (not (depends e x)) 0 (diff%deriv (list e x 1))))
546 ((not (= (length (cdr e)) (length (car grad))))
547 (merror (intl:gettext "~:M: expected exactly ~M arguments.")
548 fun
549 (length (car grad))))
551 (setq args (sdiffmap (cdr e) x))
552 (setq result
553 (addn
554 (mapcar
555 #'mul2
556 (cdr
557 (substitutel
558 (cdr e)
559 (car grad)
560 (do ((l1 (cdr grad) (cdr l1))
561 (args args (cdr args))
562 (l2))
563 ((null l1) (cons '(mlist) (nreverse l2)))
564 (setq l2
565 (cons (cond ((equal (car args) 0) 0)
566 ((functionp (car l1))
567 ;; Evaluate a lambda expression
568 ;; given as a derivative.
569 (apply (car l1) (cdr e)))
570 (t (car l1)))
571 l2)))))
572 args)
574 (if (or (null result) (not (freeof nil result)))
575 ;; A derivative has returned NIL. Return a noun form.
576 (if (not (depends e x))
578 (diff%deriv (list e x 1)))
579 result)))))
581 (defun sdiffmap (e x)
582 (mapcar #'(lambda (term) (sdiff term x)) e))
584 (defun sdifftimes (l x)
585 (prog (term left out)
586 loop (setq term (car l) l (cdr l))
587 (setq out (cons (muln (cons (sdiff term x) (append left l)) t) out))
588 (if (null l) (return out))
589 (setq left (cons term left))
590 (go loop)))
592 (defun diffexpt (e x)
593 (if (mnump (caddr e))
594 (mul3 (caddr e) (power (cadr e) (addk (caddr e) -1)) (sdiff (cadr e) x))
595 (mul2 e (add2 (mul3 (power (cadr e) -1) (caddr e) (sdiff (cadr e) x))
596 (mul2 (simplifya (list '(%log) (cadr e)) t)
597 (sdiff (caddr e) x))))))
599 (defun diff%deriv (e)
600 (let (derivflag)
601 (simplifya (cons '(%derivative) e) t)))
604 ;; grad properties
606 (let ((header '(x)))
607 (mapc #'(lambda (z) (putprop (car z) (cons header (cdr z)) 'grad))
608 ;; All these GRAD templates have been simplified and then the SIMP flags
609 ;; (which are unnecessary) have been removed to save core space.
610 '((%log ((mexpt) x -1)) (%plog ((mexpt) x -1))
611 (%gamma ((mtimes) ((mqapply) (($psi array) 0) x) ((%gamma) x)))
612 (mfactorial ((mtimes) ((mqapply) (($psi array) 0) ((mplus) 1 x)) ((mfactorial) x)))
613 (%sin ((%cos) x))
614 (%cos ((mtimes) -1 ((%sin) x)))
615 (%tan ((mexpt) ((%sec) x) 2))
616 (%cot ((mtimes) -1 ((mexpt) ((%csc) x) 2)))
617 (%sec ((mtimes) ((%sec) x) ((%tan) x)))
618 (%csc ((mtimes) -1 ((%cot) x) ((%csc) x)))
619 (%asin ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x 2))) ((rat) -1 2)))
620 (%acos ((mtimes) -1 ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x 2)))
621 ((rat) -1 2))))
622 (%atan ((mexpt) ((mplus) 1 ((mexpt) x 2)) -1))
623 (%acot ((mtimes) -1 ((mexpt) ((mplus) 1 ((mexpt) x 2)) -1)))
624 (%acsc ((mtimes) -1
625 ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x -2)))
626 ((rat) -1 2))
627 ((mexpt) x -2)))
628 (%asec ((mtimes)
629 ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x -2)))
630 ((rat) -1 2))
631 ((mexpt) x -2)))
632 (%sinh ((%cosh) x))
633 (%cosh ((%sinh) x))
634 (%tanh ((mexpt) ((%sech) x) 2))
635 (%coth ((mtimes) -1 ((mexpt) ((%csch) x) 2)))
636 (%sech ((mtimes) -1 ((%sech) x) ((%tanh) x)))
637 (%csch ((mtimes) -1 ((%coth) x) ((%csch) x)))
638 (%asinh ((mexpt) ((mplus) 1 ((mexpt) x 2)) ((rat) -1 2)))
639 (%acosh ((mexpt) ((mplus) -1 ((mexpt) x 2)) ((rat) -1 2)))
640 (%atanh ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x 2))) -1))
641 (%acoth ((mtimes) -1 ((mexpt) ((mplus) -1 ((mexpt) x 2)) -1)))
642 (%asech ((mtimes) -1
643 ((mexpt) ((mplus) -1 ((mexpt) x -2)) ((rat) -1 2))
644 ((mexpt) x -2)))
645 (%acsch ((mtimes) -1
646 ((mexpt) ((mplus) 1 ((mexpt) x -2)) ((rat) -1 2))
647 ((mexpt) x -2)))
648 (mabs ((mtimes) x ((mexpt) ((mabs) x) -1)))
649 (%erf ((mtimes) 2 ((mexpt) $%pi ((rat) -1 2))
650 ((mexpt) $%e ((mtimes) -1 ((mexpt) x 2)))))
653 (defprop $atan2 ((x y) ((mtimes) y ((mexpt) ((mplus) ((mexpt) x 2) ((mexpt) y 2)) -1))
654 ((mtimes) -1 x ((mexpt) ((mplus) ((mexpt) x 2) ((mexpt) y 2)) -1)))
655 grad)
657 (defprop $li
658 ((n x)
659 ; Do not put a noun form on the property list, but NIL.
660 ; SDIFFGRAD generates the noun form.
661 ; ((%derivative) ((mqapply) (($li array) n) x) n 1)
663 ((mtimes) ((mqapply) (($li array) ((mplus) -1 n)) x) ((mexpt) x -1)))
664 grad)
666 (defprop $psi
667 ((n x)
668 ; Do not put a noun form on the property list, but NIL.
669 ; SDIFFGRAD generates the noun form.
671 ((mqapply) (($psi array) ((mplus) 1 n)) x))
672 grad)
674 (defun atvarschk (argl)
675 (do ((largl (length argl) (1- largl))
676 (latvrs (length atvars))
677 (l))
678 ((not (< latvrs largl)) (nconc atvars l))
679 (setq l (cons (implode (cons '$ (cons '@ (mexploden largl)))) l))))
681 (defun notloreq (x)
682 (or (atom x)
683 (not (member (caar x) '(mlist mequal) :test #'eq))
684 (and (eq (caar x) 'mlist)
685 (dolist (u (cdr x))
686 (if (not (mequalp u)) (return t))))))
688 (defun substitutel (l1 l2 e)
689 "l1 is a list of expressions. l2 is a list of variables. For each
690 element in list l2, substitute corresponding element of l1 into e"
691 (do ((l1 l1 (cdr l1))
692 (l2 l2 (cdr l2)))
693 ((null l1) e)
694 (setq e (maxima-substitute (car l1) (car l2) e))))
696 (defun union* (a b)
697 (do ((a a (cdr a))
698 (x b))
699 ((null a) x)
700 (if (not (memalike (car a) b)) (setq x (cons (car a) x)))))
702 (defun intersect* (a b)
703 (do ((a a (cdr a))
704 (x))
705 ((null a) x)
706 (if (memalike (car a) b) (setq x (cons (car a) x)))))
708 (defun nthelem (n e)
709 (car (nthcdr (1- n) e)))
711 (defun delsimp (e) (remove 'simp e))
713 (defun remsimp (e)
714 (if (atom e) e (cons (delsimp (car e)) (mapcar #'remsimp (cdr e)))))
716 (defmfun $trunc (e)
717 (cond ((atom e) e)
718 ((eq (caar e) 'mplus) (cons (append (car e) '(trunc)) (cdr e)))
719 ((mbagp e) (cons (car e) (mapcar #'$trunc (cdr e))))
720 ((specrepp e) ($trunc (specdisrep e)))
721 (t e)))
723 (defun nonvarcheck (e fn)
724 (if (or (mnump e)
725 (maxima-integerp e)
726 ($constantp e)
727 (and (not (atom e)) (not (eq (caar e) 'mqapply)) (mopp1 (caar e))))
728 (merror (intl:gettext "~:M: second argument must be a variable; found ~M") fn e)))
730 (defmspec $ldisplay (form)
731 (disp1 (cdr form) t t))
733 (defmfun $ldisp (&rest args)
734 (declare (dynamic-extent args))
735 (disp1 args t nil))
737 (defmspec $display (form)
738 (disp1 (cdr form) nil t))
740 (defmfun $disp (&rest args)
741 (declare (dynamic-extent args))
742 (disp1 args nil nil))
744 (defun disp1 (ll lablist eqnsp)
745 (if lablist (setq lablist (cons '(mlist simp) nil)))
746 (do ((ll ll (cdr ll))
748 (ans)
749 ($dispflag t)
750 (tim 0))
751 ((null ll) (or lablist '$done))
752 (setq l (car ll) ans (if eqnsp (meval l) l))
753 (if (and eqnsp (not (mequalp ans)))
754 (setq ans (list '(mequal simp) (disp2 l) ans)))
755 (if lablist (nconc lablist (cons (elabel ans) nil)))
756 (setq tim (get-internal-run-time))
757 (let ((*display-labels-p* (not (null lablist))))
758 (displa (list '(mlabel) (if lablist *linelabel*) ans)))
759 (mterpri)
760 (timeorg tim)))
762 (defun disp2 (e)
763 (cond ((atom e) e)
764 ((eq (caar e) 'mqapply)
765 (cons '(mqapply) (cons (cons (caadr e) (mapcar #'meval (cdadr e)))
766 (mapcar #'meval (cddr e)))))
767 ((eq (caar e) 'msetq) (disp2 (cadr e)))
768 ((eq (caar e) 'mset) (disp2 (meval (cadr e))))
769 ((eq (caar e) 'mlist) (cons (car e) (mapcar #'disp2 (cdr e))))
770 ((mspecfunp (caar e)) e)
771 (t (cons (car e) (mapcar #'meval (cdr e))))))
773 ; Construct a new intermediate result label,
774 ; and bind it to the expression e.
775 ; The global flag $NOLABELS is ignored; the label is always bound.
776 ; Otherwise (if ELABEL were to observe $NOLABELS) it would be
777 ; impossible to programmatically refer to intermediate result expression.
779 (defun elabel (e)
780 (if (not (checklabel $linechar)) (setq $linenum (1+ $linenum)))
781 (let (($nolabels nil)) ; <-- This is pretty ugly. MAKELABEL should take another argument.
782 (makelabel $linechar))
783 (setf (symbol-value *linelabel*) e)
784 *linelabel*)
786 (defmfun $dispterms (e)
787 (cond ((or (atom e) (eq (caar e) 'bigfloat)) (displa e))
788 ((specrepp e) ($dispterms (specdisrep e)))
789 (t (let (($dispflag t))
790 (mterpri)
791 (displa (getop (mop e)))
792 (do ((e (if (and (eq (caar e) 'mplus) (not $powerdisp))
793 (reverse (cdr e))
794 (margs e))
795 (cdr e))) ((null e)) (mterpri) (displa (car e)) (mterpri)))
796 (mterpri)))
797 '$done)
799 (defmfun $dispform (e &optional (flag nil flag?))
800 (when (and flag? (not (eq flag '$all)))
801 (merror (intl:gettext "dispform: second argument, if present, must be 'all'; found ~M") flag))
802 (if (or (atom e)
803 (atom (setq e (if flag? (nformat-all e) (nformat e))))
804 (member 'simp (cdar e) :test #'eq))
806 (cons (cons (caar e) (cons 'simp (cdar e)))
807 (if (and (eq (caar e) 'mplus) (not $powerdisp))
808 (reverse (cdr e))
809 (cdr e)))))
811 ;;; These functions implement the Macsyma functions $op and $operatorp.
812 ;;; Dan Stanger
813 (defmfun $op (expr)
814 ($part expr 0))
816 (defmfun $operatorp (expr oplist)
817 (if ($listp oplist)
818 ($member ($op expr) oplist)
819 (alike1 ($op expr) oplist)))
821 (defmfun $part (&rest args)
822 (declare (dynamic-extent args))
823 (mpart args nil nil $inflag '$part))
825 (defmfun $inpart (&rest args)
826 (declare (dynamic-extent args))
827 (mpart args nil nil t '$inpart))
829 (defmspec $substpart (l)
830 (let ((substp t))
831 (mpart (cdr l) t nil $inflag '$substpart)))
833 (defmspec $substinpart (l)
834 (let ((substp t))
835 (mpart (cdr l) t nil t '$substinpart)))
837 (defun part1 (arglist substflag dispflag inflag fn) ; called only by TRANSLATE
838 (let ((substp t))
839 (mpart arglist substflag dispflag inflag fn)))
841 (defun mpart (arglist substflag dispflag inflag fn)
842 (prog (substitem arg arg1 exp exp1 exp* sevlist count prevcount n specp
843 lastelem lastcount)
844 (setq specp (or substflag dispflag))
845 (if substflag (setq substitem (car arglist) arglist (cdr arglist)))
846 (if (null arglist) (wna-err fn))
847 (setq exp (if substflag (meval (car arglist)) (car arglist)))
848 (when (null (setq arglist (cdr arglist)))
849 (setq $piece exp)
850 (return (cond (substflag (meval substitem))
851 (dispflag (box exp dispflag))
852 (t exp))))
853 (cond ((not inflag)
854 (cond ((or (and ($listp exp) (null (cdr arglist)))
855 (and ($matrixp exp)
856 (or (null (cdr arglist)) (null (cddr arglist)))))
857 (setq inflag t))
858 ((not specp) (setq exp (nformat exp)))
859 (t (setq exp (nformat-all exp)))))
860 ((specrepp exp) (setq exp (specdisrep exp))))
861 (when (and (atom exp) (null $partswitch))
862 (merror (intl:gettext "~:M: argument must be a non-atomic expression; found ~:M") fn exp))
863 (when (and inflag specp)
864 (setq exp (copy-tree exp)))
865 (when substflag
866 ;; Replace all occurrences of 'rat with 'mquotient when in subst.
867 (setq exp (let (($simp nil)) (maxima-substitute 'mquotient 'rat exp))))
868 (setq exp* exp)
869 start (cond ((or (atom exp) (eq (caar exp) 'bigfloat)) (go err))
870 ((equal (setq arg (if substflag (meval (car arglist)) (car arglist)))
872 (setq arglist (cdr arglist))
873 (cond ((mnump substitem)
874 (merror (intl:gettext "~:M: argument cannot be a number; found ~M") fn substitem))
875 ((and specp arglist)
876 (if (eq (caar exp) 'mqapply)
877 (prog2 (setq exp (cadr exp)) (go start))
878 ;; NOT CLEAR WHAT IS INVALID HERE. OH WELL.
879 (merror (intl:gettext "~:M: invalid operator.") fn)))
880 (t (setq $piece (getop (mop exp)))
881 (return
882 (cond (substflag
883 (setq substitem (getopr (meval substitem)))
884 (cond ((mnump substitem)
885 (merror (intl:gettext "~:M: argument cannot be a number; found ~M") fn substitem))
886 ((not (atom substitem))
887 (if (not (eq (caar exp) 'mqapply))
888 (rplaca (rplacd exp (cons (car exp)
889 (cdr exp)))
890 '(mqapply)))
891 (rplaca (cdr exp) substitem)
892 (return (resimplify exp*)))
893 ((eq (caar exp) 'mqapply)
894 (rplacd exp (cddr exp))))
895 (rplaca exp (cons substitem
896 (if (and (member 'array (cdar exp) :test #'eq)
897 (not (mopp substitem)))
898 '(array))))
899 (resimplify exp*))
900 (dispflag
901 (rplacd exp (cdr (box (copy-tree exp) dispflag)))
902 (rplaca exp (if (eq dispflag t)
903 '(mbox)
904 '(mlabox)))
905 (resimplify exp*))
906 (t (when arglist (setq exp $piece) (go a))
907 $piece))))))
908 ((not (atom arg)) (go several))
909 ((not (fixnump arg))
910 (merror (intl:gettext "~:M: argument must be an integer; found ~M") fn arg))
911 ((< arg 0) (go bad)))
912 (if (eq (caar exp) 'mqapply) (setq exp (cdr exp)))
913 loop (cond ((not (zerop arg)) (setq arg (1- arg) exp (cdr exp))
914 (if (null exp) (go err))
915 (go loop))
916 ((null (setq arglist (cdr arglist)))
917 (return (cond (substflag (setq $piece (resimplify (car exp)))
918 (rplaca exp (meval substitem))
919 (resimplify exp*))
920 (dispflag (setq $piece (resimplify (car exp)))
921 (rplaca exp (box (car exp) dispflag))
922 (resimplify exp*))
923 (inflag (setq $piece (car exp)))
924 (t (setq $piece (simplify (car exp))))))))
925 (setq exp (car exp))
926 a (cond ((and (not inflag) (not specp)) (setq exp (nformat exp)))
927 ((specrepp exp) (setq exp (specdisrep exp))))
928 (go start)
929 err (cond ((eq $partswitch 'mapply)
930 (merror (intl:gettext "~M: invalid index ~M of list or matrix.") fn (car arglist)))
931 ($partswitch (return (setq $piece '$end)))
932 (t (merror (intl:gettext "~:M: fell off the end.") fn)))
933 bad (improper-arg-err arg fn)
934 several
935 (if (or (not (member (caar arg) '(mlist $allbut) :test #'eq)) (cdr arglist))
936 (go bad))
937 (setq exp1 (cons (caar exp) (if (member 'array (cdar exp) :test #'eq) '(array))))
938 (if (eq (caar exp) 'mqapply)
939 (setq sevlist (list (cadr exp) exp1) exp (cddr exp))
940 (setq sevlist (ncons exp1) exp (cdr exp)))
941 (setq arg1 (cdr arg) prevcount 0 exp1 exp)
942 (dolist (arg* arg1)
943 (if (not (fixnump arg*))
944 (merror (intl:gettext "~:M: argument must be an integer; found ~M") fn arg*)))
945 (when (and specp (eq (caar arg) 'mlist))
946 (if substflag (setq lastelem (car (last arg1))))
947 (setq arg1 (sort (copy-list arg1) #'<)))
948 (when (eq (caar arg) '$allbut)
949 (setq n (length exp))
950 (dolist (i arg1)
951 (if (or (< i 1) (> i n))
952 (merror (intl:gettext "~:M: index must be in range 1 to ~M, inclusive; found ~M") fn n i)))
953 (do ((i n (1- i)) (arg2))
954 ((= i 0) (setq arg1 arg2))
955 (if (not (member i arg1 :test #'equal)) (setq arg2 (cons i arg2))))
956 (if substflag (setq lastelem (car (last arg1)))))
957 (if (null arg1) (if specp (go bad) (go end)))
958 (if substflag (setq lastcount lastelem))
959 sevloop
960 (if specp
961 (setq count (- (car arg1) prevcount) prevcount (car arg1))
962 (setq count (car arg1)))
963 (if (< count 1) (go bad))
964 (if (and substflag (< (car arg1) lastelem))
965 (setq lastcount (1- lastcount)))
966 count(cond ((null exp) (go err))
967 ((not (= count 1)) (setq count (1- count) exp (cdr exp)) (go count)))
968 (setq sevlist (cons (car exp) sevlist))
969 (setq arg1 (cdr arg1))
970 end (cond ((null arg1)
971 (setq sevlist (nreverse sevlist))
972 (setq $piece (if (or inflag (not specp))
973 (simplify sevlist)
974 (resimplify sevlist)))
975 (return (cond (substflag (rplaca (nthcdr (1- lastcount) exp1)
976 (meval substitem))
977 (resimplify exp*))
978 (dispflag (rplaca exp (box (car exp) dispflag))
979 (resimplify exp*))
980 (t $piece))))
981 (substflag (if (null (cdr exp)) (go err))
982 (rplaca exp (cadr exp)) (rplacd exp (cddr exp)))
983 (dispflag (rplaca exp (box (car exp) dispflag))
984 (setq exp (cdr exp)))
985 (t (setq exp exp1)))
986 (go sevloop)))
988 (defun getop (x)
989 (or (and (symbolp x) (get x 'op)) x))
991 (defmfun $listp (x)
992 (and (not (atom x))
993 (not (atom (car x)))
994 (eq (caar x) 'mlist)))
996 (defmfun $cons (x e)
997 (atomchk (setq e (format1 e)) '$cons t)
998 (mcons-exp-args e (cons x (margs e))))
1000 (defmfun $endcons (x e)
1001 (atomchk (setq e (format1 e)) '$endcons t)
1002 (mcons-exp-args e (append (margs e) (ncons x))))
1004 (defmfun $reverse (e)
1005 (atomchk (setq e (format1 e)) '$reverse nil)
1006 (mcons-exp-args e (reverse (margs e))))
1008 (defmfun $append (&rest args)
1009 (if (null args)
1010 '((mlist simp))
1011 (let ((arg1 (specrepcheck (first args))) op arrp)
1012 (atomchk arg1 '$append nil)
1013 (setq op (mop arg1)
1014 arrp (if (member 'array (cdar arg1) :test #'eq) t))
1015 (mcons-exp-args
1016 arg1
1017 (apply #'append
1018 (mapcar #'(lambda (u)
1019 (atomchk (setq u (specrepcheck u)) '$append nil)
1020 (unless (and (alike1 op (mop u))
1021 (eq arrp (if (member 'array (cdar u) :test #'eq) t)))
1022 (merror (intl:gettext "append: operators of arguments must all be the same.")))
1023 (margs u))
1024 args))))))
1026 (defun mcons-exp-args (e args)
1027 (if (eq (caar e) 'mqapply)
1028 (list* (delsimp (car e)) (cadr e) args)
1029 (cons (delsimp (car e)) args)))
1031 (defmfun $member (x e)
1032 (atomchk (setq e ($totaldisrep e)) '$member t)
1033 (if (memalike ($totaldisrep x) (margs e)) t))
1035 (defun atomchk (e fun 2ndp)
1036 (if (or (atom e) (eq (caar e) 'bigfloat))
1037 (merror (intl:gettext "~:M: ~Margument must be a non-atomic expression; found ~M") fun (if 2ndp "2nd " "") e)))
1039 (defun format1 (e)
1040 (cond (($listp e) e)
1041 ($inflag (specrepcheck e))
1042 (t (nformat e))))
1044 (defmfun $first (e)
1045 (atomchk (setq e (format1 e)) '$first nil)
1046 (if (null (cdr e)) (merror (intl:gettext "first: empty argument.")))
1047 (car (margs e)))
1049 ;; This macro is used to create functions second thru tenth.
1050 ;; Rather than try to modify mformat for ~:R, use the quoted symbol
1052 (macrolet ((make-nth (si i)
1053 (let ((sim (intern (concatenate 'string "$" (symbol-name si)))))
1054 `(defun ,sim (e)
1055 (atomchk (setq e (format1 e)) ',sim nil)
1056 (if (< (length (margs e)) ,i)
1057 (merror (intl:gettext "~:M: no such element in ~M") ',sim e))
1058 (,si (margs e))))))
1060 (make-nth second 2)
1061 (make-nth third 3)
1062 (make-nth fourth 4)
1063 (make-nth fifth 5)
1064 (make-nth sixth 6)
1065 (make-nth seventh 7)
1066 (make-nth eighth 8)
1067 (make-nth ninth 9)
1068 (make-nth tenth 10))
1070 (defmfun $rest (e &optional (n 1 n?))
1071 (prog (m fun fun1 revp)
1072 (when (and n? (equal n 0))
1073 (return e))
1074 (atomchk (setq m (format1 e)) '$rest nil)
1075 (cond ((and n? (not (fixnump n)))
1076 (merror (intl:gettext "rest: second argument, if present, must be an integer; found ~M") n))
1077 ((minusp n)
1078 (setq n (- n) revp t)))
1079 (if (< (length (margs m)) n)
1080 (if $partswitch
1081 (return '$end)
1082 (merror (intl:gettext "rest: fell off the end."))))
1083 (setq fun (car m))
1084 (when (eq (car fun) 'mqapply)
1085 (setq fun1 (cadr m)
1086 m (cdr m)))
1087 (setq m (cdr m))
1088 (when revp (setq m (reverse m)))
1089 (setq m (nthcdr n m))
1090 (setq m (cons (if (eq (car fun) 'mlist) fun (delsimp fun))
1091 (if revp (nreverse m) m)))
1092 (when (eq (car fun) 'mqapply)
1093 (return (cons (car m) (cons fun1 (cdr m)))))
1094 (return m)))
1096 (defmfun $last (e)
1097 (atomchk (setq e (format1 e)) '$last nil)
1098 (when (null (cdr e))
1099 (merror (intl:gettext "last: empty argument.")))
1100 (car (last e)))
1102 (defmfun $firstn (e n)
1103 (atomchk (setq e (format1 e)) '$firstn nil)
1104 (if (or (not (fixnump n)) (minusp n))
1105 (merror (intl:gettext "firstn: second argument must be a nonnegative integer; found: ~M") n))
1106 (let ((m ($length e)))
1107 (if (> n m)
1108 ($rest e 0)
1109 ($rest e (- n m)))))
1111 (defmfun $lastn (e n)
1112 (atomchk (setq e (format1 e)) '$lastn nil)
1113 (if (or (not (fixnump n)) (minusp n))
1114 (merror (intl:gettext "lastn: second argument must be a nonnegative integer; found: ~M") n))
1115 (let ((m ($length e)))
1116 (if (> n m)
1117 ($rest e 0)
1118 ($rest e (- m n)))))
1120 (defmfun $args (e)
1121 (atomchk (setq e (format1 e)) '$args nil)
1122 (cons '(mlist) (margs e)))
1124 (defmfun $delete (x l &optional (n -1 n?))
1125 (when (and n? (or (not (fixnump n)) (minusp n))) ; if n is set, it must be a nonneg fixnum
1126 (merror (intl:gettext "delete: third argument, if present, must be a nonnegative integer; found ~M") n))
1127 (atomchk (setq l (specrepcheck l)) '$delete t)
1128 (setq x (specrepcheck x)
1129 l (cons (delsimp (car l)) (copy-list (cdr l))))
1130 (do ((l1 (if (eq (caar l) 'mqapply) (cdr l) l)))
1131 ((or (null (cdr l1)) (zerop n)) l)
1132 (if (alike1 x (specrepcheck (cadr l1)))
1133 (progn
1134 (decf n)
1135 (rplacd l1 (cddr l1)))
1136 (setq l1 (cdr l1)))))
1138 (defmfun $length (e)
1139 (setq e (cond (($listp e) e)
1140 ((or $inflag (not ($ratp e))) (specrepcheck e))
1141 (t ($ratdisrep e))))
1142 (cond ((symbolp e) (merror (intl:gettext "length: argument cannot be a symbol; found ~:M") e))
1143 ((or (numberp e) (eq (caar e) 'bigfloat))
1144 (if (and (not $inflag) (mnegp e))
1146 (merror (intl:gettext "length: argument cannot be a number; found ~:M") e)))
1147 ((or $inflag (not (member (caar e) '(mtimes mexpt) :test #'eq))) (length (margs e)))
1148 ((eq (caar e) 'mexpt)
1149 (if (and (alike1 (caddr e) '((rat simp) 1 2)) $sqrtdispflag) 1 2))
1150 (t (length (cdr (nformat e))))))
1152 (defmfun $atom (x)
1153 (setq x (specrepcheck x)) (or (atom x) (eq (caar x) 'bigfloat)))
1155 (defmfun $symbolp (x)
1156 (setq x (specrepcheck x)) (symbolp x))
1158 (defmfun $num (e)
1159 (let (x)
1160 (cond ((atom e) e)
1161 ((eq (caar e) 'mrat) ($ratnumer e))
1162 ((eq (caar e) 'rat) (cadr e))
1163 ((eq (caar (setq x (nformat e))) 'mquotient) (simplify (cadr x)))
1164 ((and (eq (caar x) 'mminus) (not (atom (setq x (cadr x))))
1165 (eq (caar x) 'mquotient))
1166 (simplify (list '(mtimes) -1 (cadr x))))
1167 (t e))))
1169 (defmfun $denom (e)
1170 (cond ((atom e) 1)
1171 ((eq (caar e) 'mrat) ($ratdenom e))
1172 ((eq (caar e) 'rat) (caddr e))
1173 ((or (eq (caar (setq e (nformat e))) 'mquotient)
1174 (and (eq (caar e) 'mminus) (not (atom (setq e (cadr e))))
1175 (eq (caar e) 'mquotient)))
1176 (simplify (caddr e)))
1177 (t 1)))
1179 (defmfun $entier (e) (take '($floor) e))
1181 (defmfun $fix (e) (take '($floor) e))
1183 ;; Evaluate THUNK ignoring any error that might occur. If the THUNK
1184 ;; returns a good number (not infinity or NaN), then return the
1185 ;; number. Otherwise, return NIL to indicate that we the computation
1186 ;; failed. This is a pretty brute-force approach.
1187 (defun try-float-computation (thunk)
1188 (let ((errset nil)
1189 (errcatch (cons bindlist loclist))
1190 (*mdebug* nil))
1191 (let ((result (errset (funcall thunk))))
1192 (labels ((bad-number-p (num)
1193 (if (complexp num)
1194 (or (bad-number-p (realpart num))
1195 (bad-number-p (imagpart num)))
1196 (or (float-inf-p num)
1197 (float-nan-p num)))))
1198 (if (and result (bad-number-p (car result)))
1200 (car result))))))
1202 (defmfun $float (e)
1203 (cond ((numberp e)
1204 (let ((e1 (float e))) (if (float-inf-p e1) (signal 'floating-point-overflow) e1)))
1205 ((and (symbolp e) (mget e '$numer)))
1206 ((or (atom e) (member 'array (cdar e) :test #'eq)) e)
1207 ((eq (caar e) 'rat) (fpcofrat e))
1208 ((eq (caar e) 'bigfloat) (fp2flo e))
1209 ((member (caar e) '(mexpt mncexpt) :test #'eq)
1210 ;; avoid x^2 -> x^2.0, allow %e^%pi -> 23.14
1211 (let ((res (recur-apply #'$float e)))
1212 (if (floatp res)
1214 (list (ncons (caar e)) ($float (cadr e)) (caddr e)))))
1215 ((and (eq (caar e) '%log)
1216 (complex-number-p (second e) '$ratnump))
1217 ;; Basically we try to compute float(log(x)) as directly as
1218 ;; possible, expecting Lisp to return some error if it can't.
1219 ;; Then we do a more complicated approach to compute the
1220 ;; result. However, gcl and ecl don't signal errors in these
1221 ;; cases, so we always use the complicated approach for these lisps.
1222 (let ((n (second e)))
1223 (cond ((integerp n)
1224 ;; float(log(int)). First try to compute (log
1225 ;; (float n)). If that works, we're done.
1226 ;; Otherwise we need to do more.
1227 (to (or (try-float-computation #'(lambda ()
1228 (log (float n))))
1229 (let ((m (integer-length n)))
1230 ;; Write n as (n/2^m)*2^m where m is the number of
1231 ;; bits in n. Then log(n) = log(2^m) + log(n/2^m).
1232 ;; n/2^m is approximately 1, so converting that to a
1233 ;; float is no problem. log(2^m) = m * log(2).
1234 (+ (* m (log 2e0))
1235 (log (float (/ n (ash 1 m)))))))))
1236 (($ratnump n)
1237 ;; float(log(n/m)) where n and m are integers. Try computing
1238 ;; it first. If it fails, compute as log(n) - log(m).
1239 (let ((try (try-float-computation #'(lambda()
1240 (log (fpcofrat n))))))
1241 (if try
1242 (to try)
1243 (sub ($float `((%log) ,(second n)))
1244 ($float `((%log) ,(third n)))))))
1245 ((complex-number-p n 'integerp)
1246 ;; float(log(n+m*%i)).
1247 (let ((re ($realpart n))
1248 (im ($imagpart n)))
1249 (to (or (try-float-computation #'(lambda ()
1250 (log (complex (float re)
1251 (float im)))))
1252 (let* ((size (max (integer-length re)
1253 (integer-length im)))
1254 (scale (ash 1 size)))
1255 (+ (* size (log 2e0))
1256 (log (complex (float (/ re scale))
1257 (float (/ im scale))))))))))
1259 ;; log(n1/d1 + n2/d2*%i) =
1260 ;; log(s*(n+m*%i)) = log(s) + log(n+m*%i)
1262 ;; where s = lcm(d1, d2), n and m are integers
1264 (let* ((s (lcm ($denom ($realpart n))
1265 ($denom ($imagpart n))))
1266 (p ($expand (mul s n))))
1267 (add ($float `((%log) ,s))
1268 ($float `((%log) ,p))))))))
1269 ((and (eq (caar e) '%erf)
1270 (eq (second e) '$%i))
1271 ;; Handle like erf(%i). float(%i) (via recur-apply below)
1272 ;; just returns %i, so we never numerically evaluate it.
1273 (complexify (complex-erf (complex 0 1d0))))
1274 ((or (eq (caar e) '%derivative) (eq (caar e) '$diff))
1275 (append (list (remove 'simp (first e)) ($float (second e))) (rest (rest e))))
1276 (t (recur-apply #'$float e))))
1278 (defmfun $coeff (e x &optional (n 1))
1279 (if (equal n 0)
1280 (coeff e x 0)
1281 (coeff e (power x n) 1)))
1283 (defun coeff (e var pow)
1284 (simplify
1285 (cond ((alike1 e var) (if (equal pow 1) 1 0))
1286 ((atom e) (if (equal pow 0) e 0))
1287 ((eq (caar e) 'mexpt)
1288 (cond ((alike1 (cadr e) var)
1289 (if (or (equal pow 0) (not (alike1 (caddr e) pow))) 0 1))
1290 ((equal pow 0) e)
1291 (t 0)))
1292 ((or (eq (caar e) 'mplus) (mbagp e))
1293 (cons (if (eq (caar e) 'mplus) '(mplus) (car e))
1294 (mapcar #'(lambda (e) (coeff e var pow)) (cdr e))))
1295 ((eq (caar e) 'mrat) (ratcoeff e var pow))
1296 ((equal pow 0) (if (coeff-contains-powers e var) 0 e))
1297 ((eq (caar e) 'mtimes)
1298 (let ((term (if (equal pow 1) var (power var pow))))
1299 (if (memalike term (cdr e)) ($delete term e 1) 0)))
1300 (t 0))))
1302 (defun coeff-contains-powers (e var)
1303 (cond ((alike1 e var) t)
1304 ((atom e) nil)
1305 ((eq (caar e) 'mexpt)
1306 (alike1 (cadr e) var))
1307 ((eq (caar e) 'mtimes)
1308 (member t (mapcar #'(lambda (e)
1309 (coeff-contains-powers e var)) (cdr e))))
1310 (t nil)))
1312 (let (my-powers my-num my-flag)
1313 (declare (special my-powers my-num my-flag))
1315 (defmfun $hipow (e var)
1316 (findpowers e t var))
1318 ;; These work best on expanded "simple" expressions.
1320 (defmfun $lopow (e var)
1321 (findpowers e nil var))
1323 (defun findpowers (e hiflg var)
1324 (let (my-powers my-num my-flag)
1325 (declare (special my-powers my-num my-flag))
1326 (findpowers1 e hiflg var)
1327 (cond ((null my-powers) (if (null my-num) 0 my-num))
1328 (t (when my-num (setq my-powers (cons my-num my-powers)))
1329 (maximin my-powers (if hiflg '$max '$min))))))
1331 (defun findpowers1 (e hiflg var)
1332 (cond ((alike1 e var) (checkpow 1 hiflg))
1333 ((atom e))
1334 ((eq (caar e) 'mplus)
1335 (cond ((not (freel (cdr e) var))
1336 (do ((e (cdr e) (cdr e))) ((null e))
1337 (setq my-flag nil) (findpowers1 (car e) hiflg var)
1338 (if (null my-flag) (checkpow 0 hiflg))))))
1339 ((and (eq (caar e) 'mexpt) (alike1 (cadr e) var)) (checkpow (caddr e) hiflg))
1340 ((specrepp e) (findpowers1 (specdisrep e) hiflg var))
1341 (t (mapc #'(lambda (x) (findpowers1 x hiflg var)) (cdr e)))))
1343 (defun checkpow (pow hiflg)
1344 (setq my-flag t)
1345 (cond ((not (numberp pow)) (setq my-powers (cons pow my-powers)))
1346 ((null my-num) (setq my-num pow))
1347 (hiflg (if (> pow my-num) (setq my-num pow)))
1348 ((< pow my-num) (setq my-num pow)))))
1350 ;; push(x,l): The argument l must be a mapatom that is bound to a list. Prepend x to
1351 ;; the value of l and return this list. The arguments to push are evaluated from left to right.
1353 ;; We assume that if each member of a list is simplified, the list members are still simplified
1354 ;; after pushing onto (or popping) the list. Just before returning, the code calls simplifya
1355 ;; with second argument true. Without this call to simplifya, the general simplifier would simplify
1356 ;; every list member after returning. (Barton Willis, author of $push and $pop)
1358 (defmspec $push (z)
1359 (let* ((o (car (pop z)))
1360 (x (if z (pop z) (wna-err o)))
1361 (l (if z (pop z) (wna-err o)))
1362 (ll) (lo))
1364 (if z (wna-err o))
1365 (setq x (meval x))
1366 (cond (($subvarp l)
1367 (setq lo (simplifya (cons (list (meval (mop l)) 'array) (mevalargs (margs l))) t))
1368 (setq ll (let ((noevalargs t)) (meval lo))))
1370 (setq lo l)
1371 (setq ll (meval l))))
1372 (if (and ($mapatom lo) ($listp ll))
1373 (progn
1374 (setq ll (cons x (cdr ll)))
1375 (mset lo (simplifya (cons '(mlist) ll) t)))
1376 (merror "Second argument to push must be a mapatom that is bound to a list"))))
1378 ;; pop(l): The argument l must be a mapatom that is bound to a list. Remove the
1379 ;; first member of the value of l and return it.
1381 ;; We assume that if each member of a list is simplified, the list members are still simplified
1382 ;; after popping (or pushing onto) the list. Just before returning, the code calls simplifya
1383 ;; with second argument true. Without this call to simplifya, the general simplifier would simplify
1384 ;; every list member after returning.
1386 (defmspec $pop (z)
1387 (let* ((o (car (pop z)))
1388 (l (if z (pop z) (wna-err o)))
1389 (ll) (lo))
1390 (if z (wna-err o))
1391 (cond (($subvarp l)
1392 (setq lo (simplifya (cons (list (meval (mop l)) 'array) (mevalargs (margs l))) t))
1393 (setq ll (let ((noevalargs t)) (meval lo))))
1395 (setq lo l)
1396 (setq ll (meval lo))))
1397 (setq ll (meval l))
1398 (cond ((and ($mapatom lo) ($listp ll))
1399 (if ($emptyp ll) (merror "Pop called on an empty list")
1400 (prog2
1401 (setq ll (cdr ll))
1402 (pop ll)
1403 (mset lo (simplifya (cons '(mlist) ll) t)))))
1404 (t (merror "Argument to pop must be a mapatom that is bound to a list")))))