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