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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (macsyma-module compar
)
15 (load-macsyma-macros mrgmac
)
17 (declare-top (special success $props
))
19 (defvar *debug-compar
* nil
20 "Enables debugging code for this file.")
22 (defvar %initiallearnflag
)
24 (defvar $context
'$global
25 "Whenever a user assumes a new fact, it is placed in the context
26 named as the current value of the variable CONTEXT. Similarly, FORGET
27 references the current value of CONTEXT. To add or DELETE a fact from a
28 different context, one must bind CONTEXT to the intended context and then
29 perform the desired additions or deletions. The context specified by the
30 value of CONTEXT is automatically activated. All of MACSYMA's built-in
31 relational knowledge is contained in the default context GLOBAL.")
33 (defvar $contexts
'((mlist) $global
)
34 "A list of the currently active contexts.")
36 (defvar $activecontexts
'((mlist))
37 "A list of the currently activated contexts")
39 (defmvar sign-imag-errp t
40 "If T errors out in case COMPAR meets up with an imaginary quantity.
41 If NIL THROWs in that case."
44 (defmvar complexsign nil
45 "If T, COMPAR attempts to work in a complex mode.
46 This scheme is only very partially developed at this time."
49 (defvar *complexsign
* nil
50 "If T, COMPAR works in a complex mode.")
52 (defmvar $prederror nil
)
53 (defmvar $signbfloat t
)
56 (defmvar $assume_pos nil
)
57 (defmvar $assume_pos_pred nil
)
59 (defmvar factored nil
)
61 ;; The *LOCAL-SIGNS* variable contains a list of facts that are local to the
62 ;; current evaluation. These are stored in the assume database (in the global
63 ;; context) by asksign1 when the user answers questions. A "top-level"
64 ;; evaluation is run by MEVAL* and that function calls CLEARSIGN when it
65 ;; finishes to discard them.
66 (defmvar *local-signs
* nil
)
75 ;; Remove this (nil'ed out) function after a while. We should be
76 ;; using POWER instead of POW.
78 (defmacro pow
(&rest x
)
82 (simplify (cons '(mtimes) l
)))
87 (do ((x (cdr x
) (cdr x
))
90 (incf sz
(1+ (conssize (car x
)))))))
92 ;;; Functions for creating, activating, manipulating, and killing contexts
94 ;;; This "turns on" a context, making its facts visible.
96 (defmfun $activate
(&rest args
)
98 (cond ((not (symbolp c
)) (nc-err '$activate c
))
99 ((member c
(cdr $activecontexts
) :test
#'eq
))
100 ((member c
(cdr $contexts
) :test
#'eq
)
101 (setq $activecontexts
(mcons c $activecontexts
))
103 (t (merror (intl:gettext
"activate: no such context ~:M") c
))))
106 ;;; This "turns off" a context, keeping the facts, but making them invisible
108 (defmfun $deactivate
(&rest args
)
110 (cond ((not (symbolp c
)) (nc-err '$deactivate c
))
111 ((member c
(cdr $contexts
) :test
#'eq
)
112 (setq $activecontexts
($delete c $activecontexts
))
114 (t (merror (intl:gettext
"deactivate: no such context ~:M") c
))))
117 ;;; This function prints out a list of the facts in the specified context.
118 ;;; No argument implies the current context.
120 (defmfun $facts
(&optional
(ctxt $context
))
121 (if (member ctxt
(cdr $contexts
))
127 (do ((l (zl-get con
'data
) (cdr l
))
130 ((null l
) (cons '(mlist) nl
))
131 (when (visiblep (car l
))
132 (setq u
(intext (caaar l
) (cdaar l
)))
133 (unless (memalike u nl
)
136 ;; Look up facts from the database which contain expr. expr can be a symbol or
137 ;; a more general expression.
139 (labels ((among (x l
)
144 (do ((ll (cdr l
) (cdr ll
)))
146 (if (among x
(car ll
)) (return t
)))))))
147 (do ((facts (cdr ($facts $context
)) (cdr facts
))
149 ((null facts
) (return (cons '(mlist) (reverse ans
))))
150 (when (or (among expr
(cadar facts
))
151 (among expr
(caddar facts
)))
152 (push (car facts
) ans
)))))
154 (defun intext (rel body
)
155 (setq body
(mapcar #'doutern body
))
156 (cond ((eq 'kind rel
) (cons '($kind
) body
))
157 ((eq 'par rel
) (cons '($par
) body
))
158 ((eq 'mgrp rel
) (cons '(mgreaterp) body
))
159 ((eq 'mgqp rel
) (cons '(mgeqp) body
))
160 ((eq 'meqp rel
) (cons '($equal
) body
))
161 ((eq 'mnqp rel
) (list '(mnot) (cons '($equal
) body
)))))
163 (defprop $context asscontext assign
)
165 ;;; This function switches contexts, creating one if necessary.
167 (defun asscontext (xx y
)
168 (declare (ignore xx
))
169 (cond ((not (symbolp y
)) (nc-err "context assignment" y
))
170 ((member y $contexts
:test
#'eq
) (setq context y $context y
))
171 (t ($newcontext y
))))
173 ;;; This function actually creates a context whose subcontext is $GLOBAL.
174 ;;; It also switches contexts to the newly created one.
175 ;;; If no argument supplied, then invent a name via gensym and use that.
177 (defmfun $newcontext
(&rest args
)
179 ($newcontext
($gensym
"context")) ;; make up a name and try again
180 (if (> (length args
) 1)
181 (merror "newcontext: found more than one argument.")
182 (let ((x (first args
)))
184 ((not (symbolp x
)) (nc-err '$newcontext x
))
185 ((member x $contexts
:test
#'eq
)
186 (mtell (intl:gettext
"newcontext: context ~M already exists.") x
) nil
)
188 (setq $contexts
(mcons x $contexts
))
189 (putprop x
'($global
) 'subc
)
190 (setq context x $context x
)))))))
192 ;;; This function creates a supercontext. If given one argument, it
193 ;;; makes the current context be the subcontext of the argument. If
194 ;;; given more than one argument, the first is assumed the name of the
195 ;;; supercontext and the rest are the subcontexts.
196 ;;; If no arguments supplied, then invent a name via gensym and use that.
198 (defmfun $supcontext
(&rest x
)
199 (cond ((null x
) ($supcontext
($gensym
"context"))) ;; make up a name and try again
200 ((caddr x
) (merror (intl:gettext
"supcontext: found more than two arguments.")))
201 ((not (symbolp (car x
))) (nc-err '$supcontext
(car x
)))
202 ((member (car x
) $contexts
:test
#'eq
)
203 (merror (intl:gettext
"supcontext: context ~M already exists.") (car x
)))
204 ((and (cadr x
) (not (member (cadr x
) $contexts
:test
#'eq
)))
205 (merror (intl:gettext
"supcontext: no such context ~M") (cadr x
)))
206 (t (setq $contexts
(mcons (car x
) $contexts
))
207 (putprop (car x
) (ncons (or (cadr x
) $context
)) 'subc
)
208 (setq context
(car x
) $context
(car x
)))))
210 ;;; This function kills a context or a list of contexts
212 (defmfun $killcontext
(&rest args
)
216 (nc-err '$killcontext c
)))
217 (if (and (= (length args
) 1) (eq (car args
) '$global
))
221 (defun killallcontexts ()
222 (mapcar #'killcontext
(cdr $contexts
))
223 (setq $context
'$initial context
'$initial current
'$initial
224 $contexts
'((mlist) $initial $global
) dobjects
())
226 ;;conmark, conunmrk, conindex, connumber, and contexts
227 ;;concern garbage-collectible contexts, and so we're
228 ;;better off not resetting them.
229 (defprop $global
1 cmark
) (defprop $initial
1 cmark
)
230 (defprop $initial
($global
) subc
))
232 (defun killcontext (x)
233 (cond ((not (member x $contexts
:test
#'eq
))
234 (mtell (intl:gettext
"killcontext: no such context ~M.") x
))
235 ((eq x
'$global
) '$global
)
237 (mapc #'remov
(zl-get '$initial
'data
))
238 (remprop '$initial
'data
)
240 ((and (not (eq $context x
)) (contextmark) (< 0 (zl-get x
'cmark
)))
241 (mtell (intl:gettext
"killcontext: context ~M is currently active.") x
))
242 (t (if (member x $activecontexts
)
243 ;; Context is on the list of active contexts. The test above
244 ;; checks for active contexts, but it seems not to work in all
245 ;; cases. So deactivate the context at this place to remove it
246 ;; from the list of active contexts before it is deleted.
248 (setq $contexts
($delete x $contexts
))
249 (cond ((and (eq x $context
)
250 (equal ;;replace eq ?? wfs
251 (zl-get x
'subc
) '($global
)))
252 (setq $context
'$initial
)
253 (setq context
'$initial
))
255 (setq $context
(car (zl-get x
'subc
)))
256 (setq context
(car (zl-get x
'subc
)))))
261 (merror (intl:gettext
"~M: context name must be a symbol; found ~M") fn x
))
263 ;; Simplification and evaluation of boolean expressions
265 ;; Simplification of boolean expressions:
267 ;; and and or are declared nary. The sole effect of this is to allow Maxima to
268 ;; flatten nested expressions, e.g., a and (b and c) => a and b and c
269 ;; (The nary declaration does not make and and or commutative, and and and or
270 ;; are not otherwise declared commutative.)
272 ;; and: if any argument simplifies to false, return false
273 ;; otherwise omit arguments which simplify to true and simplify others
274 ;; if only one argument remains, return it
275 ;; if none remain, return true
277 ;; or: if any argument simplifies to true, return true
278 ;; otherwise omit arguments which simplify to false and simplify others
279 ;; if only one argument remains, return it
280 ;; if none remain, return false
282 ;; not: if argument simplifies to true / false, return false / true
283 ;; otherwise reverse sense of comparisons (if argument is a comparison)
284 ;; otherwise return not <simplified argument>
286 ;; Evaluation (MEVAL) of boolean expressions:
287 ;; same as simplification except evaluating (MEVALP) arguments instead of simplifying
288 ;; When prederror = true, complain if expression evaluates to something other than T / NIL
289 ;; (otherwise return unevaluated boolean expression)
291 ;; Evaluation (MEVALP) of boolean expressions:
292 ;; same as simplification except evaluating (MEVALP) arguments instead of simplifying
293 ;; When prederror = true, complain if expression evaluates to something other than T / NIL
294 ;; (otherwise return unevaluated boolean expression)
296 ;; Simplification of "is" expressions:
297 ;; if argument simplifies to true/false, return true/false
298 ;; otherwise return is (<simplified argument>)
300 ;; Evaluation of "is" expressions:
301 ;; if argument evaluates to true/false, return true/false
302 ;; otherwise return unknown if prederror = false, else trigger an error
304 ;; Simplification of "maybe" expressions:
305 ;; if argument simplifies to true/false, return true/false
306 ;; otherwise return maybe (<simplified expression>)
308 ;; Evaluation of "maybe" expressions:
309 ;; if argument evaluates to true/false, return true/false
310 ;; otherwise return unknown
312 (defprop $is simp-$is operators
)
313 (defprop %is simp-$is operators
)
314 (defprop $maybe simp-$is operators
)
315 (defprop %maybe simp-$is operators
)
317 ; I'VE ASSUMED (NULL Z) => SIMPLIFIY ARGUMENTS
318 ; SAME WITH SIMPCHECK (SRC/SIMP.LISP)
319 ; SAME WITH TELLSIMP-GENERATED SIMPLIFICATION FUNCTIONS
320 ; SAME WITH SIMPLIFICATION OF %SIN
321 ; PRETTY SURE I'VE SEEN OTHER EXAMPLES AS WELL
322 ; Z SEEMS TO SIGNIFY "ARE THE ARGUMENTS SIMPLIFIED YET"
324 (defun maybe-simplifya (x z
)
325 (if z x
(simplifya x z
)))
327 (defun maybe-simplifya-protected (x z
)
328 (let ((errcatch t
) ($errormsg nil
))
329 (declare (special errcatch $errormsg
))
330 (ignore-errors (maybe-simplifya x z
) x
)))
332 (defun simp-$is
(x yy z
)
333 (declare (ignore yy
))
334 (let ((a (maybe-simplifya (cadr x
) z
)))
335 (if (or (eq a t
) (eq a nil
))
337 `((,(caar x
) simp
) ,a
))))
340 (unless (= 1 (length (rest form
)))
341 (merror (intl:gettext
"is() expects a single argument. Found ~A")
342 (length (rest form
))))
343 (destructuring-bind (answer patevalled
)
344 (mevalp1 (cadr form
))
345 (cond ((member answer
'(t nil
) :test
#'eq
) answer
)
346 ;; I'D RATHER HAVE ($PREDERROR ($THROW `(($PREDERROR) ,PATEVALLED))) HERE
347 ($prederror
(pre-err patevalled
))
350 (defmspec $maybe
(form)
351 (let* ((pat (cadr form
))
352 (x (let (($prederror nil
)) (mevalp1 pat
)))
354 (if (member ans
'(t nil
) :test
#'eq
)
359 (let (($prederror t
))
362 ; The presence of OPERS tells SIMPLIFYA to call OPER-APPLY,
363 ; which calls NARY1 to flatten nested "and" and "or" expressions
364 ; (due to $NARY property of MAND and MOR, declared elsewhere).
366 (putprop 'mand t
'opers
)
367 (putprop 'mor t
'opers
)
369 (putprop 'mnot
'simp-mnot
'operators
)
370 (putprop 'mand
'simp-mand
'operators
)
371 (putprop 'mor
'simp-mor
'operators
)
373 (defun simp-mand (x yy z
)
374 (declare (ignore yy
))
375 (do ((l (cdr x
) (cdr l
))
379 (cond ((= (length simplified
) 0) t
)
380 ((= (length simplified
) 1) (car simplified
))
381 (t (cons '(mand simp
) (reverse simplified
)))))
382 (setq a
(maybe-simplifya (car l
) z
))
383 (cond ((null a
) (return nil
))
384 ((eq a
'$unknown
) (unless (member '$unknown simplified
:test
#'eq
) (push a simplified
)))
385 ((not (member a
'(t nil
) :test
#'eq
)) (push a simplified
)))))
387 (defun simp-mor (x yy z
)
388 (declare (ignore yy
))
389 (do ((l (cdr x
) (cdr l
))
393 (cond ((= (length simplified
) 0) nil
)
394 ((= (length simplified
) 1) (car simplified
))
395 (t (cons '(mor simp
) (reverse simplified
)))))
396 (setq a
(maybe-simplifya (car l
) z
))
397 (cond ((eq a t
) (return t
))
398 ((eq a
'$unknown
) (unless (member '$unknown simplified
:test
#'eq
) (push a simplified
)))
399 ((not (member a
'(t nil
) :test
#'eq
)) (push a simplified
)))))
401 ; ALSO CUT STUFF ABOUT NOT EQUAL => NOTEQUAL AT TOP OF ASSUME
403 (defun simp-mnot (x yy z
)
404 (declare (ignore yy
))
405 (let ((arg (maybe-simplifya (cadr x
) z
)))
407 (cond ((or (eq arg t
) (eq arg
'$true
))
409 ((or (eq arg nil
) (eq arg
'$false
))
413 (t `((mnot simp
) ,arg
)))
414 (let ((arg-op (caar arg
)) (arg-arg (cdr arg
)))
415 ;;(setq arg-arg (mapcar #'(lambda (a) (maybe-simplifya a z)) arg-arg))
416 (cond ((eq arg-op
'mlessp
)
417 (simplify `((mgeqp) ,@arg-arg
)))
419 (simplify `((mgreaterp) ,@arg-arg
)))
421 (simplify `((mnotequal) ,@arg-arg
)))
423 (simplify `(($notequal
) ,@arg-arg
)))
424 ((eq arg-op
'mnotequal
)
425 (simplify `((mequal) ,@arg-arg
)))
426 ((eq arg-op
'$notequal
)
427 (simplify `(($equal
) ,@arg-arg
)))
429 (simplify `((mlessp) ,@arg-arg
)))
430 ((eq arg-op
'mgreaterp
)
431 (simplify `((mleqp) ,@arg-arg
)))
434 ;; Distribute negation over conjunction and disjunction;
435 ;; analogous to '(- (a + b)) --> - a - b.
437 (let ((L (mapcar #'(lambda (e) `((mnot) ,e
)) arg-arg
)))
438 (simplifya `((mor) ,@L
) nil
)))
440 (let ((L (mapcar #'(lambda (e) `((mnot) ,e
)) arg-arg
)))
441 (simplifya `((mand) ,@L
) nil
)))
442 (t `((mnot simp
) ,arg
)))))))
445 ;; The function IS-BOOLE-CHECK, used by the translator, depends
446 ;; on some stuff in here. Check it out in the transl module
447 ;; ACALL before proceeding.
450 (let* ((x (mevalp1 pat
))
452 (patevalled (cadr x
)))
453 (cond ((member ans
'(t ()) :test
#'eq
) ans
)
454 ;; I'D RATHER HAVE ($PREDERROR ($THROW `(($PREDERROR) ,PATEVALLED))) HERE
455 ($prederror
(pre-err patevalled
))
456 (t (or patevalled ans
)))))
459 (let (patevalled ans
)
461 (cond ((and (not (atom pat
))
462 (member (caar pat
) '(mnot mand mor
) :test
#'eq
))
463 (cond ((eq 'mnot
(caar pat
)) (is-mnot (cadr pat
)))
464 ((eq 'mand
(caar pat
)) (is-mand (cdr pat
)))
465 (t (is-mor (cdr pat
)))))
466 ((atom (setq patevalled
(specrepcheck (meval pat
))))
468 ((member (caar patevalled
) '(mnot mand mor
) :test
#'eq
)
469 (return-from mevalp1
(mevalp1 patevalled
)))
474 (caddr patevalled
)))))
475 (list ans patevalled
)))
477 (defun mevalp2 (patevalled pred arg1 arg2
)
478 (cond ((eq 'mequal pred
) (like arg1 arg2
))
479 ((eq '$equal pred
) (meqp arg1 arg2
))
480 ((eq 'mnotequal pred
) (not (like arg1 arg2
)))
481 ((eq '$notequal pred
) (mnqp arg1 arg2
))
482 ((eq 'mgreaterp pred
) (mgrp arg1 arg2
))
483 ((eq 'mlessp pred
) (mgrp arg2 arg1
))
484 ((eq 'mgeqp pred
) (mgqp arg1 arg2
))
485 ((eq 'mleqp pred
) (mgqp arg2 arg1
))
486 (t (isp (munformat patevalled
)))))
489 (merror (intl:gettext
"Unable to evaluate predicate ~M") pat
))
491 (defun is-mnot (pred)
492 (setq pred
(mevalp pred
))
493 (cond ((eq t pred
) nil
)
495 (t (pred-reverse pred
))))
497 (defun pred-reverse (pred)
503 ((null pl
) (cond ((null npl
))
504 ((null (cdr npl
)) (car npl
))
505 (t (cons '(mand) (nreverse npl
)))))
506 (setq dummy
(mevalp (car pl
)) pl
(cdr pl
))
508 ((null dummy
) (return nil
))
509 (t (push dummy npl
)))))
514 ((null pl
) (cond ((null npl
) nil
)
515 ((null (cdr npl
)) (car npl
))
516 (t (cons '(mor) (nreverse npl
)))))
517 (setq dummy
(mevalp (car pl
)) pl
(cdr pl
))
518 (cond ((eq t dummy
) (return t
))
520 (t (push dummy npl
)))))
522 (defmspec $assume
(x)
524 (do ((nl)) ((null x
) (cons '(mlist) (nreverse nl
)))
525 (cond ((atom (car x
)) (push (assume (meval (car x
))) nl
))
526 ((eq 'mand
(caaar x
))
527 (mapc #'(lambda (l) (push (assume (meval l
)) nl
))
529 ((eq 'mnot
(caaar x
))
530 (push (assume (meval (pred-reverse (cadar x
)))) nl
))
532 (merror (intl:gettext
"assume: argument cannot be an 'or' expression; found ~M") (car x
)))
533 ((eq (caaar x
) 'mequal
)
534 (merror (intl:gettext
"assume: argument cannot be an '=' expression; found ~M~%assume: maybe you want 'equal'.") (car x
)))
535 ((eq (caaar x
) 'mnotequal
)
536 (merror (intl:gettext
"assume: argument cannot be a '#' expression; found ~M~%assume: maybe you want 'not equal'.") (car x
)))
537 (t (push (assume (meval (car x
))) nl
)))
541 (if (and (not (atom pat
))
542 (eq (caar pat
) 'mnot
)
543 (eq (caaadr pat
) '$equal
))
544 (setq pat
`(($notequal
) ,@(cdadr pat
))))
545 (let ((dummy (let ($assume_pos
) (car (mevalp1 pat
)))))
546 (cond ((eq dummy t
) '$redundant
)
547 ((null dummy
) '$inconsistent
)
548 ((atom dummy
) '$meaningless
)
551 (defun learn (pat flag
)
553 ;; Check for abs function in pattern.
555 (learn-abs pat flag
)))
556 ;; Check for constant expression in pattern.
558 (learn-numer pat flag
)))
559 ((zl-get (caar pat
) (if flag
'learn
'unlearn
))
560 (funcall (zl-get (caar pat
) (if flag
'learn
'unlearn
)) pat
))
561 ((eq (caar pat
) 'mgreaterp
) (daddgr flag
(sub (cadr pat
) (caddr pat
))))
562 ((eq (caar pat
) 'mgeqp
) (daddgq flag
(sub (cadr pat
) (caddr pat
))))
563 ((member (caar pat
) '(mequal $equal
) :test
#'eq
)
564 (daddeq flag
(sub (cadr pat
) (caddr pat
))))
565 ((member (caar pat
) '(mnotequal $notequal
) :test
#'eq
)
566 (daddnq flag
(sub (cadr pat
) (caddr pat
))))
567 ((eq (caar pat
) 'mleqp
) (daddgq flag
(sub (caddr pat
) (cadr pat
))))
568 ((eq (caar pat
) 'mlessp
) (daddgr flag
(sub (caddr pat
) (cadr pat
))))
569 (flag (true* (munformat pat
)))
572 ((eq (caar pat
) '$kind
)
573 (unkind (second pat
) (third pat
)))
574 (t (untrue (munformat pat
))))
577 ;;; When abs(x)<a is in the pattern, where a is a positive expression,
578 ;;; then learn x<a and -x<a too. The additional facts are put into the context
579 ;;; '$learndata, if the current context is user context 'initial
581 (defun learn-abs (pat flag
)
583 (when (and (setq tmp
(isinop pat
'mabs
))
584 (or (and (member (caar pat
) '(mlessp mleqp
))
585 (isinop (cadr pat
) 'mabs
)
586 (member ($sign
(caddr pat
)) '($pos $pz
)))
587 (and (member (caar pat
) '(mgreaterp mgeqp
))
588 (member ($sign
(cadr pat
)) '($pos $pz
))
589 (isinop (caddr pat
) 'mabs
))))
590 (let ((oldcontext context
))
591 (if (eq oldcontext
'$initial
)
592 (asscontext nil
'$learndata
)) ; switch to context '$learndata
593 ; learn additional facts
594 (learn ($substitute
(cadr tmp
) tmp pat
) flag
)
595 (learn ($substitute
(mul -
1 (cadr tmp
)) tmp pat
) flag
)
596 (when (eq oldcontext
'$initial
)
597 (asscontext nil oldcontext
) ; switch back to context on entry
598 ($activate
'$learndata
)))) ; context '$learndata is active
601 ;;; The value of a constant expression which can be numerically evaluated is
602 ;;; put into the context '$learndata.
604 (defun learn-numer (pat flag
)
605 (let (dum expr patnew
)
606 (do ((x (cdr pat
) (cdr x
)))
607 ((null x
) (setq patnew
(reverse patnew
)))
608 (setq dum
(constp (car x
))
610 (cond ((or (numberp (car x
))
614 (setq dum
($bfloat
(car x
)))
618 (if (and (setq dum
(numer (car x
)))
621 ((and (member dum
'(numer symbol
) :test
#'eq
)
623 (setq dum
(numer (car x
)))
628 (< (abs dum
) 1.0e-6))))))
630 (and (setq dum
($bfloat
(car x
)))
633 (setq patnew
(cons expr patnew
)))
634 (setq patnew
(cons (car pat
) patnew
))
635 (when (and (not (alike (cdr pat
) (cdr patnew
)))
636 (or (not (mnump (cadr patnew
))) ; not both sides of the
637 (not (mnump (caddr patnew
))))) ; relation can be number
638 (let ((oldcontext $context
))
639 (if (eq oldcontext
'$initial
)
640 (asscontext nil
'$learndata
)) ; switch to context '$learndata
641 (learn patnew flag
) ; learn additional fact
642 (when (eq oldcontext
'$initial
)
643 (asscontext nil oldcontext
) ; switch back to context on entry
644 ($activate
'$learndata
)))) ; context '$learndata is active
647 (defmspec $forget
(x)
650 ((null x
) (cons '(mlist) (nreverse nl
)))
651 (cond ((atom (car x
)) (push (forget (meval (car x
))) nl
))
652 ((eq 'mand
(caaar x
))
653 (mapc #'(lambda (l) (push (forget (meval l
)) nl
)) (cdar x
)))
654 ((eq 'mnot
(caaar x
))
655 (push (forget (meval (pred-reverse (cadar x
)))) nl
))
657 (merror (intl:gettext
"forget: argument cannot be an 'or' expression; found ~M") (car x
)))
658 (t (push (forget (meval (car x
))) nl
)))
663 (cons '(mlist simp
) (mapcar #'forget1
(cdr pat
))))
667 (cond ((and (not (atom pat
))
668 (eq (caar pat
) 'mnot
)
669 (eq (caaadr pat
) '$equal
))
670 (setq pat
`(($notequal
) ,@(cdadr pat
)))))
673 (defun restore-facts (factl) ; used by SAVE
675 (cond ((eq (caar fact
) '$kind
)
676 (declarekind (cadr fact
) (caddr fact
))
677 (add2lnc (getop (cadr fact
)) $props
))
678 ((eq (caar fact
) '$par
))
681 (defmacro compare
(a b
)
682 `(sign1 (sub* ,a
,b
)))
690 (defmspec mand
(form)
691 (setq form
(cdr form
))
692 (do ((l form
(cdr l
))
696 (cond ((= (length unevaluated
) 0) t
)
697 ((= (length unevaluated
) 1) (car unevaluated
))
698 (t (cons '(mand) (reverse unevaluated
)))))
699 (setq x
(mevalp (car l
)))
700 (cond ((null x
) (return nil
))
701 ((not (member x
'(t nil
) :test
#'eq
)) (push x unevaluated
)))))
704 (setq form
(cdr form
))
705 (do ((l form
(cdr l
))
709 (cond ((= (length unevaluated
) 0) nil
)
710 ((= (length unevaluated
) 1) (car unevaluated
))
711 (t (cons '(mor) (reverse unevaluated
)))))
712 (setq x
(mevalp (car l
)))
713 (cond ((eq x t
) (return t
))
714 ((not (member x
'(t nil
) :test
#'eq
)) (push x unevaluated
)))))
716 (defmspec mnot
(form)
717 (setq form
(cdr form
))
718 (let ((x (mevalp (car form
))))
719 (if (member x
'(t nil
) :test
#'eq
)
723 ;;;Toplevel functions- $askequal, $asksign, and $sign.
724 ;;;Switches- LIMITP If TRUE $ASKSIGN and $SIGN will look for special
725 ;;; symbols such as EPSILON, $INF, $MINF and attempt
726 ;;; to do the correct thing. In addition calls to
727 ;;; $REALPART and $IMAGPART are made to assure that
728 ;;; the expression is real.
730 ;;; if NIL $ASKSIGN and $SIGN assume the expression
731 ;;; given is real unless it contains an $%I, in which
732 ;;; case they call $RECTFORM.
736 (defmfun $askequal
(a b
)
737 (let ((answer (meqp (sratsimp a
) (sratsimp b
)))) ; presumably handles mbags and extended reals.
738 (cond ((eq answer t
) '$yes
)
739 ((eq answer nil
) '$no
)
741 (setq answer
(retrieve `((mtext) ,(intl:gettext
"Is ") ,a
,(intl:gettext
" equal to ") ,b
,(intl:gettext
"?")) nil
))
742 (cond ((member answer
'($no |$n| |$N|
) :test
#'eq
)
745 ((member answer
'($yes |$y| |$Y|
) :test
#'eq
)
749 (mtell (intl:gettext
"Acceptable answers are yes, y, Y, no, n, N. ~%"))
750 ($askequal a b
)))))))
752 (defmfun $asksign
(exp)
753 (let (sign minus odds evens factored
)
754 (asksign01 (cond (limitp (restorelim exp
))
755 ((among '$%i exp
) ($rectform exp
))
758 (defun asksign-p-or-n (e)
759 (unwind-protect (prog2
760 (assume `(($notequal
) ,e
0))
762 (forget `(($notequal
) ,e
0))))
765 (let ((e (sign-prep a
)))
766 (cond ((eq e
'$pnz
) '$pnz
)
767 ((member (setq e
(asksign1 e
)) '($pos $neg
) :test
#'eq
) e
)
768 (limitp (eps-sign a
))
771 ;; csign returns t if x appears to be complex.
772 ;; Else, it returns the sign.
774 (or (not (free x
'$%i
))
775 (let (sign-imag-errp limitp
) (catch 'sign-imag-err
($sign x
)))))
777 ;;; $csign works like $sign but switches the sign-functions into a complex
778 ;;; mode. In complex mode complex and imaginary expressions give the results
779 ;;; imagarinary or complex.
782 (let ((*complexsign
* t
)
787 (let ((x (specrepcheck x
))
788 sign minus odds evens factored
)
789 (sign01 (cond (limitp (restorelim x
))
791 ;; No rectform in Complex mode. Rectform ask unnecessary
792 ;; questions about complex expressions and can not handle
793 ;; imaginary expressions completely. Thus $csign can not
794 ;; handle something like (1+%i)*(1-%i) which is real.
795 ;; After improving rectform, we can change this. (12/2008)
797 (format t
"~&$SIGN with ~A~%" x
))
799 ((not (free x
'$%i
)) ($rectform x
))
803 (let ((e (sign-prep a
)))
804 (cond ((eq e
'$pnz
) '$pnz
)
805 (t (setq e
(sign1 e
))
806 (if (and limitp
(eq e
'$zero
)) (eps-sign a
) e
)))))
808 ;;; Preparation for asking questions from DEFINT or LIMIT.
811 (destructuring-let (((rpart . ipart
) (trisplit x
)))
812 (cond ((and (equal (sratsimp ipart
) 0)
813 (free rpart
'$infinity
))
814 (setq x
(nmr (sratsimp rpart
)))
815 (if (free x
'prin-inf
)
817 ($limit x
'prin-inf
'$inf
'$minus
)))
818 (t '$pnz
))) ; Confess ignorance if COMPLEX.
821 ;; don't ask about internal variables created by gruntz
822 (defun has-int-symbols (e)
823 (cond ((and (symbolp e
) (get e
'internal
))
826 (t (or (has-int-symbols (car e
))
827 (has-int-symbols (cdr e
))))))
829 ;;; Do substitutions for special symbols.
831 (unless (free a
'$zeroa
) (setq a
($limit a
'$zeroa
0 '$plus
)))
832 (unless (free a
'$zerob
) (setq a
($limit a
'$zerob
0 '$minus
)))
833 (unless (free a
'z
**) (setq a
($limit a
'z
** 0 '$plus
)))
834 (unless (free a
'*z
*) (setq a
($limit a
'*z
* 0 '$plus
)))
835 (unless (free a
'epsilon
) (setq a
($limit a
'epsilon
0 '$plus
)))
838 ;;; Get the sign of EPSILON-like terms. Could be made MUCH hairier.
840 (let (temp1 temp2 temp3 free1 free2 free3 limitp
)
841 ;; unset limitp to prevent infinite recursion
842 (cond ((not (free b
'$zeroa
))
843 (setq temp1
(eps-coef-sign b
'$zeroa
)))
845 (cond ((not (free b
'$zerob
))
846 (setq temp2
(eps-coef-sign b
'$zerob
)))
848 (cond ((not (free b
'epsilon
))
849 (setq temp3
(eps-coef-sign b
'epsilon
)))
851 (cond ((and free1 free2 free3
) '$zero
)
852 ((or (not (null temp1
)) (not (null temp2
)) (not (null temp3
)))
853 (cond ((and (null temp1
) (null temp2
)) temp3
)
854 ((and (null temp2
) (null temp3
)) temp1
)
855 ((and (null temp1
) (null temp3
)) temp2
)
856 (t (merror (intl:gettext
"asksign: internal error."))))))))
858 (defun eps-coef-sign (exp epskind
)
859 (let ((eps-power ($lopow exp epskind
)) eps-coef
)
860 (cond ((and (not (equal eps-power
0))
861 (not (equal (setq eps-coef
(ratcoeff exp epskind eps-power
))
863 (eq (ask-integer eps-power
'$integer
) '$yes
))
864 (cond ((eq (ask-integer eps-power
'$even
) '$yes
)
866 ((eq (ask-integer eps-power
'$odd
) '$yes
)
867 (setq eps-coef
($sign eps-coef
))
868 (cond ((or (and (eq eps-coef
'$pos
)
869 (or (eq epskind
'epsilon
)
870 (eq epskind
'$zeroa
)))
871 (and (eq eps-coef
'$neg
)
872 (or (alike epskind
(mul2* -
1 'epsilon
))
873 (eq epskind
'$zerob
))))
876 (t (merror (intl:gettext
"sign or asksign: insufficient information.")))))
877 (t (let ((deriv (sdiff exp epskind
)) deriv-sign
)
878 (cond ((not (eq (setq deriv-sign
($sign deriv
)) '$zero
))
879 (total-sign epskind deriv-sign
))
881 (eq (let ((deriv (sdiff deriv epskind
)))
882 (setq deriv-sign
($sign deriv
)))
885 (t (merror (intl:gettext
"sign or asksign: insufficient data.")))))))))
887 ;;; The above code does a partial Taylor series analysis of something
888 ;;; that isn't a polynomial.
890 (defun total-sign (epskind factor-sign
)
891 (cond ((or (eq epskind
'$zeroa
) (eq epskind
'epsilon
))
892 (cond ((eq factor-sign
'$pos
) '$pos
)
893 ((eq factor-sign
'$neg
) '$neg
)
894 ((eq factor-sign
'$zero
) '$zero
)))
895 ((eq epskind
'$zerob
)
896 (cond ((eq factor-sign
'$pos
) '$neg
)
897 ((eq factor-sign
'$neg
) '$pos
)
898 ((eq factor-sign
'$zero
) '$zero
)))))
901 (setq x
($asksign x
))
902 (cond ((eq '$pos x
) '$positive
)
903 ((eq '$neg x
) '$negative
)
904 ((eq '$pnz x
) '$pnz
) ;COMPLEX expression encountered here.
907 (defun asksign1 ($askexp
)
909 (declare (special $radexpand
))
912 ((has-int-symbols $askexp
) '$pnz
)
913 ((member sign
'($pos $neg $zero $imaginary
) :test
#'eq
) sign
)
915 (let ((domain sign
) (squared nil
))
918 (setq $askexp
(lmul evens
)
922 (if minus
(setq sign
(flip sign
)))
924 (lmul (nconc odds
(mapcar #'(lambda (l) (power l
2)) evens
))))))
925 (setq sign
(cdr (assol $askexp
*local-signs
*)))
926 (ensure-sign $askexp domain squared
)))))
928 (defun match-sign (sgn domain expression squared
)
929 "If SGN makes sense for DOMAIN store the result (see ENSURE-SIGN) and return
930 it. Otherwise, return NIL. If SQUARED is true, we are actually looking for the
931 sign of the square, so any negative results are converted to positive."
932 ;; The entries in BEHAVIOUR are of the form
933 ;; (MATCH DOMAINS REGISTRAR SIGN SIGN-SQ)
935 ;; The algorithm goes as follows:
937 ;; Look for SGN in MATCH. If found, use REGISTRAR to store SIGN for the
938 ;; expression and then return SIGN if SQUARED is false or SIGN-SQ if it is
941 '((($pos |$P| |$p| $positive
) (nil $znz $pz $pn $pnz
) tdpos $pos $pos
)
942 (($neg |$N| |$n| $negative
) (nil $znz $nz $pn $pnz
) tdneg $neg $pos
)
943 (($zero |$Z| |$z|
0 0.0) (nil $znz $pz $nz $pnz
) tdzero $zero $zero
)
944 (($pn $nonzero $nz $nonz $non0
) ($znz
) tdpn $pn $pos
)))
945 (hit (find-if (lambda (bh)
946 (and (member sgn
(first bh
) :test
#'equal
)
947 (member domain
(second bh
) :test
#'eq
)))
950 (let ((registrar (third hit
))
951 (found-sign (if squared
(fifth hit
) (fourth hit
))))
952 (funcall registrar expression
)
954 (if minus
(flip found-sign
) found-sign
))))))
956 (defun ensure-sign (expr &optional domain squared
)
957 "Try to determine the sign of EXPR. If DOMAIN is not one of the special values
958 described below, we try to tell whether EXPR is positive, negative or zero. It
959 can be more specialised ($pz => positive or zero; $nz => negative or zero; $pn
960 => positive or negative; $znz => zero or nonzero).
962 If SQUARED is true, then we're actually interested in the sign of EXPR^2. As
963 such, a nonzero sign should be regarded as positive.
965 When calling ENSURE-SIGN, set the special variable SIGN to the best current
966 guess for the sign of EXPR. The function returns the sign, calls one of (TDPOS
967 TDNEG TDZERO TDPN) to store it, and also sets SIGN."
969 (let ((new-sign (match-sign sign domain expr squared
)))
970 (when new-sign
(return new-sign
)))
976 '(($znz
" zero or nonzero?")
977 ($pz
" positive or zero?")
978 ($nz
" negative or zero?")
979 ($pn
" positive or negative?"))))
980 " positive, negative or zero?"))
983 ;; During one evaluation phase asksign writes answers from the user into the
984 ;; global context '$initial. These facts are removed by clearsign after
985 ;; finishing the evaluation phase. clearsign is called from the top-level
986 ;; evaluation function meval*. The facts which have to be removed are stored
987 ;; in the global variable *local-signs*.
990 (let ((context '$initial
))
991 (dolist (cons-pair *local-signs
*)
992 (destructuring-bind (x . sgn
) cons-pair
994 ((eq '$pos sgn
) (daddgr nil x
))
995 ((eq '$neg sgn
) (daddgr nil
(neg x
)))
996 ((eq '$zero sgn
) (daddeq nil x
))
997 ((eq '$pn sgn
) (daddnq nil x
))
998 ((eq '$pz sgn
) (daddgq nil x
))
999 ((eq '$nz sgn
) (daddgq nil
(neg x
))))))
1000 (setf *local-signs
* nil
)))
1003 (alike1 (specrepcheck x
) (specrepcheck y
)))
1005 (setf (get '$und
'sysconst
) t
)
1006 (setf (get '$ind
'sysconst
) t
)
1007 (setf (get '$zeroa
'sysconst
) t
)
1008 (setf (get '$zerob
'sysconst
) t
)
1010 ;; There have been some conversations about NaN on the list, but
1011 ;; the issue hasn't been settled.
1013 (defvar indefinites
`($und $ind
))
1015 ;; Other than sums, products, and lambda forms, meqp knows nothing
1016 ;; about dummy variables. Because of the way niceindices chooses names
1017 ;; for the sum indices, it's necessary to locally assign a new value to
1020 (defun meqp-by-csign (z a b
)
1021 (let (($niceindicespref
`((mlist) ,(gensym) ,(gensym) ,(gensym))))
1022 (setq z
($niceindices z
))
1023 (setq z
(if ($constantp z
) ($rectform z
) (sratsimp z
)))
1024 (let ((sgn ($csign z
))
1025 (dunno `(($equal
) ,a
,b
)))
1026 (cond ((eq '$zero sgn
) t
)
1027 ((memq sgn
'($pos $neg $pn
)) nil
)
1029 ;; previously checked also for (linearp z '$%i))
1030 ((memq sgn
'($complex $imaginary
))
1031 ;; We call trisplit here, which goes back to general evaluation and
1032 ;; could cause an infinite recursion. To make sure that doesn't
1033 ;; happen, use the with-safe-recursion macro.
1035 (with-safe-recursion meqp-by-csign z
1036 (let* ((ri-parts (trisplit z
))
1037 (rsgn ($csign
(car ri-parts
)))
1038 (isgn ($csign
(cdr ri-parts
))))
1039 (cond ((and (eq '$zero rsgn
)
1040 (eq '$zero isgn
)) t
)
1042 ((or (memq rsgn
'($neg $pos $pn
))
1043 (memq isgn
'($neg $pos $pn
))) nil
)
1046 (unsafe-recursion () dunno
)))
1050 ;; For each fact of the form equal(a,b) in the active context, do e : ratsubst(b,a,e).
1052 (defun equal-facts-simp (e)
1053 (let ((f (margs ($facts
))))
1055 (if (op-equalp fi
'$equal
)
1056 (setq e
($ratsubst
(nth 2 fi
) (nth 1 fi
) e
))))))
1058 (defun maxima-declared-arrayp (x)
1062 (get (mget x
'array
) 'array
)))
1064 (defun maxima-undeclared-arrayp (x)
1068 (get (mget x
'hashar
) 'array
)))
1071 ;; Check for some particular types before falling into the general case.
1073 (and (stringp b
) (equal a b
)))
1076 (and (arrayp b
) (array-meqp a b
)))
1078 ((maxima-declared-arrayp a
)
1079 (and (maxima-declared-arrayp b
) (maxima-declared-array-meqp a b
)))
1080 ((maxima-declared-arrayp b
) nil
)
1081 ((maxima-undeclared-arrayp a
)
1082 (and (maxima-undeclared-arrayp b
) (maxima-undeclared-array-meqp a b
)))
1083 ((maxima-undeclared-arrayp b
) nil
)
1086 (setq a
(specrepcheck a
))
1087 (setq b
(specrepcheck b
))
1088 (cond ((or (like a b
)) (not (member a indefinites
)))
1089 ((or (member a indefinites
) (member b indefinites
)
1090 (member a infinities
) (member b infinities
)) nil
)
1091 ((and (symbolp a
) (or (eq t a
) (eq nil a
) (get a
'sysconst
))
1092 (symbolp b
) (or (eq t b
) (eq nil b
) (get b
'sysconst
))) nil
)
1093 ((or (mbagp a
) (mrelationp a
) (mbagp b
) (mrelationp b
))
1094 (cond ((and (or (and (mbagp a
) (mbagp b
)) (and (mrelationp a
) (mrelationp b
)))
1095 (eq (mop a
) (mop b
)) (= (length (margs a
)) (length (margs b
))))
1096 (setq z
(list-meqp (margs a
) (margs b
)))
1097 (if (or (eq z t
) (eq z nil
)) z
`(($equal
) ,a
,b
)))
1099 ((and (op-equalp a
'lambda
) (op-equalp b
'lambda
)) (lambda-meqp a b
))
1100 (($setp a
) (set-meqp a b
))
1101 ;; 0 isn't in the range of an exponential function.
1102 ((or (and (mexptp a
) (not (eq '$minf
(third a
))) (zerop1 b
) (eq t
(mnqp (second a
) 0)))
1103 (and (mexptp b
) (not (eq '$minf
(third b
))) (zerop1 a
) (eq t
(mnqp (second b
) 0))))
1106 ;; DCOMPARE emits new stuff (via DINTERNP) into the assume database.
1107 ;; Let's avoid littering the database with numbers.
1108 ((and (mnump a
) (mnump b
)) (zerop1 (sub a b
)))
1110 ;; lookup in assumption database
1111 ((and (dcompare a b
) (eq '$zero sign
))) ; dcompare sets sign
1112 ((memq sign
'($pos $neg $pn
)) nil
)
1114 ;; if database lookup failed, apply all equality facts
1115 (t (meqp-by-csign (equal-facts-simp (sratsimp (sub a b
))) a b
)))))))
1117 ;; Two arrays are equal (according to MEQP)
1118 ;; if (1) they have the same dimensions,
1119 ;; and (2) their elements are MEQP.
1121 (defun array-meqp (p q
)
1123 (equal (array-dimensions p
) (array-dimensions q
))
1125 (dotimes (i (array-total-size p
))
1126 (let ((z (let ($ratprint
)
1127 (declare (special $ratprint
))
1128 (meqp (row-major-aref p i
) (row-major-aref q i
)))))
1129 (cond ((eq z nil
) (return-from array-meqp nil
))
1131 (t (return-from array-meqp
`(($equal
) ,p
,q
))))))
1134 (defun maxima-declared-array-meqp (p q
)
1135 (array-meqp (get (mget p
'array
) 'array
) (get (mget q
'array
) 'array
)))
1137 (defun maxima-undeclared-array-meqp (p q
)
1139 (alike1 (mfuncall '$arrayinfo p
) (mfuncall '$arrayinfo q
))
1141 (declare (special $ratprint
))
1142 (meqp ($listarray p
) ($listarray q
)))))
1144 (defun list-meqp (p q
)
1146 (cond ((or (null p
) (null q
)) (and (null p
) (null q
)))
1148 (setq z
(meqp (car p
) (car q
)))
1149 (cond ((eq z nil
) nil
)
1150 ((or (eq z
'$unknown
) (op-equalp z
'$equal
)) z
)
1151 (t (list-meqp (cdr p
) (cdr q
))))))))
1153 (defun lambda-meqp (a b
)
1155 (cond ((= (length (second a
)) (length (second b
)))
1156 (let ((x) (n ($length
(second a
))))
1157 (dotimes (i n
(push '(mlist) x
)) (push (gensym) x
))
1158 (setq z
(meqp (mfuncall '$apply a x
) (mfuncall '$apply b x
)))
1159 (if (or (eq t z
) (eq nil z
)) z
`(($equal
) ,a
,b
))))
1162 (defun set-meqp (a b
)
1163 (let ((aa (equal-facts-simp a
))
1164 (bb (equal-facts-simp b
)))
1165 (cond ((or (not ($setp bb
))
1166 (and ($emptyp aa
) (not ($emptyp bb
)))
1167 (and ($emptyp bb
) (not ($emptyp aa
))))
1169 ((and (= (length aa
) (length bb
))
1170 (every #'(lambda (p q
) (eq t
(meqp p q
))) (margs aa
) (margs bb
))) t
)
1171 ((set-not-eqp (margs aa
) (margs bb
)) nil
)
1172 (t `(($equal
,a
,b
))))))
1174 (defun set-not-eqp (a b
)
1177 (if (every #'(lambda (s) (eq nil
(meqp ak s
))) b
) (throw 'done t
)))
1179 (if (every #'(lambda (s) (eq nil
(meqp bk s
))) a
) (throw 'done t
)))
1183 (let ((*complexsign
* t
))
1185 (let ((sgn (csign a
)))
1186 (cond ((eq sgn
'$pos
) t
)
1187 ((eq sgn t
) nil
) ;; csign thinks a - b isn't real
1188 ((member sgn
'($neg $zero $nz
) :test
#'eq
) nil
)
1189 (t `((mgreaterp) ,a
0))))))
1195 (let ((*complexsign
* t
))
1197 (let ((sgn (csign a
)))
1198 (cond ((member sgn
'($pos $zero $pz
) :test
#'eq
) t
)
1199 ((eq sgn t
) nil
) ;; csign thinks a - b isn't real
1200 ((eq sgn
'$neg
) nil
)
1201 (t `((mgeqp) ,a
0))))))
1204 (let ((b (meqp x y
)))
1205 (cond ((eq b
'$unknown
) b
)
1206 ((or (eq b t
) (eq b nil
)) (not b
))
1207 (t `(($notequal
) ,x
,y
)))))
1210 (list '(mnot) (c-$zero o e
)))
1212 (defun c-$zero
(o e
)
1213 (list '($equal
) (lmul (nconc o e
)) 0))
1216 (cond ((null o
) (list '(mnot) (list '($equal
) (lmul e
) 0)))
1217 ((null e
) (list '(mgreaterp) (lmul o
) 0))
1218 (t (setq e
(mapcar #'(lambda (l) (power l
2)) e
))
1219 (list '(mgreaterp) (lmul (nconc o e
)) 0))))
1222 (cond ((null o
) (list '(mnot) (list '($equal
) (lmul e
) 0)))
1223 ((null e
) (list '(mgeqp) (lmul o
) 0))
1224 (t (setq e
(mapcar #'(lambda (l) (power l
2)) e
))
1225 (list '(mgeqp) (lmul (nconc o e
)) 0))))
1228 (let (sign minus odds evens
)
1232 (if (or (atom e
) (and (free e
'$inf
) (free e
'$minf
)))
1236 ;; Like WITH-COMPSPLT, but runs COMPSPLT-EQ instead
1237 (defmacro with-compsplt-eq
((lhs rhs x
) &body forms
)
1238 `(multiple-value-bind (,lhs
,rhs
) (compsplt-eq ,x
)
1241 ;; Call FORMS with LHS and RHS bound to the splitting of EXPR by COMPSPLT.
1242 (defmacro with-compsplt
((lhs rhs expr
) &body forms
)
1243 `(multiple-value-bind (,lhs
,rhs
) (compsplt ,expr
)
1247 (setq x
(specrepcheck x
))
1248 (setq x
(infsimp* x
))
1249 (when (and *complexsign
* (atom x
) (eq x
'$infinity
))
1250 ;; In Complex Mode the sign of infinity is complex.
1251 (when *debug-compar
* (format t
"~& in sign1 detect $infinity.~%"))
1252 (return-from sign1
'$complex
))
1253 (if (member x
'($und $ind $infinity
) :test
#'eq
)
1254 (if limitp
'$pnz
(merror (intl:gettext
"sign: sign of ~:M is undefined.") x
)))
1256 (setq dum
(constp x
) exp x
)
1257 (cond ((or (numberp x
) (ratnump x
)))
1259 (if (prog2 (setq dum
($bfloat x
)) ($bfloatp dum
))
1262 (if (and (setq dum
(numer x
)) (numberp dum
)) (setq exp dum
)))
1263 ((and (member dum
'(numer symbol
) :test
#'eq
)
1264 (prog2 (setq dum
(numer x
))
1267 (prog2 (setq exp dum
)
1268 (< (abs dum
) 1.0e-6))))))
1270 (and (setq dum
($bfloat x
)) ($bfloatp dum
) (setq exp dum
)))
1271 (t (setq sign
'$pnz evens nil odds
(ncons x
) minus nil
)
1273 (or (and (not (atom x
)) (not (mnump x
)) (equal x exp
)
1275 (with-compsplt (lhs rhs x
)
1277 (cond ((member sign
'($pos $neg
) :test
#'eq
))
1278 ((eq sign
'$pnz
) nil
)
1279 (t (setq s sign o odds e evens m minus
)
1281 (if (not (strongp sign s
))
1282 (if (and (eq sign
'$pnz
) (eq s
'$pn
))
1284 (setq sign s odds o evens e minus m
)))
1290 (let (($numer t
) ; currently, no effect on $float, but proposed to
1293 ;; Catch a Lisp error, if a floating point overflow occurs.
1294 (setq result
(let ((errset nil
)) (errset ($float x
))))
1295 (if result
(car result
) nil
)))
1298 (cond ((floatp x
) 'float
)
1299 ((numberp x
) 'numer
)
1300 ((symbolp x
) (if (member x
'($%pi $%e $%phi $%gamma
) :test
#'eq
) 'symbol
))
1302 ((eq (caar x
) 'rat
) 'numer
)
1303 ((eq (caar x
) 'bigfloat
) 'bigfloat
)
1304 ((specrepp x
) (constp (specdisrep x
)))
1305 (t (do ((l (cdr x
) (cdr l
)) (dum) (ans 'numer
))
1307 (setq dum
(constp (car l
)))
1308 (cond ((eq dum
'float
) (return 'float
))
1310 ((eq dum
'bigfloat
) (setq ans
'bigfloat
))
1312 (if (eq ans
'numer
) (setq ans
'symbol
)))
1313 (t (return nil
)))))))
1315 (mapcar #'(lambda (s) (putprop (first s
) (second s
) 'sign-function
))
1317 (list 'mtimes
'sign-mtimes
)
1318 (list 'mplus
'sign-mplus
)
1319 (list 'mexpt
'sign-mexpt
)
1320 (list '%log
'sign-log
)
1321 (list 'mabs
'sign-mabs
)
1322 (list '$min
#'(lambda (x) (sign-minmax (caar x
) (cdr x
))))
1323 (list '$max
#'(lambda (x) (sign-minmax (caar x
) (cdr x
))))
1324 (list '%csc
#'(lambda (x) (sign (inv* (cons (ncons (zl-get (caar x
) 'recip
)) (cdr x
))))))
1325 (list '%csch
#'(lambda (x) (sign (inv* (cons (ncons (zl-get (caar x
) 'recip
)) (cdr x
))))))
1327 (list '%signum
#'(lambda (x) (sign (cadr x
))))
1328 (list '%erf
#'(lambda (x) (sign (cadr x
))))
1329 (list '$li
#'(lambda (x)
1330 (let ((z (first (margs x
))) (n (cadadr x
)))
1331 (if (and (mnump n
) (eq t
(mgrp z
0)) (eq t
(mgrp 1 z
))) (sign z
) (sign-any x
)))))))
1333 (cond ((mnump x
) (setq sign
(rgrp x
0) minus nil odds nil evens nil
))
1334 ((and *complexsign
* (symbolp x
) (eq x
'$%i
))
1335 ;; In Complex Mode the sign of %i is $imaginary.
1336 (setq sign
'$imaginary
))
1337 ((symbolp x
) (if (eq x
'$%i
) (imag-err x
)) (sign-any x
))
1338 ((and (consp x
) (symbolp (caar x
)) (not (specrepp x
)) (get (caar x
) 'sign-function
))
1339 (funcall (get (caar x
) 'sign-function
) x
))
1340 ((and (consp x
) (not (specrepp x
)) ($subvarp
(mop x
)) (get (mop (mop x
)) 'sign-function
))
1341 (funcall (get (mop (mop x
)) 'sign-function
) x
))
1342 ((specrepp x
) (sign (specdisrep x
)))
1343 ((kindp (caar x
) '$posfun
) (sign-posfun x
))
1344 ((kindp (caar x
) '$oddfun
) (sign-oddfun x
))
1348 (cond ((and *complexsign
*
1351 ;; In Complex Mode look for symbols declared to be complex.
1352 (if ($featurep x
'$imaginary
)
1353 (setq sign
'$imaginary
)
1354 (setq sign
'$complex
)))
1357 (decl-complexp (caar x
)))
1358 ;; A function f(x), where f is declared to be imaginary or complex.
1359 (if ($featurep
(caar x
) '$imaginary
)
1360 (setq sign
'$imaginary
)
1361 (setq sign
'$complex
)))
1364 (if (and $assume_pos
1365 (member sign
'($pnz $pz $pn
) :test
#'eq
)
1366 (if $assume_pos_pred
1368 (declare (special *x
*))
1369 (is '(($assume_pos_pred
) *x
*)))
1372 (setq minus nil evens nil
1373 odds
(if (not (member sign
'($pos $neg $zero
) :test
#'eq
))
1376 (defun sign-mtimes (x)
1378 (do ((s '$pos
) (m) (o) (e)) ((null x
) (setq sign s minus m odds o evens e
))
1380 (cond ((eq sign
'$zero
) (return t
))
1381 ((and *complexsign
* (eq sign
'$complex
))
1382 ;; Found a complex factor. We don't return immediately
1383 ;; because another factor could be zero.
1385 ((and *complexsign
* (eq s
'$complex
))) ; continue the loop
1386 ((and *complexsign
* (eq sign
'$imaginary
))
1387 ;; Found an imaginary factor. Look if we have already one.
1388 (cond ((eq s
'$imaginary
)
1389 ;; imaginary*imaginary is real. But remember the sign in m.
1390 (setq s
(if m
'$pos
'$neg
) m
(not m
)))
1392 ((and *complexsign
* (eq s
'$imaginary
))) ; continue the loop
1394 ((eq sign
'$neg
) (setq s
(flip s
) m
(not m
)))
1395 ((prog2 (setq m
(not (eq m minus
)) o
(nconc odds o
) e
(nconc evens e
))
1397 ((eq s sign
) (when (eq s
'$nz
) (setq s
'$pz
)))
1398 ((eq s
'$pos
) (setq s sign
))
1399 ((eq s
'$neg
) (setq s
(flip sign
)))
1400 ((or (and (eq s
'$pz
) (eq sign
'$nz
))
1401 (and (eq s
'$nz
) (eq sign
'$pz
)))
1406 (defun sign-mplus (x &aux s o e m
)
1407 (cond ((signdiff x
))
1408 ((prog2 (setq s sign e evens o odds m minus
) nil
))
1410 ((prog2 (cond ((strongp s sign
))
1411 (t (setq s sign e evens o odds m minus
)))
1413 ((and (not factored
) (signfactor x
)))
1415 (t (setq sign s evens e odds o minus m
))))
1419 (let ((swapped nil
) (retval))
1420 (with-compsplt (lhs rhs x
)
1421 (if (and (mplusp lhs
) (equal rhs
0) (null (cdddr lhs
)))
1422 (cond ((and (negp (cadr lhs
)) (not (negp (caddr lhs
))))
1423 (setq rhs
(neg (cadr lhs
)) lhs
(caddr lhs
)))
1424 ;; The following fixes SourceForge bug #3148
1425 ;; where sign(a-b) returned pnz and sign(b-a) returned pos.
1426 ;; Previously, only the case (-a)+b was handled.
1427 ;; Now we also handle a+(-b) by swapping lhs and rhs,
1428 ;; setting a flag "swapped", running through the same code and
1429 ;; then flipping the answer.
1430 ((and (negp (caddr lhs
)) (not (negp (cadr lhs
))))
1431 (setq rhs
(cadr lhs
) lhs
(neg (caddr lhs
)) swapped t
))))
1434 (cond ((or (equal rhs
0) (mplusp lhs
)) nil
)
1435 ((and (member (constp rhs
) '(numer symbol
) :test
#'eq
)
1436 (numberp (setq dum
(numer rhs
)))
1437 (prog2 (setq rhs dum
) nil
)))
1439 ((and (dcompare lhs rhs
) (member sign
'($pos $neg $zero
) :test
#'eq
)))
1440 ((and (not (atom lhs
)) (not (atom rhs
))
1441 (eq (caar lhs
) (caar rhs
))
1442 (kindp (caar lhs
) '$increasing
))
1443 (sign (sub (cadr lhs
) (cadr rhs
)))
1445 ((and (not (atom lhs
)) (not (atom rhs
))
1446 (eq (caar lhs
) (caar rhs
))
1447 (kindp (caar lhs
) '$decreasing
))
1448 (sign (sub (cadr rhs
) (cadr lhs
)))
1450 ((and (not (atom lhs
)) (eq (caar lhs
) 'mabs
)
1451 (alike1 (cadr lhs
) rhs
))
1452 (setq sign
'$pz minus nil odds nil evens nil
) t
)
1453 ((signdiff-special lhs rhs
))))))
1455 (setq sign
(flip sign
)))
1458 (defun signdiff-special (xlhs xrhs
)
1459 ;; xlhs may be a constant
1461 (when (or (and (realp xrhs
) (minusp xrhs
)
1462 (not (atom xlhs
)) (eq (sign* xlhs
) '$pos
))
1463 ; e.g. sign(a^3+%pi-1) where a>0
1465 ;; e.g. sign(%e^x-1) where x>0
1466 (eq (sign* (caddr xlhs
)) '$pos
)
1468 ;; Q^Rpos - S, S<=1, Q>1
1469 (member (sign* (sub 1 xrhs
)) '($pos $zero $pz
) :test
#'eq
)
1470 (eq (sign* (sub (cadr xlhs
) 1)) '$pos
))
1471 (and (not (eq $domain
'$complex
))
1472 ;; Qpos ^ Rpos - Spos => Qpos - Spos^(1/Rpos)
1473 (eq (sign* (cadr xlhs
)) '$pos
)
1474 (eq (sign* xrhs
) '$pos
)
1475 (eq (sign* (sub (cadr xlhs
)
1476 (power xrhs
(div 1 (caddr xlhs
)))))
1478 (and (mexptp xlhs
) (mexptp xrhs
)
1479 ;; Q^R - Q^T, Q>1, (R-T) > 0
1480 ;; e.g. sign(2^x-2^y) where x>y
1481 (alike1 (cadr xlhs
) (cadr xrhs
))
1482 (eq (sign* (sub (cadr xlhs
) 1)) '$pos
)
1483 (eq (sign* (sub (caddr xlhs
) (caddr xrhs
))) '$pos
)))
1487 (when (and (not (atom xlhs
))
1488 (member (caar xlhs
) '(%sin %cos
))
1489 (zerop1 ($imagpart
(cadr xlhs
))))
1490 (cond ((eq (sign* (add xrhs
1)) '$neg
) ;; c > 1
1492 ((eq (sign* (add xrhs -
1)) '$pos
) ;; c < -1
1494 ((zerop1 (add xrhs
1)) ;; c = 1
1496 ((zerop1 (add xrhs -
1)) ;; c = -1
1499 (when (and $useminmax
(or (minmaxp xlhs
) (minmaxp xrhs
)))
1500 (setq sgn
(signdiff-minmax xlhs xrhs
)))
1501 (when sgn
(setq sign sgn minus nil odds nil evens nil
)
1504 ;;; Look for symbols with an assumption a > n or a < -n, where n is a number.
1505 ;;; For this case shift the symbol a -> a+n in a summation and multiplication.
1506 ;;; This handles cases like a>1 and b>1 gives sign(a+b-2) -> pos.
1508 (defun sign-shift (expr)
1509 (do ((l (cdr expr
) (cdr l
))
1512 ((null l
) (addn acc nil
))
1513 (cond ((symbolp (car l
))
1514 ;; Get the facts related to the symbol (car l)
1515 ;; Reverse the order to test the newest facts first.
1516 (setq fl
(reverse (cdr (facts1 (car l
)))))
1519 (cond ((and (eq (caar f
) 'mgreaterp
)
1521 (eq ($sign
(caddr f
)) '$pos
))
1522 ;; The case a > n, where a is a symbol and n a number.
1523 ;; Add the number to the list of terms.
1524 (return (push (caddr f
) acc
)))
1525 ((and (eq (caar f
) 'mgreaterp
)
1527 (eq ($sign
(cadr f
)) '$neg
))
1528 ;; The case a < -n, where a is a symbol and n a number.
1529 ;; Add the number to the list of terms.
1530 (return (push (cadr f
) acc
))))))
1532 (let ((acctimes) (flag))
1533 ;; Go through the factors of the multiplication.
1534 (dolist (ll (cdar l
))
1536 ;; Get the facts related to the symbol (car l)
1537 ;; Reverse the order to test the newest facts first.
1538 (setq fl
(reverse (cdr (facts1 ll
))))
1540 (cond ((and (eq (caar f
) 'mgreaterp
)
1542 (eq ($sign
(caddr f
)) '$pos
))
1543 ;; The case a > n, where a is a symbol and n a
1544 ;; number. Add the number to the list of terms.
1546 (return (push (add ll
(caddr f
)) acctimes
)))
1547 ((and (eq (caar f
) 'mgreaterp
)
1549 (eq ($sign
(cadr f
)) '$neg
))
1550 ;; The case a < -n, where a is a symbol and n a
1551 ;; number. Add the number to the list of terms.
1553 (return (push (add ll
(cadr f
)) acctimes
)))))
1554 (when (not flag
) (push ll acctimes
)))
1556 (push ll acctimes
))))
1558 ;; If a shift has been done expand the factors.
1559 (push ($multthru
(muln acctimes nil
)) acc
)
1560 (push (muln acctimes nil
) acc
))))
1562 (push (car l
) acc
)))))
1565 (setq x
(sign-shift x
))
1566 ;; x might be simplified to an atom in sign-shift.
1567 (when (atom x
) (setq x
(cons '(mplus) (list x
))))
1568 (do ((l (cdr x
) (cdr l
)) (s '$zero
))
1569 ((null l
) (setq sign s minus nil odds
(list x
) evens nil
)
1570 (cond (*complexsign
*
1571 ;; Because we have continued the loop in Complex Mode
1572 ;; we have to look for the sign '$pnz and return nil.
1573 (if (eq s
'$pnz
) nil t
))
1574 (t t
))) ; in Real Mode return T
1575 ;; Call sign1 and not sign, because sign1 handles constant expressions.
1577 (cond ((and *complexsign
*
1578 (or (eq sign
'$complex
) (eq sign
'$imaginary
)))
1579 ;; Found a complex or imaginary expression. The sign is $complex.
1580 (setq sign
'$complex odds nil evens nil minus nil
)
1582 ((or (and (eq sign
'$zero
)
1583 (setq x
(sub x
(car l
))))
1584 (and (eq s sign
) (not (eq s
'$pn
))) ; $PN + $PN = $PNZ
1585 (and (eq s
'$pos
) (eq sign
'$pz
))
1586 (and (eq s
'$neg
) (eq sign
'$nz
))))
1587 ((or (and (member sign
'($pz $pos
) :test
#'eq
) (member s
'($zero $pz
) :test
#'eq
))
1588 (and (member sign
'($nz $neg
) :test
#'eq
) (member s
'($zero $nz
) :test
#'eq
))
1589 (and (eq sign
'$pn
) (eq s
'$zero
)))
1592 (cond (*complexsign
*
1593 ;; In Complex Mode we have to continue the loop to look further
1594 ;; for a complex or imaginay expression.
1597 ;; In Real mode the loop stops when the sign is 'pnz.
1598 (setq sign
'$pnz odds
(list x
) evens nil minus nil
)
1601 (defun signfactor (x)
1602 (let (y (factored t
))
1603 (setq y
(factor-if-small x
))
1604 (cond ((or (mplusp y
) (> (conssize y
) 50.
))
1609 (defun factor-if-small (x)
1610 (if (< (conssize x
) 51.
)
1612 (declare (special $ratprint
))
1615 (defun sign-mexpt (x)
1616 (let* ((expt (caddr x
)) (base1 (cadr x
))
1617 (sign-expt (sign1 expt
)) (sign-base (sign1 base1
))
1619 (cond ((and *complexsign
* (or (eq sign-expt
'$complex
)
1620 (eq sign-expt
'$imaginary
)
1621 (eq sign-base
'$complex
)))
1622 ;; Base is complex or exponent is complex or imaginary.
1623 ;; The sign is $complex.
1624 (when *debug-compar
*
1625 (format t
"~&in SIGN-MEXPT for ~A, sign is complex.~%" x
))
1626 (setq sign
'$complex
))
1629 (eq sign-base
'$neg
)
1630 (eq (evod ($expand
(mul 2 expt
))) '$odd
))
1631 ;; Base is negative and the double of the exponent is odd.
1632 ;; Result is imaginary.
1633 (when *debug-compar
*
1634 (format t
"~&in SIGN-MEXPT for ~A, sign is $imaginary.~%" x
))
1635 (setq sign
'$imaginary
))
1638 (eq sign-base
'$imaginary
))
1639 ;; An imaginary base. Look for even or odd exponent.
1640 (when *debug-compar
*
1641 (format t
"~&in SIGN-MEXPT for ~A, base is $imaginary.~%" x
))
1643 ((and (integerp expt
) (eq evod
'$even
))
1644 (setq sign
(if (eql (mod expt
4) 0) '$pz
'$nz
)))
1645 ((and (integerp expt
) (eq evod
'$odd
))
1646 (setq sign
'$imaginary
1647 minus
(if (eql (mod (- expt
1) 4) 0) t nil
)))
1648 (t (setq sign
'$complex
))))
1650 ((and (eq sign-base
'$zero
)
1651 (member sign-expt
'($zero $neg
) :test
#'eq
))
1653 ((eq sign-expt
'$zero
) (setq sign
'$pos
))
1654 ((eq sign-base
'$pos
))
1655 ((eq sign-base
'$zero
))
1657 (cond ((eq sign-expt
'$neg
)
1658 (setq sign
'$pos minus nil evens
(ncons base1
) odds nil
))
1659 ((member sign-base
'($pn $neg
) :test
#'eq
)
1660 (setq sign
'$pos minus nil
1661 evens
(nconc odds evens
)
1663 (t (setq sign
'$pz minus nil
1664 evens
(nconc odds evens
)
1666 ((and (member sign-expt
'($neg $nz
) :test
#'eq
)
1667 (member sign-base
'($nz $pz $pnz
) :test
#'eq
))
1668 (setq sign
(cond ((eq sign-base
'$pnz
) '$pn
)
1669 ((eq sign-base
'$pz
) '$pos
)
1670 ((eq sign-expt
'$neg
) '$neg
)
1672 ((member sign-expt
'($pz $nz $pnz
) :test
#'eq
)
1673 (cond ((eq sign-base
'$neg
)
1674 (setq odds
(ncons x
) sign
'$pn
))))
1675 ((eq sign-expt
'$pn
))
1677 (cond ((mevenp (cadr expt
))
1678 (cond ((member sign-base
'($pn $neg
) :test
#'eq
)
1679 (setq sign-base
'$pos
))
1680 ((member sign-base
'($pnz $nz
) :test
#'eq
)
1681 (setq sign-base
'$pz
)))
1682 (setq evens
(nconc odds evens
)
1683 odds nil minus nil
))
1684 ((mevenp (caddr expt
))
1685 (cond ((and *complexsign
* (eq sign-base
'$neg
))
1686 ;; In Complex Mode the sign is $complex.
1687 (setq sign-base
(setq sign-expt
'$complex
)))
1689 ;; The only place the variable complexsign
1690 ;; is used. Unfortunately, one routine in
1691 ;; to_poly.lisp in /share/to_poly_solve depends on
1692 ;; this piece of code. Perhaps we can remove
1693 ;; the dependency. (12/2008)
1694 (setq sign-base
(setq sign-expt
'$pnz
)))
1695 ((eq sign-base
'$neg
) (imag-err x
))
1696 ((eq sign-base
'$pn
)
1697 (setq sign-base
'$pos
))
1698 ((eq sign-base
'$nz
)
1699 (setq sign-base
'$zero
))
1700 (t (setq sign-base
'$pz
)))))
1701 (cond ((eq sign-expt
'$neg
)
1702 (cond ((eq sign-base
'$zero
) (dbzs-err x
))
1703 ((eq sign-base
'$pz
)
1704 (setq sign-base
'$pos
))
1705 ((eq sign-base
'$nz
)
1706 (setq sign-base
'$neg
))
1707 ((eq sign-base
'$pnz
)
1708 (setq sign-base
'$pn
)))))
1709 (setq sign sign-base
))
1710 ((eq sign-base
'$pos
)
1712 ((eq sign-base
'$neg
)
1715 (setq sign
(if *complexsign
* '$complex
'$pn
)))))))
1717 ;;; Determine the sign of log(expr). This function changes the special variable sign.
1722 (cond ((eq t
(mgrp x
0))
1723 (cond ((eq t
(mgrp 1 x
)) '$neg
)
1724 ((eq t
(meqp x
1)) '$zero
);; log(1) = 0.
1725 ((eq t
(mgqp 1 x
)) '$nz
)
1726 ((eq t
(mgrp x
1)) '$pos
)
1727 ((eq t
(mgqp x
1)) '$pz
)
1728 ((eq t
(mnqp x
1)) '$pn
)
1730 ((and *complexsign
* (eql 1 (cabs x
))) '$imaginary
)
1731 (*complexsign
* '$complex
)
1734 (defun sign-mabs (x)
1735 (let ((*complexsign
* t
))
1737 (cond ((member sign
'($pos $zero
) :test
#'eq
))
1738 ((member sign
'($neg $pn
) :test
#'eq
) (setq sign
'$pos
))
1739 (t (setq sign
'$pz minus nil evens
(nconc odds evens
) odds nil
)))))
1743 ;;; Macros used in simp min/max
1744 ;;; If op is min, use body; if not, negate sign constants in body
1745 ;;; Used to avoid writing min and max code separately: just write the min code
1746 ;;; in such a way that its dual works for max
1747 (defmacro minmaxforms
(op &rest body
)
1750 ,@(sublis '(($neg . $pos
)
1765 (defun sign-minmax (op args
)
1766 (do ((sgn (minmaxforms op
'$pos
) ;identity element for min
1767 (sminmax op sgn
(sign* (car l
))))
1768 (end (minmaxforms op
'$neg
))
1770 ((or (null l
) (eq sgn end
))
1772 odds
(if (not (member sgn
'($pos $neg $zero
) :test
#'eq
))
1773 (ncons (cons (list op
) args
)))
1777 ;; sign(op(a,b)) = sminmax(sign(a),sign(b))
1778 ;; op is $min/$max; s1/s2 in neg, nz, zero, pz, pos, pn, pnz
1779 (defun sminmax (op s1 s2
)
1782 ;; Many of these cases don't come up in simplified expressions,
1783 ;; since e.g. sign(a)=neg and sign(b)=pos implies min(a,b)=a
1784 ;; the order of these clauses is important
1785 (cond ((eq s1
'$pos
) s2
)
1788 ((or (eq s1
'$neg
) (eq s2
'$neg
)) '$neg
)
1789 ((or (eq s1
'$nz
) (eq s2
'$nz
)) '$nz
)
1790 ((eq s1
'$zero
) (if (eq s2
'$pz
) '$zero
'$nz
))
1791 ((eq s2
'$zero
) (if (eq s1
'$pz
) '$zero
'$nz
))
1795 (cond ((atom ex
) nil
)
1796 ((member (caar ex
) '($min $max
) :test
#'eq
) (caar ex
))
1799 (defun signdiff-minmax (l r
)
1800 ;; sign of l-r; nil if unknown (not PNZ)
1801 (let* ((lm (minmaxp l
))
1803 (ll (if lm
(cdr l
)))
1804 (rr (if rm
(cdr r
)))) ;distinguish between < and <= argument lists of min/max
1807 (cond ((eq lm rm
) ; min(a,...) - min(b,...)
1808 (multiple-value-bind (both onlyl onlyr
) (intersect-info ll rr
)
1809 (declare (ignore both
))
1810 (cond ((null onlyl
) '$pz
) ; min(a,b) - min(a,b,c)
1811 ((null onlyr
) '$nz
) ; min(a,b,c) - min(a,b)
1812 ;; TBD: add processing for full onlyl/onlyr case
1814 ;; TBD: memalike and set-disjointp are crude approx.
1815 ((null lm
) (if (memalike l rr
) '$pz
)) ; a - min(a,b)
1816 ((null rm
) (if (memalike r ll
) '$nz
)) ; min(a,b) - a
1817 (t ; min/max or max/min
1818 (if (not (set-disjointp ll rr
)) '$pz
)))))) ; max(a,q,r) - min(a,s,t)
1820 (defun intersect-info (a b
)
1828 #'(lambda (x) (push x both
))
1829 #'(lambda (x) (push x onlya
))
1830 #'(lambda (x) (push x onlyb
)))
1836 ;;; end compare min/max
1838 (defun sign-posfun (xx)
1839 (declare (ignore xx
))
1845 (defun sign-oddfun (x)
1846 (cond ((kindp (caar x
) '$increasing
)
1847 ; Take the sign of the argument
1849 ((kindp (caar x
) '$decreasing
)
1850 ; Take the sign of negative of the argument
1851 (sign (neg (cadr x
))))
1853 ; If the sign of the argument is zero, then we're done (the sign of
1854 ; the function value is the same). Otherwise, punt to SIGN-ANY.
1856 (unless (eq sign
'$zero
)
1861 (merror (intl:gettext
"sign: argument cannot be imaginary; found ~M") x
)
1862 (throw 'sign-imag-err t
)))
1865 (merror (intl:gettext
"sign: division by zero in ~M") x
))
1867 ;; Return true iff e is an expression with operator op1, op2,...,or opn.
1869 (defun op-equalp (e &rest op
)
1870 (and (consp e
) (consp (car e
)) (some #'(lambda (s) (equal (caar e
) s
)) op
)))
1872 ;; Return true iff the operator of a is a Maxima relation operator.
1874 (defun mrelationp (a)
1875 (op-equalp a
'mlessp
'mleqp
'mequal
'mnotequal
'mgeqp
'mgreaterp
))
1877 ;; This version of featurep applies ratdisrep to the first argument. This
1878 ;; change allows things like featurep(rat(n),integer) --> true when n has
1879 ;; been declared an integer.
1881 (defmfun $featurep
(e ind
)
1882 (setq e
($ratdisrep e
))
1883 (cond ((not (symbolp ind
))
1885 (intl:gettext
"featurep: second argument must be a symbol; found ~M")
1887 ;; Properties not related to the assume database.
1888 ((and (member ind opers
) (safe-get e ind
)))
1889 ((and (member ind
'($evfun $evflag $bindtest $nonarray
))
1890 (safe-get e
(stripdollar ind
))))
1891 ((and (eq ind
'$noun
)
1892 (safe-get e
(stripdollar ind
))
1894 ((and (member ind
'($scalar $nonscalar $mainvar
))
1896 ((and (eq ind
'$feature
)
1897 (member e $features
)
1899 ((eq ind
'$alphabetic
)
1900 (dolist (l (coerce e
'list
) t
)
1901 (when (not (member l
*alphabet
*)) (return nil
))))
1902 ;; Properties related to the assume database.
1903 ((eq ind
'$integer
) (maxima-integerp e
))
1904 ((eq ind
'$noninteger
) (nonintegerp e
))
1905 ((eq ind
'$even
) (mevenp e
))
1906 ((eq ind
'$odd
) (moddp e
))
1909 (or (numberp e
) (kindp e
'$real
) (numberp (numer e
)))
1910 (free ($rectform e
) '$%i
)))
1911 ((symbolp e
) (kindp e ind
))))
1913 ;; Give a function the maps-integers-to-integers property when it is integer
1914 ;; valued on the integers; give it the integer-valued property when its
1915 ;; range is a subset of the integers. What have I missed?
1917 (setf (get 'mplus
'maps-integers-to-integers
) t
)
1918 (setf (get 'mtimes
'maps-integers-to-integers
) t
)
1919 (setf (get 'mabs
'maps-integers-to-integers
) t
)
1920 (setf (get '$max
'maps-integers-to-integers
) t
)
1921 (setf (get '$min
'maps-integers-to-integers
) t
)
1923 (setf (get '$floor
'integer-valued
) t
)
1924 (setf (get '$ceiling
'integer-valued
) t
)
1925 (setf (get '$charfun
'integer-valued
) t
)
1927 (defun maxima-integerp (x)
1928 (cond ((integerp x
))
1931 (or (kindp x
'$integer
)
1934 (check-integer-facts x
))))
1935 (t (let ((x-op (and (consp x
) (consp (car x
)) (caar x
))) ($prederror nil
))
1936 (cond ((null x-op
) nil
)
1937 ((not (symbolp x-op
)) nil
) ; fix for mqapply at some point?
1938 ((eq x-op
'mrat
) (and (integerp (cadr x
)) (equal (cddr x
) 1)))
1939 ;; mtimes and mplus are generally handled by this clause
1940 ((and (get x-op
'maps-integers-to-integers
) (every #'maxima-integerp
(margs x
))))
1941 ;; Special case for 1/2*...*even
1943 (and (mnump (cadr x
))
1944 (integerp (mul 2 (cadr x
)))
1945 (every 'maxima-integerp
(cddr x
))
1946 (some #'(lambda (s) ($featurep s
'$even
)) (rest (margs x
)))))
1948 (and (every #'maxima-integerp
(margs x
))
1949 (null (mevalp (mlsp (caddr x
) 0)))))
1950 ;; ! in Maxima allows real arguments
1951 ((eq x-op
'mfactorial
)
1952 (and (maxima-integerp (cadr x
))
1953 (not (mevalp (mlsp (cadr x
) 0)))))
1955 (and (maxima-integerp (cadr x
))
1956 (not (mevalp (mlsp (cadr x
) 1)))))
1958 ((or ($featurep
($verbify x-op
) '$integervalued
)
1959 (get x-op
'integer-valued
))))))))
1961 ;; When called with mode 'integer look into the database for symbols which are
1962 ;; declared to be equal to an integer or an expression which is an integer.
1963 ;; In mode 'evod look for odd and even expressions.
1964 (defun check-integer-facts (x &optional
(mode 'integer
))
1965 (do ((factsl (cdr (facts1 x
)) (cdr factsl
))
1968 (setq fact
(car factsl
))
1969 (cond ((and (not (atom fact
))
1970 (eq (caar fact
) '$equal
))
1971 (cond ((and (symbolp (cadr fact
))
1973 ;; Case equal(x,expr): Test expr to be an integer.
1974 (cond ((symbolp (caddr fact
))
1975 (cond ((and (eq mode
'integer
)
1976 (or (kindp (caddr fact
) '$integer
)
1977 (kindp (caddr fact
) '$odd
)
1978 (kindp (caddr fact
) '$even
)))
1981 (cond ((kindp (caddr fact
) '$odd
)
1983 ((kindp (caddr fact
) '$even
)
1988 (cond ((eq mode
'integer
)
1989 (return (maxima-integerp (caddr fact
))))
1991 (return (evod (caddr fact
))))
1992 (t (return nil
))))))
1993 ((and (symbolp (caddr fact
))
1994 (eq (caddr fact
) x
))
1995 ;; Case equal(expr,x): Test expr to be an integer.
1996 (cond ((symbolp (caddr fact
))
1997 (cond ((and (eq mode
'integer
)
1998 (or (kindp (cadr fact
) '$integer
)
1999 (kindp (cadr fact
) '$odd
)
2000 (kindp (cadr fact
) '$even
)))
2003 (cond ((kindp (cadr fact
) '$odd
)
2005 ((kindp (cadr fact
) '$even
)
2010 (cond ((eq mode
'integer
)
2011 (return (maxima-integerp (cadr fact
))))
2013 (return (evod (cadr fact
))))
2014 (t (return nil
)))))))))))
2016 (defun nonintegerp (e)
2017 (cond ((and (symbolp e
) (or (kindp e
'$noninteger
) (check-noninteger-facts e
) (kindp e
'$irrational
)))) ;declared noninteger
2019 (if (integerp e
) nil t
)) ;all floats are noninteger and integers are not nonintegers
2021 (nonintegerp ($ratdisrep e
)))
2022 (t (eq t
(mgrp e
(take '($floor
) e
))))))
2025 ;; Look into the database for symbols which are declared to be equal
2026 ;; to a noninteger or an expression which is a noninteger.
2027 (defun check-noninteger-facts (x)
2028 (do ((factsl (cdr (facts1 x
)) (cdr factsl
)))
2030 (cond ((and (not (atom (car factsl
)))
2031 (eq (caar (car factsl
)) '$equal
))
2032 (cond ((and (symbolp (cadr (car factsl
)))
2033 (eq (cadr (car factsl
)) x
))
2034 ;; Case equal(x,expr): Test expr to be a noninteger.
2035 (cond ((symbolp (caddr (car factsl
)))
2036 (if (kindp (caddr (car factsl
)) '$noninteger
)
2039 (return (nonintegerp (caddr (car factsl
)))))))
2040 ((and (symbolp (caddr (car factsl
)))
2041 (eq (caddr (car factsl
)) x
))
2042 ;; Case equal(expr,x): Test expr to be a noninteger.
2043 (cond ((symbolp (cadr (car factsl
)))
2044 (if (kindp (cadr (car factsl
)) '$noninteger
)
2047 (return (nonintegerp (cadr (car factsl
))))))))))))
2050 (every #'maxima-integerp
(cdr l
)))
2053 (cond ((integerp e
) (not (oddp e
)))
2055 (t (eq '$even
(evod e
)))))
2058 (cond ((integerp e
) (oddp e
))
2060 (t (eq '$odd
(evod e
)))))
2062 ;; An extended evod that recognizes that abs(even) is even and
2066 (cond ((integerp e
) (if (oddp e
) '$odd
'$even
))
2069 (cond ((kindp e
'$odd
) '$odd
)
2070 ((kindp e
'$even
) '$even
)
2071 ;; Check the database for facts.
2072 ((symbolp e
) (check-integer-facts e
'evod
))))
2073 ((eq 'mtimes
(caar e
)) (evod-mtimes e
))
2074 ((eq 'mplus
(caar e
)) (evod-mplus e
))
2075 ((eq 'mabs
(caar e
)) (evod (cadr e
))) ;; extra code
2076 ((eq 'mexpt
(caar e
)) (evod-mexpt e
))))
2078 (defun evod-mtimes (x)
2079 (do ((l (cdr x
) (cdr l
)) (flag '$odd
))
2081 (setq x
(evod (car l
)))
2082 (cond ((eq '$odd x
))
2083 ((eq '$even x
) (setq flag
'$even
))
2084 ((maxima-integerp (car l
)) (cond ((eq '$odd flag
) (setq flag nil
))))
2087 (defun evod-mplus (x)
2088 (do ((l (cdr x
) (cdr l
)) (flag))
2089 ((null l
) (cond (flag '$odd
) (t '$even
)))
2090 (setq x
(evod (car l
)))
2091 (cond ((eq '$odd x
) (setq flag
(not flag
)))
2095 (defun evod-mexpt (x)
2096 (when (and (integerp (caddr x
)) (not (minusp (caddr x
))))
2099 (declare-top (special mgqp mlqp
))
2102 (atom (selector +labs
) (selector -labs
) (selector data
)))
2104 (defmacro c-dobj
(&rest x
)
2107 (defun dcompare (x y
)
2108 (setq odds
(list (sub x y
)) evens nil minus nil
2109 sign
(cond ((eq x y
) '$zero
)
2110 ((or (eq '$inf x
) (eq '$minf y
)) '$pos
)
2111 ((or (eq '$minf x
) (eq '$inf y
)) '$neg
)
2116 (setq x
(dinternp x
) y
(dinternp y
))
2117 (cond ((or (null x
) (null y
)) '$pnz
)
2118 ((progn (clear) (deq x y
) (sel y
+labs
)))
2122 (cond ((dmark x
'$zero
) nil
)
2124 (t (do ((l (sel x data
) (cdr l
))) ((null l
))
2125 (if (and (visiblep (car l
)) (deqf x y
(car l
))) (return t
))))))
2128 (cond ((eq 'meqp
(caar f
))
2129 (if (eq x
(cadar f
)) (deq (caddar f
) y
) (deq (cadar f
) y
)))
2130 ((eq 'mgrp
(caar f
))
2131 (if (eq x
(cadar f
)) (dgr (caddar f
) y
) (dls (cadar f
) y
)))
2132 ((eq 'mgqp
(caar f
))
2133 (if (eq x
(cadar f
)) (dgq (caddar f
) y
) (dlq (cadar f
) y
)))
2134 ((eq 'mnqp
(caar f
))
2135 (if (eq x
(cadar f
)) (dnq (caddar f
) y
) (dnq (cadar f
) y
)))))
2138 (cond ((dmark x
'$pos
) nil
)
2140 (t (do ((l (sel x data
) (cdr l
)))
2142 (when (or mlqp
(and (visiblep (car l
)) (dgrf x y
(car l
))))
2146 (cond ((eq 'mgrp
(caar f
)) (if (eq x
(cadar f
)) (dgr (caddar f
) y
)))
2147 ((eq 'mgqp
(caar f
)) (if (eq x
(cadar f
)) (dgr (caddar f
) y
)))
2148 ((eq 'meqp
(caar f
))
2149 (if (eq x
(cadar f
))
2151 (dgr (cadar f
) y
)))))
2154 (cond ((dmark x
'$neg
) nil
)
2156 (t (do ((l (sel x data
) (cdr l
)))
2158 (when (or mgqp
(and (visiblep (car l
)) (dlsf x y
(car l
))))
2162 (cond ((eq 'mgrp
(caar f
)) (if (eq x
(caddar f
)) (dls (cadar f
) y
)))
2163 ((eq 'mgqp
(caar f
)) (if (eq x
(caddar f
)) (dls (cadar f
) y
)))
2164 ((eq 'meqp
(caar f
))
2165 (if (eq x
(cadar f
)) (dls (caddar f
) y
) (dls (cadar f
) y
)))))
2168 (cond ((member (sel x
+labs
) '($pos $zero
) :test
#'eq
) nil
)
2169 ((eq '$nz
(sel x
+labs
)) (deq x y
))
2170 ((eq '$pn
(sel x
+labs
)) (dgr x y
))
2171 ((dmark x
'$pz
) nil
)
2172 ((eq x y
) (setq mgqp t
) nil
)
2173 (t (do ((l (sel x data
) (cdr l
))) ((null l
))
2174 (if (and (visiblep (car l
)) (dgqf x y
(car l
))) (return t
))))))
2177 (cond ((eq 'mgrp
(caar f
)) (if (eq x
(cadar f
)) (dgr (caddar f
) y
)))
2178 ((eq 'mgqp
(caar f
)) (if (eq x
(cadar f
)) (dgq (caddar f
) y
)))
2179 ((eq 'meqp
(caar f
))
2180 (if (eq x
(cadar f
)) (dgq (caddar f
) y
) (dgq (cadar f
) y
)))))
2183 (cond ((member (sel x
+labs
) '($neg $zero
) :test
#'eq
) nil
)
2184 ((eq '$pz
(sel x
+labs
)) (deq x y
))
2185 ((eq '$pn
(sel x
+labs
)) (dls x y
))
2186 ((dmark x
'$nz
) nil
)
2187 ((eq x y
) (setq mlqp t
) nil
)
2188 (t (do ((l (sel x data
) (cdr l
))) ((null l
))
2189 (if (and (visiblep (car l
)) (dlqf x y
(car l
))) (return t
))))))
2192 (cond ((eq 'mgrp
(caar f
)) (if (eq x
(caddar f
)) (dls (cadar f
) y
)))
2193 ((eq 'mgqp
(caar f
)) (if (eq x
(caddar f
)) (dlq (cadar f
) y
)))
2194 ((eq 'meqp
(caar f
))
2195 (if (eq x
(cadar f
)) (dlq (caddar f
) y
) (dlq (cadar f
) y
)))))
2198 (cond ((member (sel x
+labs
) '($pos $neg
) :test
#'eq
) nil
)
2199 ((eq '$pz
(sel x
+labs
)) (dgr x y
))
2200 ((eq '$nz
(sel x
+labs
)) (dls x y
))
2201 ((dmark x
'$pn
) nil
)
2203 (t (do ((l (sel x data
) (cdr l
))) ((null l
))
2204 (if (and (visiblep (car l
)) (dnqf x y
(car l
))) (return t
))))))
2207 (cond ((eq 'meqp
(caar f
))
2208 (if (eq x
(cadar f
)) (dnq (caddar f
) y
) (dnq (cadar f
) y
)))))
2210 ;; mark sign of x to be m, relative to current comparison point for dcomp.
2211 ;; returns true if this fact is already known, nil otherwise.
2213 (cond ((eq m
(sel x
+labs
)))
2214 ((and dbtrace
(prog1
2216 (mtell (intl:gettext
"DMARK: marking ~M ~M") (if (atom x
) x
(car x
)) m
))
2220 (push+sto
(sel x
+labs
) m
)
2223 (defun daddgr (flag x
)
2224 (with-compsplt (lhs rhs x
)
2225 (mdata flag
'mgrp
(dintern lhs
) (dintern rhs
))
2226 (if (or (mnump lhs
) (constant lhs
))
2227 (list '(mlessp) rhs lhs
)
2228 (list '(mgreaterp) lhs rhs
))))
2230 (defun daddgq (flag x
)
2231 (with-compsplt (lhs rhs x
)
2232 (mdata flag
'mgqp
(dintern lhs
) (dintern rhs
))
2233 (if (or (mnump lhs
) (constant lhs
))
2234 (list '(mleqp) rhs lhs
)
2235 (list '(mgeqp) lhs rhs
))))
2237 (defun daddeq (flag x
)
2238 (with-compsplt-eq (lhs rhs x
)
2239 (mdata flag
'meqp
(dintern lhs
) (dintern rhs
))
2240 (list '($equal
) lhs rhs
)))
2242 (defun daddnq (flag x
)
2243 (with-compsplt-eq (lhs rhs x
)
2244 (cond ((and (mtimesp lhs
) (equal rhs
0))
2245 (dolist (term (cdr lhs
)) (daddnq flag term
)))
2246 ((and (mexptp lhs
) (mexptp rhs
)
2247 (integerp (caddr lhs
)) (integerp (caddr rhs
))
2248 (equal (caddr lhs
) (caddr rhs
)))
2249 (mdata flag
'mnqp
(dintern (cadr lhs
)) (dintern (cadr rhs
)))
2250 (cond ((not (oddp (caddr lhs
)))
2251 (mdata flag
'mnqp
(dintern (cadr lhs
))
2252 (dintern (neg (cadr rhs
)))))))
2253 (t (mdata flag
'mnqp
(dintern lhs
) (dintern rhs
))))
2254 (list '(mnot) (list '($equal
) lhs rhs
))))
2256 ;; The following functions are used by asksign to write answers into the
2257 ;; database. We make sure that these answers are written into the global
2258 ;; context '$initial and not in a local context which might be generated during
2259 ;; the evaluation phase and which will be destroyed before the evaluation has
2261 ;; The additional facts are removed from the global context '$initial after
2262 ;; finishing the evaluation phase of meval with a call to clearsign.
2265 (let ((context '$initial
))
2267 (push (cons x
'$pos
) *local-signs
*)))
2270 (let ((context '$initial
))
2272 (push (cons x
'$neg
) *local-signs
*)))
2275 (let ((context '$initial
))
2277 (push (cons x
'$zero
) *local-signs
*)))
2280 (let ((context '$initial
))
2282 (push (cons x
'$pn
) *local-signs
*)))
2285 (let ((context '$initial
))
2287 (push (cons x
'$pz
) *local-signs
*)))
2289 (defun compsplt-eq (x)
2290 (with-compsplt (lhs rhs x
)
2292 (setq lhs rhs rhs
0))
2293 (if (and (equal rhs
0)
2295 (and (not (atom lhs
))
2296 (kindp (caar lhs
) '$oddfun
)
2297 (kindp (caar lhs
) '$increasing
))))
2298 (setq lhs
(cadr lhs
)))
2301 (defun mdata (flag r x y
)
2306 (defun mfact (r x y
)
2307 (let ((f (datum (list r x y
))))
2312 (defun mkill (r x y
)
2313 (let ((f (car (datum (list r x y
)))))
2319 (kind (dintern x
) (dintern y
)))
2321 ;; To guess from the previous incarnation of this code,
2322 ;; each argument is assumed to be a float, bigfloat, integer, or Maxima rational.
2323 ;; Convert Maxima rationals to Lisp rationals to make them comparable to others.
2326 (when (or ($bfloatp x
) ($bfloatp y
))
2328 x
(let (($float2bf t
))
2329 (declare (special $float2bf
))
2330 (cadr ($bfloat
(sub x y
))))
2332 (if (not (numberp x
))
2333 (setq x
(/ (cadr x
) (caddr x
))))
2334 (if (not (numberp y
))
2335 (setq y
(/ (cadr y
) (caddr y
))))
2337 (#-ecl
(> x y
) #+ecl
(> (- x y
) 0) '$pos
)
2338 (#-ecl
(> y x
) #+ecl
(> (- y x
) 0) '$neg
)
2342 (cons (car l
) (cons x
(cdr l
))))
2345 (cond ((eq '$pos s
) '$neg
)
2346 ((eq '$neg s
) '$pos
)
2351 (defun strongp (x y
)
2352 (cond ((eq '$pnz y
))
2354 ((member y
'($pz $nz $pn
) :test
#'eq
))))
2356 (defun munformat (form)
2359 (cons (caar form
) (mapcar #'munformat
(cdr form
)))))
2361 (defun declarekind (var prop
) ; This function is for $DECLARE to use.
2363 (cond ((truep (list 'kind var prop
)) t
)
2364 ((or (falsep (list 'kind var prop
))
2365 (and (setq prop2
(assoc prop
'(($integer . $noninteger
)
2366 ($noninteger . $integer
)
2367 ($increasing . $decreasing
)
2368 ($decreasing . $increasing
)
2369 ($symmetric . $antisymmetric
)
2370 ($antisymmetric . $symmetric
)
2371 ($rational . $irrational
)
2372 ($irrational . $rational
)
2373 ($oddfun . $evenfun
)
2374 ($evenfun . $oddfun
)) :test
#'eq
))
2375 (truep (list 'kind var
(cdr prop2
)))))
2376 (merror (intl:gettext
"declare: inconsistent declaration ~:M") `(($declare
) ,var
,prop
)))
2377 (t (mkind var prop
) t
))))
2379 ;;; These functions reformat expressions to be stored in the data base.
2381 ;; Return a list of all the atoms in X that aren't either numbers or constants
2382 ;; whose numerical value we know.
2383 (defun unknown-atoms (x)
2384 (let (($listconstvars t
))
2385 (declare (special $listconstvars
))
2386 (remove-if (lambda (sym) (mget sym
'$numer
))
2387 (cdr ($listofvars x
)))))
2389 ;; Rewrite a^b to a simpler expression that has the same sign:
2390 ;; If b is odd or 1/b is odd, remove the exponent, e.g. x^3 becomes x.
2391 ;; If b has a negative sign, return a^-b, e.g. 1/x^a becomes x^a.
2392 ;; Otherwise, do nothing.
2393 (defun rewrite-mexpt-retaining-sign (x)
2395 (let ((base (cadr x
)) (exponent (caddr x
)))
2396 (cond ((or (eq (evod exponent
) '$odd
) (eq (evod (inv exponent
)) '$odd
)) base
)
2397 ((negp exponent
) (inv x
))
2403 ;; Split X into (values LHS RHS) so that X>0 <=> LHS > RHS. This is supposed to
2404 ;; be a canonical form for X that can be stored in the database and then looked
2407 ;; This uses two worker routines: COMPSPLT-SINGLE and COMPSPLT-GENERAL. The
2408 ;; former assumes that X only contains one symbol whose value is not known (eg not %e,
2409 ;; %pi etc.). The latter tries to deal with arbitrary collections of variables.
2411 (multiple-value-bind (lhs rhs
)
2413 ((or (atom x
) (atom (car x
))) (values x
0))
2414 ((null (cdr (unknown-atoms x
))) (compsplt-single x
))
2415 (t (compsplt-general x
)))
2416 (let ((swapped nil
))
2417 ;; If lhs is zero, swap lhs and rhs to make the following code simpler.
2418 ;; Remember that they were swapped to swap them back afterwards.
2420 (setq lhs rhs rhs
0 swapped t
))
2422 ;; Rewrite mexpts in factors so that e.g. x^3/y>0 becomes x*y>0. */
2425 (cons (car lhs
) (mapcar #'rewrite-mexpt-retaining-sign
(cdr lhs
)))
2426 (rewrite-mexpt-retaining-sign lhs
))))
2427 ;; Undo swapping lhs and rhs.
2430 (values lhs rhs
)))))
2432 (defun compsplt-single (x)
2433 (do ((exp (list x
0)) (success nil
))
2434 ((or success
(symbols (cadr exp
))) (values (car exp
) (cadr exp
)))
2435 (cond ((atom (car exp
)) (setq success t
))
2436 ((eq (caaar exp
) 'mplus
) (setq exp
(splitsum exp
)))
2437 ((eq (caaar exp
) 'mtimes
) (setq exp
(splitprod exp
)))
2438 (t (setq success t
)))))
2440 (defun compsplt-general (x)
2441 ;; Let compsplt-single work on it first to treat constant factors/terms.
2442 (multiple-value-bind (lhs rhs
) (compsplt-single x
) (setq x
(sub lhs rhs
)))
2444 ;; If x is an atom or a single level list then we won't change it any.
2445 ((or (atom x
) (atom (car x
)))
2447 ;; If x is a negative expression but not a sum, then get rid of the
2449 ((negp x
) (values 0 (neg x
)))
2450 ;; If x is not a sum, or is a sum with more than 2 terms, or has some
2451 ;; symbols common to both summands, then do nothing.
2453 (not (eq (caar x
) 'mplus
))
2454 (intersect* (symbols (cadr x
)) (symbols (caddr x
))))
2456 ;; -x + y gives (y, x)
2457 ((and (or (negp (cadr x
)) (mnump (cadr x
)))
2458 (not (negp (caddr x
))))
2459 (values (caddr x
) (neg (cadr x
))))
2460 ;; x - y gives (x, y)
2461 ((and (not (negp (cadr x
)))
2462 (or (negp (caddr x
)) (mnump (caddr x
))))
2463 (values (cadr x
) (neg (caddr x
))))
2464 ;; - x - y gives (0, x+y)
2465 ((and (negp (cadr x
)) (negp (caddr x
)))
2472 (and (mtimesp x
) (mnegp (cadr x
))))
2474 (defun splitsum (exp)
2475 (do ((llist (cdar exp
) (cdr llist
))
2479 (if (mplusp lhs1
) (setq success t
))
2481 (cond ((member '$inf llist
:test
#'eq
)
2482 (setq rhs1
(add2 '$inf
(sub* rhs1
(addn llist t
)))
2483 lhs1
(add2 '$inf
(sub* lhs1
(addn llist t
)))
2485 ((member '$minf llist
:test
#'eq
)
2486 (setq rhs1
(add2 '$minf
(sub* rhs1
(addn llist t
)))
2487 lhs1
(add2 '$minf
(sub* lhs1
(addn llist t
)))
2489 ((null (symbols (car llist
)))
2490 (setq lhs1
(sub lhs1
(car llist
))
2491 rhs1
(sub rhs1
(car llist
)))))))
2493 (defun splitprod (exp)
2497 (llist (cdar exp
) (cdr llist
))
2503 (if (mtimesp lhs1
) (setq success t
))
2507 (t (list lhs1 rhs1
))))
2508 (when (null (symbols (car llist
)))
2510 (if (eq sign
'$neg
) (setq flipsign
(not flipsign
)))
2511 (if (member sign
'($pos $neg
) :test
#'eq
)
2512 (setq lhs1
(div lhs1
(car llist
))
2513 rhs1
(div rhs1
(car llist
)))))))
2516 (let (($listconstvars %initiallearnflag
))
2517 (declare (special $listconstvars
))
2518 (cdr ($listofvars x
))))
2520 ;; %initiallearnflag is only necessary so that %PI, %E, etc. can be LEARNed.
2522 (defun initialize-numeric-constant (c)
2523 (setq %initiallearnflag t
)
2524 (let ((context '$global
))
2525 (learn `((mequal) ,c
,(mget c
'$numer
)) t
))
2526 (setq %initiallearnflag nil
))
2528 (eval-when (:load-toplevel
:execute
)
2530 '(;; even and odd are integer
2531 (par ($even $odd
) $integer
)
2533 ; Cutting out inferences for integer, rational, real, complex (DK 10/2009).
2534 ; (kind $integer $rational)
2535 ; (par ($rational $irrational) $real)
2536 ; (par ($real $imaginary) $complex)
2538 ;; imaginary is complex
2539 (kind $imaginary $complex
)
2541 ;; Declarations for constants
2542 (kind $%i $noninteger
)
2543 (kind $%i $imaginary
)
2544 (kind $%e $noninteger
)
2546 (kind $%pi $noninteger
)
2548 (kind $%gamma $noninteger
)
2549 (kind $%gamma $real
)
2550 (kind $%phi $noninteger
)
2552 (kind $%pi $irrational
)
2553 (kind $%e $irrational
)
2554 (kind $%phi $irrational
)
2556 ;; Declarations for functions
2557 (kind %log $increasing
)
2558 (kind %atan $increasing
) (kind %atan $oddfun
)
2559 (kind $delta $evenfun
)
2560 (kind %sinh $increasing
) (kind %sinh $oddfun
)
2561 (kind %cosh $posfun
)
2562 (kind %tanh $increasing
) (kind %tanh $oddfun
)
2563 (kind %coth $oddfun
)
2564 (kind %csch $oddfun
)
2565 (kind %sech $posfun
)
2566 (kind %asinh $increasing
) (kind %asinh $oddfun
)
2567 ;; It would be nice to say %acosh is $posfun, but then
2568 ;; assume(xn<0); abs(acosh(xn)) -> acosh(xn), which is wrong
2569 ;; since acosh(xn) is complex.
2570 (kind %acosh $increasing
)
2571 (kind %atanh $increasing
) (kind %atanh $oddfun
)
2573 (kind $lambert_w $complex
)
2574 (kind %cabs $complex
)))
2576 ;; Create an initial context for the user which is a subcontext of $global.
2577 ($newcontext
'$initial
))