Replace some constant expressions with functions
[maxima.git] / src / simp.lisp
blob5ec59f9f72563b9b6ac565b67725d88869a51303
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 simp)
15 (defvar *rulesw*)
17 ;; Switches dealing with matrices and non-commutative multiplication.
19 (defmvar $doscmxplus nil
20 "Causes SCALAR + MATRIX to return a matrix answer. This switch
21 is not subsumed under DOALLMXOPS.")
23 (defmvar $domxexpt t
24 "Causes SCALAR^MATRIX([1,2],[3,4]) to return
25 MATRIX([SCALAR,SCALAR^2],[SCALAR^3,SCALAR^4]). In general, this
26 transformation affects exponentiations where the *print-base* is a
27 scalar and the power is a matrix or list.")
29 (defmvar $domxplus nil)
31 (defmvar $domxtimes nil)
33 (defmvar $mx0simp t)
35 ;; Lisp level variables
36 (defmvar derivsimp t "Hack in `simpderiv' for `rwg'")
38 (defmvar $grindswitch nil)
39 (defmvar $on t)
40 (defmvar $off nil)
41 (defmvar $limitdomain '$complex)
42 (defmvar $lognegint nil
43 nil
44 :properties ((evflag t)))
45 (defmvar $distribute_over t
46 "If T, functions are distributed over bags.")
48 (defvar *expandp* nil)
49 (defvar *timesinp* nil)
50 (defvar *exptrlsw* nil)
51 (defvar *expandflag* nil)
53 (defprop mnctimes t associative)
54 (defprop lambda t lisp-no-simp)
56 ;; Local functions should not be simplified. Various lisps
57 ;; use various names for the list structure defining these:
58 (eval-when
59 (:load-toplevel)
60 (eval '(let* ((x 1)
61 (z #'(lambda () 3)))
62 (dolist (y (list x z))
63 (and (consp y)
64 (symbolp (car y))
65 (setf (get (car y) 'lisp-no-simp) t))))))
67 (dolist (x '(mplus mtimes mnctimes mexpt mncexpt %sum))
68 (setf (get x 'msimpind) (cons x '(simp))))
70 ;; operators properties
72 (mapc #'(lambda (x) (setf (get (first x) 'operators) (second x)))
73 '((mplus simplus) (mtimes simptimes) (mncexpt simpncexpt)
74 (mminus simpmin) (%gamma simpgamma) (mfactorial simpfact)
75 (mnctimes simpnct) (mquotient simpquot) (mexpt simpexpt)
76 (%log simpln)
77 (%derivative simpderiv)
78 (%signum simpsignum)
79 (%integrate simpinteg) (%limit simp-limit)
80 (bigfloat simpbigfloat) (lambda simplambda) (mdefine simpmdef)
81 (mqapply simpmqapply) (%gamma simpgamma)
82 ($beta simpbeta) (%sum simpsum) (%binomial simpbinocoef)
83 (%plog simpplog) (%product simpprod) (%genfact simpgfact)
84 ($atan2 simpatan2) ($matrix simpmatrix) (%matrix simpmatrix)
85 ($bern simpbern) ($euler simpeuler)))
87 (defprop $li lisimp specsimp)
88 (defprop $psi psisimp specsimp)
90 (defprop $equal t binary)
91 (defprop $notequal t binary)
93 (defmfun $bfloatp (x)
94 "Returns true if X is a bigfloat"
95 (and (consp x)
96 (consp (car x))
97 (eq (caar x) 'bigfloat)))
99 (defun zerop1 (x)
100 "Returns non-NIL if X is an integer, float, or bfloat that is equal
101 to 0"
102 (or (and (integerp x) (= 0 x))
103 (and (floatp x) (= 0.0 x))
104 (and ($bfloatp x) (= 0 (second x)))))
106 (defun onep1 (x)
107 "Returns non-NIL if X is an integer, float, or bfloat that is equal
108 to 1"
109 (or (and (integerp x) (= 1 x))
110 (and (floatp x) (= 1.0 x))
111 (and ($bfloatp x) (zerop1 (sub x 1)))))
113 (defun mnump (x)
114 "Returns non-NIL if X is a Lisp number or if it is a Maxima rational
115 form or a bigfloat form"
116 (or (numberp x)
117 (and (not (atom x)) (not (atom (car x)))
118 (member (caar x) '(rat bigfloat)))))
120 ;; Does X or a subexpression match PREDICATE?
122 ;; If X is a tree then we recurse depth-first down its arguments. The specrep
123 ;; check is because rat forms are built rather differently from normal Maxima
124 ;; expressions so we need to unpack them for the recursion to work properly.
125 (defun subexpression-matches-p (predicate x)
126 (or (funcall predicate x)
127 (and (consp x)
128 (if (specrepp x)
129 (subexpression-matches-p predicate (specdisrep x))
130 (some (lambda (arg) (subexpression-matches-p predicate arg))
131 (cdr x))))))
133 ;; Is there a bfloat anywhere in X?
134 (defun some-bfloatp (x) (subexpression-matches-p '$bfloatp x))
136 ;; Is there a float anywhere in X?
137 (defun some-floatp (x) (subexpression-matches-p 'floatp x))
139 ;; EVEN works for any arbitrary lisp object since it does an integer
140 ;; check first. In other cases, you may want the Lisp EVENP function
141 ;; which only works for integers.
143 (defun even (a) (and (integerp a) (not (oddp a))))
145 ;; Predicates to determine if X satisfies some condition.
147 (defun ratnump (x)
148 "Determines if X is a Maxima rational form: ((rat ...) a b)"
149 (and (not (atom x)) (eq (caar x) 'rat)))
151 (defun mplusp (x)
152 "Determines if X is a Maxima sum form: ((mplus ...) ...)"
153 (and (not (atom x)) (eq (caar x) 'mplus)))
155 (defun mtimesp (x)
156 "Determines if X is a Maxima product form: ((mtimes ...) ...)"
157 (and (not (atom x)) (eq (caar x) 'mtimes)))
159 (defun mexptp (x)
160 "Determines if X is a Maxima exponential form: ((mexpt ...) ...)"
161 (and (not (atom x)) (eq (caar x) 'mexpt)))
163 (defun mnctimesp (x) (and (not (atom x)) (eq (caar x) 'mnctimes)))
165 (defun mncexptp (x) (and (not (atom x)) (eq (caar x) 'mncexpt)))
167 (defun mlogp (x)
168 "Determines if X is a Maxima log form: ((%log ...) ...)"
169 (and (not (atom x)) (eq (caar x) '%log)))
171 (defun mmminusp (x)
172 "Determines if X is a Maxima negative form: ((mminus ...) ...)
174 This generally only happens on input forms like a - b:
175 ((mplus) $a ((mminus) $b)).
176 After simplification a - b becomes
177 ((mplus) $a ((mtimes) -1 $b))"
179 (and (not (atom x)) (eq (caar x) 'mminus)))
181 (defun mnegp (x)
182 "Determines if X is negative if X is a Lisp number or a Maxima rat
183 form or bigfloat form"
184 (cond ((realp x) (minusp x))
185 ((or (ratnump x) ($bfloatp x)) (minusp (cadr x)))))
187 (defun mqapplyp (e) (and (not (atom e)) (eq (caar e) 'mqapply)))
189 (defun ratdisrep (e) (simplifya ($ratdisrep e) nil))
191 (defun sratsimp (e) (simplifya ($ratsimp e) nil))
193 (defun simpcheck (e flag)
194 (cond ((specrepp e) (specdisrep e))
195 (flag e)
196 (t (let (($%enumer $numer))
197 ;; Switch $%enumer on, when $numer is TRUE to allow
198 ;; simplification of $%e to its numerical value.
199 (simplifya e nil)))))
201 (defun mratcheck (e) (if ($ratp e) (ratdisrep e) e))
203 (defmfun $numberp (e)
204 "Returns true if E is a Maxima rational, a float, or a bigfloat number"
205 (or ($ratnump e) ($floatnump e) ($bfloatp e)))
207 (defmfun $integerp (x)
208 (or (integerp x)
209 (and ($ratp x)
210 (not (member 'trunc (car x)))
211 (integerp (cadr x))
212 (equal (cddr x) 1))))
214 ;; The call to $INTEGERP in the following two functions checks for a CRE
215 ;; rational number with an integral numerator and a unity denominator.
217 (defmfun $oddp (x)
218 (cond ((integerp x) (oddp x))
219 (($integerp x) (oddp (cadr x)))))
221 (defmfun $evenp (x)
222 (cond ((integerp x) (evenp x))
223 (($integerp x) (not (oddp (cadr x))))))
225 (defmfun $floatnump (x)
226 (or (floatp x)
227 (and ($ratp x) (floatp (cadr x)) (onep1 (cddr x)))))
229 (defmfun $ratnump (x)
230 (or (integerp x)
231 (ratnump x)
232 (and ($ratp x)
233 (not (member 'trunc (car x)))
234 (integerp (cadr x))
235 (integerp (cddr x)))))
237 (defmfun $ratp (x)
238 (and (not (atom x))
239 (consp (car x))
240 (eq (caar x) 'mrat)))
242 (defmfun $taylorp (x)
243 (and (not (atom x))
244 (eq (caar x) 'mrat)
245 (member 'trunc (cdar x)) t))
247 (defun specrepcheck (e) (if (specrepp e) (specdisrep e) e))
249 ;; Note that the following two functions are carefully coupled.
251 (defun specrepp (e)
252 (and (not (atom e))
253 (member (caar e) '(mrat mpois))))
255 (defun specdisrep (e)
256 (cond ((eq (caar e) 'mrat) (ratdisrep e))
257 (t ($outofpois e))))
259 (defmfun $polysign (x)
260 (setq x (cadr (ratf x)))
261 (cond ((equal x 0) 0) ((pminusp x) -1) (t 1)))
263 ;; These check for the correct number of operands within Macsyma expressions,
264 ;; not arguments in a procedure call as the name may imply.
266 (defun arg-count-check (required-arg-count expr)
267 (unless (= required-arg-count (length (rest expr)))
268 (wna-err expr required-arg-count)))
270 (defun oneargcheck (expr)
271 (arg-count-check 1 expr))
273 (defun twoargcheck (expr)
274 (arg-count-check 2 expr))
276 ;; WNA-ERR: Wrong Number of Arguments error
278 ;; If REQUIRED-ARG-COUNT is non-NIL, then we check that EXPR has the
279 ;; correct number of arguments. A informative error message is shown
280 ;; if the number of arguments is not given.
282 ;; Otherwise, EXPR must be a symbol and a generic message is printed.
283 ;; (This is for backward compatibility for existing uses of WNA-ERR.)
284 (defun wna-err (exprs &optional required-arg-count)
285 (if required-arg-count
286 (let ((op (caar exprs))
287 (actual-count (length (rest exprs))))
288 (merror (intl:gettext "~M: expected exactly ~M arguments but got ~M: ~M")
289 op required-arg-count actual-count (list* '(mlist) (rest exprs))))
290 (merror (intl:gettext "~:@M: wrong number of arguments.")
291 exprs)))
293 (defun improper-arg-err (exp fn)
294 (merror (intl:gettext "~:M: improper argument: ~M") fn exp))
296 (defun subargcheck (form subsharp argsharp fun)
297 (if (or (not (= (length (subfunsubs form)) subsharp))
298 (not (= (length (subfunargs form)) argsharp)))
299 (merror (intl:gettext "~:@M: wrong number of arguments or subscripts.") fun)))
301 ;; Constructor and extractor primitives for subscripted functions, e.g.
302 ;; F[1,2](X,Y). SUBL is (1 2) and ARGL is (X Y).
304 ;; These will be flushed when NOPERS is finished. They will be macros in
305 ;; NOPERS instead of functions, so we have to be careful that they aren't
306 ;; mapped or applied anyplace. What we really want is open-codable routines.
308 (defun subfunmakes (fun subl argl)
309 `((mqapply simp) ((,fun simp array) . ,subl) . ,argl))
311 (defun subfunmake (fun subl argl)
312 `((mqapply) ((,fun simp array) . ,subl) . ,argl))
314 (defun subfunname (exp) (caaadr exp))
316 (defun subfunsubs (exp) (cdadr exp))
318 (defun subfunargs (exp) (cddr exp))
320 (defmfun $numfactor (x)
321 (setq x (specrepcheck x))
322 (cond ((mnump x) x)
323 ((atom x) 1)
324 ((not (eq (caar x) 'mtimes)) 1)
325 ((mnump (cadr x)) (cadr x))
326 (t 1)))
328 (defun scalar-or-constant-p (x flag)
329 (if flag (not ($nonscalarp x)) ($scalarp x)))
331 (defmfun $constantp (x)
332 (cond ((atom x) (or ($numberp x) (kindp x '$constant)))
333 ((member (caar x) '(rat bigfloat)) t)
334 ((specrepp x) ($constantp (specdisrep x)))
335 ((or (mopp (caar x)) (kindp (caar x) '$constant))
336 (do ((x (cdr x) (cdr x))) ((null x) t)
337 (if (not ($constantp (car x))) (return nil))))))
339 (defun constant (x)
340 (cond ((symbolp x) (kindp x '$constant))
341 (($subvarp x)
342 (and (kindp (caar x) '$constant)
343 (do ((x (cdr x) (cdr x))) ((null x) t)
344 (if (not ($constantp (car x))) (return nil)))))))
346 (defun maxima-constantp (x)
347 (or (numberp x)
348 (and (symbolp x) (kindp x '$constant))))
350 (defun consttermp (x) (and ($constantp x) (not ($nonscalarp x))))
352 (defmfun $scalarp (x) (or (consttermp x) (eq (scalarclass x) '$scalar)))
354 (defmfun $nonscalarp (x) (eq (scalarclass x) '$nonscalar))
356 (defun scalarclass (exp) ; Returns $SCALAR, $NONSCALAR, or NIL (unknown).
357 (cond ((mnump exp)
358 ;; Maxima numbers are scalar.
359 '$scalar)
360 ((atom exp)
361 (cond ((or (mget exp '$nonscalar)
362 (and (not (mget exp '$scalar))
363 ;; Arrays are nonscalar, but not if declared scalar.
364 (or (arrayp exp)
365 ($member exp $arrays))))
366 '$nonscalar)
367 ((or (mget exp '$scalar)
368 ;; Include constant atoms which are not declared nonscalar.
369 ($constantp exp))
370 '$scalar)))
371 ((member 'array (car exp))
372 (cond ((mget (caar exp) '$scalar) '$scalar)
373 ((mget (caar exp) '$nonscalar) '$nonscalar)
374 (t nil)))
375 ((specrepp exp) (scalarclass (specdisrep exp)))
376 ;; If the function is declared scalar or nonscalar, then return. If it
377 ;; isn't explicitly declared, then try to be intelligent by looking at
378 ;; the arguments to the function.
379 ((scalarclass (caar exp)))
380 ;; <number> + <scalar> is SCALARP because that seems to be useful.
381 ;; This should probably only be true if <number> is a member of the
382 ;; field of scalars. <number> * <scalar> is SCALARP since
383 ;; <scalar> + <scalar> is SCALARP. Also, this has to be done to make
384 ;; <scalar> - <scalar> SCALARP.
385 ((member (caar exp) '(mplus mtimes))
386 (do ((l (cdr exp) (cdr l))) ((null l) '$scalar)
387 (if (not (consttermp (car l)))
388 (return (scalarclass-list l)))))
389 ((and (eq (caar exp) 'mqapply) (scalarclass (cadr exp))))
390 ((mxorlistp exp) '$nonscalar)
391 ;; If we can't find out anything about the operator, then look at the
392 ;; arguments to the operator. I think NIL should be returned at this
393 ;; point. -cwh
395 (do ((exp (cdr exp) (cdr exp)) (l '(1)))
396 ((null exp) (scalarclass-list l))
397 (if (not (consttermp (car exp)))
398 (setq l (cons (car exp) l)))))))
400 ;; Could also do <scalar> +|-|*|/ |^ <declared constant>, but this is not
401 ;; always correct and could screw somebody.
403 ;; SCALARCLASS-LIST takes a list of expressions as its argument. If their
404 ;; scalarclasses all agree, then that scalarclass is returned.
406 (defun scalarclass-list (llist)
407 (cond ((null llist) nil)
408 ((null (cdr llist)) (scalarclass (car llist)))
409 (t (let ((sc-car (scalarclass (car llist)))
410 (sc-cdr (scalarclass-list (cdr llist))))
411 (cond ((or (eq sc-car '$nonscalar)
412 (eq sc-cdr '$nonscalar))
413 '$nonscalar)
414 ((and (eq sc-car '$scalar) (eq sc-cdr '$scalar))
415 '$scalar))))))
417 (defun mbagp (x)
418 (and (not (atom x))
419 (member (caar x) '(mequal mlist $matrix))))
421 (defun mequalp (x) (and (not (atom x)) (eq (caar x) 'mequal)))
423 (defun mxorlistp (x)
424 (and (not (atom x))
425 (member (caar x) '(mlist $matrix))))
427 (defun mxorlistp1 (x)
428 (and (not (atom x))
429 (or (eq (caar x) '$matrix)
430 (and (eq (caar x) 'mlist) $listarith))))
432 (defun constmx (const x)
433 (simplifya (fmapl1 #'(lambda (ign)
434 (declare (ignore ign))
435 const)
439 ;;; ISINOP returns the complete subexpression with the operator OP, when the
440 ;;; operator OP is found in EXPR.
442 (defun isinop (expr op) ; OP is assumed to be an atom
443 (cond ((atom expr) nil)
444 ((and (eq (caar expr) op)
445 (not (member 'array (cdar expr))))
446 expr)
448 (do ((expr (cdr expr) (cdr expr))
449 (res nil))
450 ((null expr))
451 (when (setq res (isinop (car expr) op))
452 (return res))))))
454 (defun free (exp free-var)
455 (cond ((alike1 exp free-var) nil)
456 ((atom exp) t)
458 (and (listp (car exp))
459 (free (caar exp) free-var)
460 (freel (cdr exp) free-var)))))
462 (defun freel (l free-var)
463 (do ((l l (cdr l))) ((null l) t)
464 (cond
465 ((atom l) (return (free l free-var))) ;; second element of a pair
466 ((not (free (car l) free-var)) (return nil)))))
469 (defun simplifya (x y)
470 (cond ((not $simp) x)
471 ((atom x)
472 (cond ((and $%enumer $numer (eq x '$%e))
473 ;; Replace $%e with its numerical value,
474 ;; when %enumer and $numer TRUE
475 (setq x %e-val))
476 (t x)))
477 ((atom (car x))
478 (cond ((and (cdr x) (atom (cdr x)))
479 (merror (intl:gettext "simplifya: malformed expression (atomic cdr).")))
480 ((get (car x) 'lisp-no-simp)
481 ;; this feature is to be used with care. it is meant to be
482 ;; used to implement data objects with minimum of consing.
483 ;; forms must not bash the DISPLA package. Only new forms
484 ;; with carefully chosen names should use this feature.
486 (t (cons (car x)
487 (mapcar #'(lambda (x) (simplifya x y)) (cdr x))))))
488 ((eq (caar x) 'rat) (*red1 x))
489 ;; Enforced resimplification: Reset dosimp and strip 'simp tags from x.
490 (dosimp (let ((dosimp nil)) (simplifya (unsimplify x) y)))
491 ((member 'simp (cdar x)) x)
492 ((eq (caar x) 'mrat) x)
493 ((not (atom (caar x)))
494 (cond ((or (eq (caaar x) 'lambda)
495 (and (not (atom (caaar x))) (eq (caaaar x) 'lambda)))
496 (mapply1 (caar x) (cdr x) (caar x) x))
497 (t (merror (intl:gettext "simplifya: operator is neither an atom nor a lambda expression: ~S") x))))
498 ((and $distribute_over
499 (get (caar x) 'distribute_over)
500 ;; A function with the property 'distribute_over.
501 ;; Look, if we have a bag as argument to the function.
502 (distribute-over x)))
503 ((get (caar x) 'opers)
504 (let ((opers-list *opers-list)) (oper-apply x y)))
505 ((and (eq (caar x) 'mqapply)
506 (or (atom (cadr x))
507 (and (eq substp 'mqapply)
508 (or (eq (car (cadr x)) 'lambda)
509 (eq (caar (cadr x)) 'lambda)))))
510 (cond ((or (symbolp (cadr x)) (not (atom (cadr x))))
511 (simplifya (cons (cons (cadr x) (cdar x)) (cddr x)) y))
512 ((or (not (member 'array (cdar x))) (not $subnumsimp))
513 (merror (intl:gettext "simplifya: I don't know how to simplify this operator: ~M") x))
514 (t (cadr x))))
515 (t (let ((w (get (caar x) 'operators)))
516 (cond ((and w
517 (or (not (member 'array (cdar x)))
518 (rulechk (caar x))))
519 (funcall w x 1 y))
520 (t (simpargs x y)))))))
522 ;; EQTEST returns an expression which is the same as X
523 ;; except that it is marked with SIMP and maybe other flags from CHECK.
525 ;; Following description is inferred from the code. Dunno why the function is named "EQTEST".
527 ;; (1) if X is already marked with SIMP flag or doesn't need it: return X.
528 ;; (2) if X is pretty much the same as CHECK (same operator and same arguments),
529 ;; then return CHECK after marking it with SIMP flag.
530 ;; (3) if operator of X has the MSIMPIND property, replace it
531 ;; with value of MSIMPIND (something like '(MPLUS SIMP)) and return X.
532 ;; (4) if X or CHECK is an array expression, return X after marking it with SIMP and ARRAY flags.
533 ;; (5) otherwise, return X after marking it with SIMP flag.
535 (defun eqtest (x check)
536 (let ((y nil))
537 (cond ((or (atom x)
538 (eq (caar x) 'rat)
539 (eq (caar x) 'mrat)
540 (member 'simp (cdar x)))
542 ((and (eq (caar x) (caar check))
543 (equal (cdr x) (cdr check)))
544 (cond ((and (null (cdar check))
545 (setq y (get (caar check) 'msimpind)))
546 (cons y (cdr check)))
547 ((member 'simp (cdar check))
548 check)
550 (cons (cons (caar check)
551 (if (cdar check)
552 (cons 'simp (cdar check))
553 '(simp)))
554 (cdr check)))))
555 ((setq y (get (caar x) 'msimpind))
556 (rplaca x y))
557 ((or (member 'array (cdar x))
558 (and (eq (caar x) (caar check))
559 (member 'array (cdar check))))
560 (rplaca x (cons (caar x) '(simp array))))
562 (rplaca x (cons (caar x) '(simp)))))))
564 ;; A function, which distributes of bags like a list, matrix, or equation.
565 ;; Check, if we have to distribute of one of the bags or any other operator.
566 (defun distribute-over (expr)
567 (cond ((= 1 (length (cdr expr)))
568 ;; Distribute over for a function with one argument.
569 (cond ((and (not (atom (cadr expr)))
570 (member (caaadr expr) (get (caar expr) 'distribute_over)))
571 (simplify
572 (cons (caadr expr)
573 (mapcar #'(lambda (u) (simplify (list (car expr) u)))
574 (cdadr expr)))))
575 (t nil)))
577 ;; A function with more than one argument.
578 (do ((args (cdr expr) (cdr args))
579 (first-args nil))
580 ((null args) nil)
581 (when (and (not (atom (car args)))
582 (member (caar (car args))
583 (get (caar expr) 'distribute_over)))
584 ;; Distribute the function over the arguments and simplify again.
585 (return
586 (simplify
587 (cons (ncons (caar (car args)))
588 (mapcar #'(lambda (u)
589 (simplify
590 (append
591 (append
592 (cons (ncons (caar expr))
593 (reverse first-args))
594 (ncons u))
595 (rest args))))
596 (cdr (car args)))))))
597 (setq first-args (cons (car args) first-args))))))
599 (defun rulechk (x) (or (mget x 'oldrules) (get x 'rules)))
601 (defun resimplify (x) (let ((dosimp t)) (simplifya x nil)))
603 (defun unsimplify (x)
604 (if (or (atom x) (specrepp x))
606 (cons (remove 'simp (car x) :count 1) (mapcar #'unsimplify (cdr x)))))
608 (defun simpargs (x y)
609 (if (or (and (eq (get (caar x) 'dimension) 'dimension-infix)
610 (not (getl (caar x) '($lassociative $rassociative))))
611 (get (caar x) 'binary))
612 (twoargcheck x))
613 (if (and (member 'array (cdar x)) (null (margs x)))
614 (merror (intl:gettext "SIMPARGS: subscripted variable found with no subscripts.")))
615 (eqtest (if y x (let ((flag (member (caar x) '(mlist mequal))))
616 (cons (ncons (caar x))
617 (mapcar #'(lambda (u)
618 (if flag (simplifya u nil)
619 (simpcheck u nil)))
620 (cdr x)))))
623 ;;;-----------------------------------------------------------------------------
624 ;;; ADDK (X Y) 27.09.2010/DK
626 ;;; Arguments and values:
627 ;;; X - a Maxima number
628 ;;; Y - a Maxima number
629 ;;; result - a simplified Maxima number
631 ;;; Description:
632 ;;; ADDK adds two Maxima numbers and returns a simplified Maxima number.
633 ;;; ADDK can be called in Lisp code, whenever the arguments are valid
634 ;;; Maxima numbers, these are integer, float, Maxima rational, or
635 ;;; Maxima bigfloat numbers. The arguments must not be simplified. The
636 ;;; precision of a bigfloat result depends on the setting of the
637 ;;; global variable $FPPREC. If the option variable $FLOAT is T, a
638 ;;; Maxima rational number as a result is converted to a float number.
640 ;;; Examples:
641 ;;; (addk 2 3) -> 5
642 ;;; (addk 2.0 3) -> 5.0
643 ;;; (addk ($bfloat 2) 3)-> ((BIGFLOAT SIMP 56) 45035996273704960 3)
644 ;;; (addk 2 '((rat) 1 2)) -> ((RAT SIMP) 5 2)
645 ;;; (let (($float t)) (addk 2 '((rat) 1 2))) -> 2.5
647 ;;; Affected by:
648 ;;; The option variables $FLOAT and $FPPREC.
650 ;;; See also:
651 ;;; TIMESK to multiply and EXPTRL to exponentiate two Maxima numbers.
653 ;;; Notes:
654 ;;; The routine works for Lisp rational and Lisp complex numbers too.
655 ;;; This feature is not used in Maxima code. If Lisp complex and
656 ;;; rational numbers are mixed with Maxima rational or bigfloat
657 ;;; numbers the result is wrong or a Lisp error is generated.
658 ;;;-----------------------------------------------------------------------------
660 (defun addk (x y)
661 (cond ((eql x 0) y)
662 ((eql y 0) x)
663 ((and (numberp x) (numberp y)) (+ x y))
664 ((or ($bfloatp x) ($bfloatp y)) ($bfloat (list '(mplus) x y)))
665 (t (prog (g a b)
666 (cond ((numberp x)
667 (cond ((floatp x) (return (+ x (fpcofrat y))))
668 (t (setq x (list '(rat) x 1)))))
669 ((numberp y)
670 (cond ((floatp y) (return (+ y (fpcofrat x))))
671 (t (setq y (list '(rat) y 1))))))
672 (setq g (gcd (caddr x) (caddr y)))
673 (setq a (truncate (caddr x) g)
674 b (truncate (caddr y) g))
675 (return (timeskl (list '(rat) 1 g)
676 (list '(rat)
677 (+ (* (cadr x) b)
678 (* (cadr y) a))
679 (* a b))))))))
681 ;;;-----------------------------------------------------------------------------
682 ;;; *RED1 (X) 27.09.2010/DK
683 ;;; *RED (N D)
685 ;;; Arguments and values:
686 ;;; X - a Maxima rational number (for *RED1)
687 ;;; N - an integer number representing the numerator of a rational
688 ;;; D - an integer number representing the denominator of a rational
689 ;;; result - a simplified Maxima rational number
691 ;;; Description:
692 ;;; *RED1 is called from SIMPLIFYA to reduce and simplify a Maxima rational
693 ;;; number. *RED1 checks if the rational number is already simplified. If
694 ;;; the option variable $FLOAT is T, the rational number is converted to a
695 ;;; float number. If the number is not simplified, *RED is called.
697 ;;; *RED reduces the numerator N and the demoniator D and returns a
698 ;;; simplified Maxima rational number. The result is converted to a float
699 ;;; number, if the option variable $FLOAT is T.
701 ;;; Affected by:
702 ;;; The option variable $FLOAT.
703 ;;;-----------------------------------------------------------------------------
705 (defun *red1 (x)
706 (cond ((member 'simp (cdar x))
707 (if $float (fpcofrat x) x))
708 (t (*red (cadr x) (caddr x)))))
710 (defun *red (n d)
711 (cond ((zerop n) 0)
712 ((equal d 1) n)
713 (t (let ((u (gcd n d)))
714 (setq n (truncate n u)
715 d (truncate d u))
716 (if (minusp d) (setq n (- n) d (- d)))
717 (cond ((equal d 1) n)
718 ($float (fpcofrat1 n d))
719 (t (list '(rat simp) n d)))))))
721 ;;;-----------------------------------------------------------------------------
722 ;;; TIMESK (X Y) 27.09.2010/DK
724 ;;; Arguments and values:
725 ;;; X - a Maxima number
726 ;;; Y - a Maxima number
727 ;;; result - a simplified Maxima number
729 ;;; Description:
730 ;;; TIMESK Multiplies two Maxima numbers and returns a simplified Maxima
731 ;;; number. TIMESK can be called in Lisp code, whenever the arguments are
732 ;;; valid Maxima numbers, these are integer, float, Maxima rational, or
733 ;;; Maxima bigfloat numbers. The arguments must not be simplified. The
734 ;;; precision of a bigfloat result depends on the setting of the
735 ;;; global variable $FPPREC. If the option variable $FLOAT is T, a
736 ;;; Maxima rational number as a result is converted to a float number.
738 ;;; TIMESKL is called from TIMESK to multiply two Maxima rational numbers or
739 ;;; a rational number with an integer number.
741 ;;; Examples:
742 ;;; (timesk 2 3) -> 6
743 ;;; (timesk 2.0 3) -> 6.0
744 ;;; (timesk ($bfloat 2) 3)-> ((BIGFLOAT SIMP 56) 54043195528445952 3)
745 ;;; (timesk 3 '((rat) 1 2)) -> ((RAT SIMP) 3 2)
746 ;;; (let (($float t)) (timesk 3 '((rat) 1 2))) -> 1.5
748 ;;; Affected by:
749 ;;; The option variables $FLOAT and $FPPREC.
751 ;;; See also:
752 ;;; ADDK to add and EXPTRL to exponentiate two Maxima numbers.
754 ;;; Notes:
755 ;;; The routine works for Lisp rational and Lisp complex numbers too.
756 ;;; This feature is not used in Maxima code. If Lisp complex and
757 ;;; rational numbers are mixed with Maxima rational or bigfloat
758 ;;; numbers the result is wrong or a Lisp error is generated.
759 ;;;-----------------------------------------------------------------------------
761 ;; NUM1 and DENOM1 are helper functions for TIMESKL to get the numerator and the
762 ;; denominator of an integer or Maxima rational number. For an integer the
763 ;; denominator is 1. Both functions are used at other places in Maxima code too.
765 (defun num1 (a)
766 (if (numberp a) a (cadr a)))
768 (defun denom1 (a)
769 (if (numberp a) 1 (caddr a)))
771 (defun timesk (x y) ; X and Y are assumed to be already reduced
772 (cond ((equal x 1) y)
773 ((equal y 1) x)
774 ((and (numberp x) (numberp y)) (* x y))
775 ((or ($bfloatp x) ($bfloatp y)) ($bfloat (list '(mtimes) x y)))
776 ((floatp x) (* x (fpcofrat y)))
777 ((floatp y) (* y (fpcofrat x)))
778 (t (timeskl x y))))
780 ;; TIMESKL takes one or two Maxima rational numbers, one argument can be an
781 ;; integer number. The result is a Maxima rational or an integer number.
782 ;; If the option variable $FLOAT is T, a Maxima rational number in converted
783 ;; to a float value.
785 (defun timeskl (x y)
786 (prog (u v g)
787 (setq u (*red (num1 x) (denom1 y)))
788 (setq v (*red (num1 y) (denom1 x)))
789 (setq g (cond ((or (equal u 0) (equal v 0)) 0)
790 ((equal v 1) u)
791 ((and (numberp u) (numberp v)) (* u v))
792 (t (list '(rat simp)
793 (* (num1 u) (num1 v))
794 (* (denom1 u) (denom1 v))))))
795 (return (cond ((numberp g) g)
796 ((equal (caddr g) 1) (cadr g))
797 ($float (fpcofrat g))
798 (t g)))))
800 ;;;-----------------------------------------------------------------------------
801 ;;; FPCOFRAT (RATNO) 27.09.2010/DK
802 ;;; FPCOFRT1 (NU D)
804 ;;; Arguments and values:
805 ;;; RATNO - a Maxima rational number (for FPCOFRAT)
806 ;;; NU - an integer number which represents the numerator of a rational
807 ;;; D - an integer number which represents the denominator of a rational
808 ;;; result - floating point approximation of a rational number
810 ;;; Description:
811 ;;; Floating Point Conversion OF RATional number routine.
812 ;;; Finds floating point approximation to rational number.
814 ;;; FPCOFRAT1 computes the quotient of NU/D.
816 ;;; Exceptional situations:
817 ;;; A Lisp error is generated, if the rational number does not fit into a
818 ;;; float number.
819 ;;;-----------------------------------------------------------------------------
821 ;; This constant is only needed in the file float.lisp.
822 (eval-when
823 (:compile-toplevel :load-toplevel :execute)
824 (defconstant machine-mantissa-precision (float-digits 1.0)))
826 (defun fpcofrat (ratno)
827 (fpcofrat1 (cadr ratno) (caddr ratno)))
829 (defun fpcofrat1 (nu d)
830 (float (/ nu d)))
832 ;;;-----------------------------------------------------------------------------
833 ;;; EXPTA (X Y) 27.09.2010/DK
834 ;;;
835 ;;; Arguments and values:
836 ;;; X - a Maxima number
837 ;;; Y - an integer number
838 ;;; result - a simplified Maxima number
840 ;;; Description:
841 ;;; Computes X^Y, where X is Maxima number and Y an integer. The result is
842 ;;; a simplified Maxima number. Y can be a rational Maxima number. For this
843 ;;; case the numerator is taken as the power.
845 ;;; Affected by:
846 ;;; The option variables $FLOAT and $FPPREC.
848 ;;; Notes:
849 ;;; This routine is not used within the simplifier. There is only one
850 ;;; call from the file hayat.lisp. This call can be replaced with a
851 ;;; call of the function power.
852 ;;;-----------------------------------------------------------------------------
854 (defun expta (x y)
855 (cond ((equal y 1)
857 ((numberp x)
858 (exptb x (num1 y)))
859 (($bfloatp x)
860 ($bfloat (list '(mexpt) x y)))
861 ((minusp (num1 y))
862 (*red (exptb (caddr x) (- (num1 y)))
863 (exptb (cadr x) (- (num1 y)))))
865 (*red (exptb (cadr x) (num1 y))
866 (exptb (caddr x) (num1 y))))))
868 ;;;-----------------------------------------------------------------------------
869 ;;; EXPTB (A B) 27.09.2010/DK
871 ;;; Arguments and values:
872 ;;; A - a float or integer number
873 ;;; B - an integer number
874 ;;; result - a simplified Maxima number
876 ;;; Description:
877 ;;; Computes A^B, where A is a float or an integer number and B is an
878 ;;; integer number. The result is an integer, float, or Maxima
879 ;;; rational number.
881 ;;; Examples:
882 ;;; (exptb 3 2) -> 9
883 ;;; (exptb 3.0 2) -> 9.0
884 ;;; (exptb 3 -2) -> ((RAT SiMP) 1 9)
885 ;;; (let (($float t)) (exptb 3 -2)) -> 0.1111111111111111
887 ;;; Affected by:
888 ;;; The option variable $FLOAT.
890 ;;; Notes:
891 ;;; EXPTB calls the Lisp functions EXP or EXPT to compute the result.
892 ;;;-----------------------------------------------------------------------------
894 (defun exptb (a b)
895 (let ((result
896 (cond ((equal a %e-val)
897 ;; Make B a float so we'll get double-precision result.
898 (exp (float b)))
899 ((or (floatp a) (not (minusp b)))
900 (expt a b))
902 (setq b (expt a (- b)))
903 (*red 1 b)))))
904 (when (float-inf-p result) ;; needed for gcl and sbcl - (sometimes) no trap of overflow
905 (error 'floating-point-overflow))
906 result))
909 ;;;-----------------------------------------------------------------------------
910 ;;; SIMPLUS (X W Z) 27.09.2010/DK
912 ;;; Arguments and values:
913 ;;; X - a Maxima expression of the form ((mplus) term1 term2 ...)
914 ;;; W - an arbitrary value, the value is ignored
915 ;;; Z - T or NIL, if T the arguments are assumed to be simplified
916 ;;; result - a simplified mplus-expression or an atom
918 ;;; Description:
919 ;;; Implementation of the simplifier for the "+" operator.
920 ;;; A general description of SIMPLUS can be found in the paper:
921 ;;; http://www.cs.berkeley.edu/~fateman/papers/simplifier.txt
923 ;;; Affected by:
924 ;;; The addition of matrices and lists is affected by the following option
925 ;;; variables:
926 ;;; $DOALLMXOPS, $DOMXMXOPS, $DOMXPLUS, $DOSCMXOPPS, $DOSCMXPLUS, $LISTARITH
928 ;;; Notes:
929 ;;; This routine should not be called directely. It is called by SIMPLIFYA.
930 ;;; A save access is to call the function ADD.
931 ;;;-----------------------------------------------------------------------------
933 (defun simplus (x w z)
934 (prog (res check eqnflag matrixflag sumflag)
935 (if (null (cdr x)) (return 0))
936 (setq check x)
937 start
938 (setq x (cdr x))
939 (if (null x) (go end))
940 (setq w (if z (car x) (simplifya (car x) nil)))
942 (cond ((atom w) nil)
943 ((eq (caar w) 'mrat)
944 (cond ((or eqnflag
945 matrixflag
946 (and sumflag
947 (not (member 'trunc (cdar w))))
948 (spsimpcases (cdr x) w))
949 (setq w (ratdisrep w))
950 (go st1))
952 (return
953 (ratf (cons '(mplus)
954 (nconc (mapcar #'simplify (cons w (cdr x)))
955 (cdr res))))))))
956 ((eq (caar w) 'mequal)
957 (setq eqnflag
958 (if (not eqnflag)
960 (list (car eqnflag)
961 (add2 (cadr eqnflag) (cadr w))
962 (add2 (caddr eqnflag) (caddr w)))))
963 (go start))
964 ((member (caar w) '(mlist $matrix))
965 (setq matrixflag
966 (cond ((not matrixflag) w)
967 ((and (or $doallmxops $domxmxops $domxplus
968 (and (eq (caar w) 'mlist)
969 ($listp matrixflag)))
970 (or (not (eq (caar w) 'mlist)) $listarith))
971 (addmx matrixflag w))
972 (t (setq res (pls w res)) matrixflag)))
973 (go start))
974 ((eq (caar w) '%sum)
975 (setq sumflag t res (sumpls w res))
976 (setq w (car res) res (cdr res))))
977 (setq res (pls w res))
978 (go start)
980 (setq res (testp res))
981 (if matrixflag
982 (setq res
983 (cond ((and (or ($listp matrixflag)
984 $doallmxops $doscmxplus $doscmxops)
985 (or (not ($listp matrixflag)) $listarith))
986 (mxplusc res matrixflag))
987 (t (testp (pls matrixflag (pls res nil)))))))
988 (setq res (eqtest res check))
989 (return (if eqnflag
990 (list (car eqnflag)
991 (add2 (cadr eqnflag) res)
992 (add2 (caddr eqnflag) res))
993 res))))
995 ;;;-----------------------------------------------------------------------------
996 ;;; PLS (X OUT) 27.09.2010/DK
998 ;;; Arguments and values:
999 ;;; X - a Maxima expression or an atom
1000 ;;; OUT - a form ((mplus) <number> term1 term2 ...) or NIL
1001 ;;; result - a form ((mplus) <number> term1 ...), where x is added in.
1003 ;;; Description:
1004 ;;; Adds the argument X into the form OUT. If OUT is NIL a form
1005 ;;; ((mplus) 0 X) is initialized, if X is an expression or a symbol,
1006 ;;; or ((mplus) X), if X is a number. Numbers are added to the first
1007 ;;; term <number> of the form. Any other symbol or expression is added
1008 ;;; into the canonical ordered list of arguments. The result is in a
1009 ;;; canonical order, but it is not a valid Maxima expression. To get a
1010 ;;; valid Maxima expression the result has to be checked with the
1011 ;;; function TESTP. This is done by the calling routine SIMPLUS.
1013 ;;; PLS checks the global flag *PLUSFLAG*, which is set in PLUSIN to T,
1014 ;;; if a mplus-expression is part of the result.
1016 ;;; Examples:
1017 ;;; (pls 2 nil) -> ((MPLUS) 2)
1018 ;;; (pls '$A nil) -> ((MPLUS) 0 $A)
1019 ;;; (pls '$B '((mplus) 0 $A)) -> ((MPLUS) 0 $A $B)
1020 ;;; (pls '$A '((mplus) 0 $A)) -> ((MPLUS) 0 ((MTIMES SIMP) 2 $A))
1022 ;;; Examples with the option variables $NUMER and $NEGDISTRIB:
1023 ;;; (let (($numer t)) (pls '$%e nil)) -> ((MPLUS) 2.718281828459045)
1024 ;;; (let (($negdistrib t)) (pls '((mtimes) -1 ((mplus) $A $B)) nil))
1025 ;;; -> ((MPLUS) 0 ((MTIMES SIMP) -1 $A) ((MTIMES SIMP) -1 $B))
1026 ;;; (let (($negdistrib nil)) (pls '((mtimes) -1 ((mplus) $A $B)) nil))
1027 ;;; -> ((MPLUS) 0 ((MTIMES) -1 ((MPLUS) $A $B)))
1029 ;;; Affected by:
1030 ;;; The option variables $NUMER and $NEGDISTRIB and the global flag
1031 ;;; *PLUSFLAG*, which is set in the routine PLUSIN.
1033 ;;; See also:
1034 ;;; PLUSIN and ADDK which are called from PLS and SIMPLUS.
1036 ;;; Notes:
1037 ;;; To add an expression into the list (CDR OUT), the list is passed
1038 ;;; to the routine PLUSIN as an argument. PLUSIN adds the argument to
1039 ;;; the list of terms by modifying the list (CDR OUT) destructively.
1040 ;;; The new value of OUT is returned as a result by PLS.
1041 ;;;-----------------------------------------------------------------------------
1043 ;; Set in PLUSIN to T to indicate a nested mplus expression.
1044 (defvar *plusflag* nil)
1046 ;; TESTP checks the result of PLS to get a valid Maxima mplus-expression.
1048 (defun testp (x)
1049 (cond ((atom x) 0)
1050 ((null (cddr x)) (cadr x))
1051 ((zerop1 (cadr x))
1052 (cond ((null (cdddr x)) (caddr x)) (t (rplacd x (cddr x)))))
1053 (t x)))
1055 (defun pls (x out)
1056 (prog (fm *plusflag*)
1057 (if (mtimesp x) (setq x (testtneg x)))
1058 (when (and $numer (atom x) (eq x '$%e))
1059 ;; Replace $%e with its numerical value, when $numer ist TRUE
1060 (setq x %e-val))
1061 (cond ((null out)
1062 ;; Initialize a form like ((mplus) <number> expr)
1063 (return
1064 (cons '(mplus)
1065 (cond ((mnump x) (ncons x))
1066 ((not (mplusp x))
1067 (list 0 (cond ((atom x) x) (t (copy-list x)))))
1068 ((mnump (cadr x)) (copy-list (cdr x) ))
1069 (t (cons 0 (copy-list (cdr x) )))))))
1070 ((mnump x)
1071 ;; Add a number into the first term of the list out.
1072 (return (cons '(mplus)
1073 (if (mnump (cadr out))
1074 (cons (addk (cadr out) x) (cddr out))
1075 (cons x (cdr out))))))
1076 ((not (mplusp x)) (plusin x (cdr out)) (go end)))
1077 ;; At this point we have a mplus expression as argument x. The following
1078 ;; code assumes that the argument x is already simplified and the terms
1079 ;; are in a canonical order.
1080 ;; First we add the number to the first term of the list out.
1081 (rplaca (cdr out)
1082 (addk (if (mnump (cadr out)) (cadr out) 0)
1083 (cond ((mnump (cadr x)) (setq x (cdr x)) (car x)) (t 0))))
1084 ;; Initialize fm with the list of terms and start the loop to add the
1085 ;; terms of an mplus expression into the list out.
1086 (setq fm (cdr out))
1087 start
1088 (if (null (setq x (cdr x))) (go end))
1089 ;; The return value of PLUSIN is a list, where the first element is the
1090 ;; added argument and the rest are the terms which follow the added
1091 ;; argument.
1092 (setq fm (plusin (car x) fm))
1093 (go start)
1095 (if (not *plusflag*) (return out))
1096 (setq *plusflag* nil) ; *PLUSFLAG* T handles e.g. a+b+3*(a+b)-2*(a+b)
1098 ;; *PLUSFLAG* is set by PLUSIN to indicate that a mplus expression is
1099 ;; part of the result. For this case go again through the terms of the
1100 ;; result and add any term of the mplus expression into the list out.
1101 (setq fm (cdr out))
1102 loop
1103 (when (mplusp (cadr fm))
1104 (setq x (cadr fm))
1105 (rplacd fm (cddr fm))
1106 (pls x out)
1107 (go a))
1108 (setq fm (cdr fm))
1109 (if (null (cdr fm)) (return out))
1110 (go loop)))
1112 ;;;-----------------------------------------------------------------------------
1113 ;;; PLUSIN (X FM) 27.09.2010/DK
1115 ;;; Arguments and values:
1116 ;;; X - a Maxima expression or atom
1117 ;;; FM - a list with the terms of an addition
1118 ;;; result - part of the list fm, which starts at the inserted expression
1120 ;;; Description:
1121 ;;; Adds X into running list of additive terms FM. The routine modifies
1122 ;;; the argument FM destructively, but does not return the modified list as
1123 ;;; a result. The return value is a part of the list FM, which starts at the
1124 ;;; inserted term. PLUSIN can not handle Maxima numbers. PLUSIN is called
1125 ;;; only from the routine PLS.
1127 ;;; Examples:
1128 ;;; (setq fm '(0))
1129 ;;; (plusin '$a fm) -> ($A)
1130 ;;; fm -> (0 $A)
1131 ;;; (plusin '$b fm) -> ($B)
1132 ;;; fm -> (0 $A $B)
1133 ;;; (plusin '$a fm) -> (((MTIMES SIMP) 2 $A) $B)
1134 ;;; fm -> (0 ((MTIMES SIMP) 2 $A) $B)
1136 ;;; Side effects:
1137 ;;; Modifies destructively the argument FM, which contains the result of the
1138 ;;; addition of the argument X into the list FM.
1140 ;;; Affected by;
1141 ;;; The option variables $doallmxops and $listarith.
1143 ;;; Notes:
1144 ;;; The return value is used in PLS to go in parallel through the list of
1145 ;;; terms, when adding a complete mplus-expression into the list of terms.
1146 ;;; This is triggered by the flag *PLUSFLAG*, which is set in PLUSIN, if
1147 ;;; a mplus-expression is added to the result list.
1148 ;;;-----------------------------------------------------------------------------
1150 (defun plusin (x fm)
1151 (prog (x1 x2 flag check v w xnew a n m c)
1152 (setq w 1)
1153 (setq v 1)
1154 (cond ((mtimesp x)
1155 (setq check x)
1156 (if (mnump (cadr x)) (setq w (cadr x) x (cddr x))
1157 (setq x (cdr x))))
1158 (t (setq x (ncons x))))
1159 (setq x1 (if (null (cdr x)) (car x) (cons '(mtimes) x))
1160 xnew (list* '(mtimes) w x))
1161 start
1162 (cond ((null (cdr fm)))
1163 ((and (alike1 x1 (cadr fm)) (null (cdr x)))
1164 (go equ))
1165 ;; Implement the simplification of
1166 ;; v*a^(c+n)+w*a^(c+m) -> (v*a^n+w*a^m)*a^c
1167 ;; where a, v, w, and (n-m) are integers.
1168 ((and (or (and (mexptp (setq x2 (cadr fm)))
1169 (setq v 1))
1170 (and (mtimesp x2)
1171 (not (alike1 x1 x2))
1172 (null (cadddr x2))
1173 (integerp (setq v (cadr x2)))
1174 (mexptp (setq x2 (caddr x2)))))
1175 (integerp (setq a (cadr x2)))
1176 (mexptp x1)
1177 (equal a (cadr x1))
1178 (integerp (sub (caddr x2) (caddr x1))))
1179 (setq n (if (and (mplusp (caddr x2))
1180 (mnump (cadr (caddr x2))))
1181 (cadr (caddr x2))
1182 (if (mnump (caddr x2))
1183 (caddr x2)
1184 0)))
1185 (setq m (if (and (mplusp (caddr x1))
1186 (mnump (cadr (caddr x1))))
1187 (cadr (caddr x1))
1188 (if (mnump (caddr x1))
1189 (caddr x1)
1190 0)))
1191 (setq c (sub (caddr x2) n))
1192 (cond ((integerp n)
1193 ;; The simple case:
1194 ;; n and m are integers and the result is (v*a^n+w*a^m)*a^c.
1195 (setq x1 (mul (addk (timesk v (exptb a n))
1196 (timesk w (exptb a m)))
1197 (power a c)))
1198 (go equt2))
1200 ;; n and m are rational numbers: The difference n-m is an
1201 ;; integer. The rational numbers might be improper fractions.
1202 ;; The mixed numbers are: n = n1 + d1/r and m = n2 + d2/r,
1203 ;; where r is the common denominator. We have two cases:
1204 ;; I) d1 = d2: e.g. 2^(1/3+c)+2^(4/3+c)
1205 ;; The result is (v*a^n1+w*a^n2)*a^(c+d1/r)
1206 ;; II) d1 # d2: e.g. 2^(1/2+c)+2^(-1/2+c)
1207 ;; In this case one of the exponents d1 or d2 must
1208 ;; be negative. The negative exponent is factored out.
1209 ;; This guarantees that the factor (v*a^n1+w*a^n2)
1210 ;; is an integer. But the positive exponent has to be
1211 ;; adjusted accordingly. E.g. when we factor out
1212 ;; a^(d2/r) because d2 is negative, then we have to
1213 ;; adjust the positive exponent to n1 -> n1+(d1-d2)/r.
1214 ;; Remark:
1215 ;; Part of the simplification is done in simptimes. E.g.
1216 ;; this algorithm simplifies the sum sqrt(2)+3*sqrt(2)
1217 ;; to 4*sqrt(2). In simptimes this is further simplified
1218 ;; to 2^(5/2).
1219 (multiple-value-bind (n1 d1)
1220 (truncate (num1 n) (denom1 n))
1221 (multiple-value-bind (n2 d2)
1222 (truncate (num1 m) (denom1 m))
1223 (cond ((equal d1 d2)
1224 ;; Case I: -> (v*a^n1+w*a^n2)*a^(c+d1/r)
1225 (setq x1
1226 (mul (addk (timesk v (exptb a n1))
1227 (timesk w (exptb a n2)))
1228 (power a
1229 (add c
1230 (div d1 (denom1 n))))))
1231 (go equt2))
1232 ((minusp d2)
1233 ;; Case II:: d2 is negative, adjust n1.
1234 (setq n1 (add n1 (div (sub d1 d2) (denom1 n))))
1235 (setq x1
1236 (mul (addk (timesk v (exptb a n1))
1237 (timesk w (exptb a n2)))
1238 (power a
1239 (add c
1240 (div d2 (denom1 n))))))
1241 (go equt2))
1242 ((minusp d1)
1243 ;; Case II: d1 is negative, adjust n2.
1244 (setq n2 (add n2 (div (sub d2 d1) (denom1 n))))
1245 (setq x1
1246 (mul (addk (timesk v (exptb a n1))
1247 (timesk w (exptb a n2)))
1248 (power a
1249 (add c
1250 (div d1 (denom1 n))))))
1251 (go equt2))
1252 ;; This clause should never be reached.
1253 (t (merror "Internal error in simplus."))))))))
1254 ((mtimesp (cadr fm))
1255 (cond ((alike1 x1 (cadr fm))
1256 (go equt))
1257 ((and (mnump (cadadr fm)) (alike x (cddadr fm)))
1258 (setq flag t) ; found common factor
1259 (go equt))
1260 ((great xnew (cadr fm)) (go gr))))
1261 ((great x1 (cadr fm)) (go gr)))
1262 (setq xnew (eqtest (testt xnew) (or check '((foo)))))
1263 (return (cdr (rplacd fm (cons xnew (cdr fm)))))
1265 (setq fm (cdr fm))
1266 (go start)
1268 (rplaca (cdr fm)
1269 (if (equal w -1)
1270 (list* '(mtimes simp) 0 x)
1271 ;; Call muln to get a simplified product.
1272 (if (mtimesp (setq x1 (muln (cons (addk 1 w) x) t)))
1273 (testtneg x1)
1274 x1)))
1276 (cond ((not (mtimesp (cadr fm)))
1277 (go check))
1278 ((onep (cadadr fm))
1279 ;; Do this simplification for an integer 1, not for 1.0 and 1.0b0
1280 (rplacd (cadr fm) (cddadr fm))
1281 (return (cdr fm)))
1282 ((not (zerop1 (cadadr fm)))
1283 (return (cdr fm)))
1284 ;; Handle the multiplication with a zero.
1285 ((and (or (not $listarith) (not $doallmxops))
1286 (mxorlistp (caddr (cadr fm))))
1287 (return (rplacd fm
1288 (cons (constmx 0 (caddr (cadr fm))) (cddr fm))))))
1289 ;; (cadadr fm) is zero. If the first term of fm is a number,
1290 ;; add it to preserve the type.
1291 (when (mnump (car fm))
1292 (rplaca fm (addk (car fm) (cadadr fm))))
1293 (return (rplacd fm (cddr fm)))
1294 equt
1295 ;; Call muln to get a simplified product.
1296 (setq x1 (muln (cons (addk w (if flag (cadadr fm) 1)) x) t))
1297 ;; Make a mplus expression to guarantee that x1 is added again into the sum
1298 (setq x1 (list '(mplus) x1))
1299 equt2
1300 (rplaca (cdr fm)
1301 (if (zerop1 x1)
1302 (list* '(mtimes) x1 x)
1303 (if (mtimesp x1) (testtneg x1) x1)))
1304 (if (not (mtimesp (cadr fm))) (go check))
1305 (when (and (onep (cadadr fm)) flag (null (cdddr (cadr fm))))
1306 ;; Do this simplification for an integer 1, not for 1.0 and 1.0b0
1307 (rplaca (cdr fm) (caddr (cadr fm))) (go check))
1308 (go del)
1309 check
1310 (if (mplusp (cadr fm)) (setq *plusflag* t)) ; A nested mplus expression
1311 (return (cdr fm))))
1313 ;;;-----------------------------------------------------------------------------
1315 ;; Routines to add matrices
1317 (defun mxplusc (sc mx)
1318 (cond ((mplusp sc)
1319 (setq sc (partition-ns (cdr sc)))
1320 (cond ((null (car sc)) (cons '(mplus) (cons mx (cadr sc))))
1321 ((not (null (cadr sc)))
1322 (cons '(mplus)
1323 (cons (simplify
1324 (outermap1 'mplus (cons '(mplus) (car sc)) mx))
1325 (cadr sc))))
1326 (t (simplify (outermap1 'mplus (cons '(mplus) (car sc)) mx)))))
1327 ((not (scalar-or-constant-p sc $assumescalar))
1328 (testp (pls mx (pls sc nil))))
1329 (t (simplify (outermap1 'mplus sc mx)))))
1331 (defun partition-ns (x)
1332 (let (sp nsp) ; SP = scalar part, NSP = nonscalar part
1333 (mapc #'(lambda (z) (if (scalar-or-constant-p z $assumescalar)
1334 (setq sp (cons z sp))
1335 (setq nsp (cons z nsp))))
1337 (list (nreverse sp) (nreverse nsp))))
1339 (defun addmx (x1 x2)
1340 (let (($doscmxops t) ($domxmxops t) ($listarith t))
1341 (simplify (fmapl1 'mplus x1 x2))))
1343 ;;; ----------------------------------------------------------------------------
1345 ;;; Simplification of the Log function
1347 ;; The log function distributes over lists, matrices, and equations
1348 (defprop %log (mlist $matrix mequal) distribute_over)
1350 (defun simpln (x y z)
1351 (oneargcheck x)
1352 (setq y (simpcheck (cadr x) z))
1353 (cond ((onep1 y) (addk -1 y))
1354 ((zerop1 y)
1355 (cond (radcanp (list '(%log simp) 0))
1356 ((not errorsw)
1357 (merror (intl:gettext "log: encountered log(0).")))
1358 (t (throw 'errorsw t))))
1359 ;; Check evaluation in floating point precision.
1360 ((flonum-eval (mop x) y))
1361 ;; Check evaluation in bigfloag precision.
1362 ((and (not (member 'simp (car x)))
1363 (big-float-eval (mop x) y)))
1364 ((eq y '$%e) 1)
1365 ((mexptp y)
1366 (cond ((or (and $logexpand (eq $domain '$real))
1367 (member $logexpand '($all $super))
1368 (and (eq ($csign (cadr y)) '$pos)
1369 (not (member ($csign (caddr y))
1370 '($complex $imaginary)))))
1371 ;; Simplify log(x^a) -> a*log(x), where x > 0 and a is real
1372 (mul (caddr y) (take '(%log) (cadr y))))
1373 ((or (and (ratnump (caddr y))
1374 (or (eql 1 (cadr (caddr y)))
1375 (eql -1 (cadr (caddr y)))))
1376 (maxima-integerp (inv (caddr y))))
1377 ;; Simplify log(z^(1/n)) -> log(z)/n, where n is an integer
1378 (mul (caddr y)
1379 (take '(%log) (cadr y))))
1380 ((and (eq (cadr y) '$%e)
1381 (or (not (member ($csign (caddr y))
1382 '($complex $imaginary)))
1383 (not (member ($csign (mul '$%i (caddr y)))
1384 '($complex $imaginary)))))
1385 ;; Simplify log(exp(x)) and log(exp(%i*x)), where x is a real
1386 (caddr y))
1387 (t (eqtest (list '(%log) y) x))))
1388 ((ratnump y)
1389 ;; Simplify log(n/d)
1390 (cond ((eql (cadr y) 1)
1391 (mul -1 (take '(%log) (caddr y))))
1392 ((eq $logexpand '$super)
1393 (sub (take '(%log) (cadr y)) (take '(%log) (caddr y))))
1394 (t (eqtest (list '(%log) y) x))))
1395 ((and (member $logexpand '($all $super))
1396 (mtimesp y))
1397 (do ((y (cdr y) (cdr y))
1398 (b nil))
1399 ((null y) (return (addn b t)))
1400 (setq b (cons (take '(%log) (car y)) b))))
1401 ((and (member $logexpand '($all $super))
1402 (consp y)
1403 (member (caar y) '(%product $product)))
1404 (let ((new-op (if (char= (get-first-char (caar y)) #\%) '%sum '$sum)))
1405 (simplifya `((,new-op) ((%log) ,(cadr y)) ,@(cddr y)) t)))
1406 ((and $lognegint
1407 (maxima-integerp y)
1408 (eq ($sign y) '$neg))
1409 (add (mul '$%i '$%pi) (take '(%log) (neg y))))
1410 ((taylorize (mop x) (second x)))
1411 (t (eqtest (list '(%log) y) x))))
1413 (defun simpln1 (w)
1414 (simplifya (list '(mtimes) (caddr w)
1415 (simplifya (list '(%log) (cadr w)) t)) t))
1417 ;;; ----------------------------------------------------------------------------
1419 ;;; Implementation of the Square root function
1421 (defprop $sqrt %sqrt verb)
1422 (defprop $sqrt %sqrt alias)
1424 (defprop %sqrt $sqrt noun)
1425 (defprop %sqrt $sqrt reversealias)
1427 (defprop %sqrt simp-sqrt operators)
1429 (defmfun $sqrt (z)
1430 (simplify (list '(%sqrt) z)))
1432 (defun simp-sqrt (x ignored z)
1433 (declare (ignore ignored))
1434 (oneargcheck x)
1435 (simplifya (list '(mexpt) (cadr x) '((rat simp) 1 2)) z))
1437 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1439 ;;; Simplification of the "/" operator.
1441 (defun simpquot (x y z)
1442 (twoargcheck x)
1443 (cond ((and (integerp (cadr x)) (integerp (caddr x)) (not (zerop (caddr x))))
1444 (*red (cadr x) (caddr x)))
1445 ((and (numberp (cadr x)) (numberp (caddr x)) (not (zerop (caddr x))))
1446 (/ (cadr x) (caddr x)))
1447 ((and (floatp (cadr x)) (floatp (caddr x)) #-ieee-floating-point (not (zerop (caddr x))))
1448 (/ (cadr x) (caddr x)))
1449 ((and ($bfloatp (cadr x)) ($bfloatp (caddr x)) (not (equal bigfloatzero (caddr x))))
1450 ;; Call BIGFLOATP to ensure that arguments have same precision.
1451 ;; Otherwise FPQUOTIENT could return a spurious value.
1452 (bcons (fpquotient (cdr (bigfloatp (cadr x))) (cdr (bigfloatp (caddr x))))))
1453 (t (setq y (simplifya (cadr x) z))
1454 (setq x (simplifya (list '(mexpt) (caddr x) -1) z))
1455 (if (equal y 1) x (simplifya (list '(mtimes) y x) t)))))
1457 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1459 ;;; Implementation of the abs function.
1461 ;; Put the properties alias, reversealiases, noun and verb on the property list.
1462 (defprop $abs mabs alias)
1463 (defprop $abs mabs verb)
1464 (defprop mabs $abs reversealias)
1465 (defprop mabs $abs noun)
1467 ;; The abs function distributes over bags.
1468 (defprop mabs (mlist $matrix mequal) distribute_over)
1470 ;; Define a verb function $abs
1471 (defmfun $abs (x)
1472 (simplify (list '(mabs) x)))
1474 ;; The abs function is a simplifying function.
1475 (defprop mabs simpabs operators)
1477 (defun simpabs (e y z)
1478 (declare (ignore y))
1479 (oneargcheck e)
1480 (let ((sgn)
1481 (x (simpcheck (second e) z)))
1483 (cond ((complex-number-p x #'(lambda (s) (or (floatp s) ($bfloatp s))))
1484 (maxima::to (bigfloat::abs (bigfloat:to x))))
1486 ((complex-number-p x #'mnump)
1487 ($cabs x))
1489 ;; nounform for arrays...
1490 ((or (arrayp x) ($member x $arrays)) `((mabs simp) ,x))
1492 ;; taylor polynomials
1493 ((taylorize 'mabs x))
1495 ;; values for extended real arguments:
1496 ((member x '($inf $infinity $minf)) '$inf)
1497 ((member x '($ind $und)) x)
1499 ;; abs(abs(expr)) --> abs(expr). Since x is simplified, it's OK to return x.
1500 ((and (consp x) (consp (car x)) (eq (caar x) 'mabs))
1503 ;; abs(conjugate(expr)) = abs(expr).
1504 ((and (consp x) (consp (car x)) (eq (caar x) '$conjugate))
1505 (take '(mabs) (cadr x)))
1508 (setq sgn ($csign x))
1509 (cond ((member sgn '($neg $nz)) (mul -1 x))
1510 ((eq '$zero sgn) (mul 0 x))
1511 ((member sgn '($pos $pz)) x)
1513 ;; for complex constant expressions, use $cabs
1514 ((and (eq sgn '$complex) ($constantp x))
1515 ($cabs x))
1517 ;; abs(pos^complex) --> pos^(realpart(complex)).
1518 ((and (eq sgn '$complex) (mexptp x) (eq '$pos ($csign (second x))))
1519 (power (second x) ($realpart (third x))))
1521 ;; for abs(neg^z), use cabs.
1522 ((and (mexptp x) (eq '$neg ($csign (second x))))
1523 ($cabs x))
1525 ;; When x # 0, we have abs(signum(x)) = 1.
1526 ((and (eq '$pn sgn) (consp x) (consp (car x)) (eq (caar x) '%signum)) 1)
1528 ;; multiplicative property: abs(x*y) = abs(x) * abs(y). We would like
1529 ;; assume(a*b > 0), abs(a*b) --> a*b. Thus the multiplicative property
1530 ;; is applied after the sign test.
1531 ((mtimesp x)
1532 (muln (mapcar #'(lambda (u) (take '(mabs) u)) (margs x)) t))
1534 ;; abs(x^n) = abs(x)^n for integer n. Is the featurep check worthwhile?
1535 ;; Again the sign check is done first because we'd like abs(x^2) --> x^2.
1536 ((and (mexptp x) ($featurep (caddr x) '$integer))
1537 (power (take '(mabs) (cadr x)) (caddr x)))
1539 ;; Reflection rule: abs(-x) --> abs(x)
1540 ((great (neg x) x) (take '(mabs) (neg x)))
1542 ;; nounform return
1543 (t (eqtest (list '(mabs) x) e)))))))
1545 (defun abs-integral (x)
1546 (mul (div 1 2) x (take '(mabs) x)))
1548 (putprop 'mabs `((x) ,'abs-integral) 'integral)
1550 ;; I (rtoy) think this does some simple optimizations of x * y.
1551 (defun testt (x)
1552 (cond ((mnump x)
1554 ((null (cddr x))
1555 ;; We have something like ((mtimes) foo). This is the same as foo.
1556 (cadr x))
1557 ((eql 1 (cadr x))
1558 ;; We have 1*foo. Which is the same as foo. This should not
1559 ;; be applied to 1.0 or 1b0!
1560 (cond ((null (cdddr x))
1561 (caddr x))
1562 (t (rplacd x (cddr x)))))
1564 (testtneg x))))
1566 ;; This basically converts -(a+b) to -a-b.
1567 (defun testtneg (x)
1568 (cond ((and (equal (cadr x) -1)
1569 (null (cdddr x))
1570 (mplusp (caddr x))
1571 $negdistrib)
1572 ;; If x is exactly of the form -1*(sum), and $negdistrib is
1573 ;; true, we distribute the -1 across the sum.
1574 (addn (mapcar #'(lambda (z)
1575 (mul2 -1 z))
1576 (cdaddr x))
1578 (t x)))
1580 ;; Simplification of the "-" operator
1581 (defun simpmin (x vestigial z)
1582 (declare (ignore vestigial))
1583 (cond ((null (cdr x)) 0)
1584 ((null (cddr x))
1585 (mul -1 (simplifya (cadr x) z)))
1587 ;; ((mminus) a b ...) -> ((mplus) a ((mtimes) -1 b) ...)
1588 (sub (simplifya (cadr x) z) (addn (cddr x) z)))))
1590 (defun simptimes (x w z) ; W must be 1
1591 (prog (res check eqnflag matrixflag sumflag)
1592 (if (null (cdr x)) (return 1))
1593 (setq check x)
1594 start
1595 (setq x (cdr x))
1596 (cond ((zerop1 res)
1597 (cond ($mx0simp
1598 (cond ((and matrixflag (mxorlistp1 matrixflag))
1599 (return (constmx res matrixflag)))
1600 (eqnflag (return (list '(mequal simp)
1601 (mul2 res (cadr eqnflag))
1602 (mul2 res (caddr eqnflag)))))
1604 (dolist (u x)
1605 (cond ((mxorlistp u)
1606 (return (setq res (constmx res u))))
1607 ((and (mexptp u)
1608 (mxorlistp1 (cadr u))
1609 ($numberp (caddr u)))
1610 (return (setq res (constmx res (cadr u)))))
1611 ((mequalp u)
1612 (return
1613 (setq res
1614 (list '(mequal simp)
1615 (mul2 res (cadr u))
1616 (mul2 res (caddr u))))))))))))
1617 (return res))
1618 ((null x) (go end)))
1619 (setq w (if z (car x) (simplifya (car x) nil)))
1621 (cond ((atom w) nil)
1622 ((eq (caar w) 'mrat)
1623 (cond ((or eqnflag matrixflag
1624 (and sumflag
1625 (not (member 'trunc (cdar w))))
1626 (spsimpcases (cdr x) w))
1627 (setq w (ratdisrep w))
1628 (go st1))
1630 (return
1631 (ratf (cons '(mtimes)
1632 (nconc (mapcar #'simplify (cons w (cdr x)))
1633 (cdr res))))))))
1634 ((eq (caar w) 'mequal)
1635 (setq eqnflag
1636 (if (not eqnflag)
1638 (list (car eqnflag)
1639 (mul2 (cadr eqnflag) (cadr w))
1640 (mul2 (caddr eqnflag) (caddr w)))))
1641 (go start))
1642 ((member (caar w) '(mlist $matrix))
1643 (setq matrixflag
1644 (cond ((not matrixflag) w)
1645 ((and (or $doallmxops $domxmxops $domxtimes)
1646 (or (not (eq (caar w) 'mlist)) $listarith)
1647 (not (eq *inv* '$detout)))
1648 (stimex matrixflag w))
1649 (t (setq res (tms (copy-tree w) 1 (copy-tree res))) matrixflag)))
1650 (go start))
1651 ((and (eq (caar w) '%sum) $sumexpand)
1652 (setq sumflag (sumtimes sumflag w))
1653 (go start)))
1654 (setq res (tms (copy-tree w) 1 (copy-tree res)))
1655 (go start)
1657 (cond ((mtimesp res) (setq res (testt res))))
1658 (cond (sumflag (setq res (cond ((or (null res) (equal res 1)) sumflag)
1659 ((not (mtimesp res))
1660 (list '(mtimes) res sumflag))
1661 (t (nconc res (list sumflag)))))))
1662 (cond ((or (atom res)
1663 (not (member (caar res) '(mexpt mtimes)))
1664 (and (zerop $expop) (zerop $expon))
1665 *expandflag*))
1666 ((eq (caar res) 'mtimes) (setq res (expandtimes res)))
1667 ((and (mplusp (cadr res))
1668 (fixnump (caddr res))
1669 (not (or (> (caddr res) $expop)
1670 (> (- (caddr res)) $expon))))
1671 (setq res (expandexpt (cadr res) (caddr res)))))
1672 (cond (matrixflag
1673 (setq res
1674 (cond ((null res) matrixflag)
1675 ((and (or ($listp matrixflag)
1676 $doallmxops
1677 (and $doscmxops
1678 (not (member res '(-1 -1.0))))
1679 ;; RES should only be -1 here (not = 1)
1680 (and $domxmxops
1681 (member res '(-1 -1.0))))
1682 (or (not ($listp matrixflag)) $listarith))
1683 (mxtimesc res matrixflag))
1684 (t (testt (tms matrixflag 1 (tms res 1 nil))))))))
1685 (if res (setq res (eqtest res check)))
1686 (return (cond (eqnflag
1687 (if (null res) (setq res 1))
1688 (list (car eqnflag)
1689 (mul2 (cadr eqnflag) res)
1690 (mul2 (caddr eqnflag) res)))
1691 (t res)))))
1693 (defun spsimpcases (l e)
1694 (dolist (u l)
1695 (if (or (mbagp u) (and (not (atom u))
1696 (eq (caar u) '%sum)
1697 (not (member 'trunc (cdar e)))))
1698 (return t))))
1700 (defun mxtimesc (sc mx)
1701 (let (sign out)
1702 (and (mtimesp sc) (member (cadr sc) '(-1 -1.0))
1703 $doscmxops (not (or $doallmxops $domxmxops $domxtimes))
1704 (setq sign (cadr sc)) (rplaca (cdr sc) nil))
1705 (setq out (let ((scp* (cond ((mtimesp sc) (partition-ns (cdr sc)))
1706 ((not (scalar-or-constant-p sc $assumescalar))
1707 nil)
1708 (t sc))))
1709 (cond ((null scp*) (list '(mtimes simp) sc mx))
1710 ((and (not (atom scp*)) (null (car scp*)))
1711 (append '((mtimes)) (cadr scp*) (list mx)))
1712 ((or (atom scp*) (and (null (cdr scp*))
1713 (not (null (cdr sc)))
1714 (setq scp* (cons '(mtimes) (car scp*))))
1715 (not (mtimesp sc)))
1716 (simplifya (outermap1 'mtimes scp* mx) nil))
1717 (t (append '((mtimes))
1718 (list (simplifya
1719 (outermap1 'mtimes
1720 (cons '(mtimes) (car scp*)) mx)
1722 (cadr scp*))))))
1723 (cond (sign (if (mtimesp out)
1724 (rplacd out (cons sign (cdr out)))
1725 (list '(mtimes) sign out)))
1726 ((mtimesp out) (testt out))
1727 (t out))))
1729 (defun stimex (x y)
1730 (let (($doscmxops t) ($domxmxops t) ($listarith t))
1731 (simplify (fmapl1 'mtimes x y))))
1733 ;; TMS takes a simplified expression FACTOR and a cumulative
1734 ;; PRODUCT as arguments and modifies the cumulative product so
1735 ;; that the expression is now one of its factors. The
1736 ;; exception to this occurs when a tellsimp rule is triggered.
1737 ;; The second argument is the POWER to which the expression is
1738 ;; to be raised within the product.
1740 (defun tms (factor power product &aux tem)
1741 (let ((*rulesw* nil)
1742 (z nil))
1743 (when (mplusp product) (setq product (list '(mtimes simp) product)))
1744 (cond ((zerop1 factor)
1745 (cond ((mnegp power)
1746 (if errorsw
1747 (throw 'errorsw t)
1748 (merror (intl:gettext "Division by 0"))))
1749 (t factor)))
1750 ((and (null product)
1751 (or (and (mtimesp factor) (equal power 1))
1752 (and (setq product (list '(mtimes) 1)) nil)))
1753 (setq tem (append '((mtimes)) (if (mnump (cadr factor)) nil '(1))
1754 (cdr factor) nil))
1755 (if (= (length tem) 1)
1756 (setq tem (copy-list tem))
1757 tem))
1758 ((mtimesp factor)
1759 (do ((factor-list (cdr factor) (cdr factor-list)))
1760 ((or (null factor-list) (zerop1 product)) product)
1761 (setq z (timesin (car factor-list) (cdr product) power))
1762 (when *rulesw*
1763 (setq *rulesw* nil)
1764 (setq product (tms-format-product z)))))
1766 (setq z (timesin factor (cdr product) power))
1767 (if *rulesw*
1768 (tms-format-product z)
1769 product)))))
1771 (defun tms-format-product (x)
1772 (cond ((zerop1 x) x)
1773 ((mnump x) (list '(mtimes) x))
1774 ((not (mtimesp x)) (list '(mtimes) 1 x))
1775 ((not (mnump (cadr x))) (cons '(mtimes) (cons 1 (cdr x))))
1776 (t x)))
1778 (defun plsk (x y)
1779 (cond ($ratsimpexpons (sratsimp (list '(mplus) x y)))
1780 ((and (mnump x) (mnump y)) (addk x y))
1781 (t (add2 x y))))
1783 (defun mult (x y)
1784 (if (and (mnump x) (mnump y))
1785 (timesk x y)
1786 (mul2 x y)))
1788 (defun simp-limit (x vestigial z)
1789 (declare (ignore vestigial))
1790 (let ((l1 (length x))
1792 (unless (or (= l1 2) (= l1 4) (= l1 5))
1793 (merror (intl:gettext "limit: wrong number of arguments.")))
1794 (setq y (simpmap (cdr x) z))
1795 (cond ((and (= l1 5) (not (member (cadddr y) '($plus $minus))))
1796 (merror (intl:gettext "limit: direction must be either 'plus' or 'minus': ~M") (cadddr y)))
1797 ((mnump (cadr y))
1798 (merror (intl:gettext "limit: variable must not be a number; found: ~M") (cadr y)))
1799 ((equal (car y) 1)
1802 (eqtest (cons '(%limit) y) x)))))
1804 (defun simpinteg (x vestigial z)
1805 (declare (ignore vestigial))
1806 (let ((l1 (length x))
1808 (unless (or (= l1 3) (= l1 5))
1809 (merror (intl:gettext "integrate: wrong number of arguments.")))
1810 (setq y (simpmap (cdr x) z))
1811 (cond ((mnump (cadr y))
1812 (merror (intl:gettext "integrate: variable must not be a number; found: ~M") (cadr y)))
1813 ((and (= l1 5) (alike1 (caddr y) (cadddr y)))
1815 ((and (= l1 5)
1816 (free (setq z (sub (cadddr y) (caddr y))) '$%i)
1817 (eq ($sign z) '$neg))
1818 (neg (simplifya (list '(%integrate) (car y) (cadr y) (cadddr y) (caddr y)) t)))
1819 ((equal (car y) 1)
1820 (if (= l1 3)
1821 (cadr y)
1822 (if (or (among '$inf z) (among '$minf z))
1823 (infsimp z)
1824 z)))
1826 (eqtest (cons '(%integrate) y) x)))))
1828 (defun simpbigfloat (x vestigial simp-flag)
1829 (declare (ignore vestigial simp-flag))
1830 (bigfloatm* x))
1832 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1834 ;;; Implementation of the Exp function.
1836 (defprop $exp %exp verb)
1837 (defprop $exp %exp alias)
1839 (defprop %exp $exp noun)
1840 (defprop %exp $exp reversealias)
1842 (defprop %exp simp-exp operators)
1844 (defmfun $exp (z)
1845 (simplify (list '(%exp) z)))
1847 ;; Support a function for code,
1848 ;; which depends on an unsimplified noun form.
1849 (defmfun $exp-form (z)
1850 (list '(mexpt) '$%e z))
1852 (defun simp-exp (x ignored z)
1853 (declare (ignore ignored))
1854 (oneargcheck x)
1855 (simplifya (list '(mexpt) '$%e (cadr x)) z))
1857 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1859 (defun simplambda (x vestigial simp-flag)
1860 (declare (ignore vestigial simp-flag))
1861 ; Check for malformed lambda expressions.
1862 ; We verify that we have a valid list of parameters and a non-empty body.
1863 (let ((params (cadr x)))
1864 (unless ($listp params)
1865 (merror (intl:gettext "lambda: first argument must be a list; found: ~M") params))
1866 (do ((params (cdr params) (cdr params))
1867 (seen-params nil))
1868 ((null params))
1869 (when (mdeflistp params)
1870 (setq params (cdar params)))
1871 (let ((p (car params)))
1872 (unless (or (mdefparam p)
1873 (and (op-equalp p 'mquote)
1874 (mdefparam (cadr p))))
1875 (merror (intl:gettext "lambda: parameter must be a symbol and must not be a system constant; found: ~M") p))
1876 (setq p (mparam p))
1877 (when (member p seen-params :test #'eq)
1878 (merror (intl:gettext "lambda: ~M occurs more than once in the parameter list") p))
1879 (push p seen-params))))
1880 (when (null (cddr x))
1881 (merror (intl:gettext "lambda: no body present")))
1882 (cons '(lambda simp) (cdr x)))
1884 (defun simpmdef (x vestigial simp-flag)
1885 (declare (ignore vestigial simp-flag))
1886 (twoargcheck x)
1887 (cons '(mdefine simp) (cdr x)))
1889 (defun simpmap (e z)
1890 (mapcar #'(lambda (u) (simpcheck u z)) e))
1892 (defun infsimp (e)
1893 (let ((x ($expand e 1 1)))
1894 (cond ((or (not (free x '$ind)) (not (free x '$und))
1895 (not (free x '$zeroa)) (not (free x '$zerob))
1896 (not (free x '$infinity))
1897 (mbagp x))
1898 (infsimp2 x e))
1899 ((and (free x '$inf) (free x '$minf)) x)
1900 (t (infsimp1 x e)))))
1902 (defun infsimp1 (x e)
1903 (let ((minf-coef (coeff x '$minf 1))
1904 (inf-coef (coeff x '$inf 1)))
1905 (cond ((or (and (equal minf-coef 0)
1906 (equal inf-coef 0))
1907 (and (not (free minf-coef '$inf))
1908 (not (free inf-coef '$minf)))
1909 (let ((new-exp (sub (add2 (mul2 minf-coef '$minf)
1910 (mul2 inf-coef '$inf))
1911 x)))
1912 (and (not (free new-exp '$inf))
1913 (not (free new-exp '$minf)))))
1914 (infsimp2 x e))
1915 (t (let ((sign-minf-coef ($asksign minf-coef))
1916 (sign-inf-coef ($asksign inf-coef)))
1917 (cond ((or (and (eq sign-inf-coef '$zero)
1918 (eq sign-minf-coef '$neg))
1919 (and (eq sign-inf-coef '$pos)
1920 (eq sign-minf-coef '$zero))
1921 (and (eq sign-inf-coef '$pos)
1922 (eq sign-minf-coef '$neg))) '$inf)
1923 ((or (and (eq sign-inf-coef '$zero)
1924 (eq sign-minf-coef '$pos))
1925 (and (eq sign-inf-coef '$neg)
1926 (eq sign-minf-coef '$zero))
1927 (and (eq sign-inf-coef '$neg)
1928 (eq sign-minf-coef '$pos))) '$minf)
1929 ((or (and (eq sign-inf-coef '$pos)
1930 (eq sign-minf-coef '$pos))
1931 (and (eq sign-inf-coef '$neg)
1932 (eq sign-minf-coef '$neg))) '$und)))))))
1934 (defun infsimp2 (x e)
1935 (setq x ($limit x))
1936 (if (isinop x '%limit) e x))
1938 (defun simpderiv (x y z)
1939 (prog (flag w u)
1940 (cond ((not (even (length x)))
1941 (cond ((and (cdr x) (null (cdddr x))) (nconc x '(1)))
1942 (t (wna-err '%derivative)))))
1943 (setq w (cons '(%derivative) (simpmap (cdr x) z)))
1944 (setq y (cadr w))
1945 (do ((u (cddr w) (cddr u))) ((null u))
1946 (cond ((mnump (car u))
1947 (merror (intl:gettext "diff: variable must not be a number; found: ~M") (car u)))))
1948 (cond ((or (zerop1 y)
1949 (and (or (mnump y) (and (atom y) (constant y)))
1950 (or (null (cddr w))
1951 (and (not (alike1 y (caddr w)))
1952 (do ((u (cddr w) (cddr u))) ((null u))
1953 (cond ((and (numberp (cadr u))
1954 (not (zerop (cadr u))))
1955 (return t))))))))
1956 (return 0))
1957 ((and (not (atom y)) (eq (caar y) '%derivative) derivsimp)
1958 (rplacd w (append (cdr y) (cddr w)))))
1959 (if (null (cddr w))
1960 (return (if (null derivflag) (list '(%del simp) y) (deriv (cdr w)))))
1961 (setq u (cdr w))
1962 ztest
1963 (cond ((null u) (go next))
1964 ((zerop1 (caddr u)) (rplacd u (cdddr u)))
1965 (t (setq u (cddr u))))
1966 (go ztest)
1967 next
1968 (cond ((null (cddr w)) (return y))
1969 ((and (null (cddddr w))
1970 (onep (cadddr w))
1971 (alike1 (cadr w) (caddr w)))
1972 (return 1)))
1973 again
1974 (setq z (cddr w))
1975 sort
1976 (cond ((null (cddr z)) (go loop))
1977 ((alike1 (car z) (caddr z))
1978 (rplaca (cdddr z) (add2 (cadr z) (cadddr z)))
1979 (rplacd z (cdddr z)))
1980 ((great (car z) (caddr z))
1981 (let ((u1 (car z)) (u2 (cadr z)) (v1 (caddr z)) (v2 (cadddr z)))
1982 (setq flag t)
1983 (rplaca z v1)
1984 (rplacd z (cons v2 (cons u1 (cons u2 (cddddr z))))))))
1985 (cond ((setq z (cddr z)) (go sort)))
1986 loop
1987 (cond ((null flag) (return (cond ((null derivflag) (eqtest w x))
1988 (t (deriv (cdr w)))))))
1989 (setq flag nil)
1990 (go again)))
1992 (defun signum1 (x)
1993 (cond ((mnump x)
1994 (setq x (num1 x)) (cond ((plusp x) 1) ((minusp x) -1) (t 0)))
1995 ((atom x) 1)
1996 ((mplusp x) (if *expandp* 1 (signum1 (car (last x)))))
1997 ((mtimesp x) (if (mplusp (cadr x)) 1 (signum1 (cadr x))))
1998 (t 1)))
2000 (defprop %signum (mlist $matrix mequal) distribute_over)
2002 (defun simpsignum (e y z)
2003 (declare (ignore y))
2004 (oneargcheck e)
2005 (let ((x (simpcheck (second e) z)) (sgn))
2007 (cond ((complex-number-p x #'mnump)
2008 (if (complex-number-p x #'$ratnump) ;; nonfloat complex
2009 (if (zerop1 x) 0 ($rectform (div x ($cabs x))))
2010 (maxima::to (bigfloat::signum (bigfloat::to x)))))
2012 ;; idempotent: signum(signum(z)) = signum(z).
2013 ((and (consp x) (consp (car x)) (eq '%signum (mop x))) x)
2016 (setq sgn ($csign x))
2017 (cond ((eq sgn '$neg) -1)
2018 ((eq sgn '$zero) 0)
2019 ((eq sgn '$pos) 1)
2021 ;; multiplicative: signum(ab) = signum(a) * signum(b).
2022 ((mtimesp x)
2023 (muln (mapcar #'(lambda (s) (take '(%signum) s)) (margs x)) t))
2025 ;; Reflection rule: signum(-x) --> -signum(x).
2026 ((great (neg x) x) (neg (take '(%signum) (neg x))))
2028 ;; nounform return
2029 (t (eqtest (list '(%signum) x) e)))))))
2031 (defun exptrl (r1 r2)
2032 (cond ((equal r2 1) r1)
2033 ((equal r2 1.0)
2034 (cond ((mnump r1) (addk 0.0 r1))
2035 ;; Do not simplify the type of the number away.
2036 (t (list '(mexpt simp) r1 1.0))))
2037 ((equal r2 bigfloatone)
2038 (cond ((mnump r1) ($bfloat r1))
2039 ;; Do not simplify the type of the number away.
2040 (t (list '(mexpt simp) r1 bigfloatone))))
2041 ((zerop1 r1)
2042 (cond ((or (zerop1 r2) (mnegp r2))
2043 (if (not errorsw)
2044 (merror (intl:gettext "expt: undefined: ~M") (list '(mexpt) r1 r2))
2045 (throw 'errorsw t)))
2046 (t (zerores r1 r2))))
2047 ((or (zerop1 r2) (onep1 r1))
2048 (cond ((or ($bfloatp r1) ($bfloatp r2)) bigfloatone)
2049 ((or (floatp r1) (floatp r2)) 1.0)
2050 (t 1)))
2051 ((or ($bfloatp r1) ($bfloatp r2)) ($bfloat (list '(mexpt) r1 r2)))
2052 ((and (numberp r1) (integerp r2)) (exptb r1 r2))
2053 ((and (numberp r1) (floatp r2) (equal r2 (float (floor r2))))
2054 (exptb (float r1) (floor r2)))
2055 ((or $numer (and (floatp r2) (or (plusp (num1 r1)) $numer_pbranch)))
2056 (let (y)
2057 (cond ((minusp (setq r1 (addk 0.0 r1)))
2058 (cond ((or $numer_pbranch (eq $domain '$complex))
2059 ;; for R1<0:
2060 ;; R1^R2 = (-R1)^R2*cos(pi*R2) + i*(-R1)^R2*sin(pi*R2)
2061 (setq r2 (addk 0.0 r2))
2062 (setq y (exptrl (- r1) r2) r2 (* (mget '$%pi '$numer) r2))
2063 (add2 (* y (cos r2))
2064 (list '(mtimes simp) (* y (sin r2)) '$%i)))
2065 (t (setq y (let ($numer $float $keepfloat $ratprint)
2066 (power -1 r2)))
2067 (mul2 y (exptrl (- r1) r2)))))
2068 ((equal (setq r2 (addk 0.0 r2)) (float (floor r2)))
2069 (exptb r1 (floor r2)))
2070 ((and (equal (setq y (* 2.0 r2)) (float (floor y)))
2071 (not (equal r1 %e-val)))
2072 (exptb (sqrt r1) (floor y)))
2073 (t (exp (* r2 (log r1)))))))
2074 ((floatp r2) (list '(mexpt simp) r1 r2))
2075 ((integerp r2)
2076 (cond ((minusp r2)
2077 (exptrl (cond ((equal (abs (cadr r1)) 1)
2078 (* (cadr r1) (caddr r1)))
2079 ;; We set the simp flag at this place. This
2080 ;; changes nothing for an exponent r2 # -1.
2081 ;; exptrl is called again and does not look at
2082 ;; the simp flag. For the case r2 = -1 exptrl
2083 ;; is called with an exponent 1. For this case
2084 ;; the base is immediately returned. Now the
2085 ;; base has the correct simp flag. (DK 02/2010)
2086 ((minusp (cadr r1))
2087 (list '(rat simp) (- (caddr r1)) (- (cadr r1))))
2088 (t (list '(rat simp) (caddr r1) (cadr r1))))
2089 (- r2)))
2090 (t (list '(rat simp) (exptb (cadr r1) r2) (exptb (caddr r1) r2)))))
2091 ((and (floatp r1) (alike1 r2 '((rat) 1 2)))
2092 (if (minusp r1)
2093 (list '(mtimes simp) (sqrt (- r1)) '$%i)
2094 (sqrt r1)))
2095 ((and (floatp r1) (alike1 r2 '((rat) -1 2)))
2096 (if (minusp r1)
2097 (list '(mtimes simp) (/ -1.0 (sqrt (- r1))) '$%i)
2098 (/ (sqrt r1))))
2099 ((floatp r1)
2100 (if (plusp r1)
2101 (exptrl r1 (fpcofrat r2))
2102 (mul2 (exptrl -1 r2) ;; (-4.5)^(1/4) -> (4.5)^(1/4) * (-1)^(1/4)
2103 (exptrl (- r1) r2))))
2104 (*exptrlsw* (list '(mexpt simp) r1 r2))
2106 (let ((*exptrlsw* t))
2107 (simptimes (list '(mtimes)
2108 (exptrl r1 (truncate (cadr r2) (caddr r2)))
2109 (let ((y (let ($keepfloat $ratprint)
2110 (simpnrt r1 (caddr r2))))
2111 (z (rem (cadr r2) (caddr r2))))
2112 (if (mexptp y)
2113 (list (car y) (cadr y) (mul2 (caddr y) z))
2114 (power y z))))
2115 1 t)))))
2117 (defun simpexpt (x y z)
2118 (prog (gr pot check res *rulesw* w mlpgr mlppot)
2119 (setq check x)
2120 (cond (z (setq gr (cadr x) pot (caddr x)) (go cont)))
2121 (twoargcheck x)
2122 (setq gr (simplifya (cadr x) nil))
2123 (setq pot
2124 (let (($%enumer $numer))
2125 ;; Switch $%enumer on, when $numer is TRUE to allow
2126 ;; simplification of $%e to its numerical value.
2127 (simplifya (if $ratsimpexpons ($ratsimp (caddr x)) (caddr x))
2128 nil)))
2129 cont
2130 (cond (($ratp pot)
2131 (setq pot (ratdisrep pot))
2132 (go cont))
2133 (($ratp gr)
2134 (cond ((member 'trunc (car gr))
2135 (return (srf (list '(mexpt) gr pot))))
2136 ((integerp pot)
2137 (let ((varlist (caddar gr)) (genvar (cadddr (car gr))))
2138 (return (ratrep* (list '(mexpt) gr pot)))))
2140 (setq gr (ratdisrep gr))
2141 (go cont))))
2142 ((or (setq mlpgr (mxorlistp gr))
2143 (setq mlppot (mxorlistp pot)))
2144 (go matrix))
2145 ((onep1 pot) (go atgr))
2146 ((or (zerop1 pot) (onep1 gr)) (go retno))
2148 ;; This code tries to handle 0^a more complete.
2149 ;; If the sign of realpart(a) is not known return an unsimplified
2150 ;; expression. The handling of the flag *zexptsimp? is not changed.
2151 ;; Reverting the return of an unsimplified 0^a, because timesin
2152 ;; can not handle such expressions. (DK 02/2010)
2153 ((zerop1 gr)
2154 (cond ((or (member (setq z ($csign pot)) '($neg $nz))
2155 (and *zexptsimp? (eq ($asksign pot) '$neg)))
2156 ;; A negative exponent. Maxima error.
2157 (cond ((not errorsw) (merror (intl:gettext "expt: undefined: 0 to a negative exponent.")))
2158 (t (throw 'errorsw t))))
2159 ((and (member z '($complex $imaginary))
2160 ;; A complex exponent. Look at the sign of the realpart.
2161 (member (setq z ($sign ($realpart pot)))
2162 '($neg $nz $zero)))
2163 (cond ((not errorsw)
2164 (merror (intl:gettext "expt: undefined: 0 to a complex exponent.")))
2165 (t (throw 'errorsw t))))
2166 ((and *zexptsimp? (eq ($asksign pot) '$zero))
2167 (cond ((not errorsw)
2168 (merror (intl:gettext "expt: undefined: 0^0")))
2169 (t (throw 'errorsw t))))
2170 ((not (member z '($pos $pz)))
2171 ;; The sign of realpart(pot) is not known. We can not return
2172 ;; an unsimplified 0^a expression, because timesin can not
2173 ;; handle it. We return ZERO. That is the old behavior.
2174 ;; Look for the imaginary symbol to be consistent with
2175 ;; old code.
2176 (cond ((not (free pot '$%i))
2177 (cond ((not errorsw)
2178 (merror (intl:gettext "expt: undefined: 0 to a complex exponent.")))
2179 (t (throw 'errorsw t))))
2181 ;; Return ZERO and not an unsimplified expression.
2182 (return (zerores gr pot)))))
2183 (t (return (zerores gr pot)))))
2185 ((and (mnump gr)
2186 (mnump pot)
2187 (or (not (ratnump gr)) (not (ratnump pot))))
2188 (return (eqtest (exptrl gr pot) check)))
2189 ;; Check for numerical evaluation of the sqrt.
2190 ((and (alike1 pot '((rat) 1 2))
2191 (or (setq res (flonum-eval '%sqrt gr))
2192 (and (not (member 'simp (car x)))
2193 (setq res (big-float-eval '%sqrt gr)))))
2194 (return res))
2195 ((eq gr '$%i)
2196 (return (%itopot pot)))
2197 ((and (realp gr) (minusp gr) (mevenp pot))
2198 (setq gr (- gr))
2199 (go cont))
2200 ((and (realp gr) (minusp gr) (moddp pot))
2201 (return (mul2 -1 (power (- gr) pot))))
2202 ((and (equal gr -1) (maxima-integerp pot) (mminusp pot))
2203 (setq pot (neg pot))
2204 (go cont))
2205 ((and (equal gr -1)
2206 (maxima-integerp pot)
2207 (mtimesp pot)
2208 (= (length pot) 3)
2209 (integerp (cadr pot))
2210 (oddp (cadr pot))
2211 (maxima-integerp (caddr pot)))
2212 (setq pot (caddr pot))
2213 (go cont))
2214 ((atom gr) (go atgr))
2215 ((and (eq (caar gr) 'mabs)
2216 (or (evnump pot)
2217 (mevenp pot))
2218 (or (and (eq $domain '$real) (not (apparently-complex-to-judge-by-$csign-p (cadr gr))))
2219 (and (eq $domain '$complex) (apparently-real-to-judge-by-$csign-p (cadr gr)))))
2220 (return (power (cadr gr) pot)))
2221 ((and (eq (caar gr) 'mabs)
2222 (integerp pot)
2223 (oddp pot)
2224 (not (equal pot -1))
2225 (or (and (eq $domain '$real) (not (apparently-complex-to-judge-by-$csign-p (cadr gr))))
2226 (and (eq $domain '$complex) (apparently-real-to-judge-by-$csign-p (cadr gr)))))
2227 ;; abs(x)^(2*n+1) -> abs(x)*x^(2*n), n an integer number
2228 (if (plusp pot)
2229 (return (mul (power (cadr gr) (add pot -1))
2230 gr))
2231 (return (mul (power (cadr gr) (add pot 1))
2232 (inv gr)))))
2233 ((eq (caar gr) 'mequal)
2234 (return (eqtest (list (ncons (caar gr))
2235 (power (cadr gr) pot)
2236 (power (caddr gr) pot))
2237 gr)))
2238 ((symbolp pot) (go opp))
2239 ((eq (caar gr) 'mexpt) (go e1))
2240 ((and (eq (caar gr) '%sum)
2241 $sumexpand
2242 (integerp pot)
2243 (signp g pot)
2244 (< pot $maxposex))
2245 (return (do ((i (1- pot) (1- i))
2246 (an gr (simptimes (list '(mtimes) an gr) 1 t)))
2247 ((signp e i) an))))
2248 ((equal pot -1)
2249 (return (eqtest (testt (tms gr pot nil)) check)))
2250 ((fixnump pot)
2251 (return (eqtest (cond ((and (mplusp gr)
2252 (not (or (> pot $expop)
2253 (> (- pot) $expon))))
2254 (expandexpt gr pot))
2255 (t (simplifya (tms gr pot nil) t)))
2256 check))))
2259 (cond ((eq (caar gr) 'mexpt) (go e1))
2260 ((eq (caar gr) 'rat)
2261 (return (mul2 (power (cadr gr) pot)
2262 (power (caddr gr) (mul2 -1 pot)))))
2263 ((not (eq (caar gr) 'mtimes)) (go up))
2264 ((or (eq $radexpand '$all) (and $radexpand (simplexpon pot)))
2265 (setq res (list 1))
2266 (go start))
2267 ((and (or (not (numberp (cadr gr)))
2268 (equal (cadr gr) -1))
2269 (equal -1 ($num gr)) ; only for -1
2270 ;; Do not simplify for a complex base.
2271 (not (member ($csign gr) '($complex $imaginary)))
2272 (and (eq $domain '$real) $radexpand))
2273 ;; (-1/x)^a -> 1/(-x)^a for x negative
2274 ;; For all other cases (-1)^a/x^a
2275 (if (eq ($csign (setq w ($denom gr))) '$neg)
2276 (return (inv (power (neg w) pot)))
2277 (return (div (power -1 pot)
2278 (power w pot)))))
2279 ((or (eq $domain '$complex) (not $radexpand)) (go up)))
2280 (return (do ((l (cdr gr) (cdr l)) (res (ncons 1)) (rad))
2281 ((null l)
2282 (cond ((equal res '(1))
2283 (eqtest (list '(mexpt) gr pot) check))
2284 ((null rad)
2285 (testt (cons '(mtimes simp) res)))
2287 (setq rad (power* ; RADEXPAND=()?
2288 (cons '(mtimes) (nreverse rad)) pot))
2289 (cond ((not (onep1 rad))
2290 (setq rad
2291 (testt (tms rad 1 (cons '(mtimes) res))))
2292 (cond (*rulesw*
2293 (setq *rulesw* nil res (cdr rad))))))
2294 (eqtest (testt (cons '(mtimes) res)) check))))
2295 ;; Check with $csign to be more complete. This prevents wrong
2296 ;; simplifications like sqrt(-z^2)->%i*sqrt(z^2) for z complex.
2297 (setq z ($csign (car l)))
2298 (if (member z '($complex $imaginary))
2299 (setq z '$pnz)) ; if appears complex, unknown sign
2300 (setq w (cond ((member z '($neg $nz))
2301 (setq rad (cons -1 rad))
2302 (mult -1 (car l)))
2303 (t (car l))))
2304 (cond ((onep1 w))
2305 ((alike1 w gr) (return (list '(mexpt simp) gr pot)))
2306 ((member z '($pn $pnz))
2307 (setq rad (cons w rad)))
2309 (setq w (testt (tms (simplifya (list '(mexpt) w pot) t)
2310 1 (cons '(mtimes) res))))))
2311 (cond (*rulesw* (setq *rulesw* nil res (cdr w))))))
2313 start
2314 (cond ((and (cdr res) (onep1 (car res)) (ratnump (cadr res)))
2315 (setq res (cdr res))))
2316 (cond ((null (setq gr (cdr gr)))
2317 (return (eqtest (testt (cons '(mtimes) res)) check)))
2318 ((mexptp (car gr))
2319 (setq y (power (cadar gr) (mult (caddar gr) pot))))
2320 ((eq (car gr) '$%i)
2321 (setq y (%itopot pot)))
2322 ((mnump (car gr))
2323 (setq y (list '(mexpt) (car gr) pot)))
2324 (t (setq y (list '(mexpt simp) (car gr) pot))))
2325 (setq w (testt (tms (simplifya y t) 1 (cons '(mtimes) res))))
2326 (cond (*rulesw* (setq *rulesw* nil res (cdr w))))
2327 (go start)
2329 retno
2330 (return (exptrl gr pot))
2332 atgr
2333 (cond ((zerop1 pot) (go retno))
2334 ((onep1 pot)
2335 (let ((y (mget gr '$numer)))
2336 (if (and y (floatp y) (or $numer (not (equal pot 1))))
2337 ;; A numeric constant like %e, %pi, ... and
2338 ;; exponent is a float or bigfloat value.
2339 (return (if (and (member gr *builtin-numeric-constants*)
2340 (equal pot bigfloatone))
2341 ;; Return a bigfloat value.
2342 ($bfloat gr)
2343 ;; Return a float value.
2345 ;; In all other cases exptrl simplifies accordingly.
2346 (return (exptrl gr pot)))))
2347 ((eq gr '$%e)
2348 ;; Numerically evaluate if the power is a flonum.
2349 (when $%emode
2350 (let ((val (flonum-eval '%exp pot)))
2351 (when (float-inf-p val)
2352 ;; needed for gcl and sbcl - (sometimes) no trap of overflow
2353 (error 'floating-point-overflow))
2354 (when val
2355 (return val)))
2356 ;; Numerically evaluate if the power is a (complex)
2357 ;; big-float. (This is basically the guts of
2358 ;; big-float-eval, but we can't use big-float-eval.)
2359 (when (and (not (member 'simp (car x)))
2360 (complex-number-p pot 'bigfloat-or-number-p))
2361 (let ((x ($realpart pot))
2362 (y ($imagpart pot)))
2363 (cond ((and ($bfloatp x) (like 0 y))
2364 (return ($bfloat `((mexpt simp) $%e ,pot))))
2365 ((or ($bfloatp x) ($bfloatp y))
2366 (let ((z (add ($bfloat x) (mul '$%i ($bfloat y)))))
2367 (setq z ($rectform `((mexpt simp) $%e ,z)))
2368 (return ($bfloat z))))))))
2369 (cond ((and $logsimp (among '%log pot)) (return (%etolog pot)))
2370 ((and $demoivre (setq z (demoivre pot))) (return z))
2371 ((and $%emode
2372 (among '$%i pot)
2373 (among '$%pi pot)
2374 ;; Exponent contains %i and %pi and %emode is TRUE:
2375 ;; Check simplification of exp(%i*%pi*p/q*x)
2376 (setq z (%especial pot)))
2377 (return z))
2378 (($taylorp (third x))
2379 ;; taylorize %e^taylor(...)
2380 (return ($taylor x)))))
2382 (let ((y (mget gr '$numer)))
2383 ;; Check for a numeric constant.
2384 (and y
2385 (floatp y)
2386 (or (floatp pot)
2387 ;; The exponent is a bigfloat. Convert base to bigfloat.
2388 (and ($bfloatp pot)
2389 (member gr *builtin-numeric-constants*)
2390 (setq y ($bfloat gr)))
2391 (and $numer (integerp pot)))
2392 (return (exptrl y pot))))))
2395 (return (eqtest (list '(mexpt) gr pot) check))
2397 matrix
2398 (cond ((zerop1 pot)
2399 (cond ((mxorlistp1 gr) (return (constmx (addk 1 pot) gr)))
2400 (t (go retno))))
2401 ((onep1 pot) (return gr))
2402 ((or $doallmxops $doscmxops $domxexpt)
2403 (cond ((or (and mlpgr
2404 (or (not ($listp gr)) $listarith)
2405 (scalar-or-constant-p pot $assumescalar))
2406 (and $domxexpt
2407 mlppot
2408 (or (not ($listp pot)) $listarith)
2409 (scalar-or-constant-p gr $assumescalar)))
2410 (return (simplifya (outermap1 'mexpt gr pot) t)))
2411 (t (go up))))
2412 ((and $domxmxops (member pot '(-1 -1.0)))
2413 (return (simplifya (outermap1 'mexpt gr pot) t)))
2414 (t (go up)))
2416 ;; At this point we have an expression: (z^a)^b with gr = z^a and pot = b
2417 (cond ((or (eq $radexpand '$all)
2418 ;; b is an integer or an odd rational
2419 (simplexpon pot)
2420 (and (eq $domain '$complex)
2421 (not (member ($csign (caddr gr)) '($complex $imaginary)))
2422 ;; z >= 0 and a not a complex
2423 (or (member ($csign (cadr gr)) '($pos $pz $zero))
2424 ;; -1 < a <= 1
2425 (and (mnump (caddr gr))
2426 (eq ($sign (sub 1 (take '(mabs) (caddr gr))))
2427 '$pos))))
2428 (and (eq $domain '$real)
2429 (member ($csign (cadr gr)) '($pos $pz $zero)))
2430 ;; (1/z)^a -> 1/z^a when z a constant complex
2431 (and (eql (caddr gr) -1)
2432 (or (and $radexpand
2433 (eq $domain '$real))
2434 (and (eq ($csign (cadr gr)) '$complex)
2435 ($constantp (cadr gr)))))
2436 ;; This does (1/z)^a -> 1/z^a. This is in general wrong.
2437 ;; We switch this type of simplification on, when
2438 ;; $ratsimpexpons is T. E.g. radcan sets this flag to T.
2439 ;; radcan hangs for expressions like sqrt(1/(1+x)) without
2440 ;; this simplification.
2441 (and $ratsimpexpons
2442 (equal (caddr gr) -1))
2443 (and $radexpand
2444 (eq $domain '$real)
2445 (odnump (caddr gr))))
2446 ;; Simplify (z^a)^b -> z^(a*b)
2447 (setq pot (mul pot (caddr gr))
2448 gr (cadr gr)))
2449 ((and (eq $domain '$real)
2450 (free gr '$%i)
2451 $radexpand
2452 (not (apparently-complex-to-judge-by-$csign-p (cadr gr)))
2453 (evnump (caddr gr)))
2454 ;; Simplify (x^a)^b -> abs(x)^(a*b)
2455 (setq pot (mul pot (caddr gr))
2456 gr (radmabs (cadr gr))))
2457 ((and $radexpand
2458 (eq $domain '$real)
2459 (mminusp (caddr gr)))
2460 ;; Simplify (1/z^a)^b -> 1/(z^a)^b
2461 (setq pot (neg pot)
2462 gr (power (cadr gr) (neg (caddr gr)))))
2463 (t (go up)))
2464 (go cont)))
2466 (defun apparently-complex-to-judge-by-$csign-p (e)
2467 (let ((s ($csign e)))
2468 (member s '($complex $imaginary))))
2470 (defun apparently-real-to-judge-by-$csign-p (e)
2471 (let ((s ($csign e)))
2472 (member s '($pos $neg $zero $pn $pnz $pz $nz))))
2474 ;; Basically computes log of m base b. Except if m is not a power
2475 ;; of b, we return nil. m is a positive integer and base an integer
2476 ;; not equal to +/-1.
2477 (defun exponent-of (m base)
2478 ;; Just compute base^k until base^k >= m. Then check if they're equal.
2479 ;; If so, we have the exponent. Otherwise, give up.
2480 (let ((expo 0))
2481 (loop
2482 (multiple-value-bind (q r)
2483 (floor m base)
2484 (cond ((zerop r)
2485 (setf m q)
2486 (incf expo))
2487 (t (return nil)))))
2488 (if (zerop expo) nil expo)))
2490 (defun timesin (x y w) ; Multiply X^W into Y
2491 (prog (fm temp z check u expo)
2492 (if (mexptp x) (setq check x))
2494 ;; Prepare the factor x^w and initialize the work of timesin
2495 (cond ((equal w 1)
2496 (setq temp x))
2498 (setq temp (cons '(mexpt) (if check
2499 (list (cadr x) (mult (caddr x) w))
2500 (list x w))))
2501 (if (and (not *timesinp*) (not (eq x '$%i)))
2502 (let ((*timesinp* t))
2503 (setq temp (simplifya temp t))))))
2504 (setq x (if (mexptp temp)
2505 (cdr temp)
2506 (list temp 1)))
2507 (setq w (cadr x)
2508 fm y)
2509 start
2510 ;; Go through the list of terms in fm and look what is to do.
2511 (cond ((null (cdr fm))
2512 ;; The list of terms is empty. The loop is finshed.
2513 (go less))
2514 ((or (and (mnump temp)
2515 (not (or (integerp temp)
2516 (ratnump temp))))
2517 (and (integerp temp)
2518 (equal temp -1)))
2519 ;; Stop the loop for a float or bigfloat number, or number -1.
2520 (go less))
2521 ((mexptp (cadr fm))
2522 (cond ((alike1 (car x) (cadadr fm))
2523 (cond ((zerop1 (setq w (plsk (caddr (cadr fm)) w)))
2524 (go del))
2525 ((and (mnump w)
2526 (or (mnump (car x))
2527 (eq (car x) '$%i)))
2528 (rplacd fm (cddr fm))
2529 (cond ((mnump (setq x (if (mnump (car x))
2530 (exptrl (car x) w)
2531 (power (car x) w))))
2532 (return (rplaca y (timesk (car y) x))))
2533 ((mtimesp x)
2534 (go times))
2536 (setq temp x
2537 x (if (mexptp x) (cdr x) (list x 1)))
2538 (setq w (cadr x)
2539 fm y)
2540 (go start))))
2541 ((maxima-constantp (car x))
2542 (go const))
2543 ((onep1 w)
2544 (cond ((mtimesp (car x))
2545 ;; A base which is a mtimes expression. Remove
2546 ;; the factor from the lists of products.
2547 (rplacd fm (cddr fm))
2548 ;; Multiply the factors of the base with
2549 ;; the list of all remaining products.
2550 (setq *rulesw* t)
2551 (return (muln (nconc y (cdar x)) t)))
2552 (t (return (rplaca (cdr fm) (car x))))))
2554 (go spcheck))))
2555 ;; At this place we have to add code for a rational number
2556 ;; as a factor to the list of products.
2557 ((and (onep1 w)
2558 (or (ratnump (car x))
2559 (and (integerp (car x))
2560 (not (onep (car x))))))
2561 ;; Multiplying bas^k * num/den
2562 (let ((num (num1 (car x)))
2563 (den (denom1 (car x)))
2564 (bas (second (cadr fm))))
2565 (cond ((and (integerp bas)
2566 (not (eql 1 (abs bas)))
2567 (setq expo (exponent-of (abs num) bas)))
2568 ;; We have bas^m*bas^k = bas^(k+m).
2569 (setq temp (power bas
2570 (add (third (cadr fm)) expo)))
2571 ;; Set fm to have 1/denom term.
2572 (setq x (mul (car y)
2573 (div (div num
2574 (exptrl bas expo))
2575 den))))
2576 ((and (integerp bas)
2577 (not (eql 1 (abs bas)))
2578 (setq expo (exponent-of den bas)))
2579 (setq expo (- expo))
2580 ;; We have bas^(-m)*bas^k = bas^(k-m).
2581 (setq temp (power bas
2582 (add (third (cadr fm)) expo)))
2583 ;; Set fm to have the numerator term.
2584 (setq x (mul (car y)
2585 (div num
2586 (div den
2587 (exptrl bas (- expo)))))))
2589 ;; Next term in list of products.
2590 (setq fm (cdr fm))
2591 (go start)))
2592 ;; Add in the bas^(k+m) term or bas^(k-m)
2593 (setf y (rplaca y 1))
2594 (rplacd fm (cddr fm))
2595 (rplacd fm (cons temp (cdr fm)))
2596 (setq temp x
2597 x (list x 1)
2599 fm y)
2600 (go start)))
2601 ((and (not (atom (car x)))
2602 (eq (caar (car x)) 'mabs)
2603 (equal (cadr x) 1)
2604 (integerp (caddr (cadr fm)))
2605 (< (caddr (cadr fm)) -1)
2606 (alike1 (cadr (car x)) (cadr (cadr fm)))
2607 (not (member ($csign (cadr (car x)))
2608 '($complex imaginary))))
2609 ;; 1/x^n*abs(x) -> 1/(x^(n-2)*abs(x)), where n an integer
2610 ;; Replace 1/x^n -> 1/x^(n-2)
2611 (setq temp (power (cadr (cadr fm))
2612 (add (caddr (cadr fm)) 2)))
2613 (rplacd fm (cddr fm))
2614 (if (not (equal temp 1))
2615 (rplacd fm (cons temp (cdr fm))))
2616 ;; Multiply factor 1/abs(x) into list of products.
2617 (setq x (list (car x) -1))
2618 (setq temp (power (car x) (cadr x)))
2619 (setq w (cadr x))
2620 (go start))
2622 ((and (not (atom (car x)))
2623 (eq (caar (car x)) 'mabs)
2624 (equal (cadr x) -1)
2625 (integerp (caddr (cadr fm)))
2626 (> (caddr (cadr fm)) 1)
2627 (alike1 (cadr (car x)) (cadr (cadr fm)))
2628 (not (member ($csign (cadr (car x)))
2629 '($complex imaginary))))
2630 ;; x^n/abs(x) -> x^(n-2)*abs(x), where n an integer.
2631 ;; Replace x^n -> x^(n-2)
2632 (setq temp (power (cadr (cadr fm))
2633 (add (caddr (cadr fm)) -2)))
2634 (rplacd fm (cddr fm))
2635 (if (not (equal temp 1))
2636 (rplacd fm (cons temp (cdr fm))))
2637 ;; Multiply factor abs(x) into list of products.
2638 (setq x (list (car x) 1))
2639 (setq temp (power (car x) (cadr x)))
2640 (setq w (cadr x))
2641 (go start))
2643 ((and (not (atom (cadr fm)))
2644 (not (atom (cadr (cadr fm))))
2645 (eq (caaadr (cadr fm)) 'mabs)
2646 (equal (caddr (cadr fm)) -1)
2647 (integerp (cadr x))
2648 (> (cadr x) 1)
2649 (alike1 (cadadr (cadr fm)) (car x))
2650 (not (member ($csign (cadadr (cadr fm)))
2651 '($complex imaginary))))
2652 ;; 1/abs(x)*x^n -> x^(n-2)*abs(x), where n an integer.
2653 ;; Replace 1/abs(x) -> abs(x)
2654 (setq temp (cadr (cadr fm)))
2655 (rplacd fm (cddr fm))
2656 (rplacd fm (cons temp (cdr fm)))
2657 ;; Multiply factor x^(n-2) into list of products.
2658 (setq x (list (car x) (add (cadr x) -2)))
2659 (setq temp (power (car x) (cadr x)))
2660 (setq w (cadr x))
2661 (go start))
2663 ((or (maxima-constantp (car x))
2664 (maxima-constantp (cadadr fm)))
2665 (if (great temp (cadr fm))
2666 (go gr)))
2667 ((great (car x) (cadadr fm))
2668 (go gr)))
2669 (go less))
2670 ((alike1 (car x) (cadr fm))
2671 (go equ))
2672 ((mnump temp)
2673 ;; When a number goto start and look in the next term.
2674 (setq fm (cdr fm))
2675 (go start))
2677 ((and (not (atom (cadr fm)))
2678 (eq (caar (cadr fm)) 'mabs)
2679 (integerp (cadr x))
2680 (< (cadr x) -1)
2681 (alike1 (cadr (cadr fm)) (car x))
2682 (not (member ($csign (cadr (cadr fm)))
2683 '($complex imaginary))))
2684 ;; abs(x)/x^n -> 1/(x^(n-2)*abs(x)), where n an integer.
2685 ;; Replace abs(x) -> 1/abs(x).
2686 (setq temp (power (cadr fm) -1))
2687 (rplacd fm (cddr fm))
2688 (rplacd fm (cons temp (cdr fm)))
2689 ;; Multiply factor x^(-n+2) into list of products.
2690 (setq x (list (car x) (add (cadr x) 2)))
2691 (setq temp (power (car x) (cadr x)))
2692 (setq w (cadr x))
2693 (go start))
2695 ((maxima-constantp (car x))
2696 (when (great temp (cadr fm))
2697 (go gr)))
2698 ((great (car x) (cadr fm))
2699 (go gr)))
2700 less
2701 (cond ((mnump temp)
2702 ;; Multiply a number into the list of products.
2703 (return (rplaca y (timesk (car y) temp))))
2704 ((and (eq (car x) '$%i)
2705 (fixnump w))
2706 (go %i))
2707 ((and (eq (car x) '$%e)
2708 $numer
2709 (integerp w))
2710 (return (rplaca y (timesk (car y) (exp (float w))))))
2711 ((and (onep1 w)
2712 (not (constant (car x))))
2713 (go less1))
2714 ;; At this point we will insert a mexpt expression,
2715 ;; but first we look at the car of the list of products and
2716 ;; modify the expression if we found a rational number.
2717 ((and (mexptp temp)
2718 (not (onep1 (car y)))
2719 (or (integerp (car y))
2720 (ratnump (car y))))
2721 ;; Multiplying bas^k * num/den.
2722 (let ((num (num1 (car y)))
2723 (den (denom1 (car y)))
2724 (bas (car x)))
2725 (cond ((and (integerp bas)
2726 (not (eql 1 (abs bas)))
2727 (setq expo (exponent-of (abs num) bas)))
2728 ;; We have bas^m*bas^k.
2729 (setq temp (power bas (add (cadr x) expo)))
2730 ;; Set fm to have 1/denom term.
2731 (setq x (div (div num (exptrl bas expo)) den)))
2732 ((and (integerp bas)
2733 (not (eql 1 (abs bas)))
2734 (setq expo (exponent-of den bas)))
2735 (setq expo (- expo))
2736 ;; We have bas^(-m)*bas^k.
2737 (setq temp (power bas (add (cadr x) expo)))
2738 ;; Set fm to have the numerator term.
2739 (setq x (div num (div den (exptrl bas (- expo))))))
2741 ;; The rational doesn't contain any (simple) powers of
2742 ;; the exponential term. We're done.
2743 (return (cdr (rplacd fm (cons temp (cdr fm)))))))
2744 ;; Add in the a^(m+k) or a^(k-m) term.
2745 (setf y (rplaca y 1))
2746 (rplacd fm (cons temp (cdr fm)))
2747 (setq temp x
2748 x (list x 1)
2750 fm y)
2751 (go start)))
2752 ((and (maxima-constantp (car x))
2753 (do ((l (cdr fm) (cdr l)))
2754 ((null (cdr l)))
2755 (when (and (mexptp (cadr l))
2756 (alike1 (car x) (cadadr l)))
2757 (setq fm l)
2758 (return t))))
2759 (go start))
2760 ((or (and (mnump (car x))
2761 (mnump w))
2762 (and (eq (car x) '$%e)
2763 $%emode
2764 (among '$%i w)
2765 (among '$%pi w)
2766 (setq u (%especial w))))
2767 (setq x (cond (u)
2768 ((alike (cdr check) x)
2769 check)
2771 (exptrl (car x) w))))
2772 (cond ((mnump x)
2773 (return (rplaca y (timesk (car y) x))))
2774 ((mtimesp x)
2775 (go times))
2776 ((mexptp x)
2777 (return (cdr (rplacd fm (cons x (cdr fm))))))
2779 (setq temp x
2780 x (list x 1)
2782 fm y)
2783 (go start))))
2784 ((onep1 w)
2785 (go less1))
2787 (setq temp (list '(mexpt) (car x) w))
2788 (setq temp (eqtest temp (or check '((foo)))))
2789 (return (cdr (rplacd fm (cons temp (cdr fm)))))))
2790 less1
2791 (return (cdr (rplacd fm (cons (car x) (cdr fm)))))
2793 (setq fm (cdr fm))
2794 (go start)
2796 (cond ((and (eq (car x) '$%i) (equal w 1))
2797 (rplacd fm (cddr fm))
2798 (return (rplaca y (timesk -1 (car y)))))
2799 ((zerop1 (setq w (plsk 1 w)))
2800 (go del))
2801 ((and (mnump (car x)) (mnump w))
2802 (return (rplaca (cdr fm) (exptrl (car x) w))))
2803 ((maxima-constantp (car x))
2804 (go const)))
2805 spcheck
2806 (setq z (list '(mexpt) (car x) w))
2807 (cond ((alike1 (setq x (simplifya z t)) z)
2808 (return (rplaca (cdr fm) x)))
2810 (rplacd fm (cddr fm))
2811 (setq *rulesw* t)
2812 (return (muln (cons x y) t))))
2813 const
2814 (rplacd fm (cddr fm))
2815 (setq x (car x) check nil)
2816 (go top)
2817 times
2818 (setq z (tms x 1 (setq temp (cons '(mtimes) y))))
2819 (return (cond ((eq z temp)
2820 (cdr z))
2822 (setq *rulesw* t) z)))
2824 (return (rplacd fm (cddr fm)))
2826 (if (minusp (setq w (rem w 4)))
2827 (incf w 4))
2828 (return (cond ((zerop w)
2830 ((= w 2)
2831 (rplaca y (timesk -1 (car y))))
2832 ((= w 3)
2833 (rplaca y (timesk -1 (car y)))
2834 (rplacd fm (cons '$%i (cdr fm))))
2836 (rplacd fm (cons '$%i (cdr fm))))))))
2838 (defun simpmatrix (x vestigial z)
2839 (declare (ignore vestigial))
2840 (if (and (null (cddr x))
2841 $scalarmatrixp
2842 (or (eq $scalarmatrixp '$all) (member 'mult (cdar x)))
2843 ($listp (cadr x)) (cdadr x) (null (cddadr x)))
2844 (simplifya (cadadr x) z)
2845 (let ((badp (dolist (row (cdr x)) (if (not ($listp row)) (return t))))
2846 (args (simpmap (cdr x) z)))
2847 (if (and args (not badp)) (matcheck args))
2848 (cons (if badp '(%matrix simp) '($matrix simp)) args))))
2850 (defun %itopot (pot)
2851 (if (fixnump pot)
2852 (let ((i (boole boole-and pot 3)))
2853 (cond ((= i 0) 1)
2854 ((= i 1) '$%i)
2855 ((= i 2) -1)
2856 (t (list '(mtimes simp) -1 '$%i))))
2857 (power -1 (mul2 pot '((rat simp) 1 2)))))
2859 (defun mnlogp (pot)
2860 (cond ((eq (caar pot) '%log) (simplifya (cadr pot) nil))
2861 ((and (eq (caar pot) 'mtimes)
2862 (or (maxima-integerp (cadr pot))
2863 (and $%e_to_numlog ($numberp (cadr pot))))
2864 (not (atom (caddr pot))) (eq (caar (caddr pot)) '%log)
2865 (null (cdddr pot)))
2866 (power (cadr (caddr pot)) (cadr pot)))))
2868 (defun mnlog (pot)
2869 (prog (a b c)
2870 loop (cond ((null pot)
2871 (cond (a (setq a (cons '(mtimes) a))))
2872 (cond (c (setq c (list '(mexpt simp) '$%e (addn c nil)))))
2873 (return (cond ((null c) (simptimes a 1 nil))
2874 ((null a) c)
2875 (t (simptimes (append a (list c)) 1 nil)))))
2876 ((and (among '%log (car pot)) (setq b (mnlogp (car pot))))
2877 (setq a (cons b a)))
2878 (t (setq c (cons (car pot) c))))
2879 (setq pot (cdr pot))
2880 (go loop)))
2882 (defun %etolog (pot) (cond ((mnlogp pot))
2883 ((eq (caar pot) 'mplus) (mnlog (cdr pot)))
2884 (t (list '(mexpt simp) '$%e pot))))
2886 (defun zerores (r1 r2)
2887 (cond ((or ($bfloatp r1) ($bfloatp r2)) bigfloatzero)
2888 ((or (floatp r1) (floatp r2)) 0.0)
2889 (t 0)))
2891 (defmfun $orderlessp (a b)
2892 (setq a ($totaldisrep (specrepcheck a))
2893 b ($totaldisrep (specrepcheck b)))
2894 (and (not (alike1 a b)) (great b a) t))
2896 (defmfun $ordergreatp (a b)
2897 (setq a ($totaldisrep (specrepcheck a))
2898 b ($totaldisrep (specrepcheck b)))
2899 (and (not (alike1 a b)) (great a b) t))
2901 ;; Test function to order a and b by magnitude. If it is not possible to
2902 ;; order a and b by magnitude they are ordered by great. This function
2903 ;; can be used by sort, e.g. sort([3,1,7,x,sin(1),minf],ordermagnitudep)
2904 (defmfun $ordermagnitudep (a b)
2905 (let (sgn)
2906 (setq a ($totaldisrep (specrepcheck a))
2907 b ($totaldisrep (specrepcheck b)))
2908 (cond ((and (or (constp a) (member a '($inf $minf)))
2909 (or (constp b) (member b '($inf $minf)))
2910 (member (setq sgn ($csign (sub b a))) '($pos $neg $zero)))
2911 (cond ((eq sgn '$pos) t)
2912 ((eq sgn '$zero) (and (not (alike1 a b)) (great b a)))
2913 (t nil)))
2914 ((or (constp a) (member a '($inf $minf))) t)
2915 ((or (constp b) (member b '($inf $minf))) nil)
2916 (t (and (not (alike1 a b)) (great b a))))))
2918 (defun evnump (n) (or (even n) (and (ratnump n) (even (cadr n)))))
2919 (defun odnump (n) (or (and (integerp n) (oddp n))
2920 (and (ratnump n) (oddp (cadr n)))))
2922 (defun simplexpon (e)
2923 (or (maxima-integerp e)
2924 (and (eq $domain '$real) (ratnump e) (oddp (caddr e)))))
2926 ;; This function is not called in Maxima core or share code
2927 ;; and can be cut out.
2928 (defun noneg (p)
2929 (and (free p '$%i) (member ($sign p) '($pos $pz $zero))))
2931 (defun radmabs (e)
2932 (if (and limitp (free e '$%i)) (asksign-p-or-n e))
2933 (simplifya (list '(mabs) e) t))
2935 (defun simpmqapply (exp y z)
2936 (let ((simpfun (and (not (atom (cadr exp))) (safe-get (caaadr exp) 'specsimp))) u)
2937 (if simpfun
2938 (funcall simpfun exp y z)
2939 (progn (setq u (simpargs exp z))
2940 (if (symbolp (cadr u))
2941 (simplifya (cons (cons (cadr u) (cdar u)) (cddr u)) z)
2942 u)))))
2944 ;; TRUE, if the symbol e is declared to be $complex or $imaginary.
2945 (defun decl-complexp (e)
2946 (and (symbolp e)
2947 (kindp e '$complex)))
2949 ;; TRUE, if the symbol e is declared to be $real, $rational, $irrational
2950 ;; or $integer
2951 (defun decl-realp (e)
2952 (and (symbolp e)
2953 (or (kindp e '$real)
2954 (kindp e '$rational)
2955 (kindp e '$irrational)
2956 (kindp e '$integer))))
2958 ;; WARNING: Exercise extreme caution when modifying this function!
2960 ;; Richard Fateman and Stavros Macrakis both say that changing the
2961 ;; actual ordering relations (as opposed to making them faster to
2962 ;; determine) could have very subtle and wide-ranging effects. Also,
2963 ;; the simplifier spends the vast majority of its time here, so be
2964 ;; very careful about changes that may drastically slow down the
2965 ;; simplifier.
2966 (defun great (x y)
2967 (cond ((atom x)
2968 (cond ((atom y)
2969 (cond ((numberp x)
2970 (cond ((numberp y)
2971 (setq y (- x y))
2972 (cond ((zerop y) (floatp x)) (t (plusp y))))))
2973 ((constant x)
2974 (cond ((constant y) (alphalessp y x)) (t (numberp y))))
2975 ((mget x '$scalar)
2976 (cond ((mget y '$scalar) (alphalessp y x))
2977 (t (maxima-constantp y))))
2978 ((mget x '$mainvar)
2979 (cond ((mget y '$mainvar) (alphalessp y x)) (t t)))
2980 (t (or (maxima-constantp y) (mget y '$scalar)
2981 (and (not (mget y '$mainvar)) (not (null (alphalessp y x))))))))
2982 (t (not (ordfna y x)))))
2983 ((atom y) (ordfna x y))
2984 ((eq (caar x) 'rat)
2985 (cond ((eq (caar y) 'rat)
2986 (> (* (caddr y) (cadr x)) (* (caddr x) (cadr y))))))
2987 ((eq (caar y) 'rat))
2988 ((or (member (caar x) '(mtimes mplus mexpt %del))
2989 (member (caar y) '(mtimes mplus mexpt %del)))
2990 (ordfn x y))
2991 ((and (eq (caar x) 'bigfloat) (eq (caar y) 'bigfloat)) (mgrp x y))
2992 ((or (eq (caar x) 'mrat) (eq (caar y) 'mrat))
2993 (error "GREAT: internal error: unexpected MRAT argument"))
2994 (t (do ((x1 (margs x) (cdr x1)) (y1 (margs y) (cdr y1))) (())
2995 (cond ((null x1)
2996 (return (cond (y1 nil)
2997 ((not (alike1 (mop x) (mop y)))
2998 (great (mop x) (mop y)))
2999 ((member 'array (cdar x)) t))))
3000 ((null y1) (return t))
3001 ((not (alike1 (car x1) (car y1)))
3002 (return (great (car x1) (car y1)))))))))
3004 ;; Trivial function used only in ALIKE1.
3005 ;; Should be defined as an open-codable subr.
3007 (defmacro memqarr (l)
3008 `(if (member 'array ,l) t))
3010 ;; Compares two Macsyma expressions ignoring SIMP flags and all other
3011 ;; items in the header except for the ARRAY flag.
3013 (defun alike1 (x y)
3014 ;; Clauses are ordered based on frequency of the case
3015 ;; cons, integer, and symbol are very common
3016 ;; everything else is rare
3017 (cond ((eq x y) t)
3018 ((consp x)
3019 (if (and (consp y)
3020 (not (atom (car x)))
3021 (not (atom (car y)))
3022 (eq (caar x) (caar y)))
3023 (cond
3024 ((eq (caar x) 'mrat) (like x y))
3025 ((eq (caar x) 'mpois) (equal (cdr x) (cdr y)))
3026 ((eq (memqarr (cdar x)) (memqarr (cdar y)))
3027 (alike (cdr x) (cdr y)))
3028 (t nil))
3029 ;; (foo) and (foo) test non-alike because the car's aren't standard
3030 nil))
3031 ((or (symbolp x) (symbolp y)) nil)
3032 ((integerp x) (and (integerp y) (= x y)))
3033 ;; uncommon cases from here down
3034 ((floatp x) (and (floatp y) (= x y)))
3035 ((stringp x) (and (stringp y) (string= x y)))
3036 ((vectorp x) (and (vectorp y) (lisp-vector-alike1 x y)))
3037 ((arrayp x) (and (arrayp y) (lisp-array-alike1 x y)))
3038 (t nil)
3041 (defun lisp-vector-alike1 (x y)
3042 (let ((lx (length x)))
3043 (when (eql lx (length y))
3044 (lisp-array-elements-alike1 x y lx))))
3046 (defun lisp-array-alike1 (x y)
3047 (when (equal (array-dimensions x) (array-dimensions y))
3048 (lisp-array-elements-alike1 x y (array-total-size x))))
3050 (defun lisp-array-elements-alike1 (x y n)
3051 (dotimes (i n t)
3052 (unless (alike1 (row-major-aref x i) (row-major-aref y i))
3053 (return-from lisp-array-elements-alike1 nil))))
3055 ;; Not sure if we want to enable comparison of maxima arrays.
3056 ;; Aside from that, add2lnc calls alike1 (via memalike) and that causes trouble.
3057 ;; Possible code for ALIKE1 to enable such comparisons should we choose to do so:
3059 ;; ((maxima-declared-arrayp x)
3060 ;; (and (maxima-declared-arrayp y) (maxima-declared-array-alike1 x y)))
3061 ;; ((maxima-undeclared-arrayp x)
3062 ;; (and (maxima-undeclared-arrayp y) (maxima-undeclared-array-alike1 x y)))
3064 (defun maxima-declared-array-alike1 (x y)
3065 (lisp-array-alike1 (get (mget x 'array) 'array) (get (mget y 'array) 'array)))
3067 (defun maxima-undeclared-array-alike1 (x y)
3068 (and
3069 (alike1 (mfuncall '$arrayinfo x) (mfuncall '$arrayinfo y))
3070 (alike1 ($listarray x) ($listarray y))))
3072 ;; Maps ALIKE1 down two lists.
3074 (defun alike (x y)
3075 (do ((x x (cdr x)) (y y (cdr y))) ((atom x) (equal x y))
3076 (cond ((or (atom y) (not (alike1 (car x) (car y))))
3077 (return nil)))))
3079 (defun ordfna (e a) ; A is an atom
3080 (cond ((numberp a)
3081 (or (not (eq (caar e) 'rat))
3082 (> (cadr e) (* (caddr e) a))))
3083 ((and (constant a)
3084 (not (member (caar e) '(mplus mtimes mexpt))))
3085 (not (member (caar e) '(rat bigfloat))))
3086 ((eq (caar e) 'mrat)) ;; all MRATs succeed all atoms
3087 ((null (margs e)) nil)
3088 ((eq (caar e) 'mexpt)
3089 (cond ((and (maxima-constantp (cadr e))
3090 (or (not (constant a)) (not (maxima-constantp (caddr e)))))
3091 (or (not (free (caddr e) a)) (great (caddr e) a)))
3092 ((eq (cadr e) a) (great (caddr e) 1))
3093 (t (great (cadr e) a))))
3094 ((member (caar e) '(mplus mtimes))
3095 (let ((u (car (last e))))
3096 (cond ((eq u a) (not (ordhack e))) (t (great u a)))))
3097 ((eq (caar e) '%del))
3098 ((prog2 (setq e (car (margs e))) ; use first arg of e
3099 (and (not (atom e)) (member (caar e) '(mplus mtimes))))
3100 (let ((u (car (last e)))) ; and compare using
3101 (cond ((eq u a) (not (ordhack e))) ; same procedure as above
3102 (t (great u a)))))
3103 ((eq e a))
3104 (t (great e a))))
3106 ;; compare lists a and b elementwise from back to front
3107 (defun ordlist (a b cx cy)
3108 (prog (l1 l2 c d)
3109 (setq l1 (length a) l2 (length b))
3110 loop (cond ((= l1 0)
3111 (return (cond ((= l2 0) (eq cx 'mplus))
3112 ((and (eq cx cy) (= l2 1))
3113 (great (cond ((eq cx 'mplus) 0) (t 1)) (car b))))))
3114 ((= l2 0) (return (not (ordlist b a cy cx)))))
3115 (setq c (nthelem l1 a) d (nthelem l2 b))
3116 (cond ((not (alike1 c d)) (return (great c d))))
3117 (setq l1 (1- l1) l2 (1- l2))
3118 (go loop)))
3120 (defun term-list (x)
3121 (if (mplusp x)
3122 (cdr x)
3123 (list x)))
3125 (defun factor-list (x)
3126 (if (mtimesp x)
3127 (cdr x)
3128 (list x)))
3130 ;; one of the exprs x or y should be one of:
3131 ;; %del, mexpt, mplus, mtimes
3132 (defun ordfn (x y)
3133 (let ((cx (caar x)) (cy (caar y)))
3134 (cond ((eq cx '%del) (if (eq cy '%del) (great (cadr x) (cadr y)) t))
3135 ((eq cy '%del) nil)
3136 ((or (eq cx 'mtimes) (eq cy 'mtimes))
3137 (ordlist (factor-list x) (factor-list y) 'mtimes 'mtimes))
3138 ((or (eq cx 'mplus) (eq cy 'mplus))
3139 (ordlist (term-list x) (term-list y) 'mplus 'mplus))
3140 ((eq cx 'mexpt) (ordmexpt x y))
3141 ((eq cy 'mexpt) (not (ordmexpt y x))))))
3143 (defun ordhack (x)
3144 (if (and (cddr x) (null (cdddr x)))
3145 (great (if (eq (caar x) 'mplus) 0 1) (cadr x))))
3147 (defun ordmexpt (x y)
3148 (cond ((eq (caar y) 'mexpt)
3149 (cond ((alike1 (cadr x) (cadr y)) (great (caddr x) (caddr y)))
3150 ((maxima-constantp (cadr x))
3151 (if (maxima-constantp (cadr y))
3152 (if (or (alike1 (caddr x) (caddr y))
3153 (and (mnump (caddr x)) (mnump (caddr y))))
3154 (great (cadr x) (cadr y))
3155 (great (caddr x) (caddr y)))
3156 (great x (cadr y))))
3157 ((maxima-constantp (cadr y)) (great (cadr x) y))
3158 ((mnump (caddr x))
3159 (great (cadr x) (if (mnump (caddr y)) (cadr y) y)))
3160 ((mnump (caddr y)) (great x (cadr y)))
3161 (t (let ((x1 (simpln1 x)) (y1 (simpln1 y)))
3162 (if (alike1 x1 y1) (great (cadr x) (cadr y))
3163 (great x1 y1))))))
3164 ((alike1 (cadr x) y) (great (caddr x) 1))
3165 ((mnump (caddr x)) (great (cadr x) y))
3166 (t (great (simpln1 x) (simpln (list '(%log) y) 1 t)))))
3168 (defmfun $multthru (e1 &optional e2)
3169 (let (arg1 arg2)
3170 (cond (e2 ;called with two args
3171 (setq arg1 (specrepcheck e1)
3172 arg2 (specrepcheck e2))
3173 (cond ((or (atom arg2)
3174 (not (member (caar arg2) '(mplus mequal))))
3175 (mul2 arg1 arg2))
3176 ((eq (caar arg2) 'mequal)
3177 (list (car arg2) ($multthru arg1 (cadr arg2))
3178 ($multthru arg1 (caddr arg2))))
3179 (t (expandterms arg1 (cdr arg2)))))
3180 (t ;called with only one arg
3181 (prog (l1)
3182 (setq arg1 (setq arg2 (specrepcheck e1)))
3183 (cond ((atom arg1) (return arg1))
3184 ((eq (caar arg1) 'mnctimes)
3185 (setq arg1 (cdr arg1)) (go nct))
3186 ((not (eq (caar arg1) 'mtimes)) (return arg1)))
3187 (setq arg1 (reverse (cdr arg1)))
3188 times (when (mplusp (car arg1))
3189 (setq l1 (nconc l1 (cdr arg1)))
3190 (return (expandterms (muln l1 t) (cdar arg1))))
3191 (setq l1 (cons (car arg1) l1))
3192 (setq arg1 (cdr arg1))
3193 (if (null arg1) (return arg2))
3194 (go times)
3195 nct (when (mplusp (car arg1))
3196 (setq l1 (nreverse l1))
3197 (return (addn (mapcar
3198 #'(lambda (u)
3199 (simplifya
3200 (cons '(mnctimes)
3201 (append l1 (ncons u) (cdr arg1)))
3203 (cdar arg1))
3204 t)))
3205 (setq l1 (cons (car arg1) l1))
3206 (setq arg1 (cdr arg1))
3207 (if (null arg1) (return arg2))
3208 (go nct))))))
3210 ;; EXPANDEXPT computes the expansion of (x1 + x2 + ... + xm)^n
3211 ;; taking a sum and integer power as arguments.
3212 ;; Its theory is to recurse down the binomial expansion of
3213 ;; (x1 + (x2 + x3 + ... + xm))^n using the Binomial Expansion
3214 ;; Thus it does a sigma:
3216 ;; n
3217 ;; -------
3218 ;; \ / n \ k (n - k)
3219 ;; > | | x1 (x2 + x3 + ... + xm)
3220 ;; / \ k /
3221 ;; -------
3222 ;; k=0
3224 ;; The function EXPONENTIATE-SUM does this and recurses through the second
3225 ;; sum raised to a power. It takes a list of terms and a positive integer
3226 ;; power as arguments.
3229 (defun expandexpt (sum power)
3230 (declare (fixnum power))
3231 (let ((expansion (exponentiate-sum (cdr sum) (abs power))))
3232 (cond ((plusp power) expansion)
3233 (t (inv expansion)))))
3235 (defun exponentiate-sum (terms rpower)
3236 (declare (fixnum rpower))
3237 (cond ((= rpower 0) 1)
3238 ((null (cdr terms)) (power (car terms) rpower))
3239 ((= rpower 1) (cons '(mplus simp) terms))
3240 (t (do ((i 0 (1+ i))
3241 (result 0 (add2 result
3242 (muln (list (combination rpower i)
3243 (exponentiate-sum (cdr terms)
3244 (- rpower i))
3245 (power (car terms) i)) t))))
3246 ((> i rpower) result)
3247 (declare (fixnum i))))))
3249 ;; Computes the combination of n elements taken m at a time by the formula
3251 ;; (n * (n-1) * ... * (n - m + 1)) / m! =
3252 ;; (n / 1) * ((n - 1) / 2) * ... * ((n - m + 1) / m)
3254 ;; Checks for the case when m is greater than n/2 and translates
3255 ;; to an equivalent expression.
3257 (defun combination (n m)
3258 (declare (fixnum n m))
3259 (cond ((> m (truncate n 2))
3260 (combination n (- n m)))
3262 (do ((result 1 (truncate (* result n1) m1))
3263 (n1 n (1- n1))
3264 (m1 1 (1+ m1)))
3265 ((> m1 m) result)
3266 (declare (fixnum n1 m1))))))
3268 (defun expandsums (a b)
3269 (addn (prog (c)
3270 (setq a (fixexpand a) b (cdr b))
3271 loop
3272 (when (null a) (return c))
3273 (setq c (cons (expandterms (car a) b) c))
3274 (setq a (cdr a))
3275 (go loop))
3278 (defun expandterms (a b)
3279 (addn (prog (c)
3280 loop
3281 (when (null b) (return c))
3282 (setq c (cons (mul2 a (car b)) c))
3283 (setq b (cdr b))
3284 (go loop))
3287 (defun expandtimes (a)
3288 (let (simp-prods simp-negprods simp-sums simp-negsums expsums expnegsums)
3289 (labels
3290 ((genexpands (l)
3291 (prog ()
3292 loop
3293 (setq l (cdr l))
3294 (cond ((null l)
3295 (setq simp-prods (nreverse simp-prods)
3296 simp-negprods (nreverse simp-negprods)
3297 simp-sums (nreverse simp-sums)
3298 simp-negsums (nreverse simp-negsums))
3299 (return nil))
3300 ((atom (car l))
3301 (push (car l) simp-prods))
3302 ((eq (caaar l) 'rat)
3303 (unless (equal (cadar l) 1)
3304 (push (cadar l) simp-prods))
3305 (push (caddar l) simp-negprods))
3306 ((eq (caaar l) 'mplus)
3307 (push (car l) simp-sums))
3308 ((and (eq (caaar l) 'mexpt)
3309 (equal (caddar l) -1) (mplusp (cadar l)))
3310 (push (cadar l) simp-negsums))
3311 ((and (eq (caaar l) 'mexpt)
3312 (let ((*expandp* t))
3313 (mminusp (caddar l))))
3314 (push (if (equal (caddar l) -1)
3315 (cadar l)
3316 (list (caar l) (cadar l) (neg (caddar l))))
3317 simp-negprods))
3319 (push (car l) simp-prods)))
3320 (go loop))))
3321 (genexpands a)
3322 (setq simp-prods (cond ((null simp-prods) 1)
3323 ((null (cdr simp-prods)) (car simp-prods))
3324 (t (cons '(mtimes simp) simp-prods))))
3325 (setq simp-negprods (cond ((null simp-negprods) 1)
3326 ((null (cdr simp-negprods)) (car simp-negprods))
3327 (t (cons '(mtimes simp) simp-negprods))))
3328 (block nil
3329 (tagbody
3330 (cond ((null simp-sums) (go down))
3331 (t (setq expsums (car simp-sums))
3332 (mapc #'(lambda (c)
3333 (setq expsums (expandsums expsums c)))
3334 (cdr simp-sums))))
3335 (setq simp-prods (cond ((equal simp-prods 1) expsums)
3336 (t (expandterms simp-prods (fixexpand expsums)))))
3337 down (cond ((null simp-negsums)
3338 (cond ((equal 1 simp-negprods) (return simp-prods))
3339 ((mplusp simp-prods)
3340 (return (expandterms (power simp-negprods -1) (cdr simp-prods))))
3341 (t (return (let ((*expandflag* t))
3342 (mul2 simp-prods (power simp-negprods -1)))))))
3344 (setq expnegsums (car simp-negsums))
3345 (mapc #'(lambda (c)
3346 (setq expnegsums (expandsums expnegsums c)))
3347 (cdr simp-negsums))))
3348 (setq expnegsums (expandterms simp-negprods (fixexpand expnegsums)))
3349 (return (if (mplusp simp-prods)
3350 (expandterms (inv expnegsums) (cdr simp-prods))
3351 (let ((*expandflag* t))
3352 (mul2 simp-prods (inv expnegsums))))))))))
3354 (defun expand1 (exp $expop $expon)
3355 (unless (and (integerp $expop) (> $expop -1))
3356 (merror (intl:gettext "expand: expop must be a nonnegative integer; found: ~M") $expop))
3357 (unless (and (integerp $expon) (> $expon -1))
3358 (merror (intl:gettext "expand: expon must be a nonnegative integer; found: ~M") $expon))
3359 (resimplify (specrepcheck exp)))
3361 (defmfun $expand (exp &optional (expop $maxposex) (expon $maxnegex))
3362 (expand1 exp expop expon))
3364 (defun fixexpand (a)
3365 (if (not (mplusp a))
3366 (ncons a)
3367 (cdr a)))
3369 (defun nrthk (in nth-root)
3370 (labels
3371 ((nrthk1 (in nth-root)
3372 ;; Computes the NTH-ROOT of -IN
3373 (if $radexpand
3374 (mul2 (nrthk2 in nth-root) (nrthk -1 nth-root))
3375 (nrthk2 (mul2* -1 in) nth-root)))
3377 (nrthk2 (in nth-root)
3378 ;; Computes the NTH-ROOT of IN
3379 (power* in (list '(rat) 1 nth-root))))
3380 (cond ((equal in 1)
3382 ((equal in -1)
3383 (cond ((equal nth-root 2)
3384 '$%i)
3385 ((eq $domain '$real)
3386 (if (even nth-root)
3387 (nrthk2 -1 nth-root)
3388 -1))
3389 ($m1pbranch
3390 (let (($%emode t))
3391 (power* '$%e (list '(mtimes) (list '(rat) 1 nth-root) '$%pi '$%i))))
3393 (nrthk2 -1 nth-root))))
3394 ((or (and wflag (eq ($asksign in) '$neg))
3395 (and (mnump in) (equal ($sign in) '$neg)))
3396 (nrthk1 (mul2* -1 in) nth-root))
3398 (nrthk2 in nth-root)))))
3400 (defun simpnrt (x nth-root)
3401 ;; Computes the NTH-ROOT of X
3402 (prog (simp-in simp-out varlist genvar $factorflag $dontfactor)
3403 (labels
3404 ((simpnrt1 (x)
3405 (do ((x x (cddr x)) (y))
3406 ((null x))
3407 (cond ((not (equal 1 (setq y (gcd (cadr x) nth-root))))
3408 (push (simpnrt (list '(mexpt) (car x) (quotient (cadr x) y))
3409 (quotient nth-root y))
3410 simp-out))
3411 ((and (equal (cadr x) 1) (integerp (car x)) (plusp (car x))
3412 (setq y (pnthrootp (car x) nth-root)))
3413 (push y simp-out))
3415 (unless (> nth-root (abs (cadr x)))
3416 (push (list '(mexpt) (car x) (quotient (cadr x) nth-root)) simp-out))
3417 (push (list '(mexpt) (car x) (rem (cadr x) nth-root)) simp-in))))))
3418 (setq $factorflag t)
3419 (newvar x)
3420 (setq x (ratrep* x))
3421 (when (equal (cadr x) 0) (return 0))
3422 (setq x (ratfact (cdr x) 'psqfr))
3423 (simpnrt1 (mapcar #'pdis x))
3424 (setq simp-out (if simp-out (muln simp-out nil) 1))
3425 (setq simp-in (cond (simp-in
3426 (setq simp-in (muln simp-in nil))
3427 (nrthk simp-in nth-root))
3428 (t 1)))
3429 (return (let (($%emode t))
3430 (simplifya (list '(mtimes) simp-in simp-out)
3431 (not (or (atom simp-in)
3432 (atom (cadr simp-in))
3433 (member (caaadr simp-in) '(mplus mtimes rat))))))))))
3435 ;; The following was formerly in SININT. This code was placed here because
3436 ;; SININT is now an out-of-core file on MC, and this code is needed in-core
3437 ;; because of the various calls to it. - BMT & JPG
3439 (defmfun $integrate (expr x &optional lo hi)
3440 (declare (special *in-risch-p* context))
3441 (let ($ratfac)
3442 (if (not hi)
3443 (with-new-context (context)
3444 (if (member '%risch *nounl*)
3445 (if *in-risch-p*
3446 ;; Give up; we're being called from RISCHINT by some path.
3447 (list '(%integrate) expr x)
3448 (rischint expr x))
3449 (sinint expr x)))
3450 ($defint expr x lo hi))))
3452 (defun ratp (a ratp-var)
3453 (cond ((atom a) t)
3454 ((member (caar a) '(mplus mtimes))
3455 (do ((l (cdr a) (cdr l))) ((null l) t)
3456 (or (ratp (car l) ratp-var) (return nil))))
3457 ((eq (caar a) 'mexpt)
3458 (if (free (cadr a) ratp-var)
3459 (free (caddr a) ratp-var)
3460 (and (integerp (caddr a)) (ratp (cadr a) ratp-var))))
3461 (t (free a ratp-var))))
3463 (defun ratnumerator (r)
3464 (cond ((atom r) r)
3465 ((atom (cdr r)) (car r))
3466 ((numberp (cadr r)) r)
3467 (t (car r))))
3469 (defun ratdenominator (r)
3470 (cond ((atom r) 1)
3471 ((atom (cdr r)) (cdr r))
3472 ((numberp (cadr r)) 1)
3473 (t (cdr r))))
3475 ;; (BPROG U V) appears to return A and B (as ((A1 . A2) B1 . B2) with A = A1/A2, B = B1/B2)
3476 ;; such that B/U + A/V = 1/(U*V), where U, V are polynomials represented as a list of
3477 ;; exponents and coefficients, (<gensym> E1 C1 E2 C2 ...) = C1*<gensym>^E1 + C2*<gensym>^E2 + ....
3478 ;; Example:
3479 ;; (%i73) partfrac ((2*x^2-3)/(x^4-3*x^2+2), x);
3480 ;; 1. Trace: (PARTFRAC '((#:X16910 2 2 0 -3) #:X16910 4 1 2 -3 0 2) '#:X16910)
3481 ;; 2. Trace: (BPROG '(#:X16910 2 1 0 -2) '(#:X16910 2 1 0 -1))
3482 ;; 2. Trace: BPROG ==> ((-1 . 1) 1 . 1)
3483 ;; 2. Trace: (BPROG '(#:X16910 1 1 0 1) '(#:X16910 1 1 0 -1))
3484 ;; 2. Trace: BPROG ==> ((1 . 2) -1 . 2)
3485 ;; 2. Trace: (BPROG '(#:X16910 1 1 0 -1) '1)
3486 ;; 2. Trace: BPROG ==> ((0 . 1) 1 . 1)
3487 ;; 1. Trace: PARTFRAC ==> ((0 . 1) ((1 . 2) (#:X16910 1 1 0 -1) 1) ((-1 . 2) (#:X16910 1 1 0 1) 1) ((1 . 1) (#:X16910 2 1 0 -2) 1))
3488 ;; (%o73) 1/(x^2-2)-1/(2*(x+1))+1/(2*(x-1))
3490 (defun bprog (r s bprog-var)
3491 (prog (p1b p2b coef1r coef2r coef1s coef2s f1 f2 a egcd oldalg)
3492 (setq oldalg $algebraic)
3493 (setq r (ratfix r))
3494 (setq s (ratfix s))
3495 (setq coef2r (setq coef1s 0))
3496 (setq coef2s (setq coef1r 1))
3497 (setq a 1 egcd 1)
3498 (setq p1b (car r))
3499 (unless (zerop (pdegree p1b bprog-var)) (setq egcd (pgcdexpon p1b)))
3500 (setq p2b (car s))
3501 (unless (or (zerop (pdegree p2b bprog-var)) (= egcd 1))
3502 (setq egcd (gcd egcd (pgcdexpon p2b)))
3503 (setq p1b (pexpon*// p1b egcd nil)
3504 p2b (pexpon*// p2b egcd nil)))
3505 b1 (cond ((< (pdegree p1b bprog-var) (pdegree p2b bprog-var))
3506 (rotatef p1b p2b)
3507 (rotatef coef1r coef2r)
3508 (rotatef coef1s coef2s)))
3509 (when (zerop (pdegree p2b bprog-var))
3510 (unless (zerop (pdegree coef2r bprog-var))
3511 (setq coef2r (pexpon*// coef2r egcd t)))
3512 (unless (zerop (pdegree coef2s bprog-var))
3513 (setq coef2s (pexpon*// coef2s egcd t)))
3514 (return (cons (ratreduce (ptimes (cdr r) coef2r) p2b)
3515 (ratreduce (ptimes (cdr s) coef2s) p2b))))
3516 (setq f1 (psquorem1 (cdr p1b) (cdr p2b) t))
3517 (setq $algebraic $false)
3518 (setq f2 (psimp bprog-var (cadr f1)))
3519 (setq p1b (pquotientchk (psimp bprog-var (caddr f1)) a))
3520 (setq $algebraic oldalg)
3521 (setq f1 (car f1))
3522 (setq coef1r (pquotientchk (pdifference (ptimes f1 coef1r)
3523 (ptimes f2 coef2r))
3525 (setq coef1s (pquotientchk (pdifference (ptimes f1 coef1s)
3526 (ptimes f2 coef2s))
3528 (setq a f1)
3529 (go b1)))
3531 (defun ratdifference (a b) (ratplus a (ratminus b)))
3533 (defun ratpl (a b) (ratplus (ratfix a) (ratfix b)))
3535 (defun ratti (a b c) (rattimes (ratfix a) (ratfix b) c))
3537 (defun ratqu (a b) (ratquotient (ratfix a) (ratfix b)))
3539 (defun ratfix (a) (cond ((equal a (ratnumerator a)) (cons a 1)) (t a)))
3541 (defun ratdivide (f g)
3542 (destructuring-let* (((fnum . fden) (ratfix f))
3543 ((gnum . gden) (ratfix g))
3544 ((q r) (pdivide fnum gnum)))
3545 (cons (ratqu (ratti q gden t) fden)
3546 (ratqu r fden))))
3548 (defun polcoef (l n polcoef-var)
3549 (cond ((or (atom l) (pointergp polcoef-var (car l)))
3550 (cond ((equal n 0) l)
3551 (t 0)))
3553 (ptterm (cdr l) n))))
3555 (defun disrep (l disrep-ratform)
3556 (cond ((equal (ratnumerator l) l)
3557 ($ratdisrep (cons disrep-ratform (cons l 1))))
3558 (t ($ratdisrep (cons disrep-ratform l)))))
3560 ;; The following was formerly in MATRUN. This code was placed here because
3561 ;; MATRUN is now an out-of-core file on MC, and this code is needed in-core
3562 ;; so that MACSYMA SAVE files will work. - JPG
3564 (defun matcherr ()
3565 (throw 'match nil))
3567 (defun kar (x) (if (atom x) (matcherr) (car x)))
3569 (defun kaar (x) (kar (kar x)))
3571 (defun kdr (x) (if (atom x) (matcherr) (cdr x)))
3573 (defun simpargs1 (a vestigial c)
3574 (declare (ignore vestigial))
3575 (simpargs a c))
3577 (defun *kar (x)
3578 (if (not (atom x)) (car x)))
3580 (defquote retlist (&rest l)
3581 (cons '(mlist simp)
3582 (mapcar #'(lambda (z) (list '(mequal simp) z (meval z))) l)))
3584 (defun nthkdr (x c)
3585 (if (zerop c) x (nthkdr (kdr x) (1- c))))