Rename *ll* and *ul* to ll and ul in poles-in-interval
[maxima.git] / src / compar.lisp
blob9d9ebf22d2fdbbd18a811bb98073a916626ae44b
1 ;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
13 (macsyma-module compar)
15 (load-macsyma-macros mrgmac)
17 (declare-top (special success))
19 (defvar *debug-compar* nil
20 "Enables debugging code for this file.")
22 (defvar %initiallearnflag)
24 (defmvar complexsign nil
25 "If T, COMPAR attempts to work in a complex mode.
26 This scheme is only very partially developed at this time."
27 no-reset)
29 (defmvar $signbfloat t)
30 (defmvar $askexp)
31 (defmvar $assume_pos nil)
32 (defmvar $assume_pos_pred nil)
34 (defmvar factored nil)
36 ;; The *LOCAL-SIGNS* variable contains a list of facts that are local to the
37 ;; current evaluation. These are stored in the assume database (in the global
38 ;; context) by asksign1 when the user answers questions. A "top-level"
39 ;; evaluation is run by MEVAL* and that function calls CLEARSIGN when it
40 ;; finishes to discard them.
41 (defmvar *local-signs* nil)
43 (defmvar sign nil)
44 (defmvar minus nil)
45 (defmvar odds nil)
46 (defmvar evens nil)
48 (defvar $useminmax t)
50 ;; Remove this (nil'ed out) function after a while. We should be
51 ;; using POWER instead of POW.
52 #+nil
53 (defmacro pow (&rest x)
54 `(power ,@x))
56 (defun lmul (l)
57 (simplify (cons '(mtimes) l)))
59 (defun conssize (x)
60 (if (atom x)
62 (do ((x (cdr x) (cdr x))
63 (sz 1))
64 ((null x) sz)
65 (incf sz (1+ (conssize (car x)))))))
67 ;;; Functions for creating, activating, manipulating, and killing contexts
69 ;;; This "turns on" a context, making its facts visible.
71 (defmfun $activate (&rest args)
72 (dolist (c args)
73 (cond ((not (symbolp c)) (nc-err '$activate c))
74 ((member c (cdr $activecontexts) :test #'eq))
75 ((member c (cdr $contexts) :test #'eq)
76 (setq $activecontexts (mcons c $activecontexts))
77 (activate c))
78 (t (merror (intl:gettext "activate: no such context ~:M") c))))
79 '$done)
81 ;;; This "turns off" a context, keeping the facts, but making them invisible
83 (defmfun $deactivate (&rest args)
84 (dolist (c args)
85 (cond ((not (symbolp c)) (nc-err '$deactivate c))
86 ((member c (cdr $contexts) :test #'eq)
87 (setq $activecontexts ($delete c $activecontexts))
88 (deactivate c))
89 (t (merror (intl:gettext "deactivate: no such context ~:M") c))))
90 '$done)
92 ;;; This function prints out a list of the facts in the specified context.
93 ;;; No argument implies the current context.
95 (defmfun $facts (&optional (ctxt $context))
96 (if (member ctxt (cdr $contexts))
97 (facts1 ctxt)
98 (facts2 ctxt)))
100 (defun facts1 (con)
101 (contextmark)
102 (do ((l (zl-get con 'data) (cdr l))
103 (nl)
104 (u))
105 ((null l) (cons '(mlist) nl))
106 (when (visiblep (car l))
107 (setq u (intext (caaar l) (cdaar l)))
108 (unless (memalike u nl)
109 (push u nl)))))
111 ;; Look up facts from the database which contain expr. expr can be a symbol or
112 ;; a more general expression.
113 (defun facts2 (expr)
114 (labels ((among (x l)
115 (cond ((null l) nil)
116 ((atom l) (eq x l))
117 ((alike1 x l) t)
119 (do ((ll (cdr l) (cdr ll)))
120 ((null ll) nil)
121 (if (among x (car ll)) (return t)))))))
122 (do ((facts (cdr ($facts $context)) (cdr facts))
123 (ans))
124 ((null facts) (return (cons '(mlist) (reverse ans))))
125 (when (or (among expr (cadar facts))
126 (among expr (caddar facts)))
127 (push (car facts) ans)))))
129 (defun intext (rel body)
130 (setq body (mapcar #'doutern body))
131 (cond ((eq 'kind rel) (cons '($kind) body))
132 ((eq 'par rel) (cons '($par) body))
133 ((eq 'mgrp rel) (cons '(mgreaterp) body))
134 ((eq 'mgqp rel) (cons '(mgeqp) body))
135 ((eq 'meqp rel) (cons '($equal) body))
136 ((eq 'mnqp rel) (list '(mnot) (cons '($equal) body)))))
138 (defprop $context asscontext assign)
140 ;;; This function switches contexts, creating one if necessary.
142 (defun asscontext (xx y)
143 (declare (ignore xx))
144 (cond ((not (symbolp y)) (nc-err "context assignment" y))
145 ((member y $contexts :test #'eq) (setq context y $context y))
146 (t ($newcontext y))))
148 ;;; This function actually creates a context whose subcontext is $GLOBAL.
149 ;;; It also switches contexts to the newly created one.
150 ;;; If no argument supplied, then invent a name via gensym and use that.
152 (defmfun $newcontext (&rest args)
153 (if (null args)
154 ($newcontext ($gensym "context")) ;; make up a name and try again
155 (if (> (length args) 1)
156 (merror "newcontext: found more than one argument.")
157 (let ((x (first args)))
158 (cond
159 ((not (symbolp x)) (nc-err '$newcontext x))
160 ((member x $contexts :test #'eq)
161 (mtell (intl:gettext "newcontext: context ~M already exists.") x) nil)
163 (setq $contexts (mcons x $contexts))
164 (putprop x '($global) 'subc)
165 (setq context x $context x)))))))
167 ;;; This function creates a supercontext. If given one argument, it
168 ;;; makes the current context be the subcontext of the argument. If
169 ;;; given more than one argument, the first is assumed the name of the
170 ;;; supercontext and the rest are the subcontexts.
171 ;;; If no arguments supplied, then invent a name via gensym and use that.
173 (defmfun $supcontext (&rest x)
174 (cond ((null x) ($supcontext ($gensym "context"))) ;; make up a name and try again
175 ((caddr x) (merror (intl:gettext "supcontext: found more than two arguments.")))
176 ((not (symbolp (car x))) (nc-err '$supcontext (car x)))
177 ((member (car x) $contexts :test #'eq)
178 (merror (intl:gettext "supcontext: context ~M already exists.") (car x)))
179 ((and (cadr x) (not (member (cadr x) $contexts :test #'eq)))
180 (merror (intl:gettext "supcontext: no such context ~M") (cadr x)))
181 (t (setq $contexts (mcons (car x) $contexts))
182 (putprop (car x) (ncons (or (cadr x) $context)) 'subc)
183 (setq context (car x) $context (car x)))))
185 ;;; This function kills a context or a list of contexts
187 (defmfun $killcontext (&rest args)
188 (dolist (c args)
189 (if (symbolp c)
190 (killcontext c)
191 (nc-err '$killcontext c)))
192 (if (and (= (length args) 1) (eq (car args) '$global))
193 '$not_done
194 '$done))
196 (defun killallcontexts ()
197 (mapcar #'killcontext (cdr $contexts))
198 (setq $context '$initial context '$initial current '$initial
199 $contexts '((mlist) $initial $global) dobjects ())
200 ;;The DB variables
201 ;;conmark, conunmrk, conindex, connumber, and contexts
202 ;;concern garbage-collectible contexts, and so we're
203 ;;better off not resetting them.
204 (defprop $global 1 cmark) (defprop $initial 1 cmark)
205 (defprop $initial ($global) subc))
207 (defun killcontext (x)
208 (cond ((not (member x $contexts :test #'eq))
209 (mtell (intl:gettext "killcontext: no such context ~M.") x))
210 ((eq x '$global) '$global)
211 ((eq x '$initial)
212 (mapc #'remov (zl-get '$initial 'data))
213 (remprop '$initial 'data)
214 '$initial)
215 ((and (not (eq $context x)) (contextmark) (< 0 (zl-get x 'cmark)))
216 (mtell (intl:gettext "killcontext: context ~M is currently active.") x))
217 (t (if (member x $activecontexts)
218 ;; Context is on the list of active contexts. The test above
219 ;; checks for active contexts, but it seems not to work in all
220 ;; cases. So deactivate the context at this place to remove it
221 ;; from the list of active contexts before it is deleted.
222 ($deactivate x))
223 (setq $contexts ($delete x $contexts))
224 (cond ((and (eq x $context)
225 (equal ;;replace eq ?? wfs
226 (zl-get x 'subc) '($global)))
227 (setq $context '$initial)
228 (setq context '$initial))
229 ((eq x $context)
230 (setq $context (car (zl-get x 'subc)))
231 (setq context (car (zl-get x 'subc)))))
232 (killc x)
233 x)))
235 (defun nc-err (fn x)
236 (merror (intl:gettext "~M: context name must be a symbol; found ~M") fn x))
238 ;; Simplification and evaluation of boolean expressions
240 ;; Simplification of boolean expressions:
242 ;; and and or are declared nary. The sole effect of this is to allow Maxima to
243 ;; flatten nested expressions, e.g., a and (b and c) => a and b and c
244 ;; (The nary declaration does not make and and or commutative, and and and or
245 ;; are not otherwise declared commutative.)
247 ;; and: if any argument simplifies to false, return false
248 ;; otherwise omit arguments which simplify to true and simplify others
249 ;; if only one argument remains, return it
250 ;; if none remain, return true
252 ;; or: if any argument simplifies to true, return true
253 ;; otherwise omit arguments which simplify to false and simplify others
254 ;; if only one argument remains, return it
255 ;; if none remain, return false
257 ;; not: if argument simplifies to true / false, return false / true
258 ;; otherwise reverse sense of comparisons (if argument is a comparison)
259 ;; otherwise return not <simplified argument>
261 ;; Evaluation (MEVAL) of boolean expressions:
262 ;; same as simplification except evaluating (MEVALP) arguments instead of simplifying
263 ;; When prederror = true, complain if expression evaluates to something other than T / NIL
264 ;; (otherwise return unevaluated boolean expression)
266 ;; Evaluation (MEVALP) of boolean expressions:
267 ;; same as simplification except evaluating (MEVALP) arguments instead of simplifying
268 ;; When prederror = true, complain if expression evaluates to something other than T / NIL
269 ;; (otherwise return unevaluated boolean expression)
271 ;; Simplification of "is" expressions:
272 ;; if argument simplifies to true/false, return true/false
273 ;; otherwise return is (<simplified argument>)
275 ;; Evaluation of "is" expressions:
276 ;; if argument evaluates to true/false, return true/false
277 ;; otherwise return unknown if prederror = false, else trigger an error
279 ;; Simplification of "maybe" expressions:
280 ;; if argument simplifies to true/false, return true/false
281 ;; otherwise return maybe (<simplified expression>)
283 ;; Evaluation of "maybe" expressions:
284 ;; if argument evaluates to true/false, return true/false
285 ;; otherwise return unknown
287 (defprop $is simp-$is operators)
288 (defprop %is simp-$is operators)
289 (defprop $maybe simp-$is operators)
290 (defprop %maybe simp-$is operators)
292 ; I'VE ASSUMED (NULL Z) => SIMPLIFIY ARGUMENTS
293 ; SAME WITH SIMPCHECK (SRC/SIMP.LISP)
294 ; SAME WITH TELLSIMP-GENERATED SIMPLIFICATION FUNCTIONS
295 ; SAME WITH SIMPLIFICATION OF %SIN
296 ; PRETTY SURE I'VE SEEN OTHER EXAMPLES AS WELL
297 ; Z SEEMS TO SIGNIFY "ARE THE ARGUMENTS SIMPLIFIED YET"
299 (defun maybe-simplifya (x z)
300 (if z x (simplifya x z)))
302 (defun maybe-simplifya-protected (x z)
303 (let ((errcatch t) ($errormsg nil))
304 (ignore-errors (maybe-simplifya x z) x)))
306 (defun simp-$is (x yy z)
307 (declare (ignore yy))
308 (let ((a (maybe-simplifya (cadr x) z)))
309 (if (or (eq a t) (eq a nil))
311 `((,(caar x) simp) ,a))))
313 (defmspec $is (form)
314 (unless (= 1 (length (rest form)))
315 (merror (intl:gettext "is() expects a single argument. Found ~A")
316 (length (rest form))))
317 (destructuring-bind (answer patevalled)
318 (mevalp1 (cadr form))
319 (cond ((member answer '(t nil) :test #'eq) answer)
320 ;; I'D RATHER HAVE ($PREDERROR ($THROW `(($PREDERROR) ,PATEVALLED))) HERE
321 ($prederror (pre-err patevalled))
322 (t '$unknown))))
324 (defmspec $maybe (form)
325 (let* ((pat (cadr form))
326 (x (let (($prederror nil)) (mevalp1 pat)))
327 (ans (car x)))
328 (if (member ans '(t nil) :test #'eq)
330 '$unknown)))
332 (defun is (pred)
333 (let (($prederror t))
334 (mevalp pred)))
336 ; The presence of OPERS tells SIMPLIFYA to call OPER-APPLY,
337 ; which calls NARY1 to flatten nested "and" and "or" expressions
338 ; (due to $NARY property of MAND and MOR, declared elsewhere).
340 (putprop 'mand t 'opers)
341 (putprop 'mor t 'opers)
343 (putprop 'mnot 'simp-mnot 'operators)
344 (putprop 'mand 'simp-mand 'operators)
345 (putprop 'mor 'simp-mor 'operators)
347 (defun simp-mand (x yy z)
348 (declare (ignore yy))
349 (do ((l (cdr x) (cdr l))
351 (simplified))
352 ((null l)
353 (cond ((= (length simplified) 0) t)
354 ((= (length simplified) 1) (car simplified))
355 (t (cons '(mand simp) (reverse simplified)))))
356 (setq a (maybe-simplifya (car l) z))
357 (cond ((null a) (return nil))
358 ((eq a '$unknown) (unless (member '$unknown simplified :test #'eq) (push a simplified)))
359 ((not (member a '(t nil) :test #'eq)) (push a simplified)))))
361 (defun simp-mor (x yy z)
362 (declare (ignore yy))
363 (do ((l (cdr x) (cdr l))
365 (simplified))
366 ((null l)
367 (cond ((= (length simplified) 0) nil)
368 ((= (length simplified) 1) (car simplified))
369 (t (cons '(mor simp) (reverse simplified)))))
370 (setq a (maybe-simplifya (car l) z))
371 (cond ((eq a t) (return t))
372 ((eq a '$unknown) (unless (member '$unknown simplified :test #'eq) (push a simplified)))
373 ((not (member a '(t nil) :test #'eq)) (push a simplified)))))
375 ; ALSO CUT STUFF ABOUT NOT EQUAL => NOTEQUAL AT TOP OF ASSUME
377 (defun simp-mnot (x yy z)
378 (declare (ignore yy))
379 (let ((arg (maybe-simplifya (cadr x) z)))
380 (if (atom arg)
381 (cond ((or (eq arg t) (eq arg '$true))
382 nil)
383 ((or (eq arg nil) (eq arg '$false))
385 ((eq arg '$unknown)
386 '$unknown)
387 (t `((mnot simp) ,arg)))
388 (let ((arg-op (caar arg)) (arg-arg (cdr arg)))
389 ;;(setq arg-arg (mapcar #'(lambda (a) (maybe-simplifya a z)) arg-arg))
390 (cond ((eq arg-op 'mlessp)
391 (simplify `((mgeqp) ,@arg-arg)))
392 ((eq arg-op 'mleqp)
393 (simplify `((mgreaterp) ,@arg-arg)))
394 ((eq arg-op 'mequal)
395 (simplify `((mnotequal) ,@arg-arg)))
396 ((eq arg-op '$equal)
397 (simplify `(($notequal) ,@arg-arg)))
398 ((eq arg-op 'mnotequal)
399 (simplify `((mequal) ,@arg-arg)))
400 ((eq arg-op '$notequal)
401 (simplify `(($equal) ,@arg-arg)))
402 ((eq arg-op 'mgeqp)
403 (simplify `((mlessp) ,@arg-arg)))
404 ((eq arg-op 'mgreaterp)
405 (simplify `((mleqp) ,@arg-arg)))
406 ((eq arg-op 'mnot)
407 (car arg-arg))
408 ;; Distribute negation over conjunction and disjunction;
409 ;; analogous to '(- (a + b)) --> - a - b.
410 ((eq arg-op 'mand)
411 (let ((L (mapcar #'(lambda (e) `((mnot) ,e)) arg-arg)))
412 (simplifya `((mor) ,@L) nil)))
413 ((eq arg-op 'mor)
414 (let ((L (mapcar #'(lambda (e) `((mnot) ,e)) arg-arg)))
415 (simplifya `((mand) ,@L) nil)))
416 (t `((mnot simp) ,arg)))))))
418 ;; =>* N.B. *<=
419 ;; The translator depends on some stuff in here.
420 ;; Check it out in the transl module ACALL before proceeding.
422 (defun mevalp (pat)
423 (let* ((x (mevalp1 pat))
424 (ans (car x))
425 (patevalled (cadr x)))
426 (cond ((member ans '(t ()) :test #'eq) ans)
427 ;; I'D RATHER HAVE ($PREDERROR ($THROW `(($PREDERROR) ,PATEVALLED))) HERE
428 ($prederror (pre-err patevalled))
429 (t (or patevalled ans)))))
431 (defun mevalp1 (pat)
432 (let (patevalled ans)
433 (setq ans
434 (cond ((and (not (atom pat))
435 (member (caar pat) '(mnot mand mor) :test #'eq))
436 (cond ((eq 'mnot (caar pat)) (is-mnot #'mevalp (cadr pat)))
437 ((eq 'mand (caar pat)) (is-mand #'mevalp (cdr pat)))
438 (t (is-mor #'mevalp (cdr pat)))))
439 ((atom (setq patevalled (specrepcheck (meval pat))))
440 patevalled)
441 ((member (caar patevalled) '(mnot mand mor) :test #'eq)
442 (return-from mevalp1 (mevalp1 patevalled)))
444 (mevalp2 patevalled
445 (caar patevalled)
446 (cadr patevalled)
447 (caddr patevalled)))))
448 (list ans patevalled)))
450 (defun mevalp2 (patevalled pred arg1 arg2)
451 (cond ((eq 'mequal pred) (like arg1 arg2))
452 ((eq '$equal pred) (meqp arg1 arg2))
453 ((eq 'mnotequal pred) (not (like arg1 arg2)))
454 ((eq '$notequal pred) (mnqp arg1 arg2))
455 ((eq 'mgreaterp pred) (mgrp arg1 arg2))
456 ((eq 'mlessp pred) (mgrp arg2 arg1))
457 ((eq 'mgeqp pred) (mgqp arg1 arg2))
458 ((eq 'mleqp pred) (mgqp arg2 arg1))
459 (t (isp (munformat patevalled)))))
461 (defun pre-err (pat)
462 (merror (intl:gettext "Unable to evaluate predicate ~M") pat))
464 (defun is-mnot (pred-eval pred)
465 (setq pred (funcall pred-eval pred))
466 (cond ((eq t pred) nil)
467 ((not pred))
468 (t (pred-reverse pred))))
470 (defun pred-reverse (pred)
471 (take '(mnot) pred))
473 (defun is-mand (pred-eval pl)
474 (do ((dummy)
475 (npl))
476 ((null pl) (cond ((null npl))
477 ((null (cdr npl)) (car npl))
478 (t (cons '(mand) (nreverse npl)))))
479 (setq dummy (funcall pred-eval (car pl)) pl (cdr pl))
480 (cond ((eq t dummy))
481 ((null dummy) (return nil))
482 (t (push dummy npl)))))
484 (defun is-mor (pred-eval pl)
485 (do ((dummy)
486 (npl))
487 ((null pl) (cond ((null npl) nil)
488 ((null (cdr npl)) (car npl))
489 (t (cons '(mor) (nreverse npl)))))
490 (setq dummy (funcall pred-eval (car pl)) pl (cdr pl))
491 (cond ((eq t dummy) (return t))
492 ((null dummy))
493 (t (push dummy npl)))))
495 (defmspec $assume (x)
496 (setq x (cdr x))
497 (do ((nl)) ((null x) (cons '(mlist) (nreverse nl)))
498 (cond ((atom (car x)) (push (assume (meval (car x))) nl))
499 ((eq 'mand (caaar x))
500 (mapc #'(lambda (l) (push (assume (meval l)) nl))
501 (cdar x)))
502 ((eq 'mnot (caaar x))
503 (push (assume (meval (pred-reverse (cadar x)))) nl))
504 ((eq 'mor (caaar x))
505 (merror (intl:gettext "assume: argument cannot be an 'or' expression; found ~M") (car x)))
506 ((eq (caaar x) 'mequal)
507 (merror (intl:gettext "assume: argument cannot be an '=' expression; found ~M~%assume: maybe you want 'equal'.") (car x)))
508 ((eq (caaar x) 'mnotequal)
509 (merror (intl:gettext "assume: argument cannot be a '#' expression; found ~M~%assume: maybe you want 'not equal'.") (car x)))
510 (t (push (assume (meval (car x))) nl)))
511 (setq x (cdr x))))
513 (defun assume (pat)
514 (if (and (not (atom pat))
515 (eq (caar pat) 'mnot)
516 (eq (caaadr pat) '$equal))
517 (setq pat `(($notequal) ,@(cdadr pat))))
518 (let ((dummy (let ($assume_pos) (car (mevalp1 pat)))))
519 (cond ((eq dummy t) '$redundant)
520 ((null dummy) '$inconsistent)
521 ((atom dummy) '$meaningless)
522 (t (learn pat t)))))
524 (defun learn (pat flag)
525 (cond ((atom pat))
526 ;; Check for abs function in pattern.
527 ((and (not limitp)
528 (learn-abs pat flag)))
529 ;; Check for constant expression in pattern.
530 ((and (not limitp)
531 (learn-numer pat flag)))
532 ((zl-get (caar pat) (if flag 'learn 'unlearn))
533 (funcall (zl-get (caar pat) (if flag 'learn 'unlearn)) pat))
534 ((eq (caar pat) 'mgreaterp) (daddgr flag (sub (cadr pat) (caddr pat))))
535 ((eq (caar pat) 'mgeqp) (daddgq flag (sub (cadr pat) (caddr pat))))
536 ((member (caar pat) '(mequal $equal) :test #'eq)
537 (daddeq flag (sub (cadr pat) (caddr pat))))
538 ((member (caar pat) '(mnotequal $notequal) :test #'eq)
539 (daddnq flag (sub (cadr pat) (caddr pat))))
540 ((eq (caar pat) 'mleqp) (daddgq flag (sub (caddr pat) (cadr pat))))
541 ((eq (caar pat) 'mlessp) (daddgr flag (sub (caddr pat) (cadr pat))))
542 (flag (true* (munformat pat)))
544 (cond
545 ((eq (caar pat) '$kind)
546 (unkind (second pat) (third pat)))
547 (t (untrue (munformat pat))))
548 pat)))
550 ;;; When abs(x)<a is in the pattern, where a is a positive expression,
551 ;;; then learn x<a and -x<a too. The additional facts are put into the context
552 ;;; '$learndata, if the current context is user context 'initial
554 (defun learn-abs (pat flag)
555 (let (tmp)
556 (when (and (setq tmp (isinop pat 'mabs))
557 (or (and (member (caar pat) '(mlessp mleqp))
558 (isinop (cadr pat) 'mabs)
559 (member ($sign (caddr pat)) '($pos $pz)))
560 (and (member (caar pat) '(mgreaterp mgeqp))
561 (member ($sign (cadr pat)) '($pos $pz))
562 (isinop (caddr pat) 'mabs))))
563 (let ((oldcontext context))
564 (if (eq oldcontext '$initial)
565 (asscontext nil '$learndata)) ; switch to context '$learndata
566 ; learn additional facts
567 (learn ($substitute (cadr tmp) tmp pat) flag)
568 (learn ($substitute (mul -1 (cadr tmp)) tmp pat) flag)
569 (when (eq oldcontext '$initial)
570 (asscontext nil oldcontext) ; switch back to context on entry
571 ($activate '$learndata)))) ; context '$learndata is active
572 nil))
574 ;;; The value of a constant expression which can be numerically evaluated is
575 ;;; put into the context '$learndata.
577 (defun learn-numer (pat flag)
578 (let (dum expr patnew)
579 (do ((x (cdr pat) (cdr x)))
580 ((null x) (setq patnew (reverse patnew)))
581 (setq dum (constp (car x))
582 expr (car x))
583 (cond ((or (numberp (car x))
584 (ratnump (car x))))
585 ((eq dum 'bigfloat)
586 (if (prog2
587 (setq dum ($bfloat (car x)))
588 ($bfloatp dum))
589 (setq expr dum)))
590 ((eq dum 'float)
591 (if (and (setq dum (numer (car x)))
592 (numberp dum))
593 (setq expr dum)))
594 ((and (member dum '(numer symbol) :test #'eq)
595 (prog2
596 (setq dum (numer (car x)))
597 (or (null dum)
598 (and (numberp dum)
599 (prog2
600 (setq expr dum)
601 (< (abs dum) 1.0e-6))))))
602 (cond ($signbfloat
603 (and (setq dum ($bfloat (car x)))
604 ($bfloatp dum)
605 (setq expr dum))))))
606 (setq patnew (cons expr patnew)))
607 (setq patnew (cons (car pat) patnew))
608 (when (and (not (alike (cdr pat) (cdr patnew)))
609 (or (not (mnump (cadr patnew))) ; not both sides of the
610 (not (mnump (caddr patnew))))) ; relation can be number
611 (let ((oldcontext $context))
612 (if (eq oldcontext '$initial)
613 (asscontext nil '$learndata)) ; switch to context '$learndata
614 (learn patnew flag) ; learn additional fact
615 (when (eq oldcontext '$initial)
616 (asscontext nil oldcontext) ; switch back to context on entry
617 ($activate '$learndata)))) ; context '$learndata is active
618 nil))
620 (defmspec $forget (x)
621 (setq x (cdr x))
622 (do ((nl))
623 ((null x) (cons '(mlist) (nreverse nl)))
624 (cond ((atom (car x)) (push (forget (meval (car x))) nl))
625 ((eq 'mand (caaar x))
626 (mapc #'(lambda (l) (push (forget (meval l)) nl)) (cdar x)))
627 ((eq 'mnot (caaar x))
628 (push (forget (meval (pred-reverse (cadar x)))) nl))
629 ((eq 'mor (caaar x))
630 (merror (intl:gettext "forget: argument cannot be an 'or' expression; found ~M") (car x)))
631 (t (push (forget (meval (car x))) nl)))
632 (setq x (cdr x))))
634 (defun forget (pat)
635 (cond (($listp pat)
636 (cons '(mlist simp) (mapcar #'forget1 (cdr pat))))
637 (t (forget1 pat))))
639 (defun forget1 (pat)
640 (cond ((and (not (atom pat))
641 (eq (caar pat) 'mnot)
642 (eq (caaadr pat) '$equal))
643 (setq pat `(($notequal) ,@(cdadr pat)))))
644 (learn pat nil))
646 (defun restore-facts (factl) ; used by SAVE
647 (dolist (fact factl)
648 (cond ((eq (caar fact) '$kind)
649 (declarekind (cadr fact) (caddr fact))
650 (add2lnc (getop (cadr fact)) $props))
651 ((eq (caar fact) '$par))
652 (t (assume fact)))))
654 (defmacro compare (a b)
655 `(sign1 (sub* ,a ,b)))
657 (defun maximum (l)
658 (maximin l '$max))
660 (defun minimum (l)
661 (maximin l '$min))
663 (defmspec mand (form)
664 (setq form (cdr form))
665 (do ((l form (cdr l))
667 (unevaluated))
668 ((null l)
669 (cond ((= (length unevaluated) 0) t)
670 ((= (length unevaluated) 1) (car unevaluated))
671 (t (cons '(mand) (reverse unevaluated)))))
672 (setq x (mevalp (car l)))
673 (cond ((null x) (return nil))
674 ((not (member x '(t nil) :test #'eq)) (push x unevaluated)))))
676 (defmspec mor (form)
677 (setq form (cdr form))
678 (do ((l form (cdr l))
680 (unevaluated))
681 ((null l)
682 (cond ((= (length unevaluated) 0) nil)
683 ((= (length unevaluated) 1) (car unevaluated))
684 (t (cons '(mor) (reverse unevaluated)))))
685 (setq x (mevalp (car l)))
686 (cond ((eq x t) (return t))
687 ((not (member x '(t nil) :test #'eq)) (push x unevaluated)))))
689 (defmspec mnot (form)
690 (setq form (cdr form))
691 (let ((x (mevalp (car form))))
692 (if (member x '(t nil) :test #'eq)
693 (not x)
694 `((mnot) ,x))))
696 ;;;Toplevel functions- $askequal, $asksign, and $sign.
697 ;;;Switches- LIMITP If TRUE $ASKSIGN and $SIGN will look for special
698 ;;; symbols such as EPSILON, $INF, $MINF and attempt
699 ;;; to do the correct thing. In addition calls to
700 ;;; $REALPART and $IMAGPART are made to assure that
701 ;;; the expression is real.
703 ;;; if NIL $ASKSIGN and $SIGN assume the expression
704 ;;; given is real unless it contains an $%I, in which
705 ;;; case they call $RECTFORM.
707 (setq limitp nil)
709 (defmfun $askequal (a b)
710 (let ((answer (meqp (sratsimp a) (sratsimp b)))) ; presumably handles mbags and extended reals.
711 (cond ((eq answer t) '$yes)
712 ((eq answer nil) '$no)
714 (setq answer (retrieve `((mtext) ,(intl:gettext "Is ") ,a ,(intl:gettext " equal to ") ,b ,(intl:gettext "?")) nil))
715 (cond ((member answer '($no |$n| |$N|) :test #'eq)
716 (tdpn (sub b a))
717 '$no)
718 ((member answer '($yes |$y| |$Y|) :test #'eq)
719 (tdzero (sub a b))
720 '$yes)
722 (mtell (intl:gettext "Acceptable answers are yes, y, Y, no, n, N. ~%"))
723 ($askequal a b)))))))
725 (defmfun $asksign (exp)
726 (let (sign minus odds evens factored)
727 (asksign01 (cond (limitp (restorelim exp))
728 ((among '$%i exp) ($rectform exp))
729 (t exp)))))
731 (defun asksign-p-or-n (e)
732 (unwind-protect (prog2
733 (assume `(($notequal) ,e 0))
734 ($asksign e))
735 (forget `(($notequal) ,e 0))))
737 (defun asksign01 (a)
738 (let ((e (sign-prep a)))
739 (cond ((eq e '$pnz) '$pnz)
740 ((member (setq e (asksign1 e)) '($pos $neg) :test #'eq) e)
741 (limitp (eps-sign a))
742 (t '$zero))))
744 ;; csign returns t if x appears to be complex.
745 ;; Else, it returns the sign.
746 (defun csign (x)
747 (or (not (free x '$%i))
748 (let (sign-imag-errp limitp) (catch 'sign-imag-err ($sign x)))))
750 ;;; $csign works like $sign but switches the sign-functions into a complex
751 ;;; mode. In complex mode complex and imaginary expressions give the results
752 ;;; imaginary or complex.
754 (defmfun $csign (z)
755 (let ((*complexsign* t)
756 (limitp nil))
757 ($sign z)))
759 (defmfun $sign (x)
760 (let ((x (specrepcheck x))
761 sign minus odds evens factored)
762 (sign01 (cond (limitp (restorelim x))
763 (*complexsign*
764 ;; No rectform in Complex mode. Rectform ask unnecessary
765 ;; questions about complex expressions and can not handle
766 ;; imaginary expressions completely. Thus $csign can not
767 ;; handle something like (1+%i)*(1-%i) which is real.
768 ;; After improving rectform, we can change this. (12/2008)
769 (when *debug-compar*
770 (format t "~&$SIGN with ~A~%" x))
772 ((not (free x '$%i)) ($rectform x))
773 (t x)))))
775 (defun sign01 (a)
776 (let ((e (sign-prep a)))
777 (cond ((eq e '$pnz) '$pnz)
778 (t (setq e (sign1 e))
779 (if (and limitp (eq e '$zero)) (eps-sign a) e)))))
781 ;;; Preparation for asking questions from DEFINT or LIMIT.
782 (defun sign-prep (x)
783 (if limitp
784 (destructuring-let (((rpart . ipart) (trisplit x)))
785 (cond ((and (equal (sratsimp ipart) 0)
786 (free rpart '$infinity))
787 (setq x (nmr (sratsimp rpart)))
788 (if (free x 'prin-inf)
790 ($limit x 'prin-inf '$inf '$minus)))
791 (t '$pnz))) ; Confess ignorance if COMPLEX.
794 ;; don't ask about internal variables created by gruntz
795 (defun has-int-symbols (e)
796 (cond ((and (symbolp e) (get e 'internal))
798 ((atom e) nil)
799 (t (or (has-int-symbols (car e))
800 (has-int-symbols (cdr e))))))
802 ;;; Do substitutions for special symbols.
803 (defun nmr (a)
804 (unless (free a '$zeroa) (setq a ($limit a '$zeroa 0 '$plus)))
805 (unless (free a '$zerob) (setq a ($limit a '$zerob 0 '$minus)))
806 (unless (free a 'z**) (setq a ($limit a 'z** 0 '$plus)))
807 (unless (free a '*z*) (setq a ($limit a '*z* 0 '$plus)))
808 (unless (free a 'epsilon) (setq a ($limit a 'epsilon 0 '$plus)))
809 a) ;;; Give A back.
811 ;;; Get the sign of EPSILON-like terms. Could be made MUCH hairier.
812 (defun eps-sign (b)
813 (let (temp1 temp2 temp3 free1 free2 free3 limitp)
814 ;; unset limitp to prevent infinite recursion
815 (cond ((not (free b '$zeroa))
816 (setq temp1 (eps-coef-sign b '$zeroa)))
817 (t (setq free1 t)))
818 (cond ((not (free b '$zerob))
819 (setq temp2 (eps-coef-sign b '$zerob)))
820 (t (setq free2 t)))
821 (cond ((not (free b 'epsilon))
822 (setq temp3 (eps-coef-sign b 'epsilon)))
823 (t (setq free3 t)))
824 (cond ((and free1 free2 free3) '$zero)
825 ((or (not (null temp1)) (not (null temp2)) (not (null temp3)))
826 (cond ((and (null temp1) (null temp2)) temp3)
827 ((and (null temp2) (null temp3)) temp1)
828 ((and (null temp1) (null temp3)) temp2)
829 (t (merror (intl:gettext "asksign: internal error."))))))))
831 (defun eps-coef-sign (exp epskind)
832 (let ((eps-power ($lopow exp epskind)) eps-coef)
833 (cond ((and (not (equal eps-power 0))
834 (not (equal (setq eps-coef (ratcoeff exp epskind eps-power))
836 (eq (ask-integer eps-power '$integer) '$yes))
837 (cond ((eq (ask-integer eps-power '$even) '$yes)
838 ($sign eps-coef))
839 ((eq (ask-integer eps-power '$odd) '$yes)
840 (setq eps-coef ($sign eps-coef))
841 (cond ((or (and (eq eps-coef '$pos)
842 (or (eq epskind 'epsilon)
843 (eq epskind '$zeroa)))
844 (and (eq eps-coef '$neg)
845 (or (alike epskind (mul2* -1 'epsilon))
846 (eq epskind '$zerob))))
847 '$pos)
848 (t '$neg)))
849 (t (merror (intl:gettext "sign or asksign: insufficient information.")))))
850 (t (let ((deriv (sdiff exp epskind)) deriv-sign)
851 (cond ((not (eq (setq deriv-sign ($sign deriv)) '$zero))
852 (total-sign epskind deriv-sign))
853 ((not
854 (eq (let ((deriv (sdiff deriv epskind)))
855 (setq deriv-sign ($sign deriv)))
856 '$zero))
857 deriv-sign)
858 (t (merror (intl:gettext "sign or asksign: insufficient data.")))))))))
860 ;;; The above code does a partial Taylor series analysis of something
861 ;;; that isn't a polynomial.
863 (defun total-sign (epskind factor-sign)
864 (cond ((or (eq epskind '$zeroa) (eq epskind 'epsilon))
865 (cond ((eq factor-sign '$pos) '$pos)
866 ((eq factor-sign '$neg) '$neg)
867 ((eq factor-sign '$zero) '$zero)))
868 ((eq epskind '$zerob)
869 (cond ((eq factor-sign '$pos) '$neg)
870 ((eq factor-sign '$neg) '$pos)
871 ((eq factor-sign '$zero) '$zero)))))
873 (defun asksign (x)
874 (setq x ($asksign x))
875 (cond ((eq '$pos x) '$positive)
876 ((eq '$neg x) '$negative)
877 ((eq '$pnz x) '$pnz) ;COMPLEX expression encountered here.
878 (t '$zero)))
880 (defun asksign1 ($askexp)
881 (let ($radexpand)
882 (sign1 $askexp))
883 (cond
884 ((has-int-symbols $askexp) '$pnz)
885 ((member sign '($pos $neg $zero $imaginary) :test #'eq) sign)
887 (let ((domain sign) (squared nil))
888 (cond
889 ((null odds)
890 (setq $askexp (lmul evens)
891 domain '$znz
892 squared t))
894 (if minus (setq sign (flip sign)))
895 (setq $askexp
896 (lmul (nconc odds (mapcar #'(lambda (l) (power l 2)) evens))))))
897 (setq sign (cdr (assol $askexp *local-signs*)))
898 (ensure-sign $askexp domain squared)))))
900 (defun match-sign (sgn domain expression squared)
901 "If SGN makes sense for DOMAIN store the result (see ENSURE-SIGN) and return
902 it. Otherwise, return NIL. If SQUARED is true, we are actually looking for the
903 sign of the square, so any negative results are converted to positive."
904 ;; The entries in BEHAVIOUR are of the form
905 ;; (MATCH DOMAINS REGISTRAR SIGN SIGN-SQ)
907 ;; The algorithm goes as follows:
909 ;; Look for SGN in MATCH. If found, use REGISTRAR to store SIGN for the
910 ;; expression and then return SIGN if SQUARED is false or SIGN-SQ if it is
911 ;; true.
912 (let* ((behaviour
913 '((($pos |$P| |$p| $positive) (nil $znz $pz $pn $pnz) tdpos $pos $pos)
914 (($neg |$N| |$n| $negative) (nil $znz $nz $pn $pnz) tdneg $neg $pos)
915 (($zero |$Z| |$z| 0 0.0) (nil $znz $pz $nz $pnz) tdzero $zero $zero)
916 (($pn $nonzero $nz $nonz $non0) ($znz) tdpn $pn $pos)))
917 (hit (find-if (lambda (bh)
918 (and (member sgn (first bh) :test #'equal)
919 (member domain (second bh) :test #'eq)))
920 behaviour)))
921 (when hit
922 (let ((registrar (third hit))
923 (found-sign (if squared (fifth hit) (fourth hit))))
924 (funcall registrar expression)
925 (setq sign
926 (if minus (flip found-sign) found-sign))))))
928 (defun ensure-sign (expr &optional domain squared)
929 "Try to determine the sign of EXPR. If DOMAIN is not one of the special values
930 described below, we try to tell whether EXPR is positive, negative or zero. It
931 can be more specialised ($pz => positive or zero; $nz => negative or zero; $pn
932 => positive or negative; $znz => zero or nonzero).
934 If SQUARED is true, then we're actually interested in the sign of EXPR^2. As
935 such, a nonzero sign should be regarded as positive.
937 When calling ENSURE-SIGN, set the special variable SIGN to the best current
938 guess for the sign of EXPR. The function returns the sign, calls one of (TDPOS
939 TDNEG TDZERO TDPN) to store it, and also sets SIGN."
940 (loop
941 (let ((new-sign (match-sign sign domain expr squared)))
942 (when new-sign (return new-sign)))
943 (setf sign (retrieve
944 (list '(mtext)
945 "Is " expr
946 (or (second
947 (assoc domain
948 '(($znz " zero or nonzero?")
949 ($pz " positive or zero?")
950 ($nz " negative or zero?")
951 ($pn " positive or negative?"))))
952 " positive, negative or zero?"))
953 nil))))
955 ;; During one evaluation phase asksign writes answers from the user into the
956 ;; global context '$initial. These facts are removed by clearsign after
957 ;; finishing the evaluation phase. clearsign is called from the top-level
958 ;; evaluation function meval*. The facts which have to be removed are stored
959 ;; in the global variable *local-signs*.
961 (defun clearsign ()
962 (let ((context '$initial))
963 (dolist (cons-pair *local-signs*)
964 (destructuring-bind (x . sgn) cons-pair
965 (cond
966 ((eq '$pos sgn) (daddgr nil x))
967 ((eq '$neg sgn) (daddgr nil (neg x)))
968 ((eq '$zero sgn) (daddeq nil x))
969 ((eq '$pn sgn) (daddnq nil x))
970 ((eq '$pz sgn) (daddgq nil x))
971 ((eq '$nz sgn) (daddgq nil (neg x))))))
972 (setf *local-signs* nil)))
974 (defun like (x y)
975 (alike1 (specrepcheck x) (specrepcheck y)))
977 (setf (get '$und 'sysconst) t)
978 (setf (get '$ind 'sysconst) t)
979 (setf (get '$zeroa 'sysconst) t)
980 (setf (get '$zerob 'sysconst) t)
982 ;; There have been some conversations about NaN on the list, but
983 ;; the issue hasn't been settled.
985 (defvar indefinites `($und $ind))
987 ;; Other than sums, products, and lambda forms, meqp knows nothing
988 ;; about dummy variables. Because of the way niceindices chooses names
989 ;; for the sum indices, it's necessary to locally assign a new value to
990 ;; niceindicespref.
992 (defun meqp-by-csign (z a b)
993 (let (($niceindicespref `((mlist) ,(gensym) ,(gensym) ,(gensym))))
994 (setq z ($niceindices z))
995 (setq z (if ($constantp z) ($rectform z) (sratsimp z)))
996 (let ((sgn ($csign z))
997 (dunno `(($equal) ,a ,b)))
998 (cond ((eq '$zero sgn) t)
999 ((memq sgn '($pos $neg $pn)) nil)
1001 ;; previously checked also for (linearp z '$%i))
1002 ((memq sgn '($complex $imaginary))
1003 ;; We call trisplit here, which goes back to general evaluation and
1004 ;; could cause an infinite recursion. To make sure that doesn't
1005 ;; happen, use the with-safe-recursion macro.
1006 (handler-case
1007 (with-safe-recursion meqp-by-csign z
1008 (let* ((ri-parts (trisplit z))
1009 (rsgn ($csign (car ri-parts)))
1010 (isgn ($csign (cdr ri-parts))))
1011 (cond ((and (eq '$zero rsgn)
1012 (eq '$zero isgn)) t)
1014 ((or (memq rsgn '($neg $pos $pn))
1015 (memq isgn '($neg $pos $pn))) nil)
1017 (t dunno))))
1018 (unsafe-recursion () dunno)))
1020 (t dunno)))))
1022 ;; For each fact of the form equal(a,b) in the active context, do e : ratsubst(b,a,e).
1024 (defun equal-facts-simp (e)
1025 (let ((f (margs ($facts))))
1026 (dolist (fi f e)
1027 (if (op-equalp fi '$equal)
1028 (setq e ($ratsubst (nth 2 fi) (nth 1 fi) e))))))
1030 (defun maxima-declared-arrayp (x)
1031 (and
1032 (symbolp x)
1033 (mget x 'array)
1034 (get (mget x 'array) 'array)))
1036 (defun maxima-undeclared-arrayp (x)
1037 (and
1038 (symbolp x)
1039 (mget x 'hashar)
1040 (get (mget x 'hashar) 'array)))
1042 (defun meqp (a b)
1043 ;; Check for some particular types before falling into the general case.
1044 (cond ((stringp a)
1045 (and (stringp b) (equal a b)))
1046 ((stringp b) nil)
1047 ((arrayp a)
1048 (and (arrayp b) (array-meqp a b)))
1049 ((arrayp b) nil)
1050 ((maxima-declared-arrayp a)
1051 (and (maxima-declared-arrayp b) (maxima-declared-array-meqp a b)))
1052 ((maxima-declared-arrayp b) nil)
1053 ((maxima-undeclared-arrayp a)
1054 (and (maxima-undeclared-arrayp b) (maxima-undeclared-array-meqp a b)))
1055 ((maxima-undeclared-arrayp b) nil)
1057 (let ((z) (sign))
1058 (setq a (specrepcheck a))
1059 (setq b (specrepcheck b))
1060 (cond ((or (like a b)) (not (member a indefinites)))
1061 ((or (member a indefinites) (member b indefinites)
1062 (member a *infinities*) (member b *infinities*)) nil)
1063 ((and (symbolp a) (or (eq t a) (eq nil a) (get a 'sysconst))
1064 (symbolp b) (or (eq t b) (eq nil b) (get b 'sysconst))) nil)
1065 ((or (mbagp a) (mrelationp a) (mbagp b) (mrelationp b))
1066 (cond ((and (or (and (mbagp a) (mbagp b)) (and (mrelationp a) (mrelationp b)))
1067 (eq (mop a) (mop b)) (= (length (margs a)) (length (margs b))))
1068 (setq z (list-meqp (margs a) (margs b)))
1069 (if (or (eq z t) (eq z nil)) z `(($equal) ,a ,b)))
1070 (t nil)))
1071 ((and (op-equalp a 'lambda) (op-equalp b 'lambda)) (lambda-meqp a b))
1072 (($setp a) (set-meqp a b))
1073 ;; 0 isn't in the range of an exponential function.
1074 ((or (and (mexptp a) (not (eq '$minf (third a))) (zerop1 b) (eq t (mnqp (second a) 0)))
1075 (and (mexptp b) (not (eq '$minf (third b))) (zerop1 a) (eq t (mnqp (second b) 0))))
1076 nil)
1078 ;; DCOMPARE emits new stuff (via DINTERNP) into the assume database.
1079 ;; Let's avoid littering the database with numbers.
1080 ((and (mnump a) (mnump b)) (zerop1 (sub a b)))
1082 ;; lookup in assumption database
1083 ((and (dcompare a b) (eq '$zero sign))) ; dcompare sets sign
1084 ((memq sign '($pos $neg $pn)) nil)
1086 ;; if database lookup failed, apply all equality facts
1087 (t (meqp-by-csign (equal-facts-simp (sratsimp (sub a b))) a b)))))))
1089 ;; Two arrays are equal (according to MEQP)
1090 ;; if (1) they have the same dimensions,
1091 ;; and (2) their elements are MEQP.
1093 (defun array-meqp (p q)
1094 (and
1095 (equal (array-dimensions p) (array-dimensions q))
1096 (progn
1097 (dotimes (i (array-total-size p))
1098 (let ((z (let ($ratprint)
1099 (declare (special $ratprint))
1100 (meqp (row-major-aref p i) (row-major-aref q i)))))
1101 (cond ((eq z nil) (return-from array-meqp nil))
1102 ((eq z t))
1103 (t (return-from array-meqp `(($equal) ,p ,q))))))
1104 t)))
1106 (defun maxima-declared-array-meqp (p q)
1107 (array-meqp (get (mget p 'array) 'array) (get (mget q 'array) 'array)))
1109 (defun maxima-undeclared-array-meqp (p q)
1110 (and
1111 (alike1 (mfuncall '$arrayinfo p) (mfuncall '$arrayinfo q))
1112 (let ($ratprint)
1113 (declare (special $ratprint))
1114 (meqp ($listarray p) ($listarray q)))))
1116 (defun list-meqp (p q)
1117 (let ((z))
1118 (cond ((or (null p) (null q)) (and (null p) (null q)))
1120 (setq z (meqp (car p) (car q)))
1121 (cond ((eq z nil) nil)
1122 ((or (eq z '$unknown) (op-equalp z '$equal)) z)
1123 (t (list-meqp (cdr p) (cdr q))))))))
1125 (defun lambda-meqp (a b)
1126 (let ((z))
1127 (cond ((= (length (second a)) (length (second b)))
1128 (let ((x) (n ($length (second a))))
1129 (dotimes (i n (push '(mlist) x)) (push (gensym) x))
1130 (setq z (meqp (mfuncall '$apply a x) (mfuncall '$apply b x)))
1131 (if (or (eq t z) (eq nil z)) z `(($equal) ,a ,b))))
1132 (t nil))))
1134 (defun set-meqp (a b)
1135 (let ((aa (equal-facts-simp a))
1136 (bb (equal-facts-simp b)))
1137 (cond ((or (not ($setp bb))
1138 (and ($emptyp aa) (not ($emptyp bb)))
1139 (and ($emptyp bb) (not ($emptyp aa))))
1140 nil)
1141 ((and (= (length aa) (length bb))
1142 (every #'(lambda (p q) (eq t (meqp p q))) (margs aa) (margs bb))) t)
1143 ((set-not-eqp (margs aa) (margs bb)) nil)
1144 (t `(($equal ,a ,b))))))
1146 (defun set-not-eqp (a b)
1147 (catch 'done
1148 (dolist (ak a)
1149 (if (every #'(lambda (s) (eq nil (meqp ak s))) b) (throw 'done t)))
1150 (dolist (bk b)
1151 (if (every #'(lambda (s) (eq nil (meqp bk s))) a) (throw 'done t)))
1152 (throw 'done nil)))
1154 ;; MGRP-GENERAL applies when sign of A - B (possibly complex) makes sense.
1156 (defun mgrp-general (a b)
1157 (setq a (sub a b))
1158 (let ((sgn (csign a)))
1159 (cond ((eq sgn '$pos) t)
1160 ((eq sgn t) nil) ;; csign thinks a - b isn't real
1161 ((member sgn '($neg $zero $nz) :test #'eq) nil)
1162 (t `((mgreaterp) ,a 0)))))
1164 (defun mgrp (a b)
1165 (cond
1166 ((or (stringp a) (stringp b))
1167 (if (and (stringp a) (stringp b)) (not (null (string< b a)))
1168 (when $prederror
1169 (merror (intl:gettext "greater than: arguments are incomparable; found: ~:M, ~:M") a b))))
1170 (t (mgrp-general a b))))
1172 (defun mlsp (x y)
1173 (mgrp y x))
1175 ;; MGQP-GENERAL applies when sign of A - B (possibly complex) makes sense.
1177 (defun mgqp-general (a b)
1178 (setq a (sub a b))
1179 (let ((sgn (csign a)))
1180 (cond ((member sgn '($pos $zero $pz) :test #'eq) t)
1181 ((eq sgn t) nil) ;; csign thinks a - b isn't real
1182 ((eq sgn '$neg) nil)
1183 (t `((mgeqp) ,a 0)))))
1185 (defun mgqp (a b)
1186 (cond
1187 ((or (stringp a) (stringp b))
1188 (if (and (stringp a) (stringp b)) (not (null (string<= b a)))
1189 (when $prederror
1190 (merror (intl:gettext "greater than or equal: arguments are incomparable; found: ~:M, ~:M") a b))))
1191 (t (mgqp-general a b))))
1193 (defun mnqp (x y)
1194 (let ((b (meqp x y)))
1195 (cond ((eq b '$unknown) b)
1196 ((or (eq b t) (eq b nil)) (not b))
1197 (t `(($notequal) ,x ,y)))))
1199 (defun c-$pn (o e)
1200 (list '(mnot) (c-$zero o e)))
1202 (defun c-$zero (o e)
1203 (list '($equal) (lmul (nconc o e)) 0))
1205 (defun c-$pos (o e)
1206 (cond ((null o) (list '(mnot) (list '($equal) (lmul e) 0)))
1207 ((null e) (list '(mgreaterp) (lmul o) 0))
1208 (t (setq e (mapcar #'(lambda (l) (power l 2)) e))
1209 (list '(mgreaterp) (lmul (nconc o e)) 0))))
1211 (defun c-$pz (o e)
1212 (cond ((null o) (list '(mnot) (list '($equal) (lmul e) 0)))
1213 ((null e) (list '(mgeqp) (lmul o) 0))
1214 (t (setq e (mapcar #'(lambda (l) (power l 2)) e))
1215 (list '(mgeqp) (lmul (nconc o e)) 0))))
1217 (defun sign* (x)
1218 (let (sign minus odds evens)
1219 (sign1 x)))
1221 (defun infsimp* (e)
1222 (if (or ($mapatom e) (and (free e '$inf) (free e '$minf)))
1224 (infsimp e)))
1226 ;; Like WITH-COMPSPLT, but runs COMPSPLT-EQ instead
1227 (defmacro with-compsplt-eq ((lhs rhs x) &body forms)
1228 `(multiple-value-bind (,lhs ,rhs) (compsplt-eq ,x)
1229 ,@forms))
1231 ;; Call FORMS with LHS and RHS bound to the splitting of EXPR by COMPSPLT.
1232 (defmacro with-compsplt ((lhs rhs expr) &body forms)
1233 `(multiple-value-bind (,lhs ,rhs) (compsplt ,expr)
1234 ,@forms))
1236 (defun sign1 (x)
1237 (setq x (specrepcheck x))
1238 (setq x (infsimp* x))
1239 (when (and *complexsign* (atom x) (eq x '$infinity))
1240 ;; In Complex Mode the sign of infinity is complex.
1241 (when *debug-compar* (format t "~& in sign1 detect $infinity.~%"))
1242 (return-from sign1 '$complex))
1243 (if (member x '($und $ind $infinity) :test #'eq)
1244 (if limitp '$pnz (merror (intl:gettext "sign: sign of ~:M is undefined.") x)))
1245 (prog (dum exp)
1246 (setq dum (constp x) exp x)
1247 (cond ((or (numberp x) (ratnump x)))
1248 ((eq dum 'bigfloat)
1249 (if (prog2 (setq dum ($bfloat x)) ($bfloatp dum))
1250 (setq exp dum)))
1251 ((eq dum 'float)
1252 (if (and (setq dum (numer x)) (numberp dum)) (setq exp dum)))
1253 ((and (member dum '(numer symbol) :test #'eq)
1254 (prog2 (setq dum (numer x))
1255 (or (null dum)
1256 (and (numberp dum)
1257 (prog2 (setq exp dum)
1258 (< (abs dum) 1.0e-6))))))
1259 (cond ($signbfloat
1260 (and (setq dum ($bfloat x)) ($bfloatp dum) (setq exp dum)))
1261 (t (setq sign '$pnz evens nil odds (ncons x) minus nil)
1262 (return sign)))))
1263 (or (and (not (atom x)) (not (mnump x)) (equal x exp)
1264 (let (s o e m)
1265 (with-compsplt (lhs rhs x)
1266 (dcompare lhs rhs)
1267 (cond ((member sign '($pos $neg) :test #'eq))
1268 ((eq sign '$pnz) nil)
1269 (t (setq s sign o odds e evens m minus)
1270 (sign x)
1271 (if (not (strongp sign s))
1272 (if (and (eq sign '$pnz) (eq s '$pn))
1273 (setq sign s)
1274 (setq sign s odds o evens e minus m)))
1275 t)))))
1276 (sign exp))
1277 (return sign)))
1279 (defun numer (x)
1280 (let (($numer t) ; currently, no effect on $float, but proposed to
1281 ($ratprint nil)
1282 result)
1283 ;; Catch a Lisp error, if a floating point overflow occurs.
1284 (setq result (let ((errset nil)) (errset ($float x))))
1285 (if result (car result) nil)))
1287 (defun constp (x)
1288 (cond ((floatp x) 'float)
1289 ((numberp x) 'numer)
1290 ((symbolp x) (if (member x '($%pi $%e $%phi $%gamma $%catalan) :test #'eq) 'symbol))
1291 ((atom x) nil)
1292 ((eq (caar x) 'rat) 'numer)
1293 ((eq (caar x) 'bigfloat) 'bigfloat)
1294 ((specrepp x) (constp (specdisrep x)))
1295 (t (do ((l (cdr x) (cdr l)) (dum) (ans 'numer))
1296 ((null l) ans)
1297 (setq dum (constp (car l)))
1298 (cond ((eq dum 'float) (return 'float))
1299 ((eq dum 'numer))
1300 ((eq dum 'bigfloat) (setq ans 'bigfloat))
1301 ((eq dum 'symbol)
1302 (if (eq ans 'numer) (setq ans 'symbol)))
1303 (t (return nil)))))))
1305 (mapcar #'(lambda (s) (putprop (first s) (second s) 'sign-function))
1306 (list
1307 (list 'mtimes 'sign-mtimes)
1308 (list 'mplus 'sign-mplus)
1309 (list 'mexpt 'sign-mexpt)
1310 (list '%log 'sign-log)
1311 (list 'mabs 'sign-mabs)
1312 (list '$min #'(lambda (x) (sign-minmax (caar x) (cdr x))))
1313 (list '$max #'(lambda (x) (sign-minmax (caar x) (cdr x))))
1314 (list '%csc #'(lambda (x) (sign (inv* (cons (ncons (zl-get (caar x) 'recip)) (cdr x))))))
1315 (list '%csch #'(lambda (x) (sign (inv* (cons (ncons (zl-get (caar x) 'recip)) (cdr x))))))
1316 (list '%acos 'sign-asin/acos/atanh)
1317 (list '%asin 'sign-asin/acos/atanh)
1318 (list '%acosh 'sign-acosh)
1319 (list '%atanh 'sign-asin/acos/atanh)
1321 (list '%signum #'(lambda (x) (sign (cadr x))))
1322 (list '%erf #'(lambda (x) (sign (cadr x))))
1323 (list '$li #'(lambda (x)
1324 (let ((z (first (margs x))) (n (cadadr x)))
1325 (if (and (mnump n) (eq t (mgrp z 0)) (eq t (mgrp 1 z))) (sign z) (sign-any x)))))))
1326 (defun sign (x)
1327 (cond ((mnump x) (setq sign (rgrp x 0) minus nil odds nil evens nil))
1328 ((and *complexsign* (symbolp x) (eq x '$%i))
1329 ;; In Complex Mode the sign of %i is $imaginary.
1330 (setq sign '$imaginary))
1331 ((symbolp x) (if (eq x '$%i) (imag-err x)) (sign-any x))
1332 ((and (consp x) (symbolp (caar x)) (not (specrepp x)) (get (caar x) 'sign-function))
1333 (funcall (get (caar x) 'sign-function) x))
1334 ((and (consp x) (not (specrepp x)) ($subvarp (mop x)) (get (mop (mop x)) 'sign-function))
1335 (funcall (get (mop (mop x)) 'sign-function) x))
1336 ((specrepp x) (sign (specdisrep x)))
1337 ((kindp (caar x) '$posfun) (sign-posfun x))
1338 ((kindp (caar x) '$oddfun) (sign-oddfun x))
1339 (t (sign-any x))))
1341 (defun sign-any (x)
1342 (cond ((and *complexsign*
1343 (symbolp x)
1344 (decl-complexp x))
1345 ;; In Complex Mode look for symbols declared to be complex.
1346 (if ($featurep x '$imaginary)
1347 (setq sign '$imaginary)
1348 (setq sign '$complex)))
1349 ((and *complexsign*
1350 (not (atom x))
1351 (decl-complexp (caar x)))
1352 ;; A function f(x), where f is declared to be imaginary or complex.
1353 (if ($featurep (caar x) '$imaginary)
1354 (setq sign '$imaginary)
1355 (setq sign '$complex)))
1357 (dcompare x 0)
1358 (if (and $assume_pos
1359 (member sign '($pnz $pz $pn) :test #'eq)
1360 (if $assume_pos_pred
1361 (let ((*x* x))
1362 (declare (special *x*))
1363 (is '(($assume_pos_pred) *x*)))
1364 (mapatom x)))
1365 (setq sign '$pos))
1366 (setq minus nil evens nil
1367 odds (if (not (member sign '($pos $neg $zero) :test #'eq))
1368 (ncons x))))))
1370 (defun sign-mtimes (x)
1371 (setq x (cdr x))
1372 (do ((s '$pos) (m) (o) (e)) ((null x) (setq sign s minus m odds o evens e))
1373 (sign1 (car x))
1374 (cond ((eq sign '$zero) (return t))
1375 ((and *complexsign* (eq sign '$complex))
1376 ;; Found a complex factor. We don't return immediately
1377 ;; because another factor could be zero.
1378 (setq s '$complex))
1379 ((and *complexsign* (eq s '$complex))) ; continue the loop
1380 ((and *complexsign* (eq sign '$imaginary))
1381 ;; Found an imaginary factor. Look if we have already one.
1382 (cond ((eq s '$imaginary)
1383 ;; imaginary*imaginary is real. But remember the sign in m.
1384 (setq s (if m '$pos '$neg) m (not m)))
1385 (t (setq s sign))))
1386 ((and *complexsign* (eq s '$imaginary))) ; continue the loop
1387 ((eq sign '$pos))
1388 ((eq sign '$neg) (setq s (flip s) m (not m)))
1389 ((prog2 (setq m (not (eq m minus)) o (nconc odds o) e (nconc evens e))
1390 nil))
1391 ((eq s sign) (when (eq s '$nz) (setq s '$pz)))
1392 ((eq s '$pos) (setq s sign))
1393 ((eq s '$neg) (setq s (flip sign)))
1394 ((or (and (eq s '$pz) (eq sign '$nz))
1395 (and (eq s '$nz) (eq sign '$pz)))
1396 (setq s '$nz))
1397 (t (setq s '$pnz)))
1398 (setq x (cdr x))))
1400 (defun sign-mplus (x &aux s o e m)
1401 (cond ((signdiff x))
1402 ((prog2 (setq s sign e evens o odds m minus) nil))
1403 ((signsum x))
1404 ((prog2 (cond ((strongp s sign))
1405 (t (setq s sign e evens o odds m minus)))
1406 nil))
1407 ((and (not factored) (signfactor x)))
1408 ((strongp sign s))
1409 (t (setq sign s evens e odds o minus m))))
1411 (defun signdiff (x)
1412 (setq sign '$pnz)
1413 (let ((swapped nil) (retval))
1414 (with-compsplt (lhs rhs x)
1415 (if (and (mplusp lhs) (equal rhs 0) (null (cdddr lhs)))
1416 (cond ((and (negp (cadr lhs)) (not (negp (caddr lhs))))
1417 (setq rhs (neg (cadr lhs)) lhs (caddr lhs)))
1418 ;; The following fixes SourceForge bug #3148
1419 ;; where sign(a-b) returned pnz and sign(b-a) returned pos.
1420 ;; Previously, only the case (-a)+b was handled.
1421 ;; Now we also handle a+(-b) by swapping lhs and rhs,
1422 ;; setting a flag "swapped", running through the same code and
1423 ;; then flipping the answer.
1424 ((and (negp (caddr lhs)) (not (negp (cadr lhs))))
1425 (setq rhs (cadr lhs) lhs (neg (caddr lhs)) swapped t))))
1426 (let (dum)
1427 (setq retval
1428 (cond ((or (equal rhs 0) (mplusp lhs)) nil)
1429 ((and (member (constp rhs) '(numer symbol) :test #'eq)
1430 (numberp (setq dum (numer rhs)))
1431 (prog2 (setq rhs dum) nil)))
1432 ((mplusp rhs) nil)
1433 ((and (dcompare lhs rhs) (member sign '($pos $neg $zero) :test #'eq)))
1434 ((and (not (atom lhs)) (not (atom rhs))
1435 (eq (caar lhs) (caar rhs))
1436 (kindp (caar lhs) '$increasing))
1437 (sign (sub (cadr lhs) (cadr rhs)))
1439 ((and (not (atom lhs)) (not (atom rhs))
1440 (eq (caar lhs) (caar rhs))
1441 (kindp (caar lhs) '$decreasing))
1442 (sign (sub (cadr rhs) (cadr lhs)))
1444 ((and (not (atom lhs)) (eq (caar lhs) 'mabs)
1445 (alike1 (cadr lhs) rhs))
1446 (setq sign '$pz minus nil odds nil evens nil) t)
1447 ((signdiff-special lhs rhs))))))
1448 (if swapped
1449 (setq sign (flip sign)))
1450 retval))
1452 (defun signdiff-special (xlhs xrhs)
1453 ;; xlhs may be a constant
1454 (let ((sgn nil))
1455 (when (or (and (realp xrhs) (minusp xrhs)
1456 (not (atom xlhs)) (eq (sign* xlhs) '$pos))
1457 ; e.g. sign(a^3+%pi-1) where a>0
1458 (and (mexptp xlhs)
1459 ;; e.g. sign(%e^x-1) where x>0
1460 (eq (sign* (caddr xlhs)) '$pos)
1461 (or (and
1462 ;; Q^Rpos - S, S<=1, Q>1
1463 (member (sign* (sub 1 xrhs)) '($pos $zero $pz) :test #'eq)
1464 (eq (sign* (sub (cadr xlhs) 1)) '$pos))
1465 (and (not (eq $domain '$complex))
1466 ;; Qpos ^ Rpos - Spos => Qpos - Spos^(1/Rpos)
1467 (eq (sign* (cadr xlhs)) '$pos)
1468 (eq (sign* xrhs) '$pos)
1469 (eq (sign* (sub (cadr xlhs)
1470 (power xrhs (div 1 (caddr xlhs)))))
1471 '$pos))))
1472 (and (mexptp xlhs) (mexptp xrhs)
1473 ;; Q^R - Q^T, Q>1, (R-T) > 0
1474 ;; e.g. sign(2^x-2^y) where x>y
1475 (alike1 (cadr xlhs) (cadr xrhs))
1476 (eq (sign* (sub (cadr xlhs) 1)) '$pos)
1477 (eq (sign* (sub (caddr xlhs) (caddr xrhs))) '$pos)))
1478 (setq sgn '$pos))
1480 ;; sign(sin(x)+c)
1481 (when (and (not (atom xlhs))
1482 (member (caar xlhs) '(%sin %cos))
1483 (zerop1 ($imagpart (cadr xlhs))))
1484 (cond ((eq (sign* (add xrhs 1)) '$neg) ;; c > 1
1485 (setq sgn '$pos))
1486 ((eq (sign* (add xrhs -1)) '$pos) ;; c < -1
1487 (setq sgn '$neg))
1488 ((zerop1 (add xrhs 1)) ;; c = 1
1489 (setq sgn '$pz))
1490 ((zerop1 (add xrhs -1)) ;; c = -1
1491 (setq sgn '$nz))))
1493 ;; sign(signum(x)+c)
1494 (when (and (not (atom xlhs))
1495 (eq (caar xlhs) '%signum)
1496 (zerop1 ($imagpart (cadr xlhs))))
1497 (cond ((eq (sign* (add xrhs 1)) '$neg) ;; c > 1
1498 (setq sgn '$pos))
1499 ((eq (sign* (add xrhs -1)) '$pos) ;; c < -1
1500 (setq sgn '$neg))
1501 ((zerop1 (add xrhs 1)) ;; c = 1
1502 (setq sgn '$pz))
1503 ((zerop1 (add xrhs -1)) ;; c = -1
1504 (setq sgn '$nz))
1505 ((zerop1 xrhs) ;; c = 0 (necessary?)
1506 (setq sgn '$pnz))
1507 (t ;; -1 < c < 1, but c # 0
1508 (setq sgn '$pn))))
1510 (when (and $useminmax (or (minmaxp xlhs) (minmaxp xrhs)))
1511 (setq sgn (signdiff-minmax xlhs xrhs)))
1512 (when sgn (setq sign sgn minus nil odds nil evens nil)
1513 t)))
1515 ;;; Look for symbols with an assumption a > n or a < -n, where n is a number.
1516 ;;; For this case shift the symbol a -> a+n in a summation and multiplication.
1517 ;;; This handles cases like a>1 and b>1 gives sign(a+b-2) -> pos.
1519 (defun sign-shift (expr)
1520 (do ((l (cdr expr) (cdr l))
1521 (fl nil)
1522 (acc nil))
1523 ((null l) (addn acc nil))
1524 (cond ((symbolp (car l))
1525 ;; Get the facts related to the symbol (car l)
1526 ;; Reverse the order to test the newest facts first.
1527 (setq fl (reverse (cdr (facts1 (car l)))))
1528 (push (car l) acc)
1529 (dolist (f fl)
1530 (cond ((and (eq (caar f) 'mgreaterp)
1531 (mnump (caddr f))
1532 (eq ($sign (caddr f)) '$pos))
1533 ;; The case a > n, where a is a symbol and n a number.
1534 ;; Add the number to the list of terms.
1535 (return (push (caddr f) acc)))
1536 ((and (eq (caar f) 'mgreaterp)
1537 (mnump (cadr f))
1538 (eq ($sign (cadr f)) '$neg))
1539 ;; The case a < -n, where a is a symbol and n a number.
1540 ;; Add the number to the list of terms.
1541 (return (push (cadr f) acc))))))
1542 ((mtimesp (car l))
1543 (let ((acctimes) (flag))
1544 ;; Go through the factors of the multiplication.
1545 (dolist (ll (cdar l))
1546 (cond ((symbolp ll)
1547 ;; Get the facts related to the symbol (car l)
1548 ;; Reverse the order to test the newest facts first.
1549 (setq fl (reverse (cdr (facts1 ll))))
1550 (dolist (f fl)
1551 (cond ((and (eq (caar f) 'mgreaterp)
1552 (mnump (caddr f))
1553 (eq ($sign (caddr f)) '$pos))
1554 ;; The case a > n, where a is a symbol and n a
1555 ;; number. Add the number to the list of terms.
1556 (setq flag t)
1557 (return (push (add ll (caddr f)) acctimes)))
1558 ((and (eq (caar f) 'mgreaterp)
1559 (mnump (cadr f))
1560 (eq ($sign (cadr f)) '$neg))
1561 ;; The case a < -n, where a is a symbol and n a
1562 ;; number. Add the number to the list of terms.
1563 (setq flag t)
1564 (return (push (add ll (cadr f)) acctimes)))))
1565 (when (not flag) (push ll acctimes)))
1567 (push ll acctimes))))
1568 (if flag
1569 ;; If a shift has been done expand the factors.
1570 (push ($multthru (muln acctimes nil)) acc)
1571 (push (muln acctimes nil) acc))))
1573 (push (car l) acc)))))
1575 (defun signsum (x)
1576 (setq x (sign-shift x))
1577 ;; x might be simplified to an atom in sign-shift.
1578 (when (atom x) (setq x (cons '(mplus) (list x))))
1579 (do ((l (cdr x) (cdr l)) (s '$zero))
1580 ((null l) (setq sign s minus nil odds (list x) evens nil)
1581 (cond (*complexsign*
1582 ;; Because we have continued the loop in Complex Mode
1583 ;; we have to look for the sign '$pnz and return nil.
1584 (if (eq s '$pnz) nil t))
1585 (t t))) ; in Real Mode return T
1586 ;; Call sign1 and not sign, because sign1 handles constant expressions.
1587 (sign1 (car l))
1588 (cond ((and *complexsign*
1589 (or (eq sign '$complex) (eq sign '$imaginary)))
1590 ;; Found a complex or imaginary expression. The sign is $complex.
1591 (setq sign '$complex odds nil evens nil minus nil)
1592 (return t))
1593 ((or (and (eq sign '$zero)
1594 (setq x (sub x (car l))))
1595 (and (eq s sign) (not (eq s '$pn))) ; $PN + $PN = $PNZ
1596 (and (eq s '$pos) (eq sign '$pz))
1597 (and (eq s '$neg) (eq sign '$nz))))
1598 ((or (and (member sign '($pz $pos) :test #'eq) (member s '($zero $pz) :test #'eq))
1599 (and (member sign '($nz $neg) :test #'eq) (member s '($zero $nz) :test #'eq))
1600 (and (eq sign '$pn) (eq s '$zero)))
1601 (setq s sign))
1603 (cond (*complexsign*
1604 ;; In Complex Mode we have to continue the loop to look further
1605 ;; for a complex or imaginay expression.
1606 (setq s '$pnz))
1608 ;; In Real mode the loop stops when the sign is 'pnz.
1609 (setq sign '$pnz odds (list x) evens nil minus nil)
1610 (return nil)))))))
1612 (defun signfactor (x)
1613 (let (y (factored t))
1614 (setq y (factor-if-small x))
1615 (cond ((or (mplusp y) (> (conssize y) 50.))
1616 (setq sign '$pnz)
1617 nil)
1618 (t (sign y)))))
1620 (defun factor-if-small (x)
1621 (if (< (conssize x) 51.)
1622 (let ($ratprint)
1623 (declare (special $ratprint))
1624 (factor x)) x))
1626 (defun sign-mexpt (x)
1627 (let* ((expt (caddr x)) (base1 (cadr x))
1628 (sign-expt (sign1 expt)) (sign-base (sign1 base1))
1629 (evod (evod expt)))
1630 ;; The variable sign is now equal to sign-base. This is used below
1631 ;; in some places to avoid an assignment operation for sign.
1632 (cond ((and (eq sign-base '$zero)
1633 (member sign-expt '($zero $neg $nz) :test #'eq))
1634 (dbzs-err x))
1635 ((eq sign-expt '$zero) (setq sign '$pos))
1636 ((eq sign-base '$zero))
1638 ((and *complexsign* (or (eq sign-expt '$complex)
1639 (eq sign-expt '$imaginary)
1640 (eq sign-base '$complex)))
1641 ;; Base is complex or exponent is complex or imaginary.
1642 ;; The sign is $complex.
1643 (when *debug-compar*
1644 (format t "~&in SIGN-MEXPT for ~A, sign is complex.~%" x))
1645 (setq sign '$complex))
1647 ((and *complexsign*
1648 (eq sign-base '$neg)
1649 (eq (evod ($expand (mul 2 expt))) '$odd))
1650 ;; Base is negative and the double of the exponent is odd.
1651 ;; Result is imaginary.
1652 (when *debug-compar*
1653 (format t "~&in SIGN-MEXPT for ~A, sign is $imaginary.~%" x))
1654 (setq sign '$imaginary))
1656 ((and *complexsign*
1657 (eq sign-base '$imaginary))
1658 ;; An imaginary base. Look for even or odd exponent.
1659 (when *debug-compar*
1660 (format t "~&in SIGN-MEXPT for ~A, base is $imaginary.~%" x))
1661 (cond
1662 ((and (integerp expt) (eq evod '$even))
1663 (setq sign (if (eql (mod expt 4) 0) '$pz '$nz)))
1664 ((and (integerp expt) (eq evod '$odd))
1665 (setq sign '$imaginary
1666 minus (if (eql (mod (- expt 1) 4) 0) t nil)))
1667 (t (setq sign '$complex))))
1669 ((and *complexsign*
1670 (not evod)
1671 (not (ratnump expt))
1672 (not (member sign-base '($pos $pz $zero) :test #'eq)))
1673 (when *debug-compar*
1674 (format t "~&in SIGN-MEXPT for ~A, base is not $pos, $pz or $zero.~%" x))
1675 (setq sign (if (maxima-integerp expt) '$pnz '$complex)))
1676 ((eq sign-base '$pos))
1677 ((eq evod '$even)
1678 (cond ((eq sign-expt '$neg)
1679 (setq sign '$pos minus nil evens (ncons base1) odds nil))
1680 ((member sign-base '($pn $neg) :test #'eq)
1681 (setq sign '$pos minus nil
1682 evens (nconc odds evens)
1683 odds nil))
1684 (t (setq sign '$pz minus nil
1685 evens (nconc odds evens)
1686 odds nil))))
1687 ((and (member sign-expt '($neg $nz) :test #'eq)
1688 (member sign-base '($nz $pz $pnz) :test #'eq))
1689 (setq sign (if (eq sign-base '$pz)
1690 '$pos
1691 '$pn)))
1692 ((member sign-expt '($pz $nz $pnz) :test #'eq)
1693 (cond ((member sign-base '($neg $nz) :test #'eq)
1694 (setq odds (ncons x) sign (if (eq sign-base '$neg) '$pn '$pnz)))))
1695 ((eq sign-expt '$pn)
1696 (cond ((eq sign-base '$neg)
1697 (setq sign '$pn))
1698 ((eq sign-base '$nz)
1699 (setq sign '$pnz))))
1700 ((ratnump expt)
1701 (cond ((mevenp (cadr expt))
1702 (cond ((member sign-base '($pn $neg) :test #'eq)
1703 (setq sign-base '$pos))
1704 ((member sign-base '($pnz $nz) :test #'eq)
1705 (setq sign-base '$pz)))
1706 (setq evens (nconc odds evens)
1707 odds nil minus nil))
1708 ((mevenp (caddr expt))
1709 (cond (*complexsign*
1710 (when (not (member sign-base '($pos $pz)))
1711 ;; In Complex Mode the sign is $complex.
1712 (setq sign-base (setq sign-expt '$complex))))
1713 (complexsign
1714 ;; The only place the variable complexsign
1715 ;; is used. Unfortunately, one routine in
1716 ;; to_poly.lisp in /share/to_poly_solve depends on
1717 ;; this piece of code. Perhaps we can remove
1718 ;; the dependency. (12/2008)
1719 (setq sign-base (setq sign-expt '$pnz)))
1720 ((eq sign-base '$neg) (imag-err x))
1721 ((eq sign-base '$pn)
1722 (setq sign-base '$pos))
1723 ((eq sign-base '$nz)
1724 (setq sign-base '$zero))
1725 (t (setq sign-base '$pz)))))
1726 (cond ((eq sign-expt '$neg)
1727 (cond ((eq sign-base '$zero) (dbzs-err x))
1728 ((eq sign-base '$pz)
1729 (setq sign-base '$pos))
1730 ((eq sign-base '$nz)
1731 (setq sign-base '$neg))
1732 ((eq sign-base '$pnz)
1733 (setq sign-base '$pn)))))
1734 (setq sign sign-base))
1735 ((member sign-base '($neg $nz) :test #'eq)
1736 (cond ((eq evod '$odd))
1737 (*complexsign*
1738 (setq sign '$complex))
1739 ((eq sign-base '$neg)
1740 (setq sign '$pn))
1741 (t (setq sign '$pnz)))))))
1743 ;;; Determine the sign of log(expr). This function changes the special variable sign.
1745 (defun sign-log (x)
1746 (setq x (cadr x))
1747 (setq sign
1748 (cond ((eq t (mgrp x 0))
1749 (cond ((eq t (mgrp 1 x)) '$neg)
1750 ((eq t (meqp x 1)) '$zero);; log(1) = 0.
1751 ((eq t (mgqp 1 x)) '$nz)
1752 ((eq t (mgrp x 1)) '$pos)
1753 ((eq t (mgqp x 1)) '$pz)
1754 ((eq t (mnqp x 1)) '$pn)
1755 (t '$pnz)))
1756 ((and *complexsign* (eql 1 (cabs x))) '$imaginary)
1757 (*complexsign* '$complex)
1758 (t '$pnz))))
1760 (defun sign-mabs (x)
1761 (let ((*complexsign* t))
1762 (sign (cadr x))
1763 (cond ((member sign '($pos $zero) :test #'eq))
1764 ((member sign '($neg $pn) :test #'eq) (setq sign '$pos))
1765 (t (setq sign '$pz minus nil evens (nconc odds evens) odds nil)))))
1767 (defun sign-asin/acos/atanh (x)
1768 (cond ((and *complexsign*
1769 (or (not (eq t (mgqp (cadr x) -1)))
1770 (not (eq t (mgqp 1 (cadr x)))))) ; x < -1 or x > 1
1771 (setq sign '$complex))
1772 ((not (eq (caar x) '%acos))
1773 (sign-oddfun x))
1774 ((eq t (mlsp (cadr x) 1)) ; x < 1
1775 (sign-posfun x))
1776 (t ; x <= 1
1777 (setq sign '$pz))))
1779 (defun sign-acosh (x)
1780 (cond ((and *complexsign* (not (eq t (mgqp (cadr x) 1)))) ; x < 1
1781 (setq sign '$complex))
1782 ((eq t (mgrp (cadr x) 1)) ; x > 1
1783 (sign-posfun x))
1784 (t ; x >= 1
1785 (setq sign '$pz))))
1787 ;; This code handles sign(sin(x)), where -%pi <= x <= %pi. Of course, at the
1788 ;; expense of a great deal of additional complexity, this code could catch far
1789 ;; more cases.
1791 ;; Like all sign functions, it works by setting the value of the special variable
1792 ;; sign.
1794 ;; The sign of sin(constant expression) is supposed to be handled elsewhere,
1795 ;; so we don't consider that case here. But running the testsuite,
1796 ;; sometimes sign-sin receives a constant expression. This code could
1797 ;; handle this case, but I think that's fixing a bug in the wrong place.
1799 ;; The general simplifier converts sin(%i*a) to %i*sinh(a), so I don't
1800 ;; think this code needs to handle the case of sin(x), where x is purely imaginary.
1802 ;; This code could handle sin({zerob, zeroa, ind}), but it doesn't.
1804 ;; Finally, sometimes the argument to sin is not simplified; for example,
1805 ;; running the testsuite sometimes this code recieves the expression sin(1/(1/x)).
1806 ;; We could simplify it, but I think that it would be better to fix the code
1807 ;; so that the input to sign-sin is always simplified.
1808 (defun sign-sin (e) ; e = sin(x)
1809 (let ((x (cadr e)) (y 0))
1810 ;; When *complexsign* is true, find the rectangular form of
1811 ;; the argument to sin.
1812 (when *complexsign*
1813 (setq x (risplit x))
1814 (setq y (cdr x)
1815 x (car x)))
1816 (cond
1817 ;; When y = 0 and -%pi <= x <= %pi, sign(sin(x)) = sign(x)
1818 ((and (eql y 0)
1819 (eq t (mgqp x (mul -1 '$%pi)))
1820 (eq t (mgqp '$%pi x)))
1821 (sign x))
1822 ;; When *complexsign* is true & y # 0, set sign to complex.
1823 ;; To test y # 0, we'll use (not (eql y 0)))
1824 ((and *complexsign* (not (eql y 0)))
1825 (setf sign '$complex))
1826 (t (setf sign '$pnz))))
1827 nil)
1828 (putprop '%sin 'sign-sin 'sign-function)
1830 (defun sign-cos (e) ; e = cos(x)
1831 (let ((x (cadr e)) (y 0))
1832 ;; When *complexsign* is true, find the rectangular form of
1833 ;; the argument to cos.
1834 (when *complexsign*
1835 (setq x (risplit x))
1836 (setq y (cdr x)
1837 x (car x)))
1838 (cond
1839 ;; When y = 0 and -%pi/2 <= x <= 3 %pi/2, sign(cos(x)) = sign(%pi/2-x)
1840 ((and (eql y 0)
1841 (eq t (mgqp x (div '$%pi -2)))
1842 (eq t (mgqp (div (mul 3 '$%pi) 2) x)))
1843 (sign (sub (div '$%pi 2) x)))
1844 ;; When *complexsign* is true & y # 0, set sign to complex.
1845 ;; To test y # 0, we'll use (not (eql y 0)))
1846 ((and *complexsign* (not (eql y 0)))
1847 (setf sign '$complex))
1848 (t (setf sign '$pnz))))
1849 nil)
1850 (putprop '%cos 'sign-cos 'sign-function)
1852 ;; When Maxima can deduce that when -1<x<=0, then ceiling(x) simplifies to 0. So this
1853 ;; case is simplified away before it gets to this function.
1854 (defun sign-ceiling (x)
1855 (setq x (cadr x)) ; ceiling(x) --> x
1856 (setf sign (cond ((eq t (mgrp x 0)) '$pos) ; ceiling(> 0) = pos
1857 ((eq t (mgrp x -1)) '$pz) ; ceiling(> -1) = pz
1858 ((eq t (mgqp -1 x)) '$neg) ; ceiling(<= -1) = neg
1859 ((eq t (mgrp 0 x)) '$nz) ; ceiling( < 0) = nz
1860 (t '$pnz))))
1861 (putprop '$ceiling 'sign-ceiling 'sign-function)
1863 (defun sign-floor (x)
1864 (setq x (cadr x)) ; floor(x) --> x
1865 (setf sign (cond ((eq t (mgqp x 1)) '$pos) ;floor(>= 1) = pos
1866 ((eq t (mgqp x 0)) '$pz) ;floor(>= 0) = pz
1867 ((eq t (mgrp 0 x)) '$neg) ;floor(< 0) = neg
1868 ((eq t (mgqp 0 x)) '$nz) ;floor(<= 0) = nz
1869 (t '$pnz))))
1870 (putprop '$floor 'sign-floor 'sign-function)
1872 ;;; Compare min/max
1874 ;;; Macros used in simp min/max
1875 ;;; If op is min, use body; if not, negate sign constants in body
1876 ;;; Used to avoid writing min and max code separately: just write the min code
1877 ;;; in such a way that its dual works for max
1878 (defmacro minmaxforms (op &rest body)
1879 `(if (eq ,op '$min)
1880 ,@body
1881 ,@(sublis '(($neg . $pos)
1882 ($nz . $pz)
1883 ($pz . $nz)
1884 ($pos . $neg)
1885 ;;($zero . $zero)
1886 ;;($pn . $pn)
1887 ;;($pnz . $pnz)
1889 ($max . $min)
1890 ($min . $max)
1892 ($inf . $minf)
1893 ($minf . $inf))
1894 body)))
1896 (defun sign-minmax (op args)
1897 (do ((sgn (minmaxforms op '$pos) ;identity element for min
1898 (sminmax op sgn (sign* (car l))))
1899 (end (minmaxforms op '$neg))
1900 (l args (cdr l)))
1901 ((or (null l) (eq sgn end))
1902 (setq minus nil
1903 odds (if (not (member sgn '($pos $neg $zero) :test #'eq))
1904 (ncons (cons (list op) args)))
1905 evens nil
1906 sign sgn))))
1908 ;; sign(op(a,b)) = sminmax(sign(a),sign(b))
1909 ;; op is $min/$max; s1/s2 in neg, nz, zero, pz, pos, pn, pnz
1910 (defun sminmax (op s1 s2)
1911 (minmaxforms
1913 ;; Many of these cases don't come up in simplified expressions,
1914 ;; since e.g. sign(a)=neg and sign(b)=pos implies min(a,b)=a
1915 ;; the order of these clauses is important
1916 (cond ((eq s1 '$pos) s2)
1917 ((eq s2 '$pos) s1)
1918 ((eq s1 s2) s1)
1919 ((or (eq s1 '$neg) (eq s2 '$neg)) '$neg)
1920 ((or (eq s1 '$nz) (eq s2 '$nz)) '$nz)
1921 ((eq s1 '$zero) (if (eq s2 '$pz) '$zero '$nz))
1922 ((eq s2 '$zero) (if (eq s1 '$pz) '$zero '$nz))
1923 (t '$pnz))))
1925 (defun minmaxp (ex)
1926 (cond ((atom ex) nil)
1927 ((member (caar ex) '($min $max) :test #'eq) (caar ex))
1928 (t nil)))
1930 (defun signdiff-minmax (l r)
1931 ;; sign of l-r; nil if unknown (not PNZ)
1932 (let* ((lm (minmaxp l))
1933 (rm (minmaxp r))
1934 (ll (if lm (cdr l)))
1935 (rr (if rm (cdr r)))) ;distinguish between < and <= argument lists of min/max
1936 (minmaxforms
1937 (or rm lm)
1938 (cond ((eq lm rm) ; min(a,...) - min(b,...)
1939 (multiple-value-bind (both onlyl onlyr) (intersect-info ll rr)
1940 (declare (ignore both))
1941 (cond ((null onlyl) '$pz) ; min(a,b) - min(a,b,c)
1942 ((null onlyr) '$nz) ; min(a,b,c) - min(a,b)
1943 ;; TBD: add processing for full onlyl/onlyr case
1944 (t nil))))
1945 ;; TBD: memalike and set-disjointp are crude approx.
1946 ((null lm) (if (memalike l rr) '$pz)) ; a - min(a,b)
1947 ((null rm) (if (memalike r ll) '$nz)) ; min(a,b) - a
1948 (t ; min/max or max/min
1949 (if (not (set-disjointp ll rr)) '$pz)))))) ; max(a,q,r) - min(a,s,t)
1951 (defun intersect-info (a b)
1952 (let ((both nil)
1953 (onlya nil)
1954 (onlyb nil))
1955 (do-merge-asym
1957 #'like
1958 #'$orderlessp
1959 #'(lambda (x) (push x both))
1960 #'(lambda (x) (push x onlya))
1961 #'(lambda (x) (push x onlyb)))
1962 (values
1963 (reverse both)
1964 (reverse onlya)
1965 (reverse onlyb))))
1967 ;;; end compare min/max
1969 (defun sign-posfun (xx)
1970 (declare (ignore xx))
1971 (setq sign '$pos
1972 minus nil
1973 odds nil
1974 evens nil))
1976 (defun sign-oddfun (x)
1977 (cond ((kindp (caar x) '$increasing)
1978 ; Take the sign of the argument
1979 (sign (cadr x)))
1980 ((kindp (caar x) '$decreasing)
1981 ; Take the sign of negative of the argument
1982 (sign (neg (cadr x))))
1984 ; If the sign of the argument is zero, then we're done (the sign of
1985 ; the function value is the same). Otherwise, punt to SIGN-ANY.
1986 (sign (cadr x))
1987 (unless (eq sign '$zero)
1988 (sign-any x)))))
1990 (defun imag-err (x)
1991 (if sign-imag-errp
1992 (merror (intl:gettext "sign: argument cannot be imaginary; found ~M") x)
1993 (throw 'sign-imag-err t)))
1995 (defun dbzs-err (x)
1996 (merror (intl:gettext "sign: division by zero in ~M") x))
1998 ;; Return true iff e is an expression with operator op1, op2,...,or opn.
2000 (defun op-equalp (e &rest op)
2001 (and (consp e) (consp (car e)) (some #'(lambda (s) (equal (caar e) s)) op)))
2003 ;; Return true iff the operator of a is a Maxima relation operator.
2005 (defun mrelationp (a)
2006 (op-equalp a 'mlessp 'mleqp 'mequal 'mnotequal 'mgeqp 'mgreaterp))
2008 ;; This version of featurep applies ratdisrep to the first argument. This
2009 ;; change allows things like featurep(rat(n),integer) --> true when n has
2010 ;; been declared an integer.
2012 (defmfun $featurep (e ind)
2013 (setq e ($ratdisrep e))
2014 (cond ((not (symbolp ind))
2015 (merror
2016 (intl:gettext "featurep: second argument must be a symbol; found ~M")
2017 ind))
2018 ;; Properties not related to the assume database.
2019 ((and (member ind opers) (safe-get e ind)))
2020 ((and (member ind '($evfun $evflag $bindtest $nonarray))
2021 (safe-get e (stripdollar ind))))
2022 ((and (eq ind '$noun)
2023 (safe-get e (stripdollar ind))
2025 ((and (member ind '($scalar $nonscalar $mainvar))
2026 (mget e ind)))
2027 ((and (eq ind '$feature)
2028 (member e $features)
2030 ((eq ind '$alphabetic)
2031 (dolist (l (coerce e 'list) t)
2032 (when (not (member l *alphabet*)) (return nil))))
2033 ;; Properties related to the assume database.
2034 ((eq ind '$integer) (maxima-integerp e))
2035 ((eq ind '$noninteger) (nonintegerp e))
2036 ((eq ind '$even) (mevenp e))
2037 ((eq ind '$odd) (moddp e))
2038 ((eq ind '$real)
2039 (if (atom e)
2040 (or (numberp e) (kindp e '$real) (numberp (numer e)))
2041 (free ($rectform e) '$%i)))
2042 ((symbolp e) (kindp e ind))))
2044 ;; Give a function the maps-integers-to-integers property when it is integer
2045 ;; valued on the integers; give it the integer-valued property when its
2046 ;; range is a subset of the integers. What have I missed?
2048 (setf (get 'mplus 'maps-integers-to-integers) t)
2049 (setf (get 'mtimes 'maps-integers-to-integers) t)
2050 (setf (get 'mabs 'maps-integers-to-integers) t)
2051 (setf (get '$max 'maps-integers-to-integers) t)
2052 (setf (get '$min 'maps-integers-to-integers) t)
2054 (setf (get '$floor 'integer-valued) t)
2055 (setf (get '$ceiling 'integer-valued) t)
2056 (setf (get '$charfun 'integer-valued) t)
2058 (defun maxima-integerp (x)
2059 (cond ((integerp x))
2060 ((mnump x) nil)
2061 ((and (symbolp x)
2062 (or (kindp x '$integer)
2063 (kindp x '$even)
2064 (kindp x '$odd)
2065 (check-integer-facts x))))
2066 (t (let ((x-op (and (consp x) (consp (car x)) (caar x))) ($prederror nil))
2067 (cond ((null x-op) nil)
2068 ((not (symbolp x-op)) nil) ; fix for mqapply at some point?
2069 ((eq x-op 'mrat) (and (integerp (cadr x)) (equal (cddr x) 1)))
2070 ;; mtimes and mplus are generally handled by this clause
2071 ((and (get x-op 'maps-integers-to-integers) (every #'maxima-integerp (margs x))))
2072 ;; Special case for 1/2*...*even
2073 ((eq x-op 'mtimes)
2074 (and (mnump (cadr x))
2075 (integerp (mul 2 (cadr x)))
2076 (every 'maxima-integerp (cddr x))
2077 (some #'(lambda (s) ($featurep s '$even)) (rest (margs x)))))
2078 ((eq x-op 'mexpt)
2079 (and (every #'maxima-integerp (margs x))
2080 (null (mevalp (mlsp (caddr x) 0)))))
2081 ;; ! in Maxima allows real arguments
2082 ((eq x-op 'mfactorial)
2083 (and (maxima-integerp (cadr x))
2084 (not (mevalp (mlsp (cadr x) 0)))))
2085 ((eq x-op '%gamma)
2086 (and (maxima-integerp (cadr x))
2087 (not (mevalp (mlsp (cadr x) 1)))))
2088 ;; other x-ops
2089 ((or ($featurep ($verbify x-op) '$integervalued)
2090 (get x-op 'integer-valued))))))))
2092 ;; When called with mode 'integer look into the database for symbols which are
2093 ;; declared to be equal to an integer or an expression which is an integer.
2094 ;; In mode 'evod look for odd and even expressions.
2095 (defun check-integer-facts (x &optional (mode 'integer))
2096 (do ((factsl (cdr (facts1 x)) (cdr factsl))
2097 fact)
2098 ((null factsl) nil)
2099 (setq fact (car factsl))
2100 (cond ((and (not (atom fact))
2101 (eq (caar fact) '$equal))
2102 (cond ((and (symbolp (cadr fact))
2103 (eq (cadr fact) x))
2104 ;; Case equal(x,expr): Test expr to be an integer.
2105 (cond ((symbolp (caddr fact))
2106 (cond ((and (eq mode 'integer)
2107 (or (kindp (caddr fact) '$integer)
2108 (kindp (caddr fact) '$odd)
2109 (kindp (caddr fact) '$even)))
2110 (return t))
2111 ((eq mode 'evod)
2112 (cond ((kindp (caddr fact) '$odd)
2113 (return '$odd))
2114 ((kindp (caddr fact) '$even)
2115 (return '$even))
2116 (t (return nil))))
2117 (t (return nil))))
2119 (cond ((eq mode 'integer)
2120 (return (maxima-integerp (caddr fact))))
2121 ((eq mode 'evod)
2122 (return (evod (caddr fact))))
2123 (t (return nil))))))
2124 ((and (symbolp (caddr fact))
2125 (eq (caddr fact) x))
2126 ;; Case equal(expr,x): Test expr to be an integer.
2127 (cond ((symbolp (caddr fact))
2128 (cond ((and (eq mode 'integer)
2129 (or (kindp (cadr fact) '$integer)
2130 (kindp (cadr fact) '$odd)
2131 (kindp (cadr fact) '$even)))
2132 (return t))
2133 ((eq mode 'evod)
2134 (cond ((kindp (cadr fact) '$odd)
2135 (return '$odd))
2136 ((kindp (cadr fact) '$even)
2137 (return '$even))
2138 (t (return nil))))
2139 (t (return nil))))
2141 (cond ((eq mode 'integer)
2142 (return (maxima-integerp (cadr fact))))
2143 ((eq mode 'evod)
2144 (return (evod (cadr fact))))
2145 (t (return nil)))))))))))
2147 (defun nonintegerp (e)
2148 (cond ((and (symbolp e) (or (kindp e '$noninteger) (check-noninteger-facts e) (kindp e '$irrational)))) ;declared noninteger
2149 ((mnump e)
2150 (if (integerp e) nil t)) ;all floats are noninteger and integers are not nonintegers
2151 (($ratp e)
2152 (nonintegerp ($ratdisrep e)))
2153 (t (eq t (mgrp e (take '($floor) e))))))
2156 ;; Look into the database for symbols which are declared to be equal
2157 ;; to a noninteger or an expression which is a noninteger.
2158 (defun check-noninteger-facts (x)
2159 (do ((factsl (cdr (facts1 x)) (cdr factsl)))
2160 ((null factsl) nil)
2161 (cond ((and (not (atom (car factsl)))
2162 (eq (caar (car factsl)) '$equal))
2163 (cond ((and (symbolp (cadr (car factsl)))
2164 (eq (cadr (car factsl)) x))
2165 ;; Case equal(x,expr): Test expr to be a noninteger.
2166 (cond ((symbolp (caddr (car factsl)))
2167 (if (kindp (caddr (car factsl)) '$noninteger)
2168 (return t)))
2170 (return (nonintegerp (caddr (car factsl)))))))
2171 ((and (symbolp (caddr (car factsl)))
2172 (eq (caddr (car factsl)) x))
2173 ;; Case equal(expr,x): Test expr to be a noninteger.
2174 (cond ((symbolp (cadr (car factsl)))
2175 (if (kindp (cadr (car factsl)) '$noninteger)
2176 (return t)))
2178 (return (nonintegerp (cadr (car factsl))))))))))))
2180 (defun intp (l)
2181 (every #'maxima-integerp (cdr l)))
2183 (defun mevenp (e)
2184 (cond ((integerp e) (not (oddp e)))
2185 ((mnump e) nil)
2186 (t (eq '$even (evod e)))))
2188 (defun moddp (e)
2189 (cond ((integerp e) (oddp e))
2190 ((mnump e) nil)
2191 (t (eq '$odd (evod e)))))
2193 ;; An extended evod that recognizes that abs(even) is even and
2194 ;; abs(odd) is odd.
2196 (defun evod (e)
2197 (cond ((integerp e) (if (oddp e) '$odd '$even))
2198 ((mnump e) nil)
2199 ((atom e)
2200 (cond ((kindp e '$odd) '$odd)
2201 ((kindp e '$even) '$even)
2202 ;; Check the database for facts.
2203 ((symbolp e) (check-integer-facts e 'evod))))
2204 ((eq 'mtimes (caar e)) (evod-mtimes e))
2205 ((eq 'mplus (caar e)) (evod-mplus e))
2206 ((eq 'mabs (caar e)) (evod (cadr e))) ;; extra code
2207 ((eq 'mexpt (caar e)) (evod-mexpt e))))
2209 (defun evod-mtimes (x)
2210 (do ((l (cdr x) (cdr l)) (flag '$odd))
2211 ((null l) flag)
2212 (setq x (evod (car l)))
2213 (cond ((eq '$odd x))
2214 ((eq '$even x) (setq flag '$even))
2215 ((maxima-integerp (car l)) (cond ((eq '$odd flag) (setq flag nil))))
2216 (t (return nil)))))
2218 (defun evod-mplus (x)
2219 (do ((l (cdr x) (cdr l)) (flag))
2220 ((null l) (cond (flag '$odd) (t '$even)))
2221 (setq x (evod (car l)))
2222 (cond ((eq '$odd x) (setq flag (not flag)))
2223 ((eq '$even x))
2224 (t (return nil)))))
2226 (defun evod-mexpt (x)
2227 (when (and (integerp (caddr x)) (not (minusp (caddr x))))
2228 (evod (cadr x))))
2230 (declare-top (special mgqp mlqp))
2232 (defmode cl ()
2233 (atom (selector +labs) (selector -labs) (selector data)))
2235 (defmacro c-dobj (&rest x)
2236 `(list ,@x))
2238 (defun dcompare (x y)
2239 (setq odds (list (sub x y)) evens nil minus nil
2240 sign (cond ((eq x y) '$zero)
2241 ((or (eq '$inf x) (eq '$minf y)) '$pos)
2242 ((or (eq '$minf x) (eq '$inf y)) '$neg)
2243 (t (dcomp x y)))))
2245 (defun dcomp (x y)
2246 (let (mgqp mlqp)
2247 (setq x (dinternp x) y (dinternp y))
2248 (cond ((or (null x) (null y)) '$pnz)
2249 ((progn (clear) (deq x y) (sel y +labs)))
2250 (t '$pnz))))
2252 (defun deq (x y)
2253 (cond ((dmark x '$zero) nil)
2254 ((eq x y))
2255 (t (do ((l (sel x data) (cdr l))) ((null l))
2256 (if (and (visiblep (car l)) (deqf x y (car l))) (return t))))))
2258 (defun deqf (x y f)
2259 (cond ((eq 'meqp (caar f))
2260 (if (eq x (cadar f)) (deq (caddar f) y) (deq (cadar f) y)))
2261 ((eq 'mgrp (caar f))
2262 (if (eq x (cadar f)) (dgr (caddar f) y) (dls (cadar f) y)))
2263 ((eq 'mgqp (caar f))
2264 (if (eq x (cadar f)) (dgq (caddar f) y) (dlq (cadar f) y)))
2265 ((eq 'mnqp (caar f))
2266 (if (eq x (cadar f)) (dnq (caddar f) y) (dnq (cadar f) y)))))
2268 (defun dgr (x y)
2269 (cond ((dmark x '$pos) nil)
2270 ((eq x y))
2271 (t (do ((l (sel x data) (cdr l)))
2272 ((null l))
2273 (when (or mlqp (and (visiblep (car l)) (dgrf x y (car l))))
2274 (return t))))))
2276 (defun dgrf (x y f)
2277 (cond ((eq 'mgrp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2278 ((eq 'mgqp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2279 ((eq 'meqp (caar f))
2280 (if (eq x (cadar f))
2281 (dgr (caddar f) y)
2282 (dgr (cadar f) y)))))
2284 (defun dls (x y)
2285 (cond ((dmark x '$neg) nil)
2286 ((eq x y))
2287 (t (do ((l (sel x data) (cdr l)))
2288 ((null l))
2289 (when (or mgqp (and (visiblep (car l)) (dlsf x y (car l))))
2290 (return t))))))
2292 (defun dlsf (x y f)
2293 (cond ((eq 'mgrp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2294 ((eq 'mgqp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2295 ((eq 'meqp (caar f))
2296 (if (eq x (cadar f)) (dls (caddar f) y) (dls (cadar f) y)))))
2298 (defun dgq (x y)
2299 (cond ((member (sel x +labs) '($pos $zero) :test #'eq) nil)
2300 ((eq '$nz (sel x +labs)) (deq x y))
2301 ((eq '$pn (sel x +labs)) (dgr x y))
2302 ((dmark x '$pz) nil)
2303 ((eq x y) (setq mgqp t) nil)
2304 (t (do ((l (sel x data) (cdr l))) ((null l))
2305 (if (and (visiblep (car l)) (dgqf x y (car l))) (return t))))))
2307 (defun dgqf (x y f)
2308 (cond ((eq 'mgrp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2309 ((eq 'mgqp (caar f)) (if (eq x (cadar f)) (dgq (caddar f) y)))
2310 ((eq 'meqp (caar f))
2311 (if (eq x (cadar f)) (dgq (caddar f) y) (dgq (cadar f) y)))))
2313 (defun dlq (x y)
2314 (cond ((member (sel x +labs) '($neg $zero) :test #'eq) nil)
2315 ((eq '$pz (sel x +labs)) (deq x y))
2316 ((eq '$pn (sel x +labs)) (dls x y))
2317 ((dmark x '$nz) nil)
2318 ((eq x y) (setq mlqp t) nil)
2319 (t (do ((l (sel x data) (cdr l))) ((null l))
2320 (if (and (visiblep (car l)) (dlqf x y (car l))) (return t))))))
2322 (defun dlqf (x y f)
2323 (cond ((eq 'mgrp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2324 ((eq 'mgqp (caar f)) (if (eq x (caddar f)) (dlq (cadar f) y)))
2325 ((eq 'meqp (caar f))
2326 (if (eq x (cadar f)) (dlq (caddar f) y) (dlq (cadar f) y)))))
2328 (defun dnq (x y)
2329 (cond ((member (sel x +labs) '($pos $neg) :test #'eq) nil)
2330 ((eq '$pz (sel x +labs)) (dgr x y))
2331 ((eq '$nz (sel x +labs)) (dls x y))
2332 ((dmark x '$pn) nil)
2333 ((eq x y) nil)
2334 (t (do ((l (sel x data) (cdr l))) ((null l))
2335 (if (and (visiblep (car l)) (dnqf x y (car l))) (return t))))))
2337 (defun dnqf (x y f)
2338 (cond ((eq 'meqp (caar f))
2339 (if (eq x (cadar f)) (dnq (caddar f) y) (dnq (cadar f) y)))))
2341 ;; mark sign of x to be m, relative to current comparison point for dcomp.
2342 ;; returns true if this fact is already known, nil otherwise.
2343 (defun dmark (x m)
2344 (cond ((eq m (sel x +labs)))
2345 ((and dbtrace (prog1
2347 (mtell (intl:gettext "DMARK: marking ~M ~M") (if (atom x) x (car x)) m))
2348 nil))
2350 (push x +labs)
2351 (push+sto (sel x +labs) m)
2352 nil)))
2354 (defun daddgr (flag x)
2355 (with-compsplt (lhs rhs x)
2356 (mdata flag 'mgrp (dintern lhs) (dintern rhs))
2357 (if (or (mnump lhs) (constant lhs))
2358 (list '(mlessp) rhs lhs)
2359 (list '(mgreaterp) lhs rhs))))
2361 (defun daddgq (flag x)
2362 (with-compsplt (lhs rhs x)
2363 (mdata flag 'mgqp (dintern lhs) (dintern rhs))
2364 (if (or (mnump lhs) (constant lhs))
2365 (list '(mleqp) rhs lhs)
2366 (list '(mgeqp) lhs rhs))))
2368 (defun daddeq (flag x)
2369 (with-compsplt-eq (lhs rhs x)
2370 (mdata flag 'meqp (dintern lhs) (dintern rhs))
2371 (list '($equal) lhs rhs)))
2373 (defun daddnq (flag x)
2374 (with-compsplt-eq (lhs rhs x)
2375 (cond ((and (mtimesp lhs) (equal rhs 0))
2376 (dolist (term (cdr lhs)) (daddnq flag term)))
2377 ((and (mexptp lhs) (mexptp rhs)
2378 (integerp (caddr lhs)) (integerp (caddr rhs))
2379 (equal (caddr lhs) (caddr rhs)))
2380 (mdata flag 'mnqp (dintern (cadr lhs)) (dintern (cadr rhs)))
2381 (cond ((not (oddp (caddr lhs)))
2382 (mdata flag 'mnqp (dintern (cadr lhs))
2383 (dintern (neg (cadr rhs)))))))
2384 (t (mdata flag 'mnqp (dintern lhs) (dintern rhs))))
2385 (list '(mnot) (list '($equal) lhs rhs))))
2387 ;; The following functions are used by asksign to write answers into the
2388 ;; database. We make sure that these answers are written into the global
2389 ;; context '$initial and not in a local context which might be generated during
2390 ;; the evaluation phase and which will be destroyed before the evaluation has
2391 ;; finshed.
2392 ;; The additional facts are removed from the global context '$initial after
2393 ;; finishing the evaluation phase of meval with a call to clearsign.
2395 (defun tdpos (x)
2396 (let ((context '$initial))
2397 (daddgr t x)
2398 (push (cons x '$pos) *local-signs*)))
2400 (defun tdneg (x)
2401 (let ((context '$initial))
2402 (daddgr t (neg x))
2403 (push (cons x '$neg) *local-signs*)))
2405 (defun tdzero (x)
2406 (let ((context '$initial))
2407 (daddeq t x)
2408 (push (cons x '$zero) *local-signs*)))
2410 (defun tdpn (x)
2411 (let ((context '$initial))
2412 (daddnq t x)
2413 (push (cons x '$pn) *local-signs*)))
2415 (defun tdpz (x)
2416 (let ((context '$initial))
2417 (daddgq t x)
2418 (push (cons x '$pz) *local-signs*)))
2420 (defun compsplt-eq (x)
2421 (with-compsplt (lhs rhs x)
2422 (when (equal lhs 0)
2423 (setq lhs rhs rhs 0))
2424 (if (and (equal rhs 0)
2425 (or (mexptp lhs)
2426 (and (not (atom lhs))
2427 (kindp (caar lhs) '$oddfun)
2428 (kindp (caar lhs) '$increasing))))
2429 (setq lhs (cadr lhs)))
2430 (values lhs rhs)))
2432 (defun mdata (flag r x y)
2433 (if flag
2434 (mfact r x y)
2435 (mkill r x y)))
2437 (defun mfact (r x y)
2438 (let ((f (datum (list r x y))))
2439 (cntxt f context)
2440 (addf f x)
2441 (addf f y)))
2443 (defun mkill (r x y)
2444 (let ((f (car (datum (list r x y)))))
2445 (kcntxt f context)
2446 (maxima-remf f x)
2447 (maxima-remf f y)))
2449 (defun mkind (x y)
2450 (kind (dintern x) (dintern y)))
2452 ;; To guess from the previous incarnation of this code,
2453 ;; each argument is assumed to be a float, bigfloat, integer, or Maxima rational.
2454 ;; Convert Maxima rationals to Lisp rationals to make them comparable to others.
2456 (defun rgrp (x y)
2457 (when (or ($bfloatp x) ($bfloatp y))
2458 (setq
2459 x (let (($float2bf t))
2460 (declare (special $float2bf))
2461 (cadr ($bfloat (sub x y))))
2462 y 0))
2463 (if (not (numberp x))
2464 (setq x (/ (cadr x) (caddr x))))
2465 (if (not (numberp y))
2466 (setq y (/ (cadr y) (caddr y))))
2467 (cond
2468 (#-ecl (> x y) #+ecl (> (- x y) 0) '$pos)
2469 (#-ecl (> y x) #+ecl (> (- y x) 0) '$neg)
2470 (t '$zero)))
2472 (defun mcons (x l)
2473 (cons (car l) (cons x (cdr l))))
2475 (defun flip (s)
2476 (cond ((eq '$pos s) '$neg)
2477 ((eq '$neg s) '$pos)
2478 ((eq '$pz s) '$nz)
2479 ((eq '$nz s) '$pz)
2480 (t s)))
2482 (defun strongp (x y)
2483 (cond ((eq '$pnz y))
2484 ((eq '$pnz x) nil)
2485 ((member y '($pz $nz $pn) :test #'eq))))
2487 (defun munformat (form)
2488 (if (atom form)
2489 form
2490 (cons (caar form) (mapcar #'munformat (cdr form)))))
2492 (defun declarekind (var prop) ; This function is for $DECLARE to use.
2493 (let (prop2)
2494 (cond ((truep (list 'kind var prop)) t)
2495 ((or (falsep (list 'kind var prop))
2496 (and (setq prop2 (assoc prop '(($integer . $noninteger)
2497 ($noninteger . $integer)
2498 ($increasing . $decreasing)
2499 ($decreasing . $increasing)
2500 ($symmetric . $antisymmetric)
2501 ($antisymmetric . $symmetric)
2502 ($rational . $irrational)
2503 ($irrational . $rational)
2504 ($oddfun . $evenfun)
2505 ($evenfun . $oddfun)) :test #'eq))
2506 (truep (list 'kind var (cdr prop2)))))
2507 (merror (intl:gettext "declare: inconsistent declaration ~:M") `(($declare) ,var ,prop)))
2508 (t (mkind var prop) t))))
2510 ;;; These functions reformat expressions to be stored in the data base.
2512 ;; Return a list of all the atoms in X that aren't either numbers or constants
2513 ;; whose numerical value we know.
2514 (defun unknown-atoms (x)
2515 (let (($listconstvars t))
2516 (remove-if (lambda (sym) (mget sym '$numer))
2517 (cdr ($listofvars x)))))
2519 ;; Rewrite a^b to a simpler expression that has the same sign:
2520 ;; If b is odd or 1/b is odd, remove the exponent, e.g. x^3 becomes x.
2521 ;; If b has a negative sign, return a^-b, e.g. 1/x^a becomes x^a.
2522 ;; Otherwise, do nothing.
2523 (defun rewrite-mexpt-retaining-sign (x)
2524 (if (mexptp x)
2525 (let ((base (cadr x)) (exponent (caddr x)))
2526 (cond ((or (eq (evod exponent) '$odd) (eq (evod (inv exponent)) '$odd)) base)
2527 ((negp exponent) (inv x))
2528 (t x)))
2531 ;; COMPSPLT
2533 ;; Split X into (values LHS RHS) so that X>0 <=> LHS > RHS. This is supposed to
2534 ;; be a canonical form for X that can be stored in the database and then looked
2535 ;; up in future.
2537 ;; This uses two worker routines: COMPSPLT-SINGLE and COMPSPLT-GENERAL. The
2538 ;; former assumes that X only contains one symbol whose value is not known (eg not %e,
2539 ;; %pi etc.). The latter tries to deal with arbitrary collections of variables.
2540 (defun compsplt (x)
2541 (multiple-value-bind (lhs rhs)
2542 (cond
2543 ((or (atom x) (atom (car x))) (values x 0))
2544 ((null (cdr (unknown-atoms x))) (compsplt-single x))
2545 (t (compsplt-general x)))
2546 (let ((swapped nil))
2547 ;; If lhs is zero, swap lhs and rhs to make the following code simpler.
2548 ;; Remember that they were swapped to swap them back afterwards.
2549 (when (equal lhs 0)
2550 (setq lhs rhs rhs 0 swapped t))
2551 (when (equal rhs 0)
2552 ;; Rewrite mexpts in factors so that e.g. x^3/y>0 becomes x*y>0. */
2553 (setq lhs
2554 (if (mtimesp lhs)
2555 (cons (car lhs) (mapcar #'rewrite-mexpt-retaining-sign (cdr lhs)))
2556 (rewrite-mexpt-retaining-sign lhs))))
2557 ;; Undo swapping lhs and rhs.
2558 (if swapped
2559 (values rhs lhs)
2560 (values lhs rhs)))))
2562 (defun compsplt-single (x)
2563 (do ((exp (list x 0)) (success nil))
2564 ((or success (symbols (cadr exp))) (values (car exp) (cadr exp)))
2565 (cond ((atom (car exp)) (setq success t))
2566 ((eq (caaar exp) 'mplus) (setq exp (splitsum exp)))
2567 ((eq (caaar exp) 'mtimes) (setq exp (splitprod exp)))
2568 (t (setq success t)))))
2570 (defun compsplt-general (x)
2571 ;; Let compsplt-single work on it first to treat constant factors/terms.
2572 (multiple-value-bind (lhs rhs) (compsplt-single x) (setq x (sub lhs rhs)))
2573 (cond
2574 ;; If x is an atom or a single level list then we won't change it any.
2575 ((or (atom x) (atom (car x)))
2576 (values x 0))
2577 ;; If x is a negative expression but not a sum, then get rid of the
2578 ;; negative sign.
2579 ((negp x) (values 0 (neg x)))
2580 ;; If x is not a sum, or is a sum with more than 2 terms, or has some
2581 ;; symbols common to both summands, then do nothing.
2582 ((or (cdddr x)
2583 (not (eq (caar x) 'mplus))
2584 (intersect* (symbols (cadr x)) (symbols (caddr x))))
2585 (values x 0))
2586 ;; -x + y gives (y, x)
2587 ((and (or (negp (cadr x)) (mnump (cadr x)))
2588 (not (negp (caddr x))))
2589 (values (caddr x) (neg (cadr x))))
2590 ;; x - y gives (x, y)
2591 ((and (not (negp (cadr x)))
2592 (or (negp (caddr x)) (mnump (caddr x))))
2593 (values (cadr x) (neg (caddr x))))
2594 ;; - x - y gives (0, x+y)
2595 ((and (negp (cadr x)) (negp (caddr x)))
2596 (values 0 (neg x)))
2597 ;; Give up! (x, 0)
2599 (values x 0))))
2601 (defun negp (x)
2602 (and (mtimesp x) (mnegp (cadr x))))
2604 (defun splitsum (exp)
2605 (do ((llist (cdar exp) (cdr llist))
2606 (lhs1 (car exp))
2607 (rhs1 (cadr exp)))
2608 ((null llist)
2609 (if (mplusp lhs1) (setq success t))
2610 (list lhs1 rhs1))
2611 (cond ((member '$inf llist :test #'eq)
2612 (setq rhs1 (add2 '$inf (sub* rhs1 (addn llist t)))
2613 lhs1 (add2 '$inf (sub* lhs1 (addn llist t)))
2614 llist nil))
2615 ((member '$minf llist :test #'eq)
2616 (setq rhs1 (add2 '$minf (sub* rhs1 (addn llist t)))
2617 lhs1 (add2 '$minf (sub* lhs1 (addn llist t)))
2618 llist nil))
2619 ((null (symbols (car llist)))
2620 (setq lhs1 (sub lhs1 (car llist))
2621 rhs1 (sub rhs1 (car llist)))))))
2623 (defun splitprod (exp)
2624 (do ((flipsign)
2625 (lhs1 (car exp))
2626 (rhs1 (cadr exp))
2627 (llist (cdar exp) (cdr llist))
2628 (sign)
2629 (minus)
2630 (evens)
2631 (odds))
2632 ((null llist)
2633 (if (mtimesp lhs1) (setq success t))
2634 (cond (flipsign
2635 (setq success t)
2636 (list rhs1 lhs1))
2637 (t (list lhs1 rhs1))))
2638 (when (null (symbols (car llist)))
2639 (sign (car llist))
2640 (if (eq sign '$neg) (setq flipsign (not flipsign)))
2641 (if (member sign '($pos $neg) :test #'eq)
2642 (setq lhs1 (div lhs1 (car llist))
2643 rhs1 (div rhs1 (car llist)))))))
2645 (defun symbols (x)
2646 (let (($listconstvars %initiallearnflag))
2647 (cdr ($listofvars x))))
2649 ;; %initiallearnflag is only necessary so that %PI, %E, etc. can be LEARNed.
2651 (defun initialize-numeric-constant (c)
2652 (setq %initiallearnflag t)
2653 (let ((context '$global))
2654 (learn `((mequal) ,c ,(mget c '$numer)) t))
2655 (setq %initiallearnflag nil))
2657 (eval-when (:load-toplevel :execute)
2658 (mapc #'true*
2659 '(;; even and odd are integer
2660 (par ($even $odd) $integer)
2662 ; Cutting out inferences for integer, rational, real, complex (DK 10/2009).
2663 ; (kind $integer $rational)
2664 ; (par ($rational $irrational) $real)
2665 ; (par ($real $imaginary) $complex)
2667 ;; imaginary is complex
2668 (kind $imaginary $complex)
2670 ;; Declarations for constants
2671 (kind $%i $noninteger)
2672 (kind $%i $imaginary)
2673 (kind $%e $noninteger)
2674 (kind $%e $real)
2675 (kind $%pi $noninteger)
2676 (kind $%pi $real)
2677 (kind $%gamma $noninteger)
2678 (kind $%gamma $real)
2679 (kind $%phi $noninteger)
2680 (kind $%phi $real)
2681 (kind $%pi $irrational)
2682 (kind $%e $irrational)
2683 (kind $%phi $irrational)
2684 (kind $%catalan $noninteger)
2685 (kind $%catalan $real)
2687 ;; Declarations for functions
2688 (kind %log $increasing)
2689 (kind %asin $increasing) (kind %asin $oddfun)
2690 (kind %atan $increasing) (kind %atan $oddfun)
2691 (kind $delta $evenfun)
2692 (kind %sinh $increasing) (kind %sinh $oddfun)
2693 (kind %cosh $posfun)
2694 (kind %tanh $increasing) (kind %tanh $oddfun)
2695 (kind %coth $oddfun)
2696 (kind %csch $oddfun)
2697 (kind %sech $posfun)
2698 (kind %asinh $increasing) (kind %asinh $oddfun)
2699 ;; It would be nice to say %acosh is $posfun, but then
2700 ;; assume(xn<0); abs(acosh(xn)) -> acosh(xn), which is wrong
2701 ;; since acosh(xn) is complex.
2702 (kind %acosh $increasing) (kind %acosh $complex)
2703 (kind %atanh $increasing) (kind %atanh $oddfun)
2704 (kind $li $complex)
2705 (kind $lambert_w $complex)
2706 (kind %cabs $complex)))
2708 ;; Create an initial context for the user which is a subcontext of $global.
2709 ($newcontext '$initial))