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