1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 (declare-top (special *rulesw
*
16 *prods
* *negprods
* *sums
* *negsums
*
19 ;; Switches dealing with matrices and non-commutative multiplication.
21 (defmvar $doscmxplus nil
22 "Causes SCALAR + MATRIX to return a matrix answer. This switch
23 is not subsumed under DOALLMXOPS.")
26 "Causes SCALAR^MATRIX([1,2],[3,4]) to return
27 MATRIX([SCALAR,SCALAR^2],[SCALAR^3,SCALAR^4]). In general, this
28 transformation affects exponentiations where the *print-base* is a
29 scalar and the power is a matrix or list.")
31 (defmvar $domxplus nil
)
33 (defmvar $domxtimes nil
)
37 ;; Lisp level variables
38 (defmvar derivsimp t
"Hack in `simpderiv' for `rwg'")
40 (defmvar $grindswitch nil
)
43 (defmvar $limitdomain
'$complex
)
44 (defmvar $lognegint nil
46 :properties
((evflag t
)))
47 (defmvar $distribute_over t
) ; If T, functions are distributed over bags.
51 (defvar %e-val
(mget '$%e
'$numer
))
52 (defvar %pi-val
(mget '$%pi
'$numer
))
54 (defvar expandflag nil
)
57 (defprop mnctimes t associative
)
58 (defprop lambda t lisp-no-simp
)
60 ;; Local functions should not be simplified. Various lisps
61 ;; use various names for the list structure defining these:
66 (dolist (y (list x z
))
69 (setf (get (car y
) 'lisp-no-simp
) t
))))))
71 (dolist (x '(mplus mtimes mnctimes mexpt mncexpt %sum
))
72 (setf (get x
'msimpind
) (cons x
'(simp))))
74 ;; operators properties
76 (mapc #'(lambda (x) (setf (get (first x
) 'operators
) (second x
)))
77 '((mplus simplus
) (mtimes simptimes
) (mncexpt simpncexpt
)
78 (mminus simpmin
) (%gamma simpgamma
) (mfactorial simpfact
)
79 (mnctimes simpnct
) (mquotient simpquot
) (mexpt simpexpt
)
81 (%derivative simpderiv
)
83 (%integrate simpinteg
) (%limit simp-limit
)
84 (bigfloat simpbigfloat
) (lambda simplambda
) (mdefine simpmdef
)
85 (mqapply simpmqapply
) (%gamma simpgamma
)
86 ($beta simpbeta
) (%sum simpsum
) (%binomial simpbinocoef
)
87 (%plog simpplog
) (%product simpprod
) (%genfact simpgfact
)
88 ($atan2 simpatan2
) ($matrix simpmatrix
) (%matrix simpmatrix
)
89 ($bern simpbern
) ($euler simpeuler
)))
91 (defprop $li lisimp specsimp
)
92 (defprop $psi psisimp specsimp
)
94 (defprop $equal t binary
)
95 (defprop $notequal t binary
)
98 "Returns true if X is a bigfloat"
101 (eq (caar x
) 'bigfloat
)))
104 "Returns non-NIL if X is an integer, float, or bfloat that is equal
106 (or (and (integerp x
) (= 0 x
))
107 (and (floatp x
) (= 0.0 x
))
108 (and ($bfloatp x
) (= 0 (second x
)))))
111 "Returns non-NIL if X is an integer, float, or bfloat that is equal
113 (or (and (integerp x
) (= 1 x
))
114 (and (floatp x
) (= 1.0 x
))
115 (and ($bfloatp x
) (zerop1 (sub x
1)))))
118 "Returns non-NIL if X is a Lisp number or if it is a Maxima rational
119 form or a bigfloat form"
121 (and (not (atom x
)) (not (atom (car x
)))
122 (member (caar x
) '(rat bigfloat
)))))
124 ;; Does X or a subexpression match PREDICATE?
126 ;; If X is a tree then we recurse depth-first down its arguments. The specrep
127 ;; check is because rat forms are built rather differently from normal Maxima
128 ;; expressions so we need to unpack them for the recursion to work properly.
129 (defun subexpression-matches-p (predicate x
)
130 (or (funcall predicate x
)
133 (subexpression-matches-p predicate
(specdisrep x
))
134 (some (lambda (arg) (subexpression-matches-p predicate arg
))
137 ;; Is there a bfloat anywhere in X?
138 (defun some-bfloatp (x) (subexpression-matches-p '$bfloatp x
))
140 ;; Is there a float anywhere in X?
141 (defun some-floatp (x) (subexpression-matches-p 'floatp x
))
143 ;; EVEN works for any arbitrary lisp object since it does an integer
144 ;; check first. In other cases, you may want the Lisp EVENP function
145 ;; which only works for integers.
147 (defun even (a) (and (integerp a
) (not (oddp a
))))
149 ;; Predicates to determine if X satisfies some condition.
152 "Determines if X is a Maxima rational form: ((rat ...) a b)"
153 (and (not (atom x
)) (eq (caar x
) 'rat
)))
156 "Determines if X is a Maxima sum form: ((mplus ...) ...)"
157 (and (not (atom x
)) (eq (caar x
) 'mplus
)))
160 "Determines if X is a Maxima product form: ((mtimes ...) ...)"
161 (and (not (atom x
)) (eq (caar x
) 'mtimes
)))
164 "Determines if X is a Maxima exponential form: ((mexpt ...) ...)"
165 (and (not (atom x
)) (eq (caar x
) 'mexpt
)))
167 (defun mnctimesp (x) (and (not (atom x
)) (eq (caar x
) 'mnctimes
)))
169 (defun mncexptp (x) (and (not (atom x
)) (eq (caar x
) 'mncexpt
)))
172 "Determines if X is a Maxima log form: ((%log ...) ...)"
173 (and (not (atom x
)) (eq (caar x
) '%log
)))
176 "Determines if X is a Maxima negative form: ((mminus ...) ...)
178 This generally only happens on input forms like a - b:
179 ((mplus) $a ((mminus) $b)).
180 After simplification a - b becomes
181 ((mplus) $a ((mtimes) -1 $b))"
183 (and (not (atom x
)) (eq (caar x
) 'mminus
)))
186 "Determines if X is negative if X is a Lisp number or a Maxima rat
187 form or bigfloat form"
188 (cond ((realp x
) (minusp x
))
189 ((or (ratnump x
) ($bfloatp x
)) (minusp (cadr x
)))))
191 (defun mqapplyp (e) (and (not (atom e
)) (eq (caar e
) 'mqapply
)))
193 (defun ratdisrep (e) (simplifya ($ratdisrep e
) nil
))
195 (defun sratsimp (e) (simplifya ($ratsimp e
) nil
))
197 (defun simpcheck (e flag
)
198 (cond ((specrepp e
) (specdisrep e
))
200 (t (let (($%enumer $numer
))
201 ;; Switch $%enumer on, when $numer is TRUE to allow
202 ;; simplification of $%e to its numerical value.
203 (simplifya e nil
)))))
205 (defun mratcheck (e) (if ($ratp e
) (ratdisrep e
) e
))
207 (defmfun $numberp
(e)
208 "Returns true if E is a Maxima rational, a float, or a bigfloat number"
209 (or ($ratnump e
) ($floatnump e
) ($bfloatp e
)))
211 (defmfun $integerp
(x)
214 (not (member 'trunc
(car x
)))
216 (equal (cddr x
) 1))))
218 ;; The call to $INTEGERP in the following two functions checks for a CRE
219 ;; rational number with an integral numerator and a unity denominator.
222 (cond ((integerp x
) (oddp x
))
223 (($integerp x
) (oddp (cadr x
)))))
226 (cond ((integerp x
) (evenp x
))
227 (($integerp x
) (not (oddp (cadr x
))))))
229 (defmfun $floatnump
(x)
231 (and ($ratp x
) (floatp (cadr x
)) (onep1 (cddr x
)))))
233 (defmfun $ratnump
(x)
237 (not (member 'trunc
(car x
)))
239 (integerp (cddr x
)))))
244 (eq (caar x
) 'mrat
)))
246 (defmfun $taylorp
(x)
249 (member 'trunc
(cdar x
)) t
))
251 (defun specrepcheck (e) (if (specrepp e
) (specdisrep e
) e
))
253 ;; Note that the following two functions are carefully coupled.
257 (member (caar e
) '(mrat mpois
))))
259 (defun specdisrep (e)
260 (cond ((eq (caar e
) 'mrat
) (ratdisrep e
))
263 (defmfun $polysign
(x)
264 (setq x
(cadr (ratf x
)))
265 (cond ((equal x
0) 0) ((pminusp x
) -
1) (t 1)))
267 ;; These check for the correct number of operands within Macsyma expressions,
268 ;; not arguments in a procedure call as the name may imply.
270 (defun arg-count-check (required-arg-count expr
)
271 (unless (= required-arg-count
(length (rest expr
)))
272 (wna-err expr required-arg-count
)))
274 (defun oneargcheck (expr)
275 (arg-count-check 1 expr
))
277 (defun twoargcheck (expr)
278 (arg-count-check 2 expr
))
280 ;; WNA-ERR: Wrong Number of Arguments error
282 ;; If REQUIRED-ARG-COUNT is non-NIL, then we check that EXPR has the
283 ;; correct number of arguments. A informative error message is shown
284 ;; if the number of arguments is not given.
286 ;; Otherwise, EXPR must be a symbol and a generic message is printed.
287 ;; (This is for backward compatibility for existing uses of WNA-ERR.)
288 (defun wna-err (exprs &optional required-arg-count
)
289 (if required-arg-count
290 (let ((op (caar exprs
))
291 (actual-count (length (rest exprs
))))
292 (merror (intl:gettext
"~M: expected exactly ~M arguments but got ~M: ~M")
293 op required-arg-count actual-count
(list* '(mlist) (rest exprs
))))
294 (merror (intl:gettext
"~:@M: wrong number of arguments.")
297 (defun improper-arg-err (exp fn
)
298 (merror (intl:gettext
"~:M: improper argument: ~M") fn exp
))
300 (defun subargcheck (form subsharp argsharp fun
)
301 (if (or (not (= (length (subfunsubs form
)) subsharp
))
302 (not (= (length (subfunargs form
)) argsharp
)))
303 (merror (intl:gettext
"~:@M: wrong number of arguments or subscripts.") fun
)))
305 ;; Constructor and extractor primitives for subscripted functions, e.g.
306 ;; F[1,2](X,Y). SUBL is (1 2) and ARGL is (X Y).
308 ;; These will be flushed when NOPERS is finished. They will be macros in
309 ;; NOPERS instead of functions, so we have to be careful that they aren't
310 ;; mapped or applied anyplace. What we really want is open-codable routines.
312 (defun subfunmakes (fun subl argl
)
313 `((mqapply simp
) ((,fun simp array
) .
,subl
) .
,argl
))
315 (defun subfunmake (fun subl argl
)
316 `((mqapply) ((,fun simp array
) .
,subl
) .
,argl
))
318 (defun subfunname (exp) (caaadr exp
))
320 (defun subfunsubs (exp) (cdadr exp
))
322 (defun subfunargs (exp) (cddr exp
))
324 (defmfun $numfactor
(x)
325 (setq x
(specrepcheck x
))
328 ((not (eq (caar x
) 'mtimes
)) 1)
329 ((mnump (cadr x
)) (cadr x
))
332 (defun scalar-or-constant-p (x flag
)
333 (if flag
(not ($nonscalarp x
)) ($scalarp x
)))
335 (defmfun $constantp
(x)
336 (cond ((atom x
) (or ($numberp x
) (kindp x
'$constant
)))
337 ((member (caar x
) '(rat bigfloat
)) t
)
338 ((specrepp x
) ($constantp
(specdisrep x
)))
339 ((or (mopp (caar x
)) (kindp (caar x
) '$constant
))
340 (do ((x (cdr x
) (cdr x
))) ((null x
) t
)
341 (if (not ($constantp
(car x
))) (return nil
))))))
344 (cond ((symbolp x
) (kindp x
'$constant
))
346 (and (kindp (caar x
) '$constant
)
347 (do ((x (cdr x
) (cdr x
))) ((null x
) t
)
348 (if (not ($constantp
(car x
))) (return nil
)))))))
350 (defun maxima-constantp (x)
352 (and (symbolp x
) (kindp x
'$constant
))))
354 (defun consttermp (x) (and ($constantp x
) (not ($nonscalarp x
))))
356 (defmfun $scalarp
(x) (or (consttermp x
) (eq (scalarclass x
) '$scalar
)))
358 (defmfun $nonscalarp
(x) (eq (scalarclass x
) '$nonscalar
))
360 (defun scalarclass (exp) ; Returns $SCALAR, $NONSCALAR, or NIL (unknown).
362 ;; Maxima numbers are scalar.
365 (cond ((or (mget exp
'$nonscalar
)
366 (and (not (mget exp
'$scalar
))
367 ;; Arrays are nonscalar, but not if declared scalar.
369 ($member exp $arrays
))))
371 ((or (mget exp
'$scalar
)
372 ;; Include constant atoms which are not declared nonscalar.
375 ((member 'array
(car exp
))
376 (cond ((mget (caar exp
) '$scalar
) '$scalar
)
377 ((mget (caar exp
) '$nonscalar
) '$nonscalar
)
379 ((specrepp exp
) (scalarclass (specdisrep exp
)))
380 ;; If the function is declared scalar or nonscalar, then return. If it
381 ;; isn't explicitly declared, then try to be intelligent by looking at
382 ;; the arguments to the function.
383 ((scalarclass (caar exp
)))
384 ;; <number> + <scalar> is SCALARP because that seems to be useful.
385 ;; This should probably only be true if <number> is a member of the
386 ;; field of scalars. <number> * <scalar> is SCALARP since
387 ;; <scalar> + <scalar> is SCALARP. Also, this has to be done to make
388 ;; <scalar> - <scalar> SCALARP.
389 ((member (caar exp
) '(mplus mtimes
))
390 (do ((l (cdr exp
) (cdr l
))) ((null l
) '$scalar
)
391 (if (not (consttermp (car l
)))
392 (return (scalarclass-list l
)))))
393 ((and (eq (caar exp
) 'mqapply
) (scalarclass (cadr exp
))))
394 ((mxorlistp exp
) '$nonscalar
)
395 ;; If we can't find out anything about the operator, then look at the
396 ;; arguments to the operator. I think NIL should be returned at this
399 (do ((exp (cdr exp
) (cdr exp
)) (l '(1)))
400 ((null exp
) (scalarclass-list l
))
401 (if (not (consttermp (car exp
)))
402 (setq l
(cons (car exp
) l
)))))))
404 ;; Could also do <scalar> +|-|*|/ |^ <declared constant>, but this is not
405 ;; always correct and could screw somebody.
407 ;; SCALARCLASS-LIST takes a list of expressions as its argument. If their
408 ;; scalarclasses all agree, then that scalarclass is returned.
410 (defun scalarclass-list (llist)
411 (cond ((null llist
) nil
)
412 ((null (cdr llist
)) (scalarclass (car llist
)))
413 (t (let ((sc-car (scalarclass (car llist
)))
414 (sc-cdr (scalarclass-list (cdr llist
))))
415 (cond ((or (eq sc-car
'$nonscalar
)
416 (eq sc-cdr
'$nonscalar
))
418 ((and (eq sc-car
'$scalar
) (eq sc-cdr
'$scalar
))
423 (member (caar x
) '(mequal mlist $matrix
))))
425 (defun mequalp (x) (and (not (atom x
)) (eq (caar x
) 'mequal
)))
429 (member (caar x
) '(mlist $matrix
))))
431 (defun mxorlistp1 (x)
433 (or (eq (caar x
) '$matrix
)
434 (and (eq (caar x
) 'mlist
) $listarith
))))
436 (defun constfun (ign)
437 (declare (ignore ign
)) ; Arg ignored. Function used for mapping down lists.
440 (defun constmx (*const
* x
)
441 (simplifya (fmapl1 'constfun x
) t
))
443 ;;; ISINOP returns the complete subexpression with the operator OP, when the
444 ;;; operator OP is found in EXPR.
446 (defun isinop (expr op
) ; OP is assumed to be an atom
447 (cond ((atom expr
) nil
)
448 ((and (eq (caar expr
) op
)
449 (not (member 'array
(cdar expr
))))
452 (do ((expr (cdr expr
) (cdr expr
))
455 (when (setq res
(isinop (car expr
) op
))
458 (defun free (exp var
)
459 (cond ((alike1 exp var
) nil
)
462 (and (listp (car exp
))
463 (free (caar exp
) var
)
464 (freel (cdr exp
) var
)))))
467 (do ((l l
(cdr l
))) ((null l
) t
)
469 ((atom l
) (return (free l var
))) ;; second element of a pair
470 ((not (free (car l
) var
)) (return nil
)))))
473 (defun freeargs (exp var
)
474 (cond ((alike1 exp var
) nil
)
476 (t (do ((l (margs exp
) (cdr l
))) ((null l
) t
)
477 (cond ((not (freeargs (car l
) var
)) (return nil
)))))))
479 (defun simplifya (x y
)
480 (cond ((not $simp
) x
)
482 (cond ((and $%enumer $numer
(eq x
'$%e
))
483 ;; Replace $%e with its numerical value,
484 ;; when %enumer and $numer TRUE
488 (cond ((and (cdr x
) (atom (cdr x
)))
489 (merror (intl:gettext
"simplifya: malformed expression (atomic cdr).")))
490 ((get (car x
) 'lisp-no-simp
)
491 ;; this feature is to be used with care. it is meant to be
492 ;; used to implement data objects with minimum of consing.
493 ;; forms must not bash the DISPLA package. Only new forms
494 ;; with carefully chosen names should use this feature.
497 (mapcar #'(lambda (x) (simplifya x y
)) (cdr x
))))))
498 ((eq (caar x
) 'rat
) (*red1 x
))
499 ;; Enforced resimplification: Reset dosimp and strip 'simp tags from x.
500 (dosimp (let ((dosimp nil
)) (simplifya (unsimplify x
) y
)))
501 ((member 'simp
(cdar x
)) x
)
502 ((eq (caar x
) 'mrat
) x
)
503 ((not (atom (caar x
)))
504 (cond ((or (eq (caaar x
) 'lambda
)
505 (and (not (atom (caaar x
))) (eq (caaaar x
) 'lambda
)))
506 (mapply1 (caar x
) (cdr x
) (caar x
) x
))
507 (t (merror (intl:gettext
"simplifya: operator is neither an atom nor a lambda expression: ~S") x
))))
508 ((and $distribute_over
509 (get (caar x
) 'distribute_over
)
510 ;; A function with the property 'distribute_over.
511 ;; Look, if we have a bag as argument to the function.
512 (distribute-over x
)))
513 ((get (caar x
) 'opers
)
514 (let ((opers-list *opers-list
)) (oper-apply x y
)))
515 ((and (eq (caar x
) 'mqapply
)
517 (and (eq substp
'mqapply
)
518 (or (eq (car (cadr x
)) 'lambda
)
519 (eq (caar (cadr x
)) 'lambda
)))))
520 (cond ((or (symbolp (cadr x
)) (not (atom (cadr x
))))
521 (simplifya (cons (cons (cadr x
) (cdar x
)) (cddr x
)) y
))
522 ((or (not (member 'array
(cdar x
))) (not $subnumsimp
))
523 (merror (intl:gettext
"simplifya: I don't know how to simplify this operator: ~M") x
))
525 (t (let ((w (get (caar x
) 'operators
)))
527 (or (not (member 'array
(cdar x
)))
530 (t (simpargs x y
)))))))
532 ;; EQTEST returns an expression which is the same as X
533 ;; except that it is marked with SIMP and maybe other flags from CHECK.
535 ;; Following description is inferred from the code. Dunno why the function is named "EQTEST".
537 ;; (1) if X is already marked with SIMP flag or doesn't need it: return X.
538 ;; (2) if X is pretty much the same as CHECK (same operator and same arguments),
539 ;; then return CHECK after marking it with SIMP flag.
540 ;; (3) if operator of X has the MSIMPIND property, replace it
541 ;; with value of MSIMPIND (something like '(MPLUS SIMP)) and return X.
542 ;; (4) if X or CHECK is an array expression, return X after marking it with SIMP and ARRAY flags.
543 ;; (5) otherwise, return X after marking it with SIMP flag.
545 (defun eqtest (x check
)
550 (member 'simp
(cdar x
)))
552 ((and (eq (caar x
) (caar check
))
553 (equal (cdr x
) (cdr check
)))
554 (cond ((and (null (cdar check
))
555 (setq y
(get (caar check
) 'msimpind
)))
556 (cons y
(cdr check
)))
557 ((member 'simp
(cdar check
))
560 (cons (cons (caar check
)
562 (cons 'simp
(cdar check
))
565 ((setq y
(get (caar x
) 'msimpind
))
567 ((or (member 'array
(cdar x
))
568 (and (eq (caar x
) (caar check
))
569 (member 'array
(cdar check
))))
570 (rplaca x
(cons (caar x
) '(simp array
))))
572 (rplaca x
(cons (caar x
) '(simp)))))))
574 ;; A function, which distributes of bags like a list, matrix, or equation.
575 ;; Check, if we have to distribute of one of the bags or any other operator.
576 (defun distribute-over (expr)
577 (cond ((= 1 (length (cdr expr
)))
578 ;; Distribute over for a function with one argument.
579 (cond ((and (not (atom (cadr expr
)))
580 (member (caaadr expr
) (get (caar expr
) 'distribute_over
)))
583 (mapcar #'(lambda (u) (simplify (list (car expr
) u
)))
587 ;; A function with more than one argument.
588 (do ((args (cdr expr
) (cdr args
))
591 (when (and (not (atom (car args
)))
592 (member (caar (car args
))
593 (get (caar expr
) 'distribute_over
)))
594 ;; Distribute the function over the arguments and simplify again.
597 (cons (ncons (caar (car args
)))
598 (mapcar #'(lambda (u)
602 (cons (ncons (caar expr
))
603 (reverse first-args
))
606 (cdr (car args
)))))))
607 (setq first-args
(cons (car args
) first-args
))))))
609 (defun rulechk (x) (or (mget x
'oldrules
) (get x
'rules
)))
611 (defun resimplify (x) (let ((dosimp t
)) (simplifya x nil
)))
613 (defun unsimplify (x)
614 (if (or (atom x
) (specrepp x
))
616 (cons (remove 'simp
(car x
) :count
1) (mapcar #'unsimplify
(cdr x
)))))
618 (defun simpargs (x y
)
619 (if (or (and (eq (get (caar x
) 'dimension
) 'dimension-infix
)
620 (not (getl (caar x
) '($lassociative $rassociative
))))
621 (get (caar x
) 'binary
))
623 (if (and (member 'array
(cdar x
)) (null (margs x
)))
624 (merror (intl:gettext
"SIMPARGS: subscripted variable found with no subscripts.")))
625 (eqtest (if y x
(let ((flag (member (caar x
) '(mlist mequal
))))
626 (cons (ncons (caar x
))
627 (mapcar #'(lambda (u)
628 (if flag
(simplifya u nil
)
633 ;;;-----------------------------------------------------------------------------
634 ;;; ADDK (X Y) 27.09.2010/DK
636 ;;; Arguments and values:
637 ;;; X - a Maxima number
638 ;;; Y - a Maxima number
639 ;;; result - a simplified Maxima number
642 ;;; ADDK adds two Maxima numbers and returns a simplified Maxima number.
643 ;;; ADDK can be called in Lisp code, whenever the arguments are valid
644 ;;; Maxima numbers, these are integer, float, Maxima rational, or
645 ;;; Maxima bigfloat numbers. The arguments must not be simplified. The
646 ;;; precision of a bigfloat result depends on the setting of the
647 ;;; global variable $FPPREC. If the option variable $FLOAT is T, a
648 ;;; Maxima rational number as a result is converted to a float number.
652 ;;; (addk 2.0 3) -> 5.0
653 ;;; (addk ($bfloat 2) 3)-> ((BIGFLOAT SIMP 56) 45035996273704960 3)
654 ;;; (addk 2 '((rat) 1 2)) -> ((RAT SIMP) 5 2)
655 ;;; (let (($float t)) (addk 2 '((rat) 1 2))) -> 2.5
658 ;;; The option variables $FLOAT and $FPPREC.
661 ;;; TIMESK to multiply and EXPTRL to exponentiate two Maxima numbers.
664 ;;; The routine works for Lisp rational and Lisp complex numbers too.
665 ;;; This feature is not used in Maxima code. If Lisp complex and
666 ;;; rational numbers are mixed with Maxima rational or bigfloat
667 ;;; numbers the result is wrong or a Lisp error is generated.
668 ;;;-----------------------------------------------------------------------------
673 ((and (numberp x
) (numberp y
)) (+ x y
))
674 ((or ($bfloatp x
) ($bfloatp y
)) ($bfloat
(list '(mplus) x y
)))
677 (cond ((floatp x
) (return (+ x
(fpcofrat y
))))
678 (t (setq x
(list '(rat) x
1)))))
680 (cond ((floatp y
) (return (+ y
(fpcofrat x
))))
681 (t (setq y
(list '(rat) y
1))))))
682 (setq g
(gcd (caddr x
) (caddr y
)))
683 (setq a
(truncate (caddr x
) g
)
684 b
(truncate (caddr y
) g
))
685 (return (timeskl (list '(rat) 1 g
)
691 ;;;-----------------------------------------------------------------------------
692 ;;; *RED1 (X) 27.09.2010/DK
695 ;;; Arguments and values:
696 ;;; X - a Maxima rational number (for *RED1)
697 ;;; N - an integer number representing the numerator of a rational
698 ;;; D - an integer number representing the denominator of a rational
699 ;;; result - a simplified Maxima rational number
702 ;;; *RED1 is called from SIMPLIFYA to reduce and simplify a Maxima rational
703 ;;; number. *RED1 checks if the rational number is already simplified. If
704 ;;; the option variable $FLOAT is T, the rational number is converted to a
705 ;;; float number. If the number is not simplified, *RED is called.
707 ;;; *RED reduces the numerator N and the demoniator D and returns a
708 ;;; simplified Maxima rational number. The result is converted to a float
709 ;;; number, if the option variable $FLOAT is T.
712 ;;; The option variable $FLOAT.
713 ;;;-----------------------------------------------------------------------------
716 (cond ((member 'simp
(cdar x
))
717 (if $float
(fpcofrat x
) x
))
718 (t (*red
(cadr x
) (caddr x
)))))
723 (t (let ((u (gcd n d
)))
724 (setq n
(truncate n u
)
726 (if (minusp d
) (setq n
(- n
) d
(- d
)))
727 (cond ((equal d
1) n
)
728 ($float
(fpcofrat1 n d
))
729 (t (list '(rat simp
) n d
)))))))
731 ;;;-----------------------------------------------------------------------------
732 ;;; TIMESK (X Y) 27.09.2010/DK
734 ;;; Arguments and values:
735 ;;; X - a Maxima number
736 ;;; Y - a Maxima number
737 ;;; result - a simplified Maxima number
740 ;;; TIMESK Multiplies two Maxima numbers and returns a simplified Maxima
741 ;;; number. TIMESK can be called in Lisp code, whenever the arguments are
742 ;;; valid Maxima numbers, these are integer, float, Maxima rational, or
743 ;;; Maxima bigfloat numbers. The arguments must not be simplified. The
744 ;;; precision of a bigfloat result depends on the setting of the
745 ;;; global variable $FPPREC. If the option variable $FLOAT is T, a
746 ;;; Maxima rational number as a result is converted to a float number.
748 ;;; TIMESKL is called from TIMESK to multiply two Maxima rational numbers or
749 ;;; a rational number with an integer number.
752 ;;; (timesk 2 3) -> 6
753 ;;; (timesk 2.0 3) -> 6.0
754 ;;; (timesk ($bfloat 2) 3)-> ((BIGFLOAT SIMP 56) 54043195528445952 3)
755 ;;; (timesk 3 '((rat) 1 2)) -> ((RAT SIMP) 3 2)
756 ;;; (let (($float t)) (timesk 3 '((rat) 1 2))) -> 1.5
759 ;;; The option variables $FLOAT and $FPPREC.
762 ;;; ADDK to add and EXPTRL to exponentiate two Maxima numbers.
765 ;;; The routine works for Lisp rational and Lisp complex numbers too.
766 ;;; This feature is not used in Maxima code. If Lisp complex and
767 ;;; rational numbers are mixed with Maxima rational or bigfloat
768 ;;; numbers the result is wrong or a Lisp error is generated.
769 ;;;-----------------------------------------------------------------------------
771 ;; NUM1 and DENOM1 are helper functions for TIMESKL to get the numerator and the
772 ;; denominator of an integer or Maxima rational number. For an integer the
773 ;; denominator is 1. Both functions are used at other places in Maxima code too.
776 (if (numberp a
) a
(cadr a
)))
779 (if (numberp a
) 1 (caddr a
)))
781 (defun timesk (x y
) ; X and Y are assumed to be already reduced
782 (cond ((equal x
1) y
)
784 ((and (numberp x
) (numberp y
)) (* x y
))
785 ((or ($bfloatp x
) ($bfloatp y
)) ($bfloat
(list '(mtimes) x y
)))
786 ((floatp x
) (* x
(fpcofrat y
)))
787 ((floatp y
) (* y
(fpcofrat x
)))
790 ;; TIMESKL takes one or two Maxima rational numbers, one argument can be an
791 ;; integer number. The result is a Maxima rational or an integer number.
792 ;; If the option variable $FLOAT is T, a Maxima rational number in converted
797 (setq u
(*red
(num1 x
) (denom1 y
)))
798 (setq v
(*red
(num1 y
) (denom1 x
)))
799 (setq g
(cond ((or (equal u
0) (equal v
0)) 0)
801 ((and (numberp u
) (numberp v
)) (* u v
))
803 (* (num1 u
) (num1 v
))
804 (* (denom1 u
) (denom1 v
))))))
805 (return (cond ((numberp g
) g
)
806 ((equal (caddr g
) 1) (cadr g
))
807 ($float
(fpcofrat g
))
810 ;;;-----------------------------------------------------------------------------
811 ;;; FPCOFRAT (RATNO) 27.09.2010/DK
814 ;;; Arguments and values:
815 ;;; RATNO - a Maxima rational number (for FPCOFRAT)
816 ;;; NU - an integer number which represents the numerator of a rational
817 ;;; D - an integer number which represents the denominator of a rational
818 ;;; result - floating point approximation of a rational number
821 ;;; Floating Point Conversion OF RATional number routine.
822 ;;; Finds floating point approximation to rational number.
824 ;;; FPCOFRAT1 computes the quotient of NU/D.
826 ;;; Exceptional situations:
827 ;;; A Lisp error is generated, if the rational number does not fit into a
829 ;;;-----------------------------------------------------------------------------
831 ;; This constant is only needed in the file float.lisp.
833 (:compile-toplevel
:load-toplevel
:execute
)
834 (defconstant machine-mantissa-precision
(float-digits 1.0)))
836 (defun fpcofrat (ratno)
837 (fpcofrat1 (cadr ratno
) (caddr ratno
)))
839 (defun fpcofrat1 (nu d
)
842 ;;;-----------------------------------------------------------------------------
843 ;;; EXPTA (X Y) 27.09.2010/DK
845 ;;; Arguments and values:
846 ;;; X - a Maxima number
847 ;;; Y - an integer number
848 ;;; result - a simplified Maxima number
851 ;;; Computes X^Y, where X is Maxima number and Y an integer. The result is
852 ;;; a simplified Maxima number. Y can be a rational Maxima number. For this
853 ;;; case the numerator is taken as the power.
856 ;;; The option variables $FLOAT and $FPPREC.
859 ;;; This routine is not used within the simplifier. There is only one
860 ;;; call from the file hayat.lisp. This call can be replaced with a
861 ;;; call of the function power.
862 ;;;-----------------------------------------------------------------------------
870 ($bfloat
(list '(mexpt) x y
)))
872 (*red
(exptb (caddr x
) (- (num1 y
)))
873 (exptb (cadr x
) (- (num1 y
)))))
875 (*red
(exptb (cadr x
) (num1 y
))
876 (exptb (caddr x
) (num1 y
))))))
878 ;;;-----------------------------------------------------------------------------
879 ;;; EXPTB (A B) 27.09.2010/DK
881 ;;; Arguments and values:
882 ;;; A - a float or integer number
883 ;;; B - an integer number
884 ;;; result - a simplified Maxima number
887 ;;; Computes A^B, where A is a float or an integer number and B is an
888 ;;; integer number. The result is an integer, float, or Maxima
893 ;;; (exptb 3.0 2) -> 9.0
894 ;;; (exptb 3 -2) -> ((RAT SiMP) 1 9)
895 ;;; (let (($float t)) (exptb 3 -2)) -> 0.1111111111111111
898 ;;; The option variable $FLOAT.
901 ;;; EXPTB calls the Lisp functions EXP or EXPT to compute the result.
902 ;;;-----------------------------------------------------------------------------
906 (cond ((equal a %e-val
)
907 ;; Make B a float so we'll get double-precision result.
909 ((or (floatp a
) (not (minusp b
)))
912 (setq b
(expt a
(- b
)))
914 (when (float-inf-p result
) ;; needed for gcl and sbcl - (sometimes) no trap of overflow
915 (error 'floating-point-overflow
))
919 ;;;-----------------------------------------------------------------------------
920 ;;; SIMPLUS (X W Z) 27.09.2010/DK
922 ;;; Arguments and values:
923 ;;; X - a Maxima expression of the form ((mplus) term1 term2 ...)
924 ;;; W - an arbitrary value, the value is ignored
925 ;;; Z - T or NIL, if T the arguments are assumed to be simplified
926 ;;; result - a simplified mplus-expression or an atom
929 ;;; Implementation of the simplifier for the "+" operator.
930 ;;; A general description of SIMPLUS can be found in the paper:
931 ;;; http://www.cs.berkeley.edu/~fateman/papers/simplifier.txt
934 ;;; The addition of matrices and lists is affected by the following option
936 ;;; $DOALLMXOPS, $DOMXMXOPS, $DOMXPLUS, $DOSCMXOPPS, $DOSCMXPLUS, $LISTARITH
939 ;;; This routine should not be called directely. It is called by SIMPLIFYA.
940 ;;; A save access is to call the function ADD.
941 ;;;-----------------------------------------------------------------------------
943 (defun simplus (x w z
)
944 (prog (res check eqnflag matrixflag sumflag
)
945 (if (null (cdr x
)) (return 0))
949 (if (null x
) (go end
))
950 (setq w
(if z
(car x
) (simplifya (car x
) nil
)))
957 (not (member 'trunc
(cdar w
))))
958 (spsimpcases (cdr x
) w
))
959 (setq w
(ratdisrep w
))
964 (nconc (mapcar #'simplify
(cons w
(cdr x
)))
966 ((eq (caar w
) 'mequal
)
971 (add2 (cadr eqnflag
) (cadr w
))
972 (add2 (caddr eqnflag
) (caddr w
)))))
974 ((member (caar w
) '(mlist $matrix
))
976 (cond ((not matrixflag
) w
)
977 ((and (or $doallmxops $domxmxops $domxplus
978 (and (eq (caar w
) 'mlist
)
979 ($listp matrixflag
)))
980 (or (not (eq (caar w
) 'mlist
)) $listarith
))
981 (addmx matrixflag w
))
982 (t (setq res
(pls w res
)) matrixflag
)))
985 (setq sumflag t res
(sumpls w res
))
986 (setq w
(car res
) res
(cdr res
))))
987 (setq res
(pls w res
))
990 (setq res
(testp res
))
993 (cond ((and (or ($listp matrixflag
)
994 $doallmxops $doscmxplus $doscmxops
)
995 (or (not ($listp matrixflag
)) $listarith
))
996 (mxplusc res matrixflag
))
997 (t (testp (pls matrixflag
(pls res nil
)))))))
998 (setq res
(eqtest res check
))
1001 (add2 (cadr eqnflag
) res
)
1002 (add2 (caddr eqnflag
) res
))
1005 ;;;-----------------------------------------------------------------------------
1006 ;;; PLS (X OUT) 27.09.2010/DK
1008 ;;; Arguments and values:
1009 ;;; X - a Maxima expression or an atom
1010 ;;; OUT - a form ((mplus) <number> term1 term2 ...) or NIL
1011 ;;; result - a form ((mplus) <number> term1 ...), where x is added in.
1014 ;;; Adds the argument X into the form OUT. If OUT is NIL a form
1015 ;;; ((mplus) 0 X) is initialized, if X is an expression or a symbol,
1016 ;;; or ((mplus) X), if X is a number. Numbers are added to the first
1017 ;;; term <number> of the form. Any other symbol or expression is added
1018 ;;; into the canonical ordered list of arguments. The result is in a
1019 ;;; canonical order, but it is not a valid Maxima expression. To get a
1020 ;;; valid Maxima expression the result has to be checked with the
1021 ;;; function TESTP. This is done by the calling routine SIMPLUS.
1023 ;;; PLS checks the global flag *PLUSFLAG*, which is set in PLUSIN to T,
1024 ;;; if a mplus-expression is part of the result.
1027 ;;; (pls 2 nil) -> ((MPLUS) 2)
1028 ;;; (pls '$A nil) -> ((MPLUS) 0 $A)
1029 ;;; (pls '$B '((mplus) 0 $A)) -> ((MPLUS) 0 $A $B)
1030 ;;; (pls '$A '((mplus) 0 $A)) -> ((MPLUS) 0 ((MTIMES SIMP) 2 $A))
1032 ;;; Examples with the option variables $NUMER and $NEGDISTRIB:
1033 ;;; (let (($numer t)) (pls '$%e nil)) -> ((MPLUS) 2.718281828459045)
1034 ;;; (let (($negdistrib t)) (pls '((mtimes) -1 ((mplus) $A $B)) nil))
1035 ;;; -> ((MPLUS) 0 ((MTIMES SIMP) -1 $A) ((MTIMES SIMP) -1 $B))
1036 ;;; (let (($negdistrib nil)) (pls '((mtimes) -1 ((mplus) $A $B)) nil))
1037 ;;; -> ((MPLUS) 0 ((MTIMES) -1 ((MPLUS) $A $B)))
1040 ;;; The option variables $NUMER and $NEGDISTRIB and the global flag
1041 ;;; *PLUSFLAG*, which is set in the routine PLUSIN.
1044 ;;; PLUSIN and ADDK which are called from PLS and SIMPLUS.
1047 ;;; To add an expression into the list (CDR OUT), the list is passed
1048 ;;; to the routine PLUSIN as an argument. PLUSIN adds the argument to
1049 ;;; the list of terms by modifying the list (CDR OUT) destructively.
1050 ;;; The new value of OUT is returned as a result by PLS.
1051 ;;;-----------------------------------------------------------------------------
1053 ;; Set in PLUSIN to T to indicate a nested mplus expression.
1054 (defvar *plusflag
* nil
)
1056 ;; TESTP checks the result of PLS to get a valid Maxima mplus-expression.
1060 ((null (cddr x
)) (cadr x
))
1062 (cond ((null (cdddr x
)) (caddr x
)) (t (rplacd x
(cddr x
)))))
1066 (prog (fm *plusflag
*)
1067 (if (mtimesp x
) (setq x
(testtneg x
)))
1068 (when (and $numer
(atom x
) (eq x
'$%e
))
1069 ;; Replace $%e with its numerical value, when $numer ist TRUE
1072 ;; Initialize a form like ((mplus) <number> expr)
1075 (cond ((mnump x
) (ncons x
))
1077 (list 0 (cond ((atom x
) x
) (t (copy-list x
)))))
1078 ((mnump (cadr x
)) (copy-list (cdr x
) ))
1079 (t (cons 0 (copy-list (cdr x
) )))))))
1081 ;; Add a number into the first term of the list out.
1082 (return (cons '(mplus)
1083 (if (mnump (cadr out
))
1084 (cons (addk (cadr out
) x
) (cddr out
))
1085 (cons x
(cdr out
))))))
1086 ((not (mplusp x
)) (plusin x
(cdr out
)) (go end
)))
1087 ;; At this point we have a mplus expression as argument x. The following
1088 ;; code assumes that the argument x is already simplified and the terms
1089 ;; are in a canonical order.
1090 ;; First we add the number to the first term of the list out.
1092 (addk (if (mnump (cadr out
)) (cadr out
) 0)
1093 (cond ((mnump (cadr x
)) (setq x
(cdr x
)) (car x
)) (t 0))))
1094 ;; Initialize fm with the list of terms and start the loop to add the
1095 ;; terms of an mplus expression into the list out.
1098 (if (null (setq x
(cdr x
))) (go end
))
1099 ;; The return value of PLUSIN is a list, where the first element is the
1100 ;; added argument and the rest are the terms which follow the added
1102 (setq fm
(plusin (car x
) fm
))
1105 (if (not *plusflag
*) (return out
))
1106 (setq *plusflag
* nil
) ; *PLUSFLAG* T handles e.g. a+b+3*(a+b)-2*(a+b)
1108 ;; *PLUSFLAG* is set by PLUSIN to indicate that a mplus expression is
1109 ;; part of the result. For this case go again through the terms of the
1110 ;; result and add any term of the mplus expression into the list out.
1113 (when (mplusp (cadr fm
))
1115 (rplacd fm
(cddr fm
))
1119 (if (null (cdr fm
)) (return out
))
1122 ;;;-----------------------------------------------------------------------------
1123 ;;; PLUSIN (X FM) 27.09.2010/DK
1125 ;;; Arguments and values:
1126 ;;; X - a Maxima expression or atom
1127 ;;; FM - a list with the terms of an addition
1128 ;;; result - part of the list fm, which starts at the inserted expression
1131 ;;; Adds X into running list of additive terms FM. The routine modifies
1132 ;;; the argument FM destructively, but does not return the modified list as
1133 ;;; a result. The return value is a part of the list FM, which starts at the
1134 ;;; inserted term. PLUSIN can not handle Maxima numbers. PLUSIN is called
1135 ;;; only from the routine PLS.
1139 ;;; (plusin '$a fm) -> ($A)
1141 ;;; (plusin '$b fm) -> ($B)
1143 ;;; (plusin '$a fm) -> (((MTIMES SIMP) 2 $A) $B)
1144 ;;; fm -> (0 ((MTIMES SIMP) 2 $A) $B)
1147 ;;; Modifies destructively the argument FM, which contains the result of the
1148 ;;; addition of the argument X into the list FM.
1151 ;;; The option variables $doallmxops and $listarith.
1154 ;;; The return value is used in PLS to go in parallel through the list of
1155 ;;; terms, when adding a complete mplus-expression into the list of terms.
1156 ;;; This is triggered by the flag *PLUSFLAG*, which is set in PLUSIN, if
1157 ;;; a mplus-expression is added to the result list.
1158 ;;;-----------------------------------------------------------------------------
1160 (defun plusin (x fm
)
1161 (prog (x1 x2 flag check v w xnew a n m c
)
1166 (if (mnump (cadr x
)) (setq w
(cadr x
) x
(cddr x
))
1168 (t (setq x
(ncons x
))))
1169 (setq x1
(if (null (cdr x
)) (car x
) (cons '(mtimes) x
))
1170 xnew
(list* '(mtimes) w x
))
1172 (cond ((null (cdr fm
)))
1173 ((and (alike1 x1
(cadr fm
)) (null (cdr x
)))
1175 ;; Implement the simplification of
1176 ;; v*a^(c+n)+w*a^(c+m) -> (v*a^n+w*a^m)*a^c
1177 ;; where a, v, w, and (n-m) are integers.
1178 ((and (or (and (mexptp (setq x2
(cadr fm
)))
1181 (not (alike1 x1 x2
))
1183 (integerp (setq v
(cadr x2
)))
1184 (mexptp (setq x2
(caddr x2
)))))
1185 (integerp (setq a
(cadr x2
)))
1188 (integerp (sub (caddr x2
) (caddr x1
))))
1189 (setq n
(if (and (mplusp (caddr x2
))
1190 (mnump (cadr (caddr x2
))))
1192 (if (mnump (caddr x2
))
1195 (setq m
(if (and (mplusp (caddr x1
))
1196 (mnump (cadr (caddr x1
))))
1198 (if (mnump (caddr x1
))
1201 (setq c
(sub (caddr x2
) n
))
1204 ;; n and m are integers and the result is (v*a^n+w*a^m)*a^c.
1205 (setq x1
(mul (addk (timesk v
(exptb a n
))
1206 (timesk w
(exptb a m
)))
1210 ;; n and m are rational numbers: The difference n-m is an
1211 ;; integer. The rational numbers might be improper fractions.
1212 ;; The mixed numbers are: n = n1 + d1/r and m = n2 + d2/r,
1213 ;; where r is the common denominator. We have two cases:
1214 ;; I) d1 = d2: e.g. 2^(1/3+c)+2^(4/3+c)
1215 ;; The result is (v*a^n1+w*a^n2)*a^(c+d1/r)
1216 ;; II) d1 # d2: e.g. 2^(1/2+c)+2^(-1/2+c)
1217 ;; In this case one of the exponents d1 or d2 must
1218 ;; be negative. The negative exponent is factored out.
1219 ;; This guarantees that the factor (v*a^n1+w*a^n2)
1220 ;; is an integer. But the positive exponent has to be
1221 ;; adjusted accordingly. E.g. when we factor out
1222 ;; a^(d2/r) because d2 is negative, then we have to
1223 ;; adjust the positive exponent to n1 -> n1+(d1-d2)/r.
1225 ;; Part of the simplification is done in simptimes. E.g.
1226 ;; this algorithm simplifies the sum sqrt(2)+3*sqrt(2)
1227 ;; to 4*sqrt(2). In simptimes this is further simplified
1229 (multiple-value-bind (n1 d1
)
1230 (truncate (num1 n
) (denom1 n
))
1231 (multiple-value-bind (n2 d2
)
1232 (truncate (num1 m
) (denom1 m
))
1233 (cond ((equal d1 d2
)
1234 ;; Case I: -> (v*a^n1+w*a^n2)*a^(c+d1/r)
1236 (mul (addk (timesk v
(exptb a n1
))
1237 (timesk w
(exptb a n2
)))
1240 (div d1
(denom1 n
))))))
1243 ;; Case II:: d2 is negative, adjust n1.
1244 (setq n1
(add n1
(div (sub d1 d2
) (denom1 n
))))
1246 (mul (addk (timesk v
(exptb a n1
))
1247 (timesk w
(exptb a n2
)))
1250 (div d2
(denom1 n
))))))
1253 ;; Case II: d1 is negative, adjust n2.
1254 (setq n2
(add n2
(div (sub d2 d1
) (denom1 n
))))
1256 (mul (addk (timesk v
(exptb a n1
))
1257 (timesk w
(exptb a n2
)))
1260 (div d1
(denom1 n
))))))
1262 ;; This clause should never be reached.
1263 (t (merror "Internal error in simplus."))))))))
1264 ((mtimesp (cadr fm
))
1265 (cond ((alike1 x1
(cadr fm
))
1267 ((and (mnump (cadadr fm
)) (alike x
(cddadr fm
)))
1268 (setq flag t
) ; found common factor
1270 ((great xnew
(cadr fm
)) (go gr
))))
1271 ((great x1
(cadr fm
)) (go gr
)))
1272 (setq xnew
(eqtest (testt xnew
) (or check
'((foo)))))
1273 (return (cdr (rplacd fm
(cons xnew
(cdr fm
)))))
1280 (list* '(mtimes simp
) 0 x
)
1281 ;; Call muln to get a simplified product.
1282 (if (mtimesp (setq x1
(muln (cons (addk 1 w
) x
) t
)))
1286 (cond ((not (mtimesp (cadr fm
)))
1289 ;; Do this simplification for an integer 1, not for 1.0 and 1.0b0
1290 (rplacd (cadr fm
) (cddadr fm
))
1292 ((not (zerop1 (cadadr fm
)))
1294 ;; Handle the multiplication with a zero.
1295 ((and (or (not $listarith
) (not $doallmxops
))
1296 (mxorlistp (caddr (cadr fm
))))
1298 (cons (constmx 0 (caddr (cadr fm
))) (cddr fm
))))))
1299 ;; (cadadr fm) is zero. If the first term of fm is a number,
1300 ;; add it to preserve the type.
1301 (when (mnump (car fm
))
1302 (rplaca fm
(addk (car fm
) (cadadr fm
))))
1303 (return (rplacd fm
(cddr fm
)))
1305 ;; Call muln to get a simplified product.
1306 (setq x1
(muln (cons (addk w
(if flag
(cadadr fm
) 1)) x
) t
))
1307 ;; Make a mplus expression to guarantee that x1 is added again into the sum
1308 (setq x1
(list '(mplus) x1
))
1312 (list* '(mtimes) x1 x
)
1313 (if (mtimesp x1
) (testtneg x1
) x1
)))
1314 (if (not (mtimesp (cadr fm
))) (go check
))
1315 (when (and (onep (cadadr fm
)) flag
(null (cdddr (cadr fm
))))
1316 ;; Do this simplification for an integer 1, not for 1.0 and 1.0b0
1317 (rplaca (cdr fm
) (caddr (cadr fm
))) (go check
))
1320 (if (mplusp (cadr fm
)) (setq *plusflag
* t
)) ; A nested mplus expression
1323 ;;;-----------------------------------------------------------------------------
1325 ;; Routines to add matrices
1327 (defun mxplusc (sc mx
)
1329 (setq sc
(partition-ns (cdr sc
)))
1330 (cond ((null (car sc
)) (cons '(mplus) (cons mx
(cadr sc
))))
1331 ((not (null (cadr sc
)))
1334 (outermap1 'mplus
(cons '(mplus) (car sc
)) mx
))
1336 (t (simplify (outermap1 'mplus
(cons '(mplus) (car sc
)) mx
)))))
1337 ((not (scalar-or-constant-p sc $assumescalar
))
1338 (testp (pls mx
(pls sc nil
))))
1339 (t (simplify (outermap1 'mplus sc mx
)))))
1341 (defun partition-ns (x)
1342 (let (sp nsp
) ; SP = scalar part, NSP = nonscalar part
1343 (mapc #'(lambda (z) (if (scalar-or-constant-p z $assumescalar
)
1344 (setq sp
(cons z sp
))
1345 (setq nsp
(cons z nsp
))))
1347 (list (nreverse sp
) (nreverse nsp
))))
1349 (defun addmx (x1 x2
)
1350 (let (($doscmxops t
) ($domxmxops t
) ($listarith t
))
1351 (simplify (fmapl1 'mplus x1 x2
))))
1353 ;;; ----------------------------------------------------------------------------
1355 ;;; Simplification of the Log function
1357 ;; The log function distributes over lists, matrices, and equations
1358 (defprop %log
(mlist $matrix mequal
) distribute_over
)
1360 (defun simpln (x y z
)
1362 (setq y
(simpcheck (cadr x
) z
))
1363 (cond ((onep1 y
) (addk -
1 y
))
1365 (cond (radcanp (list '(%log simp
) 0))
1367 (merror (intl:gettext
"log: encountered log(0).")))
1368 (t (throw 'errorsw t
))))
1369 ;; Check evaluation in floating point precision.
1370 ((flonum-eval (mop x
) y
))
1371 ;; Check evaluation in bigfloag precision.
1372 ((and (not (member 'simp
(car x
)))
1373 (big-float-eval (mop x
) y
)))
1376 (cond ((or (and $logexpand
(eq $domain
'$real
))
1377 (member $logexpand
'($all $super
))
1378 (and (eq ($csign
(cadr y
)) '$pos
)
1379 (not (member ($csign
(caddr y
))
1380 '($complex $imaginary
)))))
1381 ;; Simplify log(x^a) -> a*log(x), where x > 0 and a is real
1382 (mul (caddr y
) (take '(%log
) (cadr y
))))
1383 ((or (and (ratnump (caddr y
))
1384 (or (eql 1 (cadr (caddr y
)))
1385 (eql -
1 (cadr (caddr y
)))))
1386 (maxima-integerp (inv (caddr y
))))
1387 ;; Simplify log(z^(1/n)) -> log(z)/n, where n is an integer
1389 (take '(%log
) (cadr y
))))
1390 ((and (eq (cadr y
) '$%e
)
1391 (or (not (member ($csign
(caddr y
))
1392 '($complex $imaginary
)))
1393 (not (member ($csign
(mul '$%i
(caddr y
)))
1394 '($complex $imaginary
)))))
1395 ;; Simplify log(exp(x)) and log(exp(%i*x)), where x is a real
1397 (t (eqtest (list '(%log
) y
) x
))))
1399 ;; Simplify log(n/d)
1400 (cond ((eql (cadr y
) 1)
1401 (mul -
1 (take '(%log
) (caddr y
))))
1402 ((eq $logexpand
'$super
)
1403 (sub (take '(%log
) (cadr y
)) (take '(%log
) (caddr y
))))
1404 (t (eqtest (list '(%log
) y
) x
))))
1405 ((and (member $logexpand
'($all $super
))
1407 (do ((y (cdr y
) (cdr y
))
1409 ((null y
) (return (addn b t
)))
1410 (setq b
(cons (take '(%log
) (car y
)) b
))))
1411 ((and (member $logexpand
'($all $super
))
1413 (member (caar y
) '(%product $product
)))
1414 (let ((new-op (if (char= (get-first-char (caar y
)) #\%
) '%sum
'$sum
)))
1415 (simplifya `((,new-op
) ((%log
) ,(cadr y
)) ,@(cddr y
)) t
)))
1418 (eq ($sign y
) '$neg
))
1419 (add (mul '$%i
'$%pi
) (take '(%log
) (neg y
))))
1420 ((taylorize (mop x
) (second x
)))
1421 (t (eqtest (list '(%log
) y
) x
))))
1424 (simplifya (list '(mtimes) (caddr w
)
1425 (simplifya (list '(%log
) (cadr w
)) t
)) t
))
1427 ;;; ----------------------------------------------------------------------------
1429 ;;; Implementation of the Square root function
1431 (defprop $sqrt %sqrt verb
)
1432 (defprop $sqrt %sqrt alias
)
1434 (defprop %sqrt $sqrt noun
)
1435 (defprop %sqrt $sqrt reversealias
)
1437 (defprop %sqrt simp-sqrt operators
)
1440 (simplify (list '(%sqrt
) z
)))
1442 (defun simp-sqrt (x ignored z
)
1443 (declare (ignore ignored
))
1445 (simplifya (list '(mexpt) (cadr x
) '((rat simp
) 1 2)) z
))
1447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1449 ;;; Simplification of the "/" operator.
1451 (defun simpquot (x y z
)
1453 (cond ((and (integerp (cadr x
)) (integerp (caddr x
)) (not (zerop (caddr x
))))
1454 (*red
(cadr x
) (caddr x
)))
1455 ((and (numberp (cadr x
)) (numberp (caddr x
)) (not (zerop (caddr x
))))
1456 (/ (cadr x
) (caddr x
)))
1457 ((and (floatp (cadr x
)) (floatp (caddr x
)) #-ieee-floating-point
(not (zerop (caddr x
))))
1458 (/ (cadr x
) (caddr x
)))
1459 ((and ($bfloatp
(cadr x
)) ($bfloatp
(caddr x
)) (not (equal bigfloatzero
(caddr x
))))
1460 ;; Call BIGFLOATP to ensure that arguments have same precision.
1461 ;; Otherwise FPQUOTIENT could return a spurious value.
1462 (bcons (fpquotient (cdr (bigfloatp (cadr x
))) (cdr (bigfloatp (caddr x
))))))
1463 (t (setq y
(simplifya (cadr x
) z
))
1464 (setq x
(simplifya (list '(mexpt) (caddr x
) -
1) z
))
1465 (if (equal y
1) x
(simplifya (list '(mtimes) y x
) t
)))))
1467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1469 ;;; Implementation of the abs function.
1471 ;; Put the properties alias, reversealiases, noun and verb on the property list.
1472 (defprop $abs mabs alias
)
1473 (defprop $abs mabs verb
)
1474 (defprop mabs $abs reversealias
)
1475 (defprop mabs $abs noun
)
1477 ;; The abs function distributes over bags.
1478 (defprop mabs
(mlist $matrix mequal
) distribute_over
)
1480 ;; Define a verb function $abs
1482 (simplify (list '(mabs) x
)))
1484 ;; The abs function is a simplifying function.
1485 (defprop mabs simpabs operators
)
1487 (defun simpabs (e y z
)
1488 (declare (ignore y
))
1491 (x (simpcheck (second e
) z
)))
1493 (cond ((complex-number-p x
#'(lambda (s) (or (floatp s
) ($bfloatp s
))))
1494 (maxima::to
(bigfloat::abs
(bigfloat:to x
))))
1496 ((complex-number-p x
#'mnump
)
1499 ;; nounform for arrays...
1500 ((or (arrayp x
) ($member x $arrays
)) `((mabs simp
) ,x
))
1502 ;; taylor polynomials
1503 ((taylorize 'mabs x
))
1505 ;; values for extended real arguments:
1506 ((member x
'($inf $infinity $minf
)) '$inf
)
1507 ((member x
'($ind $und
)) x
)
1509 ;; abs(abs(expr)) --> abs(expr). Since x is simplified, it's OK to return x.
1510 ((and (consp x
) (consp (car x
)) (eq (caar x
) 'mabs
))
1513 ;; abs(conjugate(expr)) = abs(expr).
1514 ((and (consp x
) (consp (car x
)) (eq (caar x
) '$conjugate
))
1515 (take '(mabs) (cadr x
)))
1518 (setq sgn
($csign x
))
1519 (cond ((member sgn
'($neg $nz
)) (mul -
1 x
))
1520 ((eq '$zero sgn
) (mul 0 x
))
1521 ((member sgn
'($pos $pz
)) x
)
1523 ;; for complex constant expressions, use $cabs
1524 ((and (eq sgn
'$complex
) ($constantp x
))
1527 ;; abs(pos^complex) --> pos^(realpart(complex)).
1528 ((and (eq sgn
'$complex
) (mexptp x
) (eq '$pos
($csign
(second x
))))
1529 (power (second x
) ($realpart
(third x
))))
1531 ;; for abs(neg^z), use cabs.
1532 ((and (mexptp x
) (eq '$neg
($csign
(second x
))))
1535 ;; When x # 0, we have abs(signum(x)) = 1.
1536 ((and (eq '$pn sgn
) (consp x
) (consp (car x
)) (eq (caar x
) '%signum
)) 1)
1538 ;; multiplicative property: abs(x*y) = abs(x) * abs(y). We would like
1539 ;; assume(a*b > 0), abs(a*b) --> a*b. Thus the multiplicative property
1540 ;; is applied after the sign test.
1542 (muln (mapcar #'(lambda (u) (take '(mabs) u
)) (margs x
)) t
))
1544 ;; abs(x^n) = abs(x)^n for integer n. Is the featurep check worthwhile?
1545 ;; Again the sign check is done first because we'd like abs(x^2) --> x^2.
1546 ((and (mexptp x
) ($featurep
(caddr x
) '$integer
))
1547 (power (take '(mabs) (cadr x
)) (caddr x
)))
1549 ;; Reflection rule: abs(-x) --> abs(x)
1550 ((great (neg x
) x
) (take '(mabs) (neg x
)))
1553 (t (eqtest (list '(mabs) x
) e
)))))))
1555 (defun abs-integral (x)
1556 (mul (div 1 2) x
(take '(mabs) x
)))
1558 (putprop 'mabs
`((x) ,'abs-integral
) 'integral
)
1560 ;; I (rtoy) think this does some simple optimizations of x * y.
1565 ;; We have something like ((mtimes) foo). This is the same as foo.
1568 ;; We have 1*foo. Which is the same as foo. This should not
1569 ;; be applied to 1.0 or 1b0!
1570 (cond ((null (cdddr x
))
1572 (t (rplacd x
(cddr x
)))))
1576 ;; This basically converts -(a+b) to -a-b.
1578 (cond ((and (equal (cadr x
) -
1)
1582 ;; If x is exactly of the form -1*(sum), and $negdistrib is
1583 ;; true, we distribute the -1 across the sum.
1584 (addn (mapcar #'(lambda (z)
1590 ;; Simplification of the "-" operator
1591 (defun simpmin (x vestigial z
)
1592 (declare (ignore vestigial
))
1593 (cond ((null (cdr x
)) 0)
1595 (mul -
1 (simplifya (cadr x
) z
)))
1597 ;; ((mminus) a b ...) -> ((mplus) a ((mtimes) -1 b) ...)
1598 (sub (simplifya (cadr x
) z
) (addn (cddr x
) z
)))))
1600 (defun simptimes (x w z
) ; W must be 1
1601 (prog (res check eqnflag matrixflag sumflag
)
1602 (if (null (cdr x
)) (return 1))
1608 (cond ((and matrixflag
(mxorlistp1 matrixflag
))
1609 (return (constmx res matrixflag
)))
1610 (eqnflag (return (list '(mequal simp
)
1611 (mul2 res
(cadr eqnflag
))
1612 (mul2 res
(caddr eqnflag
)))))
1615 (cond ((mxorlistp u
)
1616 (return (setq res
(constmx res u
))))
1618 (mxorlistp1 (cadr u
))
1619 ($numberp
(caddr u
)))
1620 (return (setq res
(constmx res
(cadr u
)))))
1624 (list '(mequal simp
)
1626 (mul2 res
(caddr u
))))))))))))
1628 ((null x
) (go end
)))
1629 (setq w
(if z
(car x
) (simplifya (car x
) nil
)))
1631 (cond ((atom w
) nil
)
1632 ((eq (caar w
) 'mrat
)
1633 (cond ((or eqnflag matrixflag
1635 (not (member 'trunc
(cdar w
))))
1636 (spsimpcases (cdr x
) w
))
1637 (setq w
(ratdisrep w
))
1641 (ratf (cons '(mtimes)
1642 (nconc (mapcar #'simplify
(cons w
(cdr x
)))
1644 ((eq (caar w
) 'mequal
)
1649 (mul2 (cadr eqnflag
) (cadr w
))
1650 (mul2 (caddr eqnflag
) (caddr w
)))))
1652 ((member (caar w
) '(mlist $matrix
))
1654 (cond ((not matrixflag
) w
)
1655 ((and (or $doallmxops $domxmxops $domxtimes
)
1656 (or (not (eq (caar w
) 'mlist
)) $listarith
)
1657 (not (eq *inv
* '$detout
)))
1658 (stimex matrixflag w
))
1659 (t (setq res
(tms (copy-tree w
) 1 (copy-tree res
))) matrixflag
)))
1661 ((and (eq (caar w
) '%sum
) $sumexpand
)
1662 (setq sumflag
(sumtimes sumflag w
))
1664 (setq res
(tms (copy-tree w
) 1 (copy-tree res
)))
1667 (cond ((mtimesp res
) (setq res
(testt res
))))
1668 (cond (sumflag (setq res
(cond ((or (null res
) (equal res
1)) sumflag
)
1669 ((not (mtimesp res
))
1670 (list '(mtimes) res sumflag
))
1671 (t (nconc res
(list sumflag
)))))))
1672 (cond ((or (atom res
)
1673 (not (member (caar res
) '(mexpt mtimes
)))
1674 (and (zerop $expop
) (zerop $expon
))
1676 ((eq (caar res
) 'mtimes
) (setq res
(expandtimes res
)))
1677 ((and (mplusp (cadr res
))
1678 (fixnump (caddr res
))
1679 (not (or (> (caddr res
) $expop
)
1680 (> (- (caddr res
)) $expon
))))
1681 (setq res
(expandexpt (cadr res
) (caddr res
)))))
1684 (cond ((null res
) matrixflag
)
1685 ((and (or ($listp matrixflag
)
1688 (not (member res
'(-1 -
1.0))))
1689 ;; RES should only be -1 here (not = 1)
1691 (member res
'(-1 -
1.0))))
1692 (or (not ($listp matrixflag
)) $listarith
))
1693 (mxtimesc res matrixflag
))
1694 (t (testt (tms matrixflag
1 (tms res
1 nil
))))))))
1695 (if res
(setq res
(eqtest res check
)))
1696 (return (cond (eqnflag
1697 (if (null res
) (setq res
1))
1699 (mul2 (cadr eqnflag
) res
)
1700 (mul2 (caddr eqnflag
) res
)))
1703 (defun spsimpcases (l e
)
1705 (if (or (mbagp u
) (and (not (atom u
))
1707 (not (member 'trunc
(cdar e
)))))
1710 (defun mxtimesc (sc mx
)
1712 (and (mtimesp sc
) (member (cadr sc
) '(-1 -
1.0))
1713 $doscmxops
(not (or $doallmxops $domxmxops $domxtimes
))
1714 (setq sign
(cadr sc
)) (rplaca (cdr sc
) nil
))
1715 (setq out
(let ((scp* (cond ((mtimesp sc
) (partition-ns (cdr sc
)))
1716 ((not (scalar-or-constant-p sc $assumescalar
))
1719 (cond ((null scp
*) (list '(mtimes simp
) sc mx
))
1720 ((and (not (atom scp
*)) (null (car scp
*)))
1721 (append '((mtimes)) (cadr scp
*) (list mx
)))
1722 ((or (atom scp
*) (and (null (cdr scp
*))
1723 (not (null (cdr sc
)))
1724 (setq scp
* (cons '(mtimes) (car scp
*))))
1726 (simplifya (outermap1 'mtimes scp
* mx
) nil
))
1727 (t (append '((mtimes))
1730 (cons '(mtimes) (car scp
*)) mx
)
1733 (cond (sign (if (mtimesp out
)
1734 (rplacd out
(cons sign
(cdr out
)))
1735 (list '(mtimes) sign out
)))
1736 ((mtimesp out
) (testt out
))
1740 (let (($doscmxops t
) ($domxmxops t
) ($listarith t
))
1741 (simplify (fmapl1 'mtimes x y
))))
1743 ;; TMS takes a simplified expression FACTOR and a cumulative
1744 ;; PRODUCT as arguments and modifies the cumulative product so
1745 ;; that the expression is now one of its factors. The
1746 ;; exception to this occurs when a tellsimp rule is triggered.
1747 ;; The second argument is the POWER to which the expression is
1748 ;; to be raised within the product.
1750 (defun tms (factor power product
&aux tem
)
1751 (let ((*rulesw
* nil
)
1753 (when (mplusp product
) (setq product
(list '(mtimes simp
) product
)))
1754 (cond ((zerop1 factor
)
1755 (cond ((mnegp power
)
1758 (merror (intl:gettext
"Division by 0"))))
1760 ((and (null product
)
1761 (or (and (mtimesp factor
) (equal power
1))
1762 (and (setq product
(list '(mtimes) 1)) nil
)))
1763 (setq tem
(append '((mtimes)) (if (mnump (cadr factor
)) nil
'(1))
1765 (if (= (length tem
) 1)
1766 (setq tem
(copy-list tem
))
1769 (do ((factor-list (cdr factor
) (cdr factor-list
)))
1770 ((or (null factor-list
) (zerop1 product
)) product
)
1771 (setq z
(timesin (car factor-list
) (cdr product
) power
))
1774 (setq product
(tms-format-product z
)))))
1776 (setq z
(timesin factor
(cdr product
) power
))
1778 (tms-format-product z
)
1781 (defun tms-format-product (x)
1782 (cond ((zerop1 x
) x
)
1783 ((mnump x
) (list '(mtimes) x
))
1784 ((not (mtimesp x
)) (list '(mtimes) 1 x
))
1785 ((not (mnump (cadr x
))) (cons '(mtimes) (cons 1 (cdr x
))))
1789 (cond ($ratsimpexpons
(sratsimp (list '(mplus) x y
)))
1790 ((and (mnump x
) (mnump y
)) (addk x y
))
1794 (if (and (mnump x
) (mnump y
))
1798 (defun simp-limit (x vestigial z
)
1799 (declare (ignore vestigial
))
1800 (let ((l1 (length x
))
1802 (unless (or (= l1
2) (= l1
4) (= l1
5))
1803 (merror (intl:gettext
"limit: wrong number of arguments.")))
1804 (setq y
(simpmap (cdr x
) z
))
1805 (cond ((and (= l1
5) (not (member (cadddr y
) '($plus $minus
))))
1806 (merror (intl:gettext
"limit: direction must be either 'plus' or 'minus': ~M") (cadddr y
)))
1808 (merror (intl:gettext
"limit: variable must not be a number; found: ~M") (cadr y
)))
1812 (eqtest (cons '(%limit
) y
) x
)))))
1814 (defun simpinteg (x vestigial z
)
1815 (declare (ignore vestigial
))
1816 (let ((l1 (length x
))
1818 (unless (or (= l1
3) (= l1
5))
1819 (merror (intl:gettext
"integrate: wrong number of arguments.")))
1820 (setq y
(simpmap (cdr x
) z
))
1821 (cond ((mnump (cadr y
))
1822 (merror (intl:gettext
"integrate: variable must not be a number; found: ~M") (cadr y
)))
1823 ((and (= l1
5) (alike1 (caddr y
) (cadddr y
)))
1826 (free (setq z
(sub (cadddr y
) (caddr y
))) '$%i
)
1827 (eq ($sign z
) '$neg
))
1828 (neg (simplifya (list '(%integrate
) (car y
) (cadr y
) (cadddr y
) (caddr y
)) t
)))
1832 (if (or (among '$inf z
) (among '$minf z
))
1836 (eqtest (cons '(%integrate
) y
) x
)))))
1838 (defun simpbigfloat (x vestigial simp-flag
)
1839 (declare (ignore vestigial simp-flag
))
1842 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1844 ;;; Implementation of the Exp function.
1846 (defprop $exp %exp verb
)
1847 (defprop $exp %exp alias
)
1849 (defprop %exp $exp noun
)
1850 (defprop %exp $exp reversealias
)
1852 (defprop %exp simp-exp operators
)
1855 (simplify (list '(%exp
) z
)))
1857 ;; Support a function for code,
1858 ;; which depends on an unsimplified noun form.
1859 (defmfun $exp-form
(z)
1860 (list '(mexpt) '$%e z
))
1862 (defun simp-exp (x ignored z
)
1863 (declare (ignore ignored
))
1865 (simplifya (list '(mexpt) '$%e
(cadr x
)) z
))
1867 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1869 (defun simplambda (x vestigial simp-flag
)
1870 (declare (ignore vestigial simp-flag
))
1871 ; Check for malformed lambda expressions.
1872 ; We verify that we have a valid list of parameters and a non-empty body.
1873 (let ((params (cadr x
)))
1874 (unless ($listp params
)
1875 (merror (intl:gettext
"lambda: first argument must be a list; found: ~M") params
))
1876 (do ((params (cdr params
) (cdr params
))
1879 (when (mdeflistp params
)
1880 (setq params
(cdar params
)))
1881 (let ((p (car params
)))
1882 (unless (or (mdefparam p
)
1883 (and (op-equalp p
'mquote
)
1884 (mdefparam (cadr p
))))
1885 (merror (intl:gettext
"lambda: parameter must be a symbol and must not be a system constant; found: ~M") p
))
1887 (when (member p seen-params
:test
#'eq
)
1888 (merror (intl:gettext
"lambda: ~M occurs more than once in the parameter list") p
))
1889 (push p seen-params
))))
1890 (when (null (cddr x
))
1891 (merror (intl:gettext
"lambda: no body present")))
1892 (cons '(lambda simp
) (cdr x
)))
1894 (defun simpmdef (x vestigial simp-flag
)
1895 (declare (ignore vestigial simp-flag
))
1897 (cons '(mdefine simp
) (cdr x
)))
1899 (defun simpmap (e z
)
1900 (mapcar #'(lambda (u) (simpcheck u z
)) e
))
1903 (let ((x ($expand e
1 1)))
1904 (cond ((or (not (free x
'$ind
)) (not (free x
'$und
))
1905 (not (free x
'$zeroa
)) (not (free x
'$zerob
))
1906 (not (free x
'$infinity
))
1909 ((and (free x
'$inf
) (free x
'$minf
)) x
)
1910 (t (infsimp1 x e
)))))
1912 (defun infsimp1 (x e
)
1913 (let ((minf-coef (coeff x
'$minf
1))
1914 (inf-coef (coeff x
'$inf
1)))
1915 (cond ((or (and (equal minf-coef
0)
1917 (and (not (free minf-coef
'$inf
))
1918 (not (free inf-coef
'$minf
)))
1919 (let ((new-exp (sub (add2 (mul2 minf-coef
'$minf
)
1920 (mul2 inf-coef
'$inf
))
1922 (and (not (free new-exp
'$inf
))
1923 (not (free new-exp
'$minf
)))))
1925 (t (let ((sign-minf-coef ($asksign minf-coef
))
1926 (sign-inf-coef ($asksign inf-coef
)))
1927 (cond ((or (and (eq sign-inf-coef
'$zero
)
1928 (eq sign-minf-coef
'$neg
))
1929 (and (eq sign-inf-coef
'$pos
)
1930 (eq sign-minf-coef
'$zero
))
1931 (and (eq sign-inf-coef
'$pos
)
1932 (eq sign-minf-coef
'$neg
))) '$inf
)
1933 ((or (and (eq sign-inf-coef
'$zero
)
1934 (eq sign-minf-coef
'$pos
))
1935 (and (eq sign-inf-coef
'$neg
)
1936 (eq sign-minf-coef
'$zero
))
1937 (and (eq sign-inf-coef
'$neg
)
1938 (eq sign-minf-coef
'$pos
))) '$minf
)
1939 ((or (and (eq sign-inf-coef
'$pos
)
1940 (eq sign-minf-coef
'$pos
))
1941 (and (eq sign-inf-coef
'$neg
)
1942 (eq sign-minf-coef
'$neg
))) '$und
)))))))
1944 (defun infsimp2 (x e
)
1946 (if (isinop x
'%limit
) e x
))
1948 (defun simpderiv (x y z
)
1950 (cond ((not (even (length x
)))
1951 (cond ((and (cdr x
) (null (cdddr x
))) (nconc x
'(1)))
1952 (t (wna-err '%derivative
)))))
1953 (setq w
(cons '(%derivative
) (simpmap (cdr x
) z
)))
1955 (do ((u (cddr w
) (cddr u
))) ((null u
))
1956 (cond ((mnump (car u
))
1957 (merror (intl:gettext
"diff: variable must not be a number; found: ~M") (car u
)))))
1958 (cond ((or (zerop1 y
)
1959 (and (or (mnump y
) (and (atom y
) (constant y
)))
1961 (and (not (alike1 y
(caddr w
)))
1962 (do ((u (cddr w
) (cddr u
))) ((null u
))
1963 (cond ((and (numberp (cadr u
))
1964 (not (zerop (cadr u
))))
1967 ((and (not (atom y
)) (eq (caar y
) '%derivative
) derivsimp
)
1968 (rplacd w
(append (cdr y
) (cddr w
)))))
1970 (return (if (null derivflag
) (list '(%del simp
) y
) (deriv (cdr w
)))))
1973 (cond ((null u
) (go next
))
1974 ((zerop1 (caddr u
)) (rplacd u
(cdddr u
)))
1975 (t (setq u
(cddr u
))))
1978 (cond ((null (cddr w
)) (return y
))
1979 ((and (null (cddddr w
))
1981 (alike1 (cadr w
) (caddr w
)))
1986 (cond ((null (cddr z
)) (go loop
))
1987 ((alike1 (car z
) (caddr z
))
1988 (rplaca (cdddr z
) (add2 (cadr z
) (cadddr z
)))
1989 (rplacd z
(cdddr z
)))
1990 ((great (car z
) (caddr z
))
1991 (let ((u1 (car z
)) (u2 (cadr z
)) (v1 (caddr z
)) (v2 (cadddr z
)))
1994 (rplacd z
(cons v2
(cons u1
(cons u2
(cddddr z
))))))))
1995 (cond ((setq z
(cddr z
)) (go sort
)))
1997 (cond ((null flag
) (return (cond ((null derivflag
) (eqtest w x
))
1998 (t (deriv (cdr w
)))))))
2004 (setq x
(num1 x
)) (cond ((plusp x
) 1) ((minusp x
) -
1) (t 0)))
2006 ((mplusp x
) (if expandp
1 (signum1 (car (last x
)))))
2007 ((mtimesp x
) (if (mplusp (cadr x
)) 1 (signum1 (cadr x
))))
2010 (defprop %signum
(mlist $matrix mequal
) distribute_over
)
2012 (defun simpsignum (e y z
)
2013 (declare (ignore y
))
2015 (let ((x (simpcheck (second e
) z
)) (sgn))
2017 (cond ((complex-number-p x
#'mnump
)
2018 (if (complex-number-p x
#'$ratnump
) ;; nonfloat complex
2019 (if (zerop1 x
) 0 ($rectform
(div x
($cabs x
))))
2020 (maxima::to
(bigfloat::signum
(bigfloat::to x
)))))
2022 ;; idempotent: signum(signum(z)) = signum(z).
2023 ((and (consp x
) (consp (car x
)) (eq '%signum
(mop x
))) x
)
2026 (setq sgn
($csign x
))
2027 (cond ((eq sgn
'$neg
) -
1)
2031 ;; multiplicative: signum(ab) = signum(a) * signum(b).
2033 (muln (mapcar #'(lambda (s) (take '(%signum
) s
)) (margs x
)) t
))
2035 ;; Reflection rule: signum(-x) --> -signum(x).
2036 ((great (neg x
) x
) (neg (take '(%signum
) (neg x
))))
2039 (t (eqtest (list '(%signum
) x
) e
)))))))
2041 (defun exptrl (r1 r2
)
2042 (cond ((equal r2
1) r1
)
2044 (cond ((mnump r1
) (addk 0.0 r1
))
2045 ;; Do not simplify the type of the number away.
2046 (t (list '(mexpt simp
) r1
1.0))))
2047 ((equal r2 bigfloatone
)
2048 (cond ((mnump r1
) ($bfloat r1
))
2049 ;; Do not simplify the type of the number away.
2050 (t (list '(mexpt simp
) r1 bigfloatone
))))
2052 (cond ((or (zerop1 r2
) (mnegp r2
))
2054 (merror (intl:gettext
"expt: undefined: ~M") (list '(mexpt) r1 r2
))
2055 (throw 'errorsw t
)))
2056 (t (zerores r1 r2
))))
2057 ((or (zerop1 r2
) (onep1 r1
))
2058 (cond ((or ($bfloatp r1
) ($bfloatp r2
)) bigfloatone
)
2059 ((or (floatp r1
) (floatp r2
)) 1.0)
2061 ((or ($bfloatp r1
) ($bfloatp r2
)) ($bfloat
(list '(mexpt) r1 r2
)))
2062 ((and (numberp r1
) (integerp r2
)) (exptb r1 r2
))
2063 ((and (numberp r1
) (floatp r2
) (equal r2
(float (floor r2
))))
2064 (exptb (float r1
) (floor r2
)))
2065 ((or $numer
(and (floatp r2
) (or (plusp (num1 r1
)) $numer_pbranch
)))
2066 (let (y #+kcl
(r1 r1
) #+kcl
(r2 r2
))
2067 (cond ((minusp (setq r1
(addk 0.0 r1
)))
2068 (cond ((or $numer_pbranch
(eq $domain
'$complex
))
2070 ;; R1^R2 = (-R1)^R2*cos(pi*R2) + i*(-R1)^R2*sin(pi*R2)
2071 (setq r2
(addk 0.0 r2
))
2072 (setq y
(exptrl (- r1
) r2
) r2
(* %pi-val r2
))
2073 (add2 (* y
(cos r2
))
2074 (list '(mtimes simp
) (* y
(sin r2
)) '$%i
)))
2075 (t (setq y
(let ($numer $float $keepfloat $ratprint
)
2077 (mul2 y
(exptrl (- r1
) r2
)))))
2078 ((equal (setq r2
(addk 0.0 r2
)) (float (floor r2
)))
2079 (exptb r1
(floor r2
)))
2080 ((and (equal (setq y
(* 2.0 r2
)) (float (floor y
)))
2081 (not (equal r1 %e-val
)))
2082 (exptb (sqrt r1
) (floor y
)))
2083 (t (exp (* r2
(log r1
)))))))
2084 ((floatp r2
) (list '(mexpt simp
) r1 r2
))
2087 (exptrl (cond ((equal (abs (cadr r1
)) 1)
2088 (* (cadr r1
) (caddr r1
)))
2089 ;; We set the simp flag at this place. This
2090 ;; changes nothing for an exponent r2 # -1.
2091 ;; exptrl is called again and does not look at
2092 ;; the simp flag. For the case r2 = -1 exptrl
2093 ;; is called with an exponent 1. For this case
2094 ;; the base is immediately returned. Now the
2095 ;; base has the correct simp flag. (DK 02/2010)
2097 (list '(rat simp
) (- (caddr r1
)) (- (cadr r1
))))
2098 (t (list '(rat simp
) (caddr r1
) (cadr r1
))))
2100 (t (list '(rat simp
) (exptb (cadr r1
) r2
) (exptb (caddr r1
) r2
)))))
2101 ((and (floatp r1
) (alike1 r2
'((rat) 1 2)))
2103 (list '(mtimes simp
) (sqrt (- r1
)) '$%i
)
2105 ((and (floatp r1
) (alike1 r2
'((rat) -
1 2)))
2107 (list '(mtimes simp
) (/ -
1.0 (sqrt (- r1
))) '$%i
)
2111 (exptrl r1
(fpcofrat r2
))
2112 (mul2 (exptrl -
1 r2
) ;; (-4.5)^(1/4) -> (4.5)^(1/4) * (-1)^(1/4)
2113 (exptrl (- r1
) r2
))))
2114 (exptrlsw (list '(mexpt simp
) r1 r2
))
2117 (simptimes (list '(mtimes)
2118 (exptrl r1
(truncate (cadr r2
) (caddr r2
)))
2119 (let ((y (let ($keepfloat $ratprint
)
2120 (simpnrt r1
(caddr r2
))))
2121 (z (rem (cadr r2
) (caddr r2
))))
2123 (list (car y
) (cadr y
) (mul2 (caddr y
) z
))
2127 (defun simpexpt (x y z
)
2128 (prog (gr pot check res
*rulesw
* w mlpgr mlppot
)
2130 (cond (z (setq gr
(cadr x
) pot
(caddr x
)) (go cont
)))
2132 (setq gr
(simplifya (cadr x
) nil
))
2134 (let (($%enumer $numer
))
2135 ;; Switch $%enumer on, when $numer is TRUE to allow
2136 ;; simplification of $%e to its numerical value.
2137 (simplifya (if $ratsimpexpons
($ratsimp
(caddr x
)) (caddr x
))
2141 (setq pot
(ratdisrep pot
))
2144 (cond ((member 'trunc
(car gr
))
2145 (return (srf (list '(mexpt) gr pot
))))
2147 (let ((varlist (caddar gr
)) (genvar (cadddr (car gr
))))
2148 (return (ratrep* (list '(mexpt) gr pot
)))))
2150 (setq gr
(ratdisrep gr
))
2152 ((or (setq mlpgr
(mxorlistp gr
))
2153 (setq mlppot
(mxorlistp pot
)))
2155 ((onep1 pot
) (go atgr
))
2156 ((or (zerop1 pot
) (onep1 gr
)) (go retno
))
2158 ;; This code tries to handle 0^a more complete.
2159 ;; If the sign of realpart(a) is not known return an unsimplified
2160 ;; expression. The handling of the flag *zexptsimp? is not changed.
2161 ;; Reverting the return of an unsimplified 0^a, because timesin
2162 ;; can not handle such expressions. (DK 02/2010)
2164 (cond ((or (member (setq z
($csign pot
)) '($neg $nz
))
2165 (and *zexptsimp?
(eq ($asksign pot
) '$neg
)))
2166 ;; A negative exponent. Maxima error.
2167 (cond ((not errorsw
) (merror (intl:gettext
"expt: undefined: 0 to a negative exponent.")))
2168 (t (throw 'errorsw t
))))
2169 ((and (member z
'($complex $imaginary
))
2170 ;; A complex exponent. Look at the sign of the realpart.
2171 (member (setq z
($sign
($realpart pot
)))
2173 (cond ((not errorsw
)
2174 (merror (intl:gettext
"expt: undefined: 0 to a complex exponent.")))
2175 (t (throw 'errorsw t
))))
2176 ((and *zexptsimp?
(eq ($asksign pot
) '$zero
))
2177 (cond ((not errorsw
)
2178 (merror (intl:gettext
"expt: undefined: 0^0")))
2179 (t (throw 'errorsw t
))))
2180 ((not (member z
'($pos $pz
)))
2181 ;; The sign of realpart(pot) is not known. We can not return
2182 ;; an unsimplified 0^a expression, because timesin can not
2183 ;; handle it. We return ZERO. That is the old behavior.
2184 ;; Look for the imaginary symbol to be consistent with
2186 (cond ((not (free pot
'$%i
))
2187 (cond ((not errorsw
)
2188 (merror (intl:gettext
"expt: undefined: 0 to a complex exponent.")))
2189 (t (throw 'errorsw t
))))
2191 ;; Return ZERO and not an unsimplified expression.
2192 (return (zerores gr pot
)))))
2193 (t (return (zerores gr pot
)))))
2197 (or (not (ratnump gr
)) (not (ratnump pot
))))
2198 (return (eqtest (exptrl gr pot
) check
)))
2199 ;; Check for numerical evaluation of the sqrt.
2200 ((and (alike1 pot
'((rat) 1 2))
2201 (or (setq res
(flonum-eval '%sqrt gr
))
2202 (and (not (member 'simp
(car x
)))
2203 (setq res
(big-float-eval '%sqrt gr
)))))
2206 (return (%itopot pot
)))
2207 ((and (realp gr
) (minusp gr
) (mevenp pot
))
2210 ((and (realp gr
) (minusp gr
) (moddp pot
))
2211 (return (mul2 -
1 (power (- gr
) pot
))))
2212 ((and (equal gr -
1) (maxima-integerp pot
) (mminusp pot
))
2213 (setq pot
(neg pot
))
2216 (maxima-integerp pot
)
2219 (integerp (cadr pot
))
2221 (maxima-integerp (caddr pot
)))
2222 (setq pot
(caddr pot
))
2224 ((atom gr
) (go atgr
))
2225 ((and (eq (caar gr
) 'mabs
)
2228 (or (and (eq $domain
'$real
) (not (apparently-complex-to-judge-by-$csign-p
(cadr gr
))))
2229 (and (eq $domain
'$complex
) (apparently-real-to-judge-by-$csign-p
(cadr gr
)))))
2230 (return (power (cadr gr
) pot
)))
2231 ((and (eq (caar gr
) 'mabs
)
2234 (not (equal pot -
1))
2235 (or (and (eq $domain
'$real
) (not (apparently-complex-to-judge-by-$csign-p
(cadr gr
))))
2236 (and (eq $domain
'$complex
) (apparently-real-to-judge-by-$csign-p
(cadr gr
)))))
2237 ;; abs(x)^(2*n+1) -> abs(x)*x^(2*n), n an integer number
2239 (return (mul (power (cadr gr
) (add pot -
1))
2241 (return (mul (power (cadr gr
) (add pot
1))
2243 ((eq (caar gr
) 'mequal
)
2244 (return (eqtest (list (ncons (caar gr
))
2245 (power (cadr gr
) pot
)
2246 (power (caddr gr
) pot
))
2248 ((symbolp pot
) (go opp
))
2249 ((eq (caar gr
) 'mexpt
) (go e1
))
2250 ((and (eq (caar gr
) '%sum
)
2255 (return (do ((i (1- pot
) (1- i
))
2256 (an gr
(simptimes (list '(mtimes) an gr
) 1 t
)))
2259 (return (eqtest (testt (tms gr pot nil
)) check
)))
2261 (return (eqtest (cond ((and (mplusp gr
)
2262 (not (or (> pot $expop
)
2263 (> (- pot
) $expon
))))
2264 (expandexpt gr pot
))
2265 (t (simplifya (tms gr pot nil
) t
)))
2269 (cond ((eq (caar gr
) 'mexpt
) (go e1
))
2270 ((eq (caar gr
) 'rat
)
2271 (return (mul2 (power (cadr gr
) pot
)
2272 (power (caddr gr
) (mul2 -
1 pot
)))))
2273 ((not (eq (caar gr
) 'mtimes
)) (go up
))
2274 ((or (eq $radexpand
'$all
) (and $radexpand
(simplexpon pot
)))
2277 ((and (or (not (numberp (cadr gr
)))
2278 (equal (cadr gr
) -
1))
2279 (equal -
1 ($num gr
)) ; only for -1
2280 ;; Do not simplify for a complex base.
2281 (not (member ($csign gr
) '($complex $imaginary
)))
2282 (and (eq $domain
'$real
) $radexpand
))
2283 ;; (-1/x)^a -> 1/(-x)^a for x negative
2284 ;; For all other cases (-1)^a/x^a
2285 (if (eq ($csign
(setq w
($denom gr
))) '$neg
)
2286 (return (inv (power (neg w
) pot
)))
2287 (return (div (power -
1 pot
)
2289 ((or (eq $domain
'$complex
) (not $radexpand
)) (go up
)))
2290 (return (do ((l (cdr gr
) (cdr l
)) (res (ncons 1)) (rad))
2292 (cond ((equal res
'(1))
2293 (eqtest (list '(mexpt) gr pot
) check
))
2295 (testt (cons '(mtimes simp
) res
)))
2297 (setq rad
(power* ; RADEXPAND=()?
2298 (cons '(mtimes) (nreverse rad
)) pot
))
2299 (cond ((not (onep1 rad
))
2301 (testt (tms rad
1 (cons '(mtimes) res
))))
2303 (setq *rulesw
* nil res
(cdr rad
))))))
2304 (eqtest (testt (cons '(mtimes) res
)) check
))))
2305 ;; Check with $csign to be more complete. This prevents wrong
2306 ;; simplifications like sqrt(-z^2)->%i*sqrt(z^2) for z complex.
2307 (setq z
($csign
(car l
)))
2308 (if (member z
'($complex $imaginary
))
2309 (setq z
'$pnz
)) ; if appears complex, unknown sign
2310 (setq w
(cond ((member z
'($neg $nz
))
2311 (setq rad
(cons -
1 rad
))
2315 ((alike1 w gr
) (return (list '(mexpt simp
) gr pot
)))
2316 ((member z
'($pn $pnz
))
2317 (setq rad
(cons w rad
)))
2319 (setq w
(testt (tms (simplifya (list '(mexpt) w pot
) t
)
2320 1 (cons '(mtimes) res
))))))
2321 (cond (*rulesw
* (setq *rulesw
* nil res
(cdr w
))))))
2324 (cond ((and (cdr res
) (onep1 (car res
)) (ratnump (cadr res
)))
2325 (setq res
(cdr res
))))
2326 (cond ((null (setq gr
(cdr gr
)))
2327 (return (eqtest (testt (cons '(mtimes) res
)) check
)))
2329 (setq y
(power (cadar gr
) (mult (caddar gr
) pot
))))
2331 (setq y
(%itopot pot
)))
2333 (setq y
(list '(mexpt) (car gr
) pot
)))
2334 (t (setq y
(list '(mexpt simp
) (car gr
) pot
))))
2335 (setq w
(testt (tms (simplifya y t
) 1 (cons '(mtimes) res
))))
2336 (cond (*rulesw
* (setq *rulesw
* nil res
(cdr w
))))
2340 (return (exptrl gr pot
))
2343 (cond ((zerop1 pot
) (go retno
))
2345 (let ((y (mget gr
'$numer
)))
2346 (if (and y
(floatp y
) (or $numer
(not (equal pot
1))))
2347 ;; A numeric constant like %e, %pi, ... and
2348 ;; exponent is a float or bigfloat value.
2349 (return (if (and (member gr
*builtin-numeric-constants
*)
2350 (equal pot bigfloatone
))
2351 ;; Return a bigfloat value.
2353 ;; Return a float value.
2355 ;; In all other cases exptrl simplifies accordingly.
2356 (return (exptrl gr pot
)))))
2358 ;; Numerically evaluate if the power is a flonum.
2360 (let ((val (flonum-eval '%exp pot
)))
2361 (if (float-inf-p val
)
2362 ;; needed for gcl and sbcl - (sometimes) no trap of overflow
2363 (error 'floating-point-overflow
))
2366 ;; Numerically evaluate if the power is a (complex)
2367 ;; big-float. (This is basically the guts of
2368 ;; big-float-eval, but we can't use big-float-eval.)
2369 (when (and (not (member 'simp
(car x
)))
2370 (complex-number-p pot
'bigfloat-or-number-p
))
2371 (let ((x ($realpart pot
))
2372 (y ($imagpart pot
)))
2373 (cond ((and ($bfloatp x
) (like 0 y
))
2374 (return ($bfloat
`((mexpt simp
) $%e
,pot
))))
2375 ((or ($bfloatp x
) ($bfloatp y
))
2376 (let ((z (add ($bfloat x
) (mul '$%i
($bfloat y
)))))
2377 (setq z
($rectform
`((mexpt simp
) $%e
,z
)))
2378 (return ($bfloat z
))))))))
2379 (cond ((and $logsimp
(among '%log pot
)) (return (%etolog pot
)))
2380 ((and $demoivre
(setq z
(demoivre pot
))) (return z
))
2384 ;; Exponent contains %i and %pi and %emode is TRUE:
2385 ;; Check simplification of exp(%i*%pi*p/q*x)
2386 (setq z
(%especial pot
)))
2388 (($taylorp
(third x
))
2389 ;; taylorize %e^taylor(...)
2390 (return ($taylor x
)))))
2392 (let ((y (mget gr
'$numer
)))
2393 ;; Check for a numeric constant.
2397 ;; The exponent is a bigfloat. Convert base to bigfloat.
2399 (member gr
*builtin-numeric-constants
*)
2400 (setq y
($bfloat gr
)))
2401 (and $numer
(integerp pot
)))
2402 (return (exptrl y pot
))))))
2405 (return (eqtest (list '(mexpt) gr pot
) check
))
2409 (cond ((mxorlistp1 gr
) (return (constmx (addk 1 pot
) gr
)))
2411 ((onep1 pot
) (return gr
))
2412 ((or $doallmxops $doscmxops $domxexpt
)
2413 (cond ((or (and mlpgr
2414 (or (not ($listp gr
)) $listarith
)
2415 (scalar-or-constant-p pot $assumescalar
))
2418 (or (not ($listp pot
)) $listarith
)
2419 (scalar-or-constant-p gr $assumescalar
)))
2420 (return (simplifya (outermap1 'mexpt gr pot
) t
)))
2422 ((and $domxmxops
(member pot
'(-1 -
1.0)))
2423 (return (simplifya (outermap1 'mexpt gr pot
) t
)))
2426 ;; At this point we have an expression: (z^a)^b with gr = z^a and pot = b
2427 (cond ((or (eq $radexpand
'$all
)
2428 ;; b is an integer or an odd rational
2430 (and (eq $domain
'$complex
)
2431 (not (member ($csign
(caddr gr
)) '($complex $imaginary
)))
2432 ;; z >= 0 and a not a complex
2433 (or (member ($csign
(cadr gr
)) '($pos $pz $zero
))
2435 (and (mnump (caddr gr
))
2436 (eq ($sign
(sub 1 (take '(mabs) (caddr gr
))))
2438 (and (eq $domain
'$real
)
2439 (member ($csign
(cadr gr
)) '($pos $pz $zero
)))
2440 ;; (1/z)^a -> 1/z^a when z a constant complex
2441 (and (eql (caddr gr
) -
1)
2443 (eq $domain
'$real
))
2444 (and (eq ($csign
(cadr gr
)) '$complex
)
2445 ($constantp
(cadr gr
)))))
2446 ;; This does (1/z)^a -> 1/z^a. This is in general wrong.
2447 ;; We switch this type of simplification on, when
2448 ;; $ratsimpexpons is T. E.g. radcan sets this flag to T.
2449 ;; radcan hangs for expressions like sqrt(1/(1+x)) without
2450 ;; this simplification.
2452 (equal (caddr gr
) -
1))
2455 (odnump (caddr gr
))))
2456 ;; Simplify (z^a)^b -> z^(a*b)
2457 (setq pot
(mul pot
(caddr gr
))
2459 ((and (eq $domain
'$real
)
2462 (not (apparently-complex-to-judge-by-$csign-p
(cadr gr
)))
2463 (evnump (caddr gr
)))
2464 ;; Simplify (x^a)^b -> abs(x)^(a*b)
2465 (setq pot
(mul pot
(caddr gr
))
2466 gr
(radmabs (cadr gr
))))
2469 (mminusp (caddr gr
)))
2470 ;; Simplify (1/z^a)^b -> 1/(z^a)^b
2472 gr
(power (cadr gr
) (neg (caddr gr
)))))
2476 (defun apparently-complex-to-judge-by-$csign-p
(e)
2477 (let ((s ($csign e
)))
2478 (member s
'($complex $imaginary
))))
2480 (defun apparently-real-to-judge-by-$csign-p
(e)
2481 (let ((s ($csign e
)))
2482 (member s
'($pos $neg $zero $pn $pnz $pz $nz
))))
2484 ;; Basically computes log of m base b. Except if m is not a power
2485 ;; of b, we return nil. m is a positive integer and base an integer
2486 ;; not equal to +/-1.
2487 (defun exponent-of (m base
)
2488 ;; Just compute base^k until base^k >= m. Then check if they're equal.
2489 ;; If so, we have the exponent. Otherwise, give up.
2492 (multiple-value-bind (q r
)
2498 (if (zerop expo
) nil expo
)))
2500 (defun timesin (x y w
) ; Multiply X^W into Y
2501 (prog (fm temp z check u expo
)
2502 (if (mexptp x
) (setq check x
))
2504 ;; Prepare the factor x^w and initialize the work of timesin
2508 (setq temp
(cons '(mexpt) (if check
2509 (list (cadr x
) (mult (caddr x
) w
))
2511 (if (and (not timesinp
) (not (eq x
'$%i
)))
2513 (setq temp
(simplifya temp t
))))))
2514 (setq x
(if (mexptp temp
)
2520 ;; Go through the list of terms in fm and look what is to do.
2521 (cond ((null (cdr fm
))
2522 ;; The list of terms is empty. The loop is finshed.
2524 ((or (and (mnump temp
)
2525 (not (or (integerp temp
)
2527 (and (integerp temp
)
2529 ;; Stop the loop for a float or bigfloat number, or number -1.
2532 (cond ((alike1 (car x
) (cadadr fm
))
2533 (cond ((zerop1 (setq w
(plsk (caddr (cadr fm
)) w
)))
2538 (rplacd fm
(cddr fm
))
2539 (cond ((mnump (setq x
(if (mnump (car x
))
2541 (power (car x
) w
))))
2542 (return (rplaca y
(timesk (car y
) x
))))
2547 x
(if (mexptp x
) (cdr x
) (list x
1)))
2551 ((maxima-constantp (car x
))
2554 (cond ((mtimesp (car x
))
2555 ;; A base which is a mtimes expression. Remove
2556 ;; the factor from the lists of products.
2557 (rplacd fm
(cddr fm
))
2558 ;; Multiply the factors of the base with
2559 ;; the list of all remaining products.
2561 (return (muln (nconc y
(cdar x
)) t
)))
2562 (t (return (rplaca (cdr fm
) (car x
))))))
2565 ;; At this place we have to add code for a rational number
2566 ;; as a factor to the list of products.
2568 (or (ratnump (car x
))
2569 (and (integerp (car x
))
2570 (not (onep (car x
))))))
2571 ;; Multiplying bas^k * num/den
2572 (let ((num (num1 (car x
)))
2573 (den (denom1 (car x
)))
2574 (bas (second (cadr fm
))))
2575 (cond ((and (integerp bas
)
2576 (not (eql 1 (abs bas
)))
2577 (setq expo
(exponent-of (abs num
) bas
)))
2578 ;; We have bas^m*bas^k = bas^(k+m).
2579 (setq temp
(power bas
2580 (add (third (cadr fm
)) expo
)))
2581 ;; Set fm to have 1/denom term.
2582 (setq x
(mul (car y
)
2586 ((and (integerp bas
)
2587 (not (eql 1 (abs bas
)))
2588 (setq expo
(exponent-of den bas
)))
2589 (setq expo
(- expo
))
2590 ;; We have bas^(-m)*bas^k = bas^(k-m).
2591 (setq temp
(power bas
2592 (add (third (cadr fm
)) expo
)))
2593 ;; Set fm to have the numerator term.
2594 (setq x
(mul (car y
)
2597 (exptrl bas
(- expo
)))))))
2599 ;; Next term in list of products.
2602 ;; Add in the bas^(k+m) term or bas^(k-m)
2603 (setf y
(rplaca y
1))
2604 (rplacd fm
(cddr fm
))
2605 (rplacd fm
(cons temp
(cdr fm
)))
2611 ((and (not (atom (car x
)))
2612 (eq (caar (car x
)) 'mabs
)
2614 (integerp (caddr (cadr fm
)))
2615 (< (caddr (cadr fm
)) -
1)
2616 (alike1 (cadr (car x
)) (cadr (cadr fm
)))
2617 (not (member ($csign
(cadr (car x
)))
2618 '($complex imaginary
))))
2619 ;; 1/x^n*abs(x) -> 1/(x^(n-2)*abs(x)), where n an integer
2620 ;; Replace 1/x^n -> 1/x^(n-2)
2621 (setq temp
(power (cadr (cadr fm
))
2622 (add (caddr (cadr fm
)) 2)))
2623 (rplacd fm
(cddr fm
))
2624 (if (not (equal temp
1))
2625 (rplacd fm
(cons temp
(cdr fm
))))
2626 ;; Multiply factor 1/abs(x) into list of products.
2627 (setq x
(list (car x
) -
1))
2628 (setq temp
(power (car x
) (cadr x
)))
2632 ((and (not (atom (car x
)))
2633 (eq (caar (car x
)) 'mabs
)
2635 (integerp (caddr (cadr fm
)))
2636 (> (caddr (cadr fm
)) 1)
2637 (alike1 (cadr (car x
)) (cadr (cadr fm
)))
2638 (not (member ($csign
(cadr (car x
)))
2639 '($complex imaginary
))))
2640 ;; x^n/abs(x) -> x^(n-2)*abs(x), where n an integer.
2641 ;; Replace x^n -> x^(n-2)
2642 (setq temp
(power (cadr (cadr fm
))
2643 (add (caddr (cadr fm
)) -
2)))
2644 (rplacd fm
(cddr fm
))
2645 (if (not (equal temp
1))
2646 (rplacd fm
(cons temp
(cdr fm
))))
2647 ;; Multiply factor abs(x) into list of products.
2648 (setq x
(list (car x
) 1))
2649 (setq temp
(power (car x
) (cadr x
)))
2653 ((and (not (atom (cadr fm
)))
2654 (not (atom (cadr (cadr fm
))))
2655 (eq (caaadr (cadr fm
)) 'mabs
)
2656 (equal (caddr (cadr fm
)) -
1)
2659 (alike1 (cadadr (cadr fm
)) (car x
))
2660 (not (member ($csign
(cadadr (cadr fm
)))
2661 '($complex imaginary
))))
2662 ;; 1/abs(x)*x^n -> x^(n-2)*abs(x), where n an integer.
2663 ;; Replace 1/abs(x) -> abs(x)
2664 (setq temp
(cadr (cadr fm
)))
2665 (rplacd fm
(cddr fm
))
2666 (rplacd fm
(cons temp
(cdr fm
)))
2667 ;; Multiply factor x^(n-2) into list of products.
2668 (setq x
(list (car x
) (add (cadr x
) -
2)))
2669 (setq temp
(power (car x
) (cadr x
)))
2673 ((or (maxima-constantp (car x
))
2674 (maxima-constantp (cadadr fm
)))
2675 (if (great temp
(cadr fm
))
2677 ((great (car x
) (cadadr fm
))
2680 ((alike1 (car x
) (cadr fm
))
2683 ;; When a number goto start and look in the next term.
2687 ((and (not (atom (cadr fm
)))
2688 (eq (caar (cadr fm
)) 'mabs
)
2691 (alike1 (cadr (cadr fm
)) (car x
))
2692 (not (member ($csign
(cadr (cadr fm
)))
2693 '($complex imaginary
))))
2694 ;; abs(x)/x^n -> 1/(x^(n-2)*abs(x)), where n an integer.
2695 ;; Replace abs(x) -> 1/abs(x).
2696 (setq temp
(power (cadr fm
) -
1))
2697 (rplacd fm
(cddr fm
))
2698 (rplacd fm
(cons temp
(cdr fm
)))
2699 ;; Multiply factor x^(-n+2) into list of products.
2700 (setq x
(list (car x
) (add (cadr x
) 2)))
2701 (setq temp
(power (car x
) (cadr x
)))
2705 ((maxima-constantp (car x
))
2706 (when (great temp
(cadr fm
))
2708 ((great (car x
) (cadr fm
))
2712 ;; Multiply a number into the list of products.
2713 (return (rplaca y
(timesk (car y
) temp
))))
2714 ((and (eq (car x
) '$%i
)
2717 ((and (eq (car x
) '$%e
)
2720 (return (rplaca y
(timesk (car y
) (exp (float w
))))))
2722 (not (constant (car x
))))
2724 ;; At this point we will insert a mexpt expression,
2725 ;; but first we look at the car of the list of products and
2726 ;; modify the expression if we found a rational number.
2728 (not (onep1 (car y
)))
2729 (or (integerp (car y
))
2731 ;; Multiplying bas^k * num/den.
2732 (let ((num (num1 (car y
)))
2733 (den (denom1 (car y
)))
2735 (cond ((and (integerp bas
)
2736 (not (eql 1 (abs bas
)))
2737 (setq expo
(exponent-of (abs num
) bas
)))
2738 ;; We have bas^m*bas^k.
2739 (setq temp
(power bas
(add (cadr x
) expo
)))
2740 ;; Set fm to have 1/denom term.
2741 (setq x
(div (div num
(exptrl bas expo
)) den
)))
2742 ((and (integerp bas
)
2743 (not (eql 1 (abs bas
)))
2744 (setq expo
(exponent-of den bas
)))
2745 (setq expo
(- expo
))
2746 ;; We have bas^(-m)*bas^k.
2747 (setq temp
(power bas
(add (cadr x
) expo
)))
2748 ;; Set fm to have the numerator term.
2749 (setq x
(div num
(div den
(exptrl bas
(- expo
))))))
2751 ;; The rational doesn't contain any (simple) powers of
2752 ;; the exponential term. We're done.
2753 (return (cdr (rplacd fm
(cons temp
(cdr fm
)))))))
2754 ;; Add in the a^(m+k) or a^(k-m) term.
2755 (setf y
(rplaca y
1))
2756 (rplacd fm
(cons temp
(cdr fm
)))
2762 ((and (maxima-constantp (car x
))
2763 (do ((l (cdr fm
) (cdr l
)))
2765 (when (and (mexptp (cadr l
))
2766 (alike1 (car x
) (cadadr l
)))
2770 ((or (and (mnump (car x
))
2772 (and (eq (car x
) '$%e
)
2776 (setq u
(%especial w
))))
2778 ((alike (cdr check
) x
)
2781 (exptrl (car x
) w
))))
2783 (return (rplaca y
(timesk (car y
) x
))))
2787 (return (cdr (rplacd fm
(cons x
(cdr fm
))))))
2797 (setq temp
(list '(mexpt) (car x
) w
))
2798 (setq temp
(eqtest temp
(or check
'((foo)))))
2799 (return (cdr (rplacd fm
(cons temp
(cdr fm
)))))))
2801 (return (cdr (rplacd fm
(cons (car x
) (cdr fm
)))))
2806 (cond ((and (eq (car x
) '$%i
) (equal w
1))
2807 (rplacd fm
(cddr fm
))
2808 (return (rplaca y
(timesk -
1 (car y
)))))
2809 ((zerop1 (setq w
(plsk 1 w
)))
2811 ((and (mnump (car x
)) (mnump w
))
2812 (return (rplaca (cdr fm
) (exptrl (car x
) w
))))
2813 ((maxima-constantp (car x
))
2816 (setq z
(list '(mexpt) (car x
) w
))
2817 (cond ((alike1 (setq x
(simplifya z t
)) z
)
2818 (return (rplaca (cdr fm
) x
)))
2820 (rplacd fm
(cddr fm
))
2822 (return (muln (cons x y
) t
))))
2824 (rplacd fm
(cddr fm
))
2825 (setq x
(car x
) check nil
)
2828 (setq z
(tms x
1 (setq temp
(cons '(mtimes) y
))))
2829 (return (cond ((eq z temp
)
2832 (setq *rulesw
* t
) z
)))
2834 (return (rplacd fm
(cddr fm
)))
2836 (if (minusp (setq w
(rem w
4)))
2838 (return (cond ((zerop w
)
2841 (rplaca y
(timesk -
1 (car y
))))
2843 (rplaca y
(timesk -
1 (car y
)))
2844 (rplacd fm
(cons '$%i
(cdr fm
))))
2846 (rplacd fm
(cons '$%i
(cdr fm
))))))))
2848 (defun simpmatrix (x vestigial z
)
2849 (declare (ignore vestigial
))
2850 (if (and (null (cddr x
))
2852 (or (eq $scalarmatrixp
'$all
) (member 'mult
(cdar x
)))
2853 ($listp
(cadr x
)) (cdadr x
) (null (cddadr x
)))
2854 (simplifya (cadadr x
) z
)
2855 (let ((badp (dolist (row (cdr x
)) (if (not ($listp row
)) (return t
))))
2856 (args (simpmap (cdr x
) z
)))
2857 (if (and args
(not badp
)) (matcheck args
))
2858 (cons (if badp
'(%matrix simp
) '($matrix simp
)) args
))))
2860 (defun %itopot
(pot)
2862 (let ((i (boole boole-and pot
3)))
2866 (t (list '(mtimes simp
) -
1 '$%i
))))
2867 (power -
1 (mul2 pot
'((rat simp
) 1 2)))))
2870 (cond ((eq (caar pot
) '%log
) (simplifya (cadr pot
) nil
))
2871 ((and (eq (caar pot
) 'mtimes
)
2872 (or (maxima-integerp (cadr pot
))
2873 (and $%e_to_numlog
($numberp
(cadr pot
))))
2874 (not (atom (caddr pot
))) (eq (caar (caddr pot
)) '%log
)
2876 (power (cadr (caddr pot
)) (cadr pot
)))))
2880 loop
(cond ((null pot
)
2881 (cond (a (setq a
(cons '(mtimes) a
))))
2882 (cond (c (setq c
(list '(mexpt simp
) '$%e
(addn c nil
)))))
2883 (return (cond ((null c
) (simptimes a
1 nil
))
2885 (t (simptimes (append a
(list c
)) 1 nil
)))))
2886 ((and (among '%log
(car pot
)) (setq b
(mnlogp (car pot
))))
2887 (setq a
(cons b a
)))
2888 (t (setq c
(cons (car pot
) c
))))
2889 (setq pot
(cdr pot
))
2892 (defun %etolog
(pot) (cond ((mnlogp pot
))
2893 ((eq (caar pot
) 'mplus
) (mnlog (cdr pot
)))
2894 (t (list '(mexpt simp
) '$%e pot
))))
2896 (defun zerores (r1 r2
)
2897 (cond ((or ($bfloatp r1
) ($bfloatp r2
)) bigfloatzero
)
2898 ((or (floatp r1
) (floatp r2
)) 0.0)
2901 (defmfun $orderlessp
(a b
)
2902 (setq a
($totaldisrep
(specrepcheck a
))
2903 b
($totaldisrep
(specrepcheck b
)))
2904 (and (not (alike1 a b
)) (great b a
) t
))
2906 (defmfun $ordergreatp
(a b
)
2907 (setq a
($totaldisrep
(specrepcheck a
))
2908 b
($totaldisrep
(specrepcheck b
)))
2909 (and (not (alike1 a b
)) (great a b
) t
))
2911 ;; Test function to order a and b by magnitude. If it is not possible to
2912 ;; order a and b by magnitude they are ordered by great. This function
2913 ;; can be used by sort, e.g. sort([3,1,7,x,sin(1),minf],ordermagnitudep)
2914 (defmfun $ordermagnitudep
(a b
)
2916 (setq a
($totaldisrep
(specrepcheck a
))
2917 b
($totaldisrep
(specrepcheck b
)))
2918 (cond ((and (or (constp a
) (member a
'($inf $minf
)))
2919 (or (constp b
) (member b
'($inf $minf
)))
2920 (member (setq sgn
($csign
(sub b a
))) '($pos $neg $zero
)))
2921 (cond ((eq sgn
'$pos
) t
)
2922 ((eq sgn
'$zero
) (and (not (alike1 a b
)) (great b a
)))
2924 ((or (constp a
) (member a
'($inf $minf
))) t
)
2925 ((or (constp b
) (member b
'($inf $minf
))) nil
)
2926 (t (and (not (alike1 a b
)) (great b a
))))))
2928 (defun evnump (n) (or (even n
) (and (ratnump n
) (even (cadr n
)))))
2929 (defun odnump (n) (or (and (integerp n
) (oddp n
))
2930 (and (ratnump n
) (oddp (cadr n
)))))
2932 (defun simplexpon (e)
2933 (or (maxima-integerp e
)
2934 (and (eq $domain
'$real
) (ratnump e
) (oddp (caddr e
)))))
2936 ;; This function is not called in Maxima core or share code
2937 ;; and can be cut out.
2939 (and (free p
'$%i
) (member ($sign p
) '($pos $pz $zero
))))
2942 (if (and limitp
(free e
'$%i
)) (asksign-p-or-n e
))
2943 (simplifya (list '(mabs) e
) t
))
2945 (defun simpmqapply (exp y z
)
2946 (let ((simpfun (and (not (atom (cadr exp
))) (safe-get (caaadr exp
) 'specsimp
))) u
)
2948 (funcall simpfun exp y z
)
2949 (progn (setq u
(simpargs exp z
))
2950 (if (symbolp (cadr u
))
2951 (simplifya (cons (cons (cadr u
) (cdar u
)) (cddr u
)) z
)
2954 ;; TRUE, if the symbol e is declared to be $complex or $imaginary.
2955 (defun decl-complexp (e)
2957 (kindp e
'$complex
)))
2959 ;; TRUE, if the symbol e is declared to be $real, $rational, $irrational
2961 (defun decl-realp (e)
2963 (or (kindp e
'$real
)
2964 (kindp e
'$rational
)
2965 (kindp e
'$irrational
)
2966 (kindp e
'$integer
))))
2968 ;; WARNING: Exercise extreme caution when modifying this function!
2970 ;; Richard Fateman and Stavros Macrakis both say that changing the
2971 ;; actual ordering relations (as opposed to making them faster to
2972 ;; determine) could have very subtle and wide-ranging effects. Also,
2973 ;; the simplifier spends the vast majority of its time here, so be
2974 ;; very careful about changes that may drastically slow down the
2982 (cond ((zerop y
) (floatp x
)) (t (plusp y
))))))
2984 (cond ((constant y
) (alphalessp y x
)) (t (numberp y
))))
2986 (cond ((mget y
'$scalar
) (alphalessp y x
))
2987 (t (maxima-constantp y
))))
2989 (cond ((mget y
'$mainvar
) (alphalessp y x
)) (t t
)))
2990 (t (or (maxima-constantp y
) (mget y
'$scalar
)
2991 (and (not (mget y
'$mainvar
)) (not (null (alphalessp y x
))))))))
2992 (t (not (ordfna y x
)))))
2993 ((atom y
) (ordfna x y
))
2995 (cond ((eq (caar y
) 'rat
)
2996 (> (* (caddr y
) (cadr x
)) (* (caddr x
) (cadr y
))))))
2997 ((eq (caar y
) 'rat
))
2998 ((or (member (caar x
) '(mtimes mplus mexpt %del
))
2999 (member (caar y
) '(mtimes mplus mexpt %del
)))
3001 ((and (eq (caar x
) 'bigfloat
) (eq (caar y
) 'bigfloat
)) (mgrp x y
))
3002 ((or (eq (caar x
) 'mrat
) (eq (caar y
) 'mrat
))
3003 (error "GREAT: internal error: unexpected MRAT argument"))
3004 (t (do ((x1 (margs x
) (cdr x1
)) (y1 (margs y
) (cdr y1
))) (())
3006 (return (cond (y1 nil
)
3007 ((not (alike1 (mop x
) (mop y
)))
3008 (great (mop x
) (mop y
)))
3009 ((member 'array
(cdar x
)) t
))))
3010 ((null y1
) (return t
))
3011 ((not (alike1 (car x1
) (car y1
)))
3012 (return (great (car x1
) (car y1
)))))))))
3014 ;; Trivial function used only in ALIKE1.
3015 ;; Should be defined as an open-codable subr.
3017 (defmacro memqarr
(l)
3018 `(if (member 'array
,l
) t
))
3020 ;; Compares two Macsyma expressions ignoring SIMP flags and all other
3021 ;; items in the header except for the ARRAY flag.
3029 (and (stringp y
) (string= x y
)))
3031 (and (vectorp y
) (lisp-vector-alike1 x y
)))
3033 (and (arrayp y
) (lisp-array-alike1 x y
))))
3035 ;; NOT SURE IF WE WANT TO ENABLE COMPARISON OF MAXIMA ARRAYS
3036 ;; ASIDE FROM THAT, ADD2LNC CALLS ALIKE1 (VIA MEMALIKE) AND THAT CAUSES TROUBLE
3037 ;; ((maxima-declared-arrayp x)
3038 ;; (and (maxima-declared-arrayp y) (maxima-declared-array-alike1 x y)))
3039 ;; ((maxima-undeclared-arrayp x)
3040 ;; (and (maxima-undeclared-arrayp y) (maxima-undeclared-array-alike1 x y)))
3045 (not (atom (car x
)))
3046 (not (atom (car y
)))
3047 (eq (caar x
) (caar y
)))
3050 ;; Punt back to LIKE, which handles specreps
3053 (eq (memqarr (cdar x
)) (memqarr (cdar y
)))
3054 (alike (cdr x
) (cdr y
))))))))
3056 (defun lisp-vector-alike1 (x y
)
3058 ((length-x (if (array-has-fill-pointer-p x
) (fill-pointer x
) (length x
)))
3059 (length-y (if (array-has-fill-pointer-p y
) (fill-pointer y
) (length y
))))
3061 (= length-x length-y
)
3063 (dotimes (i length-x
)
3064 (if (not (alike1 (aref x i
) (aref y i
)))
3065 (return-from lisp-vector-alike1 nil
)))
3068 (defun lisp-array-alike1 (x y
)
3070 (equal (array-dimensions x
) (array-dimensions y
))
3072 (dotimes (i (array-total-size x
))
3073 (if (not (alike1 (row-major-aref x i
) (row-major-aref y i
)))
3074 (return-from lisp-array-alike1 nil
)))
3077 (defun maxima-declared-array-alike1 (x y
)
3078 (lisp-array-alike1 (get (mget x
'array
) 'array
) (get (mget y
'array
) 'array
)))
3080 (defun maxima-undeclared-array-alike1 (x y
)
3082 (alike1 (mfuncall '$arrayinfo x
) (mfuncall '$arrayinfo y
))
3083 (alike1 ($listarray x
) ($listarray y
))))
3085 ;; Maps ALIKE1 down two lists.
3088 (do ((x x
(cdr x
)) (y y
(cdr y
))) ((atom x
) (equal x y
))
3089 (cond ((or (atom y
) (not (alike1 (car x
) (car y
))))
3092 (defun ordfna (e a
) ; A is an atom
3094 (or (not (eq (caar e
) 'rat
))
3095 (> (cadr e
) (* (caddr e
) a
))))
3097 (not (member (caar e
) '(mplus mtimes mexpt
))))
3098 (not (member (caar e
) '(rat bigfloat
))))
3099 ((eq (caar e
) 'mrat
)) ;; all MRATs succeed all atoms
3100 ((null (margs e
)) nil
)
3101 ((eq (caar e
) 'mexpt
)
3102 (cond ((and (maxima-constantp (cadr e
))
3103 (or (not (constant a
)) (not (maxima-constantp (caddr e
)))))
3104 (or (not (free (caddr e
) a
)) (great (caddr e
) a
)))
3105 ((eq (cadr e
) a
) (great (caddr e
) 1))
3106 (t (great (cadr e
) a
))))
3107 ((member (caar e
) '(mplus mtimes
))
3108 (let ((u (car (last e
))))
3109 (cond ((eq u a
) (not (ordhack e
))) (t (great u a
)))))
3110 ((eq (caar e
) '%del
))
3111 ((prog2 (setq e
(car (margs e
))) ; use first arg of e
3112 (and (not (atom e
)) (member (caar e
) '(mplus mtimes
))))
3113 (let ((u (car (last e
)))) ; and compare using
3114 (cond ((eq u a
) (not (ordhack e
))) ; same procedure as above
3119 ;; compare lists a and b elementwise from back to front
3120 (defun ordlist (a b cx cy
)
3122 (setq l1
(length a
) l2
(length b
))
3123 loop
(cond ((= l1
0)
3124 (return (cond ((= l2
0) (eq cx
'mplus
))
3125 ((and (eq cx cy
) (= l2
1))
3126 (great (cond ((eq cx
'mplus
) 0) (t 1)) (car b
))))))
3127 ((= l2
0) (return (not (ordlist b a cy cx
)))))
3128 (setq c
(nthelem l1 a
) d
(nthelem l2 b
))
3129 (cond ((not (alike1 c d
)) (return (great c d
))))
3130 (setq l1
(1- l1
) l2
(1- l2
))
3133 (defun term-list (x)
3138 (defun factor-list (x)
3143 ;; one of the exprs x or y should be one of:
3144 ;; %del, mexpt, mplus, mtimes
3146 (let ((cx (caar x
)) (cy (caar y
)))
3147 (cond ((eq cx
'%del
) (if (eq cy
'%del
) (great (cadr x
) (cadr y
)) t
))
3149 ((or (eq cx
'mtimes
) (eq cy
'mtimes
))
3150 (ordlist (factor-list x
) (factor-list y
) 'mtimes
'mtimes
))
3151 ((or (eq cx
'mplus
) (eq cy
'mplus
))
3152 (ordlist (term-list x
) (term-list y
) 'mplus
'mplus
))
3153 ((eq cx
'mexpt
) (ordmexpt x y
))
3154 ((eq cy
'mexpt
) (not (ordmexpt y x
))))))
3157 (if (and (cddr x
) (null (cdddr x
)))
3158 (great (if (eq (caar x
) 'mplus
) 0 1) (cadr x
))))
3160 (defun ordmexpt (x y
)
3161 (cond ((eq (caar y
) 'mexpt
)
3162 (cond ((alike1 (cadr x
) (cadr y
)) (great (caddr x
) (caddr y
)))
3163 ((maxima-constantp (cadr x
))
3164 (if (maxima-constantp (cadr y
))
3165 (if (or (alike1 (caddr x
) (caddr y
))
3166 (and (mnump (caddr x
)) (mnump (caddr y
))))
3167 (great (cadr x
) (cadr y
))
3168 (great (caddr x
) (caddr y
)))
3169 (great x
(cadr y
))))
3170 ((maxima-constantp (cadr y
)) (great (cadr x
) y
))
3172 (great (cadr x
) (if (mnump (caddr y
)) (cadr y
) y
)))
3173 ((mnump (caddr y
)) (great x
(cadr y
)))
3174 (t (let ((x1 (simpln1 x
)) (y1 (simpln1 y
)))
3175 (if (alike1 x1 y1
) (great (cadr x
) (cadr y
))
3177 ((alike1 (cadr x
) y
) (great (caddr x
) 1))
3178 ((mnump (caddr x
)) (great (cadr x
) y
))
3179 (t (great (simpln1 x
) (simpln (list '(%log
) y
) 1 t
)))))
3181 (defmfun $multthru
(e1 &optional e2
)
3183 (cond (e2 ;called with two args
3184 (setq arg1
(specrepcheck e1
)
3185 arg2
(specrepcheck e2
))
3186 (cond ((or (atom arg2
)
3187 (not (member (caar arg2
) '(mplus mequal
))))
3189 ((eq (caar arg2
) 'mequal
)
3190 (list (car arg2
) ($multthru arg1
(cadr arg2
))
3191 ($multthru arg1
(caddr arg2
))))
3192 (t (expandterms arg1
(cdr arg2
)))))
3193 (t ;called with only one arg
3195 (setq arg1
(setq arg2
(specrepcheck e1
)))
3196 (cond ((atom arg1
) (return arg1
))
3197 ((eq (caar arg1
) 'mnctimes
)
3198 (setq arg1
(cdr arg1
)) (go nct
))
3199 ((not (eq (caar arg1
) 'mtimes
)) (return arg1
)))
3200 (setq arg1
(reverse (cdr arg1
)))
3201 times
(when (mplusp (car arg1
))
3202 (setq l1
(nconc l1
(cdr arg1
)))
3203 (return (expandterms (muln l1 t
) (cdar arg1
))))
3204 (setq l1
(cons (car arg1
) l1
))
3205 (setq arg1
(cdr arg1
))
3206 (if (null arg1
) (return arg2
))
3208 nct
(when (mplusp (car arg1
))
3209 (setq l1
(nreverse l1
))
3210 (return (addn (mapcar
3214 (append l1
(ncons u
) (cdr arg1
)))
3218 (setq l1
(cons (car arg1
) l1
))
3219 (setq arg1
(cdr arg1
))
3220 (if (null arg1
) (return arg2
))
3223 ;; EXPANDEXPT computes the expansion of (x1 + x2 + ... + xm)^n
3224 ;; taking a sum and integer power as arguments.
3225 ;; Its theory is to recurse down the binomial expansion of
3226 ;; (x1 + (x2 + x3 + ... + xm))^n using the Binomial Expansion
3227 ;; Thus it does a sigma:
3231 ;; \ / n \ k (n - k)
3232 ;; > | | x1 (x2 + x3 + ... + xm)
3237 ;; The function EXPONENTIATE-SUM does this and recurses through the second
3238 ;; sum raised to a power. It takes a list of terms and a positive integer
3239 ;; power as arguments.
3242 (defun expandexpt (sum power
)
3243 (declare (fixnum power
))
3244 (let ((expansion (exponentiate-sum (cdr sum
) (abs power
))))
3245 (cond ((plusp power
) expansion
)
3246 (t (inv expansion
)))))
3248 (defun exponentiate-sum (terms rpower
)
3249 (declare (fixnum rpower
))
3250 (cond ((= rpower
0) 1)
3251 ((null (cdr terms
)) (power (car terms
) rpower
))
3252 ((= rpower
1) (cons '(mplus simp
) terms
))
3253 (t (do ((i 0 (1+ i
))
3254 (result 0 (add2 result
3255 (muln (list (combination rpower i
)
3256 (exponentiate-sum (cdr terms
)
3258 (power (car terms
) i
)) t
))))
3259 ((> i rpower
) result
)
3260 (declare (fixnum i
))))))
3262 ;; Computes the combination of n elements taken m at a time by the formula
3264 ;; (n * (n-1) * ... * (n - m + 1)) / m! =
3265 ;; (n / 1) * ((n - 1) / 2) * ... * ((n - m + 1) / m)
3267 ;; Checks for the case when m is greater than n/2 and translates
3268 ;; to an equivalent expression.
3270 (defun combination (n m
)
3271 (declare (fixnum n m
))
3272 (cond ((> m
(truncate n
2))
3273 (combination n
(- n m
)))
3275 (do ((result 1 (truncate (* result n1
) m1
))
3279 (declare (fixnum n1 m1
))))))
3281 (defun expandsums (a b
)
3283 (setq a
(fixexpand a
) b
(cdr b
))
3285 (when (null a
) (return c
))
3286 (setq c
(cons (expandterms (car a
) b
) c
))
3291 (defun expandterms (a b
)
3294 (when (null b
) (return c
))
3295 (setq c
(cons (mul2 a
(car b
)) c
))
3300 (defun genexpands (l)
3305 (setq *prods
* (nreverse *prods
*)
3306 *negprods
* (nreverse *negprods
*)
3307 *sums
* (nreverse *sums
*)
3308 *negsums
* (nreverse *negsums
*))
3311 (push (car l
) *prods
*))
3312 ((eq (caaar l
) 'rat
)
3313 (unless (equal (cadar l
) 1)
3314 (push (cadar l
) *prods
*))
3315 (push (caddar l
) *negprods
*))
3316 ((eq (caaar l
) 'mplus
)
3317 (push (car l
) *sums
*))
3318 ((and (eq (caaar l
) 'mexpt
)
3319 (equal (caddar l
) -
1) (mplusp (cadar l
)))
3320 (push (cadar l
) *negsums
*))
3321 ((and (eq (caaar l
) 'mexpt
)
3323 (mminusp (caddar l
))))
3324 (push (if (equal (caddar l
) -
1)
3326 (list (caar l
) (cadar l
) (neg (caddar l
))))
3329 (push (car l
) *prods
*)))
3332 (defun expandtimes (a)
3333 (prog (*prods
* *negprods
* *sums
* *negsums
* expsums expnegsums
)
3335 (setq *prods
* (cond ((null *prods
*) 1)
3336 ((null (cdr *prods
*)) (car *prods
*))
3337 (t (cons '(mtimes simp
) *prods
*))))
3338 (setq *negprods
* (cond ((null *negprods
*) 1)
3339 ((null (cdr *negprods
*)) (car *negprods
*))
3340 (t (cons '(mtimes simp
) *negprods
*))))
3341 (cond ((null *sums
*) (go down
))
3342 (t (setq expsums
(car *sums
*))
3344 (setq expsums
(expandsums expsums c
)))
3346 (setq *prods
* (cond ((equal *prods
* 1) expsums
)
3347 (t (expandterms *prods
* (fixexpand expsums
)))))
3348 down
(cond ((null *negsums
*)
3349 (cond ((equal 1 *negprods
*) (return *prods
*))
3351 (return (expandterms (power *negprods
* -
1) (cdr *prods
*))))
3352 (t (return (let ((expandflag t
))
3353 (mul2 *prods
* (power *negprods
* -
1)))))))
3355 (setq expnegsums
(car *negsums
*))
3357 (setq expnegsums
(expandsums expnegsums c
)))
3359 (setq expnegsums
(expandterms *negprods
* (fixexpand expnegsums
)))
3360 (return (if (mplusp *prods
*)
3361 (expandterms (inv expnegsums
) (cdr *prods
*))
3362 (let ((expandflag t
))
3363 (mul2 *prods
* (inv expnegsums
)))))))
3365 (defun expand1 (exp $expop $expon
)
3366 (unless (and (integerp $expop
) (> $expop -
1))
3367 (merror (intl:gettext
"expand: expop must be a nonnegative integer; found: ~M") $expop
))
3368 (unless (and (integerp $expon
) (> $expon -
1))
3369 (merror (intl:gettext
"expand: expon must be a nonnegative integer; found: ~M") $expon
))
3370 (resimplify (specrepcheck exp
)))
3372 (defmfun $expand
(exp &optional
(expop $maxposex
) (expon $maxnegex
))
3373 (expand1 exp expop expon
))
3375 (defun fixexpand (a)
3376 (if (not (mplusp a
))
3380 (defun simpnrt (x *n
) ; computes X^(1/*N)
3381 (prog (*in
*out varlist genvar $factorflag $dontfactor
)
3382 (setq $factorflag t
)
3384 (setq x
(ratrep* x
))
3385 (when (equal (cadr x
) 0) (return 0))
3386 (setq x
(ratfact (cdr x
) 'psqfr
))
3387 (simpnrt1 (mapcar #'pdis x
))
3388 (setq *out
(if *out
(muln *out nil
) 1))
3389 (setq *in
(cond (*in
3390 (setq *in
(muln *in nil
))
3393 (return (let (($%emode t
))
3394 (simplifya (list '(mtimes) *in
*out
)
3397 (member (caaadr *in
) '(mplus mtimes rat
)))))))))
3400 (do ((x x
(cddr x
)) (y))
3402 (cond ((not (equal 1 (setq y
(gcd (cadr x
) *n
))))
3403 (push (simpnrt (list '(mexpt) (car x
) (quotient (cadr x
) y
))
3406 ((and (equal (cadr x
) 1) (integerp (car x
)) (plusp (car x
))
3407 (setq y
(pnthrootp (car x
) *n
)))
3410 (unless (> *n
(abs (cadr x
)))
3411 (push (list '(mexpt) (car x
) (quotient (cadr x
) *n
)) *out
))
3412 (push (list '(mexpt) (car x
) (rem (cadr x
) *n
)) *in
)))))
3414 (defun nrthk (in *n
)
3420 ((eq $domain
'$real
)
3426 (power* '$%e
(list '(mtimes) (list '(rat) 1 *n
) '$%pi
'$%i
))))
3429 ((or (and wflag
(eq ($asksign in
) '$neg
))
3430 (and (mnump in
) (equal ($sign in
) '$neg
)))
3431 (nrthk1 (mul2* -
1 in
) *n
))
3435 (defun nrthk1 (in *n
) ; computes (-IN)^(1/*N)
3437 (mul2 (nrthk2 in
*n
) (nrthk -
1 *n
))
3438 (nrthk2 (mul2* -
1 in
) *n
)))
3440 (defun nrthk2 (in *n
)
3441 (power* in
(list '(rat) 1 *n
))) ; computes IN^(1/*N)
3443 ;; The following was formerly in SININT. This code was placed here because
3444 ;; SININT is now an out-of-core file on MC, and this code is needed in-core
3445 ;; because of the various calls to it. - BMT & JPG
3447 (declare-top (special var ratform context
))
3449 (defmfun $integrate
(expr x
&optional lo hi
)
3450 (declare (special *in-risch-p
*))
3453 (with-new-context (context)
3454 (if (member '%risch
*nounl
*)
3456 ;; Give up; we're being called from RISCHINT by some path.
3457 (list '(%integrate
) expr x
)
3460 ($defint expr x lo hi
))))
3464 ((member (caar a
) '(mplus mtimes
))
3465 (do ((l (cdr a
) (cdr l
))) ((null l
) t
)
3466 (or (ratp (car l
) var
) (return nil
))))
3467 ((eq (caar a
) 'mexpt
)
3468 (if (free (cadr a
) var
)
3469 (free (caddr a
) var
)
3470 (and (integerp (caddr a
)) (ratp (cadr a
) var
))))
3473 (defun ratnumerator (r)
3475 ((atom (cdr r
)) (car r
))
3476 ((numberp (cadr r
)) r
)
3479 (defun ratdenominator (r)
3481 ((atom (cdr r
)) (cdr r
))
3482 ((numberp (cadr r
)) 1)
3485 (declare-top (special var
))
3487 ;; (BPROG U V) appears to return A and B (as ((A1 . A2) B1 . B2) with A = A1/A2, B = B1/B2)
3488 ;; such that B/U + A/V = 1/(U*V), where U, V are polynomials represented as a list of
3489 ;; exponents and coefficients, (<gensym> E1 C1 E2 C2 ...) = C1*<gensym>^E1 + C2*<gensym>^E2 + ....
3491 ;; (%i73) partfrac ((2*x^2-3)/(x^4-3*x^2+2), x);
3492 ;; 1. Trace: (PARTFRAC '((#:X16910 2 2 0 -3) #:X16910 4 1 2 -3 0 2) '#:X16910)
3493 ;; 2. Trace: (BPROG '(#:X16910 2 1 0 -2) '(#:X16910 2 1 0 -1))
3494 ;; 2. Trace: BPROG ==> ((-1 . 1) 1 . 1)
3495 ;; 2. Trace: (BPROG '(#:X16910 1 1 0 1) '(#:X16910 1 1 0 -1))
3496 ;; 2. Trace: BPROG ==> ((1 . 2) -1 . 2)
3497 ;; 2. Trace: (BPROG '(#:X16910 1 1 0 -1) '1)
3498 ;; 2. Trace: BPROG ==> ((0 . 1) 1 . 1)
3499 ;; 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))
3500 ;; (%o73) 1/(x^2-2)-1/(2*(x+1))+1/(2*(x-1))
3503 (prog (p1b p2b coef1r coef2r coef1s coef2s f1 f2 a egcd oldalg
)
3504 (setq oldalg $algebraic
)
3507 (setq coef2r
(setq coef1s
0))
3508 (setq coef2s
(setq coef1r
1))
3511 (unless (zerop (pdegree p1b var
)) (setq egcd
(pgcdexpon p1b
)))
3513 (unless (or (zerop (pdegree p2b var
)) (= egcd
1))
3514 (setq egcd
(gcd egcd
(pgcdexpon p2b
)))
3515 (setq p1b
(pexpon*// p1b egcd nil
)
3516 p2b
(pexpon*// p2b egcd nil
)))
3517 b1
(cond ((< (pdegree p1b var
) (pdegree p2b var
))
3519 (rotatef coef1r coef2r
)
3520 (rotatef coef1s coef2s
)))
3521 (when (zerop (pdegree p2b var
))
3522 (unless (zerop (pdegree coef2r var
))
3523 (setq coef2r
(pexpon*// coef2r egcd t
)))
3524 (unless (zerop (pdegree coef2s var
))
3525 (setq coef2s
(pexpon*// coef2s egcd t
)))
3526 (return (cons (ratreduce (ptimes (cdr r
) coef2r
) p2b
)
3527 (ratreduce (ptimes (cdr s
) coef2s
) p2b
))))
3528 (setq f1
(psquorem1 (cdr p1b
) (cdr p2b
) t
))
3529 (setq $algebraic $false
)
3530 (setq f2
(psimp var
(cadr f1
)))
3531 (setq p1b
(pquotientchk (psimp var
(caddr f1
)) a
))
3532 (setq $algebraic oldalg
)
3534 (setq coef1r
(pquotientchk (pdifference (ptimes f1 coef1r
)
3537 (setq coef1s
(pquotientchk (pdifference (ptimes f1 coef1s
)
3543 (defun ratdifference (a b
) (ratplus a
(ratminus b
)))
3545 (defun ratpl (a b
) (ratplus (ratfix a
) (ratfix b
)))
3547 (defun ratti (a b c
) (rattimes (ratfix a
) (ratfix b
) c
))
3549 (defun ratqu (a b
) (ratquotient (ratfix a
) (ratfix b
)))
3551 (defun ratfix (a) (cond ((equal a
(ratnumerator a
)) (cons a
1)) (t a
)))
3553 (defun ratdivide (f g
)
3554 (destructuring-let* (((fnum . fden
) (ratfix f
))
3555 ((gnum . gden
) (ratfix g
))
3556 ((q r
) (pdivide fnum gnum
)))
3557 (cons (ratqu (ratti q gden t
) fden
)
3560 (defun polcoef (l n
) (cond ((or (atom l
) (pointergp var
(car l
)))
3561 (cond ((equal n
0) l
) (t 0)))
3562 (t (ptterm (cdr l
) n
))))
3564 (defun disrep (l) (cond ((equal (ratnumerator l
) l
)
3565 ($ratdisrep
(cons ratform
(cons l
1))))
3566 (t ($ratdisrep
(cons ratform l
)))))
3568 (declare-top (unspecial var
))
3571 ;; The following was formerly in MATRUN. This code was placed here because
3572 ;; MATRUN is now an out-of-core file on MC, and this code is needed in-core
3573 ;; so that MACSYMA SAVE files will work. - JPG
3578 (defun kar (x) (if (atom x
) (matcherr) (car x
)))
3580 (defun kaar (x) (kar (kar x
)))
3582 (defun kdr (x) (if (atom x
) (matcherr) (cdr x
)))
3584 (defun simpargs1 (a vestigial c
)
3585 (declare (ignore vestigial
))
3589 (if (not (atom x
)) (car x
)))
3591 (defquote retlist
(&rest l
)
3593 (mapcar #'(lambda (z) (list '(mequal simp
) z
(meval z
))) l
)))
3596 (if (zerop c
) x
(nthkdr (kdr x
) (1- c
))))