Correct typo in Texinfo: cdf_tributions --> distributions
[maxima/cygwin.git] / src / compar.lisp
bloba9c524b323f458905feac5be1771157017149fb5
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 ;;; imagarinary 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 (let ((*complexsign* t))
1158 (setq a (sub a b))
1159 (let ((sgn (csign a)))
1160 (cond ((eq sgn '$pos) t)
1161 ((eq sgn t) nil) ;; csign thinks a - b isn't real
1162 ((member sgn '($neg $zero $nz) :test #'eq) nil)
1163 (t `((mgreaterp) ,a 0))))))
1165 (defun mgrp (a b)
1166 (cond
1167 ((or (stringp a) (stringp b))
1168 (if (and (stringp a) (stringp b)) (not (null (string< b a)))
1169 (when $prederror
1170 (merror (intl:gettext "greater than: arguments are incomparable; found: ~:M, ~:M") a b))))
1171 (t (mgrp-general a b))))
1173 (defun mlsp (x y)
1174 (mgrp y x))
1176 ;; MGQP-GENERAL applies when sign of A - B (possibly complex) makes sense.
1178 (defun mgqp-general (a b)
1179 (let ((*complexsign* t))
1180 (setq a (sub a b))
1181 (let ((sgn (csign a)))
1182 (cond ((member sgn '($pos $zero $pz) :test #'eq) t)
1183 ((eq sgn t) nil) ;; csign thinks a - b isn't real
1184 ((eq sgn '$neg) nil)
1185 (t `((mgeqp) ,a 0))))))
1187 (defun mgqp (a b)
1188 (cond
1189 ((or (stringp a) (stringp b))
1190 (if (and (stringp a) (stringp b)) (not (null (string<= b a)))
1191 (when $prederror
1192 (merror (intl:gettext "greater than or equal: arguments are incomparable; found: ~:M, ~:M") a b))))
1193 (t (mgqp-general a b))))
1195 (defun mnqp (x y)
1196 (let ((b (meqp x y)))
1197 (cond ((eq b '$unknown) b)
1198 ((or (eq b t) (eq b nil)) (not b))
1199 (t `(($notequal) ,x ,y)))))
1201 (defun c-$pn (o e)
1202 (list '(mnot) (c-$zero o e)))
1204 (defun c-$zero (o e)
1205 (list '($equal) (lmul (nconc o e)) 0))
1207 (defun c-$pos (o e)
1208 (cond ((null o) (list '(mnot) (list '($equal) (lmul e) 0)))
1209 ((null e) (list '(mgreaterp) (lmul o) 0))
1210 (t (setq e (mapcar #'(lambda (l) (power l 2)) e))
1211 (list '(mgreaterp) (lmul (nconc o e)) 0))))
1213 (defun c-$pz (o e)
1214 (cond ((null o) (list '(mnot) (list '($equal) (lmul e) 0)))
1215 ((null e) (list '(mgeqp) (lmul o) 0))
1216 (t (setq e (mapcar #'(lambda (l) (power l 2)) e))
1217 (list '(mgeqp) (lmul (nconc o e)) 0))))
1219 (defun sign* (x)
1220 (let (sign minus odds evens)
1221 (sign1 x)))
1223 (defun infsimp* (e)
1224 (if (or (atom e) (and (free e '$inf) (free e '$minf)))
1226 (infsimp e)))
1228 ;; Like WITH-COMPSPLT, but runs COMPSPLT-EQ instead
1229 (defmacro with-compsplt-eq ((lhs rhs x) &body forms)
1230 `(multiple-value-bind (,lhs ,rhs) (compsplt-eq ,x)
1231 ,@forms))
1233 ;; Call FORMS with LHS and RHS bound to the splitting of EXPR by COMPSPLT.
1234 (defmacro with-compsplt ((lhs rhs expr) &body forms)
1235 `(multiple-value-bind (,lhs ,rhs) (compsplt ,expr)
1236 ,@forms))
1238 (defun sign1 (x)
1239 (setq x (specrepcheck x))
1240 (setq x (infsimp* x))
1241 (when (and *complexsign* (atom x) (eq x '$infinity))
1242 ;; In Complex Mode the sign of infinity is complex.
1243 (when *debug-compar* (format t "~& in sign1 detect $infinity.~%"))
1244 (return-from sign1 '$complex))
1245 (if (member x '($und $ind $infinity) :test #'eq)
1246 (if limitp '$pnz (merror (intl:gettext "sign: sign of ~:M is undefined.") x)))
1247 (prog (dum exp)
1248 (setq dum (constp x) exp x)
1249 (cond ((or (numberp x) (ratnump x)))
1250 ((eq dum 'bigfloat)
1251 (if (prog2 (setq dum ($bfloat x)) ($bfloatp dum))
1252 (setq exp dum)))
1253 ((eq dum 'float)
1254 (if (and (setq dum (numer x)) (numberp dum)) (setq exp dum)))
1255 ((and (member dum '(numer symbol) :test #'eq)
1256 (prog2 (setq dum (numer x))
1257 (or (null dum)
1258 (and (numberp dum)
1259 (prog2 (setq exp dum)
1260 (< (abs dum) 1.0e-6))))))
1261 (cond ($signbfloat
1262 (and (setq dum ($bfloat x)) ($bfloatp dum) (setq exp dum)))
1263 (t (setq sign '$pnz evens nil odds (ncons x) minus nil)
1264 (return sign)))))
1265 (or (and (not (atom x)) (not (mnump x)) (equal x exp)
1266 (let (s o e m)
1267 (with-compsplt (lhs rhs x)
1268 (dcompare lhs rhs)
1269 (cond ((member sign '($pos $neg) :test #'eq))
1270 ((eq sign '$pnz) nil)
1271 (t (setq s sign o odds e evens m minus)
1272 (sign x)
1273 (if (not (strongp sign s))
1274 (if (and (eq sign '$pnz) (eq s '$pn))
1275 (setq sign s)
1276 (setq sign s odds o evens e minus m)))
1277 t)))))
1278 (sign exp))
1279 (return sign)))
1281 (defun numer (x)
1282 (let (($numer t) ; currently, no effect on $float, but proposed to
1283 ($ratprint nil)
1284 result)
1285 ;; Catch a Lisp error, if a floating point overflow occurs.
1286 (setq result (let ((errset nil)) (errset ($float x))))
1287 (if result (car result) nil)))
1289 (defun constp (x)
1290 (cond ((floatp x) 'float)
1291 ((numberp x) 'numer)
1292 ((symbolp x) (if (member x '($%pi $%e $%phi $%gamma) :test #'eq) 'symbol))
1293 ((atom x) nil)
1294 ((eq (caar x) 'rat) 'numer)
1295 ((eq (caar x) 'bigfloat) 'bigfloat)
1296 ((specrepp x) (constp (specdisrep x)))
1297 (t (do ((l (cdr x) (cdr l)) (dum) (ans 'numer))
1298 ((null l) ans)
1299 (setq dum (constp (car l)))
1300 (cond ((eq dum 'float) (return 'float))
1301 ((eq dum 'numer))
1302 ((eq dum 'bigfloat) (setq ans 'bigfloat))
1303 ((eq dum 'symbol)
1304 (if (eq ans 'numer) (setq ans 'symbol)))
1305 (t (return nil)))))))
1307 (mapcar #'(lambda (s) (putprop (first s) (second s) 'sign-function))
1308 (list
1309 (list 'mtimes 'sign-mtimes)
1310 (list 'mplus 'sign-mplus)
1311 (list 'mexpt 'sign-mexpt)
1312 (list '%log 'sign-log)
1313 (list 'mabs 'sign-mabs)
1314 (list '$min #'(lambda (x) (sign-minmax (caar x) (cdr x))))
1315 (list '$max #'(lambda (x) (sign-minmax (caar x) (cdr x))))
1316 (list '%csc #'(lambda (x) (sign (inv* (cons (ncons (zl-get (caar x) 'recip)) (cdr x))))))
1317 (list '%csch #'(lambda (x) (sign (inv* (cons (ncons (zl-get (caar x) 'recip)) (cdr x))))))
1319 (list '%signum #'(lambda (x) (sign (cadr x))))
1320 (list '%erf #'(lambda (x) (sign (cadr x))))
1321 (list '$li #'(lambda (x)
1322 (let ((z (first (margs x))) (n (cadadr x)))
1323 (if (and (mnump n) (eq t (mgrp z 0)) (eq t (mgrp 1 z))) (sign z) (sign-any x)))))))
1324 (defun sign (x)
1325 (cond ((mnump x) (setq sign (rgrp x 0) minus nil odds nil evens nil))
1326 ((and *complexsign* (symbolp x) (eq x '$%i))
1327 ;; In Complex Mode the sign of %i is $imaginary.
1328 (setq sign '$imaginary))
1329 ((symbolp x) (if (eq x '$%i) (imag-err x)) (sign-any x))
1330 ((and (consp x) (symbolp (caar x)) (not (specrepp x)) (get (caar x) 'sign-function))
1331 (funcall (get (caar x) 'sign-function) x))
1332 ((and (consp x) (not (specrepp x)) ($subvarp (mop x)) (get (mop (mop x)) 'sign-function))
1333 (funcall (get (mop (mop x)) 'sign-function) x))
1334 ((specrepp x) (sign (specdisrep x)))
1335 ((kindp (caar x) '$posfun) (sign-posfun x))
1336 ((kindp (caar x) '$oddfun) (sign-oddfun x))
1337 (t (sign-any x))))
1339 (defun sign-any (x)
1340 (cond ((and *complexsign*
1341 (symbolp x)
1342 (decl-complexp x))
1343 ;; In Complex Mode look for symbols declared to be complex.
1344 (if ($featurep x '$imaginary)
1345 (setq sign '$imaginary)
1346 (setq sign '$complex)))
1347 ((and *complexsign*
1348 (not (atom x))
1349 (decl-complexp (caar x)))
1350 ;; A function f(x), where f is declared to be imaginary or complex.
1351 (if ($featurep (caar x) '$imaginary)
1352 (setq sign '$imaginary)
1353 (setq sign '$complex)))
1355 (dcompare x 0)
1356 (if (and $assume_pos
1357 (member sign '($pnz $pz $pn) :test #'eq)
1358 (if $assume_pos_pred
1359 (let ((*x* x))
1360 (declare (special *x*))
1361 (is '(($assume_pos_pred) *x*)))
1362 (mapatom x)))
1363 (setq sign '$pos))
1364 (setq minus nil evens nil
1365 odds (if (not (member sign '($pos $neg $zero) :test #'eq))
1366 (ncons x))))))
1368 (defun sign-mtimes (x)
1369 (setq x (cdr x))
1370 (do ((s '$pos) (m) (o) (e)) ((null x) (setq sign s minus m odds o evens e))
1371 (sign1 (car x))
1372 (cond ((eq sign '$zero) (return t))
1373 ((and *complexsign* (eq sign '$complex))
1374 ;; Found a complex factor. We don't return immediately
1375 ;; because another factor could be zero.
1376 (setq s '$complex))
1377 ((and *complexsign* (eq s '$complex))) ; continue the loop
1378 ((and *complexsign* (eq sign '$imaginary))
1379 ;; Found an imaginary factor. Look if we have already one.
1380 (cond ((eq s '$imaginary)
1381 ;; imaginary*imaginary is real. But remember the sign in m.
1382 (setq s (if m '$pos '$neg) m (not m)))
1383 (t (setq s sign))))
1384 ((and *complexsign* (eq s '$imaginary))) ; continue the loop
1385 ((eq sign '$pos))
1386 ((eq sign '$neg) (setq s (flip s) m (not m)))
1387 ((prog2 (setq m (not (eq m minus)) o (nconc odds o) e (nconc evens e))
1388 nil))
1389 ((eq s sign) (when (eq s '$nz) (setq s '$pz)))
1390 ((eq s '$pos) (setq s sign))
1391 ((eq s '$neg) (setq s (flip sign)))
1392 ((or (and (eq s '$pz) (eq sign '$nz))
1393 (and (eq s '$nz) (eq sign '$pz)))
1394 (setq s '$nz))
1395 (t (setq s '$pnz)))
1396 (setq x (cdr x))))
1398 (defun sign-mplus (x &aux s o e m)
1399 (cond ((signdiff x))
1400 ((prog2 (setq s sign e evens o odds m minus) nil))
1401 ((signsum x))
1402 ((prog2 (cond ((strongp s sign))
1403 (t (setq s sign e evens o odds m minus)))
1404 nil))
1405 ((and (not factored) (signfactor x)))
1406 ((strongp sign s))
1407 (t (setq sign s evens e odds o minus m))))
1409 (defun signdiff (x)
1410 (setq sign '$pnz)
1411 (let ((swapped nil) (retval))
1412 (with-compsplt (lhs rhs x)
1413 (if (and (mplusp lhs) (equal rhs 0) (null (cdddr lhs)))
1414 (cond ((and (negp (cadr lhs)) (not (negp (caddr lhs))))
1415 (setq rhs (neg (cadr lhs)) lhs (caddr lhs)))
1416 ;; The following fixes SourceForge bug #3148
1417 ;; where sign(a-b) returned pnz and sign(b-a) returned pos.
1418 ;; Previously, only the case (-a)+b was handled.
1419 ;; Now we also handle a+(-b) by swapping lhs and rhs,
1420 ;; setting a flag "swapped", running through the same code and
1421 ;; then flipping the answer.
1422 ((and (negp (caddr lhs)) (not (negp (cadr lhs))))
1423 (setq rhs (cadr lhs) lhs (neg (caddr lhs)) swapped t))))
1424 (let (dum)
1425 (setq retval
1426 (cond ((or (equal rhs 0) (mplusp lhs)) nil)
1427 ((and (member (constp rhs) '(numer symbol) :test #'eq)
1428 (numberp (setq dum (numer rhs)))
1429 (prog2 (setq rhs dum) nil)))
1430 ((mplusp rhs) nil)
1431 ((and (dcompare lhs rhs) (member sign '($pos $neg $zero) :test #'eq)))
1432 ((and (not (atom lhs)) (not (atom rhs))
1433 (eq (caar lhs) (caar rhs))
1434 (kindp (caar lhs) '$increasing))
1435 (sign (sub (cadr lhs) (cadr rhs)))
1437 ((and (not (atom lhs)) (not (atom rhs))
1438 (eq (caar lhs) (caar rhs))
1439 (kindp (caar lhs) '$decreasing))
1440 (sign (sub (cadr rhs) (cadr lhs)))
1442 ((and (not (atom lhs)) (eq (caar lhs) 'mabs)
1443 (alike1 (cadr lhs) rhs))
1444 (setq sign '$pz minus nil odds nil evens nil) t)
1445 ((signdiff-special lhs rhs))))))
1446 (if swapped
1447 (setq sign (flip sign)))
1448 retval))
1450 (defun signdiff-special (xlhs xrhs)
1451 ;; xlhs may be a constant
1452 (let ((sgn nil))
1453 (when (or (and (realp xrhs) (minusp xrhs)
1454 (not (atom xlhs)) (eq (sign* xlhs) '$pos))
1455 ; e.g. sign(a^3+%pi-1) where a>0
1456 (and (mexptp xlhs)
1457 ;; e.g. sign(%e^x-1) where x>0
1458 (eq (sign* (caddr xlhs)) '$pos)
1459 (or (and
1460 ;; Q^Rpos - S, S<=1, Q>1
1461 (member (sign* (sub 1 xrhs)) '($pos $zero $pz) :test #'eq)
1462 (eq (sign* (sub (cadr xlhs) 1)) '$pos))
1463 (and (not (eq $domain '$complex))
1464 ;; Qpos ^ Rpos - Spos => Qpos - Spos^(1/Rpos)
1465 (eq (sign* (cadr xlhs)) '$pos)
1466 (eq (sign* xrhs) '$pos)
1467 (eq (sign* (sub (cadr xlhs)
1468 (power xrhs (div 1 (caddr xlhs)))))
1469 '$pos))))
1470 (and (mexptp xlhs) (mexptp xrhs)
1471 ;; Q^R - Q^T, Q>1, (R-T) > 0
1472 ;; e.g. sign(2^x-2^y) where x>y
1473 (alike1 (cadr xlhs) (cadr xrhs))
1474 (eq (sign* (sub (cadr xlhs) 1)) '$pos)
1475 (eq (sign* (sub (caddr xlhs) (caddr xrhs))) '$pos)))
1476 (setq sgn '$pos))
1478 ;; sign(sin(x)+c)
1479 (when (and (not (atom xlhs))
1480 (member (caar xlhs) '(%sin %cos))
1481 (zerop1 ($imagpart (cadr xlhs))))
1482 (cond ((eq (sign* (add xrhs 1)) '$neg) ;; c > 1
1483 (setq sgn '$pos))
1484 ((eq (sign* (add xrhs -1)) '$pos) ;; c < -1
1485 (setq sgn '$neg))
1486 ((zerop1 (add xrhs 1)) ;; c = 1
1487 (setq sgn '$pz))
1488 ((zerop1 (add xrhs -1)) ;; c = -1
1489 (setq sgn '$nz))))
1491 (when (and $useminmax (or (minmaxp xlhs) (minmaxp xrhs)))
1492 (setq sgn (signdiff-minmax xlhs xrhs)))
1493 (when sgn (setq sign sgn minus nil odds nil evens nil)
1494 t)))
1496 ;;; Look for symbols with an assumption a > n or a < -n, where n is a number.
1497 ;;; For this case shift the symbol a -> a+n in a summation and multiplication.
1498 ;;; This handles cases like a>1 and b>1 gives sign(a+b-2) -> pos.
1500 (defun sign-shift (expr)
1501 (do ((l (cdr expr) (cdr l))
1502 (fl nil)
1503 (acc nil))
1504 ((null l) (addn acc nil))
1505 (cond ((symbolp (car l))
1506 ;; Get the facts related to the symbol (car l)
1507 ;; Reverse the order to test the newest facts first.
1508 (setq fl (reverse (cdr (facts1 (car l)))))
1509 (push (car l) acc)
1510 (dolist (f fl)
1511 (cond ((and (eq (caar f) 'mgreaterp)
1512 (mnump (caddr f))
1513 (eq ($sign (caddr f)) '$pos))
1514 ;; The case a > n, where a is a symbol and n a number.
1515 ;; Add the number to the list of terms.
1516 (return (push (caddr f) acc)))
1517 ((and (eq (caar f) 'mgreaterp)
1518 (mnump (cadr f))
1519 (eq ($sign (cadr f)) '$neg))
1520 ;; The case a < -n, where a is a symbol and n a number.
1521 ;; Add the number to the list of terms.
1522 (return (push (cadr f) acc))))))
1523 ((mtimesp (car l))
1524 (let ((acctimes) (flag))
1525 ;; Go through the factors of the multiplication.
1526 (dolist (ll (cdar l))
1527 (cond ((symbolp ll)
1528 ;; Get the facts related to the symbol (car l)
1529 ;; Reverse the order to test the newest facts first.
1530 (setq fl (reverse (cdr (facts1 ll))))
1531 (dolist (f fl)
1532 (cond ((and (eq (caar f) 'mgreaterp)
1533 (mnump (caddr f))
1534 (eq ($sign (caddr f)) '$pos))
1535 ;; The case a > n, where a is a symbol and n a
1536 ;; number. Add the number to the list of terms.
1537 (setq flag t)
1538 (return (push (add ll (caddr f)) acctimes)))
1539 ((and (eq (caar f) 'mgreaterp)
1540 (mnump (cadr f))
1541 (eq ($sign (cadr f)) '$neg))
1542 ;; The case a < -n, where a is a symbol and n a
1543 ;; number. Add the number to the list of terms.
1544 (setq flag t)
1545 (return (push (add ll (cadr f)) acctimes)))))
1546 (when (not flag) (push ll acctimes)))
1548 (push ll acctimes))))
1549 (if flag
1550 ;; If a shift has been done expand the factors.
1551 (push ($multthru (muln acctimes nil)) acc)
1552 (push (muln acctimes nil) acc))))
1554 (push (car l) acc)))))
1556 (defun signsum (x)
1557 (setq x (sign-shift x))
1558 ;; x might be simplified to an atom in sign-shift.
1559 (when (atom x) (setq x (cons '(mplus) (list x))))
1560 (do ((l (cdr x) (cdr l)) (s '$zero))
1561 ((null l) (setq sign s minus nil odds (list x) evens nil)
1562 (cond (*complexsign*
1563 ;; Because we have continued the loop in Complex Mode
1564 ;; we have to look for the sign '$pnz and return nil.
1565 (if (eq s '$pnz) nil t))
1566 (t t))) ; in Real Mode return T
1567 ;; Call sign1 and not sign, because sign1 handles constant expressions.
1568 (sign1 (car l))
1569 (cond ((and *complexsign*
1570 (or (eq sign '$complex) (eq sign '$imaginary)))
1571 ;; Found a complex or imaginary expression. The sign is $complex.
1572 (setq sign '$complex odds nil evens nil minus nil)
1573 (return t))
1574 ((or (and (eq sign '$zero)
1575 (setq x (sub x (car l))))
1576 (and (eq s sign) (not (eq s '$pn))) ; $PN + $PN = $PNZ
1577 (and (eq s '$pos) (eq sign '$pz))
1578 (and (eq s '$neg) (eq sign '$nz))))
1579 ((or (and (member sign '($pz $pos) :test #'eq) (member s '($zero $pz) :test #'eq))
1580 (and (member sign '($nz $neg) :test #'eq) (member s '($zero $nz) :test #'eq))
1581 (and (eq sign '$pn) (eq s '$zero)))
1582 (setq s sign))
1584 (cond (*complexsign*
1585 ;; In Complex Mode we have to continue the loop to look further
1586 ;; for a complex or imaginay expression.
1587 (setq s '$pnz))
1589 ;; In Real mode the loop stops when the sign is 'pnz.
1590 (setq sign '$pnz odds (list x) evens nil minus nil)
1591 (return nil)))))))
1593 (defun signfactor (x)
1594 (let (y (factored t))
1595 (setq y (factor-if-small x))
1596 (cond ((or (mplusp y) (> (conssize y) 50.))
1597 (setq sign '$pnz)
1598 nil)
1599 (t (sign y)))))
1601 (defun factor-if-small (x)
1602 (if (< (conssize x) 51.)
1603 (let ($ratprint)
1604 (declare (special $ratprint))
1605 (factor x)) x))
1607 (defun sign-mexpt (x)
1608 (let* ((expt (caddr x)) (base1 (cadr x))
1609 (sign-expt (sign1 expt)) (sign-base (sign1 base1))
1610 (evod (evod expt)))
1611 (cond ((and *complexsign* (or (eq sign-expt '$complex)
1612 (eq sign-expt '$imaginary)
1613 (eq sign-base '$complex)))
1614 ;; Base is complex or exponent is complex or imaginary.
1615 ;; The sign is $complex.
1616 (when *debug-compar*
1617 (format t "~&in SIGN-MEXPT for ~A, sign is complex.~%" x))
1618 (setq sign '$complex))
1620 ((and *complexsign*
1621 (eq sign-base '$neg)
1622 (eq (evod ($expand (mul 2 expt))) '$odd))
1623 ;; Base is negative and the double of the exponent is odd.
1624 ;; Result is imaginary.
1625 (when *debug-compar*
1626 (format t "~&in SIGN-MEXPT for ~A, sign is $imaginary.~%" x))
1627 (setq sign '$imaginary))
1629 ((and *complexsign*
1630 (eq sign-base '$imaginary))
1631 ;; An imaginary base. Look for even or odd exponent.
1632 (when *debug-compar*
1633 (format t "~&in SIGN-MEXPT for ~A, base is $imaginary.~%" x))
1634 (cond
1635 ((and (integerp expt) (eq evod '$even))
1636 (setq sign (if (eql (mod expt 4) 0) '$pz '$nz)))
1637 ((and (integerp expt) (eq evod '$odd))
1638 (setq sign '$imaginary
1639 minus (if (eql (mod (- expt 1) 4) 0) t nil)))
1640 (t (setq sign '$complex))))
1642 ((and (eq sign-base '$zero)
1643 (member sign-expt '($zero $neg) :test #'eq))
1644 (dbzs-err x))
1645 ((eq sign-expt '$zero) (setq sign '$pos))
1646 ((eq sign-base '$pos))
1647 ((eq sign-base '$zero))
1648 ((eq evod '$even)
1649 (cond ((eq sign-expt '$neg)
1650 (setq sign '$pos minus nil evens (ncons base1) odds nil))
1651 ((member sign-base '($pn $neg) :test #'eq)
1652 (setq sign '$pos minus nil
1653 evens (nconc odds evens)
1654 odds nil))
1655 (t (setq sign '$pz minus nil
1656 evens (nconc odds evens)
1657 odds nil))))
1658 ((and (member sign-expt '($neg $nz) :test #'eq)
1659 (member sign-base '($nz $pz $pnz) :test #'eq))
1660 (setq sign (cond ((eq sign-base '$pnz) '$pn)
1661 ((eq sign-base '$pz) '$pos)
1662 ((eq sign-expt '$neg) '$neg)
1663 (t '$pn))))
1664 ((member sign-expt '($pz $nz $pnz) :test #'eq)
1665 (cond ((eq sign-base '$neg)
1666 (setq odds (ncons x) sign '$pn))))
1667 ((eq sign-expt '$pn))
1668 ((ratnump expt)
1669 (cond ((mevenp (cadr expt))
1670 (cond ((member sign-base '($pn $neg) :test #'eq)
1671 (setq sign-base '$pos))
1672 ((member sign-base '($pnz $nz) :test #'eq)
1673 (setq sign-base '$pz)))
1674 (setq evens (nconc odds evens)
1675 odds nil minus nil))
1676 ((mevenp (caddr expt))
1677 (cond ((and *complexsign* (eq sign-base '$neg))
1678 ;; In Complex Mode the sign is $complex.
1679 (setq sign-base (setq sign-expt '$complex)))
1680 (complexsign
1681 ;; The only place the variable complexsign
1682 ;; is used. Unfortunately, one routine in
1683 ;; to_poly.lisp in /share/to_poly_solve depends on
1684 ;; this piece of code. Perhaps we can remove
1685 ;; the dependency. (12/2008)
1686 (setq sign-base (setq sign-expt '$pnz)))
1687 ((eq sign-base '$neg) (imag-err x))
1688 ((eq sign-base '$pn)
1689 (setq sign-base '$pos))
1690 ((eq sign-base '$nz)
1691 (setq sign-base '$zero))
1692 (t (setq sign-base '$pz)))))
1693 (cond ((eq sign-expt '$neg)
1694 (cond ((eq sign-base '$zero) (dbzs-err x))
1695 ((eq sign-base '$pz)
1696 (setq sign-base '$pos))
1697 ((eq sign-base '$nz)
1698 (setq sign-base '$neg))
1699 ((eq sign-base '$pnz)
1700 (setq sign-base '$pn)))))
1701 (setq sign sign-base))
1702 ((eq sign-base '$pos)
1703 (setq sign '$pos))
1704 ((eq sign-base '$neg)
1705 (if (eq evod '$odd)
1706 (setq sign '$neg)
1707 (setq sign (if *complexsign* '$complex '$pn)))))))
1709 ;;; Determine the sign of log(expr). This function changes the special variable sign.
1711 (defun sign-log (x)
1712 (setq x (cadr x))
1713 (setq sign
1714 (cond ((eq t (mgrp x 0))
1715 (cond ((eq t (mgrp 1 x)) '$neg)
1716 ((eq t (meqp x 1)) '$zero);; log(1) = 0.
1717 ((eq t (mgqp 1 x)) '$nz)
1718 ((eq t (mgrp x 1)) '$pos)
1719 ((eq t (mgqp x 1)) '$pz)
1720 ((eq t (mnqp x 1)) '$pn)
1721 (t '$pnz)))
1722 ((and *complexsign* (eql 1 (cabs x))) '$imaginary)
1723 (*complexsign* '$complex)
1724 (t '$pnz))))
1726 (defun sign-mabs (x)
1727 (let ((*complexsign* t))
1728 (sign (cadr x))
1729 (cond ((member sign '($pos $zero) :test #'eq))
1730 ((member sign '($neg $pn) :test #'eq) (setq sign '$pos))
1731 (t (setq sign '$pz minus nil evens (nconc odds evens) odds nil)))))
1733 ;;; Compare min/max
1735 ;;; Macros used in simp min/max
1736 ;;; If op is min, use body; if not, negate sign constants in body
1737 ;;; Used to avoid writing min and max code separately: just write the min code
1738 ;;; in such a way that its dual works for max
1739 (defmacro minmaxforms (op &rest body)
1740 `(if (eq ,op '$min)
1741 ,@body
1742 ,@(sublis '(($neg . $pos)
1743 ($nz . $pz)
1744 ($pz . $nz)
1745 ($pos . $neg)
1746 ;;($zero . $zero)
1747 ;;($pn . $pn)
1748 ;;($pnz . $pnz)
1750 ($max . $min)
1751 ($min . $max)
1753 ($inf . $minf)
1754 ($minf . $inf))
1755 body)))
1757 (defun sign-minmax (op args)
1758 (do ((sgn (minmaxforms op '$pos) ;identity element for min
1759 (sminmax op sgn (sign* (car l))))
1760 (end (minmaxforms op '$neg))
1761 (l args (cdr l)))
1762 ((or (null l) (eq sgn end))
1763 (setq minus nil
1764 odds (if (not (member sgn '($pos $neg $zero) :test #'eq))
1765 (ncons (cons (list op) args)))
1766 evens nil
1767 sign sgn))))
1769 ;; sign(op(a,b)) = sminmax(sign(a),sign(b))
1770 ;; op is $min/$max; s1/s2 in neg, nz, zero, pz, pos, pn, pnz
1771 (defun sminmax (op s1 s2)
1772 (minmaxforms
1774 ;; Many of these cases don't come up in simplified expressions,
1775 ;; since e.g. sign(a)=neg and sign(b)=pos implies min(a,b)=a
1776 ;; the order of these clauses is important
1777 (cond ((eq s1 '$pos) s2)
1778 ((eq s2 '$pos) s1)
1779 ((eq s1 s2) s1)
1780 ((or (eq s1 '$neg) (eq s2 '$neg)) '$neg)
1781 ((or (eq s1 '$nz) (eq s2 '$nz)) '$nz)
1782 ((eq s1 '$zero) (if (eq s2 '$pz) '$zero '$nz))
1783 ((eq s2 '$zero) (if (eq s1 '$pz) '$zero '$nz))
1784 (t '$pnz))))
1786 (defun minmaxp (ex)
1787 (cond ((atom ex) nil)
1788 ((member (caar ex) '($min $max) :test #'eq) (caar ex))
1789 (t nil)))
1791 (defun signdiff-minmax (l r)
1792 ;; sign of l-r; nil if unknown (not PNZ)
1793 (let* ((lm (minmaxp l))
1794 (rm (minmaxp r))
1795 (ll (if lm (cdr l)))
1796 (rr (if rm (cdr r)))) ;distinguish between < and <= argument lists of min/max
1797 (minmaxforms
1798 (or rm lm)
1799 (cond ((eq lm rm) ; min(a,...) - min(b,...)
1800 (multiple-value-bind (both onlyl onlyr) (intersect-info ll rr)
1801 (declare (ignore both))
1802 (cond ((null onlyl) '$pz) ; min(a,b) - min(a,b,c)
1803 ((null onlyr) '$nz) ; min(a,b,c) - min(a,b)
1804 ;; TBD: add processing for full onlyl/onlyr case
1805 (t nil))))
1806 ;; TBD: memalike and set-disjointp are crude approx.
1807 ((null lm) (if (memalike l rr) '$pz)) ; a - min(a,b)
1808 ((null rm) (if (memalike r ll) '$nz)) ; min(a,b) - a
1809 (t ; min/max or max/min
1810 (if (not (set-disjointp ll rr)) '$pz)))))) ; max(a,q,r) - min(a,s,t)
1812 (defun intersect-info (a b)
1813 (let ((both nil)
1814 (onlya nil)
1815 (onlyb nil))
1816 (do-merge-asym
1818 #'like
1819 #'$orderlessp
1820 #'(lambda (x) (push x both))
1821 #'(lambda (x) (push x onlya))
1822 #'(lambda (x) (push x onlyb)))
1823 (values
1824 (reverse both)
1825 (reverse onlya)
1826 (reverse onlyb))))
1828 ;;; end compare min/max
1830 (defun sign-posfun (xx)
1831 (declare (ignore xx))
1832 (setq sign '$pos
1833 minus nil
1834 odds nil
1835 evens nil))
1837 (defun sign-oddfun (x)
1838 (cond ((kindp (caar x) '$increasing)
1839 ; Take the sign of the argument
1840 (sign (cadr x)))
1841 ((kindp (caar x) '$decreasing)
1842 ; Take the sign of negative of the argument
1843 (sign (neg (cadr x))))
1845 ; If the sign of the argument is zero, then we're done (the sign of
1846 ; the function value is the same). Otherwise, punt to SIGN-ANY.
1847 (sign (cadr x))
1848 (unless (eq sign '$zero)
1849 (sign-any x)))))
1851 (defun imag-err (x)
1852 (if sign-imag-errp
1853 (merror (intl:gettext "sign: argument cannot be imaginary; found ~M") x)
1854 (throw 'sign-imag-err t)))
1856 (defun dbzs-err (x)
1857 (merror (intl:gettext "sign: division by zero in ~M") x))
1859 ;; Return true iff e is an expression with operator op1, op2,...,or opn.
1861 (defun op-equalp (e &rest op)
1862 (and (consp e) (consp (car e)) (some #'(lambda (s) (equal (caar e) s)) op)))
1864 ;; Return true iff the operator of a is a Maxima relation operator.
1866 (defun mrelationp (a)
1867 (op-equalp a 'mlessp 'mleqp 'mequal 'mnotequal 'mgeqp 'mgreaterp))
1869 ;; This version of featurep applies ratdisrep to the first argument. This
1870 ;; change allows things like featurep(rat(n),integer) --> true when n has
1871 ;; been declared an integer.
1873 (defmfun $featurep (e ind)
1874 (setq e ($ratdisrep e))
1875 (cond ((not (symbolp ind))
1876 (merror
1877 (intl:gettext "featurep: second argument must be a symbol; found ~M")
1878 ind))
1879 ;; Properties not related to the assume database.
1880 ((and (member ind opers) (safe-get e ind)))
1881 ((and (member ind '($evfun $evflag $bindtest $nonarray))
1882 (safe-get e (stripdollar ind))))
1883 ((and (eq ind '$noun)
1884 (safe-get e (stripdollar ind))
1886 ((and (member ind '($scalar $nonscalar $mainvar))
1887 (mget e ind)))
1888 ((and (eq ind '$feature)
1889 (member e $features)
1891 ((eq ind '$alphabetic)
1892 (dolist (l (coerce e 'list) t)
1893 (when (not (member l *alphabet*)) (return nil))))
1894 ;; Properties related to the assume database.
1895 ((eq ind '$integer) (maxima-integerp e))
1896 ((eq ind '$noninteger) (nonintegerp e))
1897 ((eq ind '$even) (mevenp e))
1898 ((eq ind '$odd) (moddp e))
1899 ((eq ind '$real)
1900 (if (atom e)
1901 (or (numberp e) (kindp e '$real) (numberp (numer e)))
1902 (free ($rectform e) '$%i)))
1903 ((symbolp e) (kindp e ind))))
1905 ;; Give a function the maps-integers-to-integers property when it is integer
1906 ;; valued on the integers; give it the integer-valued property when its
1907 ;; range is a subset of the integers. What have I missed?
1909 (setf (get 'mplus 'maps-integers-to-integers) t)
1910 (setf (get 'mtimes 'maps-integers-to-integers) t)
1911 (setf (get 'mabs 'maps-integers-to-integers) t)
1912 (setf (get '$max 'maps-integers-to-integers) t)
1913 (setf (get '$min 'maps-integers-to-integers) t)
1915 (setf (get '$floor 'integer-valued) t)
1916 (setf (get '$ceiling 'integer-valued) t)
1917 (setf (get '$charfun 'integer-valued) t)
1919 (defun maxima-integerp (x)
1920 (cond ((integerp x))
1921 ((mnump x) nil)
1922 ((and (symbolp x)
1923 (or (kindp x '$integer)
1924 (kindp x '$even)
1925 (kindp x '$odd)
1926 (check-integer-facts x))))
1927 (t (let ((x-op (and (consp x) (consp (car x)) (caar x))) ($prederror nil))
1928 (cond ((null x-op) nil)
1929 ((not (symbolp x-op)) nil) ; fix for mqapply at some point?
1930 ((eq x-op 'mrat) (and (integerp (cadr x)) (equal (cddr x) 1)))
1931 ;; mtimes and mplus are generally handled by this clause
1932 ((and (get x-op 'maps-integers-to-integers) (every #'maxima-integerp (margs x))))
1933 ;; Special case for 1/2*...*even
1934 ((eq x-op 'mtimes)
1935 (and (mnump (cadr x))
1936 (integerp (mul 2 (cadr x)))
1937 (every 'maxima-integerp (cddr x))
1938 (some #'(lambda (s) ($featurep s '$even)) (rest (margs x)))))
1939 ((eq x-op 'mexpt)
1940 (and (every #'maxima-integerp (margs x))
1941 (null (mevalp (mlsp (caddr x) 0)))))
1942 ;; ! in Maxima allows real arguments
1943 ((eq x-op 'mfactorial)
1944 (and (maxima-integerp (cadr x))
1945 (not (mevalp (mlsp (cadr x) 0)))))
1946 ((eq x-op '%gamma)
1947 (and (maxima-integerp (cadr x))
1948 (not (mevalp (mlsp (cadr x) 1)))))
1949 ;; other x-ops
1950 ((or ($featurep ($verbify x-op) '$integervalued)
1951 (get x-op 'integer-valued))))))))
1953 ;; When called with mode 'integer look into the database for symbols which are
1954 ;; declared to be equal to an integer or an expression which is an integer.
1955 ;; In mode 'evod look for odd and even expressions.
1956 (defun check-integer-facts (x &optional (mode 'integer))
1957 (do ((factsl (cdr (facts1 x)) (cdr factsl))
1958 fact)
1959 ((null factsl) nil)
1960 (setq fact (car factsl))
1961 (cond ((and (not (atom fact))
1962 (eq (caar fact) '$equal))
1963 (cond ((and (symbolp (cadr fact))
1964 (eq (cadr fact) x))
1965 ;; Case equal(x,expr): Test expr to be an integer.
1966 (cond ((symbolp (caddr fact))
1967 (cond ((and (eq mode 'integer)
1968 (or (kindp (caddr fact) '$integer)
1969 (kindp (caddr fact) '$odd)
1970 (kindp (caddr fact) '$even)))
1971 (return t))
1972 ((eq mode 'evod)
1973 (cond ((kindp (caddr fact) '$odd)
1974 (return '$odd))
1975 ((kindp (caddr fact) '$even)
1976 (return '$even))
1977 (t (return nil))))
1978 (t (return nil))))
1980 (cond ((eq mode 'integer)
1981 (return (maxima-integerp (caddr fact))))
1982 ((eq mode 'evod)
1983 (return (evod (caddr fact))))
1984 (t (return nil))))))
1985 ((and (symbolp (caddr fact))
1986 (eq (caddr fact) x))
1987 ;; Case equal(expr,x): Test expr to be an integer.
1988 (cond ((symbolp (caddr fact))
1989 (cond ((and (eq mode 'integer)
1990 (or (kindp (cadr fact) '$integer)
1991 (kindp (cadr fact) '$odd)
1992 (kindp (cadr fact) '$even)))
1993 (return t))
1994 ((eq mode 'evod)
1995 (cond ((kindp (cadr fact) '$odd)
1996 (return '$odd))
1997 ((kindp (cadr fact) '$even)
1998 (return '$even))
1999 (t (return nil))))
2000 (t (return nil))))
2002 (cond ((eq mode 'integer)
2003 (return (maxima-integerp (cadr fact))))
2004 ((eq mode 'evod)
2005 (return (evod (cadr fact))))
2006 (t (return nil)))))))))))
2008 (defun nonintegerp (e)
2009 (cond ((and (symbolp e) (or (kindp e '$noninteger) (check-noninteger-facts e) (kindp e '$irrational)))) ;declared noninteger
2010 ((mnump e)
2011 (if (integerp e) nil t)) ;all floats are noninteger and integers are not nonintegers
2012 (($ratp e)
2013 (nonintegerp ($ratdisrep e)))
2014 (t (eq t (mgrp e (take '($floor) e))))))
2017 ;; Look into the database for symbols which are declared to be equal
2018 ;; to a noninteger or an expression which is a noninteger.
2019 (defun check-noninteger-facts (x)
2020 (do ((factsl (cdr (facts1 x)) (cdr factsl)))
2021 ((null factsl) nil)
2022 (cond ((and (not (atom (car factsl)))
2023 (eq (caar (car factsl)) '$equal))
2024 (cond ((and (symbolp (cadr (car factsl)))
2025 (eq (cadr (car factsl)) x))
2026 ;; Case equal(x,expr): Test expr to be a noninteger.
2027 (cond ((symbolp (caddr (car factsl)))
2028 (if (kindp (caddr (car factsl)) '$noninteger)
2029 (return t)))
2031 (return (nonintegerp (caddr (car factsl)))))))
2032 ((and (symbolp (caddr (car factsl)))
2033 (eq (caddr (car factsl)) x))
2034 ;; Case equal(expr,x): Test expr to be a noninteger.
2035 (cond ((symbolp (cadr (car factsl)))
2036 (if (kindp (cadr (car factsl)) '$noninteger)
2037 (return t)))
2039 (return (nonintegerp (cadr (car factsl))))))))))))
2041 (defun intp (l)
2042 (every #'maxima-integerp (cdr l)))
2044 (defun mevenp (e)
2045 (cond ((integerp e) (not (oddp e)))
2046 ((mnump e) nil)
2047 (t (eq '$even (evod e)))))
2049 (defun moddp (e)
2050 (cond ((integerp e) (oddp e))
2051 ((mnump e) nil)
2052 (t (eq '$odd (evod e)))))
2054 ;; An extended evod that recognizes that abs(even) is even and
2055 ;; abs(odd) is odd.
2057 (defun evod (e)
2058 (cond ((integerp e) (if (oddp e) '$odd '$even))
2059 ((mnump e) nil)
2060 ((atom e)
2061 (cond ((kindp e '$odd) '$odd)
2062 ((kindp e '$even) '$even)
2063 ;; Check the database for facts.
2064 ((symbolp e) (check-integer-facts e 'evod))))
2065 ((eq 'mtimes (caar e)) (evod-mtimes e))
2066 ((eq 'mplus (caar e)) (evod-mplus e))
2067 ((eq 'mabs (caar e)) (evod (cadr e))) ;; extra code
2068 ((eq 'mexpt (caar e)) (evod-mexpt e))))
2070 (defun evod-mtimes (x)
2071 (do ((l (cdr x) (cdr l)) (flag '$odd))
2072 ((null l) flag)
2073 (setq x (evod (car l)))
2074 (cond ((eq '$odd x))
2075 ((eq '$even x) (setq flag '$even))
2076 ((maxima-integerp (car l)) (cond ((eq '$odd flag) (setq flag nil))))
2077 (t (return nil)))))
2079 (defun evod-mplus (x)
2080 (do ((l (cdr x) (cdr l)) (flag))
2081 ((null l) (cond (flag '$odd) (t '$even)))
2082 (setq x (evod (car l)))
2083 (cond ((eq '$odd x) (setq flag (not flag)))
2084 ((eq '$even x))
2085 (t (return nil)))))
2087 (defun evod-mexpt (x)
2088 (when (and (integerp (caddr x)) (not (minusp (caddr x))))
2089 (evod (cadr x))))
2091 (declare-top (special mgqp mlqp))
2093 (defmode cl ()
2094 (atom (selector +labs) (selector -labs) (selector data)))
2096 (defmacro c-dobj (&rest x)
2097 `(list ,@x))
2099 (defun dcompare (x y)
2100 (setq odds (list (sub x y)) evens nil minus nil
2101 sign (cond ((eq x y) '$zero)
2102 ((or (eq '$inf x) (eq '$minf y)) '$pos)
2103 ((or (eq '$minf x) (eq '$inf y)) '$neg)
2104 (t (dcomp x y)))))
2106 (defun dcomp (x y)
2107 (let (mgqp mlqp)
2108 (setq x (dinternp x) y (dinternp y))
2109 (cond ((or (null x) (null y)) '$pnz)
2110 ((progn (clear) (deq x y) (sel y +labs)))
2111 (t '$pnz))))
2113 (defun deq (x y)
2114 (cond ((dmark x '$zero) nil)
2115 ((eq x y))
2116 (t (do ((l (sel x data) (cdr l))) ((null l))
2117 (if (and (visiblep (car l)) (deqf x y (car l))) (return t))))))
2119 (defun deqf (x y f)
2120 (cond ((eq 'meqp (caar f))
2121 (if (eq x (cadar f)) (deq (caddar f) y) (deq (cadar f) y)))
2122 ((eq 'mgrp (caar f))
2123 (if (eq x (cadar f)) (dgr (caddar f) y) (dls (cadar f) y)))
2124 ((eq 'mgqp (caar f))
2125 (if (eq x (cadar f)) (dgq (caddar f) y) (dlq (cadar f) y)))
2126 ((eq 'mnqp (caar f))
2127 (if (eq x (cadar f)) (dnq (caddar f) y) (dnq (cadar f) y)))))
2129 (defun dgr (x y)
2130 (cond ((dmark x '$pos) nil)
2131 ((eq x y))
2132 (t (do ((l (sel x data) (cdr l)))
2133 ((null l))
2134 (when (or mlqp (and (visiblep (car l)) (dgrf x y (car l))))
2135 (return t))))))
2137 (defun dgrf (x y f)
2138 (cond ((eq 'mgrp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2139 ((eq 'mgqp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2140 ((eq 'meqp (caar f))
2141 (if (eq x (cadar f))
2142 (dgr (caddar f) y)
2143 (dgr (cadar f) y)))))
2145 (defun dls (x y)
2146 (cond ((dmark x '$neg) nil)
2147 ((eq x y))
2148 (t (do ((l (sel x data) (cdr l)))
2149 ((null l))
2150 (when (or mgqp (and (visiblep (car l)) (dlsf x y (car l))))
2151 (return t))))))
2153 (defun dlsf (x y f)
2154 (cond ((eq 'mgrp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2155 ((eq 'mgqp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2156 ((eq 'meqp (caar f))
2157 (if (eq x (cadar f)) (dls (caddar f) y) (dls (cadar f) y)))))
2159 (defun dgq (x y)
2160 (cond ((member (sel x +labs) '($pos $zero) :test #'eq) nil)
2161 ((eq '$nz (sel x +labs)) (deq x y))
2162 ((eq '$pn (sel x +labs)) (dgr x y))
2163 ((dmark x '$pz) nil)
2164 ((eq x y) (setq mgqp t) nil)
2165 (t (do ((l (sel x data) (cdr l))) ((null l))
2166 (if (and (visiblep (car l)) (dgqf x y (car l))) (return t))))))
2168 (defun dgqf (x y f)
2169 (cond ((eq 'mgrp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2170 ((eq 'mgqp (caar f)) (if (eq x (cadar f)) (dgq (caddar f) y)))
2171 ((eq 'meqp (caar f))
2172 (if (eq x (cadar f)) (dgq (caddar f) y) (dgq (cadar f) y)))))
2174 (defun dlq (x y)
2175 (cond ((member (sel x +labs) '($neg $zero) :test #'eq) nil)
2176 ((eq '$pz (sel x +labs)) (deq x y))
2177 ((eq '$pn (sel x +labs)) (dls x y))
2178 ((dmark x '$nz) nil)
2179 ((eq x y) (setq mlqp t) nil)
2180 (t (do ((l (sel x data) (cdr l))) ((null l))
2181 (if (and (visiblep (car l)) (dlqf x y (car l))) (return t))))))
2183 (defun dlqf (x y f)
2184 (cond ((eq 'mgrp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2185 ((eq 'mgqp (caar f)) (if (eq x (caddar f)) (dlq (cadar f) y)))
2186 ((eq 'meqp (caar f))
2187 (if (eq x (cadar f)) (dlq (caddar f) y) (dlq (cadar f) y)))))
2189 (defun dnq (x y)
2190 (cond ((member (sel x +labs) '($pos $neg) :test #'eq) nil)
2191 ((eq '$pz (sel x +labs)) (dgr x y))
2192 ((eq '$nz (sel x +labs)) (dls x y))
2193 ((dmark x '$pn) nil)
2194 ((eq x y) nil)
2195 (t (do ((l (sel x data) (cdr l))) ((null l))
2196 (if (and (visiblep (car l)) (dnqf x y (car l))) (return t))))))
2198 (defun dnqf (x y f)
2199 (cond ((eq 'meqp (caar f))
2200 (if (eq x (cadar f)) (dnq (caddar f) y) (dnq (cadar f) y)))))
2202 ;; mark sign of x to be m, relative to current comparison point for dcomp.
2203 ;; returns true if this fact is already known, nil otherwise.
2204 (defun dmark (x m)
2205 (cond ((eq m (sel x +labs)))
2206 ((and dbtrace (prog1
2208 (mtell (intl:gettext "DMARK: marking ~M ~M") (if (atom x) x (car x)) m))
2209 nil))
2211 (push x +labs)
2212 (push+sto (sel x +labs) m)
2213 nil)))
2215 (defun daddgr (flag x)
2216 (with-compsplt (lhs rhs x)
2217 (mdata flag 'mgrp (dintern lhs) (dintern rhs))
2218 (if (or (mnump lhs) (constant lhs))
2219 (list '(mlessp) rhs lhs)
2220 (list '(mgreaterp) lhs rhs))))
2222 (defun daddgq (flag x)
2223 (with-compsplt (lhs rhs x)
2224 (mdata flag 'mgqp (dintern lhs) (dintern rhs))
2225 (if (or (mnump lhs) (constant lhs))
2226 (list '(mleqp) rhs lhs)
2227 (list '(mgeqp) lhs rhs))))
2229 (defun daddeq (flag x)
2230 (with-compsplt-eq (lhs rhs x)
2231 (mdata flag 'meqp (dintern lhs) (dintern rhs))
2232 (list '($equal) lhs rhs)))
2234 (defun daddnq (flag x)
2235 (with-compsplt-eq (lhs rhs x)
2236 (cond ((and (mtimesp lhs) (equal rhs 0))
2237 (dolist (term (cdr lhs)) (daddnq flag term)))
2238 ((and (mexptp lhs) (mexptp rhs)
2239 (integerp (caddr lhs)) (integerp (caddr rhs))
2240 (equal (caddr lhs) (caddr rhs)))
2241 (mdata flag 'mnqp (dintern (cadr lhs)) (dintern (cadr rhs)))
2242 (cond ((not (oddp (caddr lhs)))
2243 (mdata flag 'mnqp (dintern (cadr lhs))
2244 (dintern (neg (cadr rhs)))))))
2245 (t (mdata flag 'mnqp (dintern lhs) (dintern rhs))))
2246 (list '(mnot) (list '($equal) lhs rhs))))
2248 ;; The following functions are used by asksign to write answers into the
2249 ;; database. We make sure that these answers are written into the global
2250 ;; context '$initial and not in a local context which might be generated during
2251 ;; the evaluation phase and which will be destroyed before the evaluation has
2252 ;; finshed.
2253 ;; The additional facts are removed from the global context '$initial after
2254 ;; finishing the evaluation phase of meval with a call to clearsign.
2256 (defun tdpos (x)
2257 (let ((context '$initial))
2258 (daddgr t x)
2259 (push (cons x '$pos) *local-signs*)))
2261 (defun tdneg (x)
2262 (let ((context '$initial))
2263 (daddgr t (neg x))
2264 (push (cons x '$neg) *local-signs*)))
2266 (defun tdzero (x)
2267 (let ((context '$initial))
2268 (daddeq t x)
2269 (push (cons x '$zero) *local-signs*)))
2271 (defun tdpn (x)
2272 (let ((context '$initial))
2273 (daddnq t x)
2274 (push (cons x '$pn) *local-signs*)))
2276 (defun tdpz (x)
2277 (let ((context '$initial))
2278 (daddgq t x)
2279 (push (cons x '$pz) *local-signs*)))
2281 (defun compsplt-eq (x)
2282 (with-compsplt (lhs rhs x)
2283 (when (equal lhs 0)
2284 (setq lhs rhs rhs 0))
2285 (if (and (equal rhs 0)
2286 (or (mexptp lhs)
2287 (and (not (atom lhs))
2288 (kindp (caar lhs) '$oddfun)
2289 (kindp (caar lhs) '$increasing))))
2290 (setq lhs (cadr lhs)))
2291 (values lhs rhs)))
2293 (defun mdata (flag r x y)
2294 (if flag
2295 (mfact r x y)
2296 (mkill r x y)))
2298 (defun mfact (r x y)
2299 (let ((f (datum (list r x y))))
2300 (cntxt f context)
2301 (addf f x)
2302 (addf f y)))
2304 (defun mkill (r x y)
2305 (let ((f (car (datum (list r x y)))))
2306 (kcntxt f context)
2307 (maxima-remf f x)
2308 (maxima-remf f y)))
2310 (defun mkind (x y)
2311 (kind (dintern x) (dintern y)))
2313 ;; To guess from the previous incarnation of this code,
2314 ;; each argument is assumed to be a float, bigfloat, integer, or Maxima rational.
2315 ;; Convert Maxima rationals to Lisp rationals to make them comparable to others.
2317 (defun rgrp (x y)
2318 (when (or ($bfloatp x) ($bfloatp y))
2319 (setq
2320 x (let (($float2bf t))
2321 (declare (special $float2bf))
2322 (cadr ($bfloat (sub x y))))
2323 y 0))
2324 (if (not (numberp x))
2325 (setq x (/ (cadr x) (caddr x))))
2326 (if (not (numberp y))
2327 (setq y (/ (cadr y) (caddr y))))
2328 (cond
2329 (#-ecl (> x y) #+ecl (> (- x y) 0) '$pos)
2330 (#-ecl (> y x) #+ecl (> (- y x) 0) '$neg)
2331 (t '$zero)))
2333 (defun mcons (x l)
2334 (cons (car l) (cons x (cdr l))))
2336 (defun flip (s)
2337 (cond ((eq '$pos s) '$neg)
2338 ((eq '$neg s) '$pos)
2339 ((eq '$pz s) '$nz)
2340 ((eq '$nz s) '$pz)
2341 (t s)))
2343 (defun strongp (x y)
2344 (cond ((eq '$pnz y))
2345 ((eq '$pnz x) nil)
2346 ((member y '($pz $nz $pn) :test #'eq))))
2348 (defun munformat (form)
2349 (if (atom form)
2350 form
2351 (cons (caar form) (mapcar #'munformat (cdr form)))))
2353 (defun declarekind (var prop) ; This function is for $DECLARE to use.
2354 (let (prop2)
2355 (cond ((truep (list 'kind var prop)) t)
2356 ((or (falsep (list 'kind var prop))
2357 (and (setq prop2 (assoc prop '(($integer . $noninteger)
2358 ($noninteger . $integer)
2359 ($increasing . $decreasing)
2360 ($decreasing . $increasing)
2361 ($symmetric . $antisymmetric)
2362 ($antisymmetric . $symmetric)
2363 ($rational . $irrational)
2364 ($irrational . $rational)
2365 ($oddfun . $evenfun)
2366 ($evenfun . $oddfun)) :test #'eq))
2367 (truep (list 'kind var (cdr prop2)))))
2368 (merror (intl:gettext "declare: inconsistent declaration ~:M") `(($declare) ,var ,prop)))
2369 (t (mkind var prop) t))))
2371 ;;; These functions reformat expressions to be stored in the data base.
2373 ;; Return a list of all the atoms in X that aren't either numbers or constants
2374 ;; whose numerical value we know.
2375 (defun unknown-atoms (x)
2376 (let (($listconstvars t))
2377 (remove-if (lambda (sym) (mget sym '$numer))
2378 (cdr ($listofvars x)))))
2380 ;; Rewrite a^b to a simpler expression that has the same sign:
2381 ;; If b is odd or 1/b is odd, remove the exponent, e.g. x^3 becomes x.
2382 ;; If b has a negative sign, return a^-b, e.g. 1/x^a becomes x^a.
2383 ;; Otherwise, do nothing.
2384 (defun rewrite-mexpt-retaining-sign (x)
2385 (if (mexptp x)
2386 (let ((base (cadr x)) (exponent (caddr x)))
2387 (cond ((or (eq (evod exponent) '$odd) (eq (evod (inv exponent)) '$odd)) base)
2388 ((negp exponent) (inv x))
2389 (t x)))
2392 ;; COMPSPLT
2394 ;; Split X into (values LHS RHS) so that X>0 <=> LHS > RHS. This is supposed to
2395 ;; be a canonical form for X that can be stored in the database and then looked
2396 ;; up in future.
2398 ;; This uses two worker routines: COMPSPLT-SINGLE and COMPSPLT-GENERAL. The
2399 ;; former assumes that X only contains one symbol whose value is not known (eg not %e,
2400 ;; %pi etc.). The latter tries to deal with arbitrary collections of variables.
2401 (defun compsplt (x)
2402 (multiple-value-bind (lhs rhs)
2403 (cond
2404 ((or (atom x) (atom (car x))) (values x 0))
2405 ((null (cdr (unknown-atoms x))) (compsplt-single x))
2406 (t (compsplt-general x)))
2407 (let ((swapped nil))
2408 ;; If lhs is zero, swap lhs and rhs to make the following code simpler.
2409 ;; Remember that they were swapped to swap them back afterwards.
2410 (when (equal lhs 0)
2411 (setq lhs rhs rhs 0 swapped t))
2412 (when (equal rhs 0)
2413 ;; Rewrite mexpts in factors so that e.g. x^3/y>0 becomes x*y>0. */
2414 (setq lhs
2415 (if (mtimesp lhs)
2416 (cons (car lhs) (mapcar #'rewrite-mexpt-retaining-sign (cdr lhs)))
2417 (rewrite-mexpt-retaining-sign lhs))))
2418 ;; Undo swapping lhs and rhs.
2419 (if swapped
2420 (values rhs lhs)
2421 (values lhs rhs)))))
2423 (defun compsplt-single (x)
2424 (do ((exp (list x 0)) (success nil))
2425 ((or success (symbols (cadr exp))) (values (car exp) (cadr exp)))
2426 (cond ((atom (car exp)) (setq success t))
2427 ((eq (caaar exp) 'mplus) (setq exp (splitsum exp)))
2428 ((eq (caaar exp) 'mtimes) (setq exp (splitprod exp)))
2429 (t (setq success t)))))
2431 (defun compsplt-general (x)
2432 ;; Let compsplt-single work on it first to treat constant factors/terms.
2433 (multiple-value-bind (lhs rhs) (compsplt-single x) (setq x (sub lhs rhs)))
2434 (cond
2435 ;; If x is an atom or a single level list then we won't change it any.
2436 ((or (atom x) (atom (car x)))
2437 (values x 0))
2438 ;; If x is a negative expression but not a sum, then get rid of the
2439 ;; negative sign.
2440 ((negp x) (values 0 (neg x)))
2441 ;; If x is not a sum, or is a sum with more than 2 terms, or has some
2442 ;; symbols common to both summands, then do nothing.
2443 ((or (cdddr x)
2444 (not (eq (caar x) 'mplus))
2445 (intersect* (symbols (cadr x)) (symbols (caddr x))))
2446 (values x 0))
2447 ;; -x + y gives (y, x)
2448 ((and (or (negp (cadr x)) (mnump (cadr x)))
2449 (not (negp (caddr x))))
2450 (values (caddr x) (neg (cadr x))))
2451 ;; x - y gives (x, y)
2452 ((and (not (negp (cadr x)))
2453 (or (negp (caddr x)) (mnump (caddr x))))
2454 (values (cadr x) (neg (caddr x))))
2455 ;; - x - y gives (0, x+y)
2456 ((and (negp (cadr x)) (negp (caddr x)))
2457 (values 0 (neg x)))
2458 ;; Give up! (x, 0)
2460 (values x 0))))
2462 (defun negp (x)
2463 (and (mtimesp x) (mnegp (cadr x))))
2465 (defun splitsum (exp)
2466 (do ((llist (cdar exp) (cdr llist))
2467 (lhs1 (car exp))
2468 (rhs1 (cadr exp)))
2469 ((null llist)
2470 (if (mplusp lhs1) (setq success t))
2471 (list lhs1 rhs1))
2472 (cond ((member '$inf llist :test #'eq)
2473 (setq rhs1 (add2 '$inf (sub* rhs1 (addn llist t)))
2474 lhs1 (add2 '$inf (sub* lhs1 (addn llist t)))
2475 llist nil))
2476 ((member '$minf llist :test #'eq)
2477 (setq rhs1 (add2 '$minf (sub* rhs1 (addn llist t)))
2478 lhs1 (add2 '$minf (sub* lhs1 (addn llist t)))
2479 llist nil))
2480 ((null (symbols (car llist)))
2481 (setq lhs1 (sub lhs1 (car llist))
2482 rhs1 (sub rhs1 (car llist)))))))
2484 (defun splitprod (exp)
2485 (do ((flipsign)
2486 (lhs1 (car exp))
2487 (rhs1 (cadr exp))
2488 (llist (cdar exp) (cdr llist))
2489 (sign)
2490 (minus)
2491 (evens)
2492 (odds))
2493 ((null llist)
2494 (if (mtimesp lhs1) (setq success t))
2495 (cond (flipsign
2496 (setq success t)
2497 (list rhs1 lhs1))
2498 (t (list lhs1 rhs1))))
2499 (when (null (symbols (car llist)))
2500 (sign (car llist))
2501 (if (eq sign '$neg) (setq flipsign (not flipsign)))
2502 (if (member sign '($pos $neg) :test #'eq)
2503 (setq lhs1 (div lhs1 (car llist))
2504 rhs1 (div rhs1 (car llist)))))))
2506 (defun symbols (x)
2507 (let (($listconstvars %initiallearnflag))
2508 (cdr ($listofvars x))))
2510 ;; %initiallearnflag is only necessary so that %PI, %E, etc. can be LEARNed.
2512 (defun initialize-numeric-constant (c)
2513 (setq %initiallearnflag t)
2514 (let ((context '$global))
2515 (learn `((mequal) ,c ,(mget c '$numer)) t))
2516 (setq %initiallearnflag nil))
2518 (eval-when (:load-toplevel :execute)
2519 (mapc #'true*
2520 '(;; even and odd are integer
2521 (par ($even $odd) $integer)
2523 ; Cutting out inferences for integer, rational, real, complex (DK 10/2009).
2524 ; (kind $integer $rational)
2525 ; (par ($rational $irrational) $real)
2526 ; (par ($real $imaginary) $complex)
2528 ;; imaginary is complex
2529 (kind $imaginary $complex)
2531 ;; Declarations for constants
2532 (kind $%i $noninteger)
2533 (kind $%i $imaginary)
2534 (kind $%e $noninteger)
2535 (kind $%e $real)
2536 (kind $%pi $noninteger)
2537 (kind $%pi $real)
2538 (kind $%gamma $noninteger)
2539 (kind $%gamma $real)
2540 (kind $%phi $noninteger)
2541 (kind $%phi $real)
2542 (kind $%pi $irrational)
2543 (kind $%e $irrational)
2544 (kind $%phi $irrational)
2546 ;; Declarations for functions
2547 (kind %log $increasing)
2548 (kind %atan $increasing) (kind %atan $oddfun)
2549 (kind $delta $evenfun)
2550 (kind %sinh $increasing) (kind %sinh $oddfun)
2551 (kind %cosh $posfun)
2552 (kind %tanh $increasing) (kind %tanh $oddfun)
2553 (kind %coth $oddfun)
2554 (kind %csch $oddfun)
2555 (kind %sech $posfun)
2556 (kind %asinh $increasing) (kind %asinh $oddfun)
2557 ;; It would be nice to say %acosh is $posfun, but then
2558 ;; assume(xn<0); abs(acosh(xn)) -> acosh(xn), which is wrong
2559 ;; since acosh(xn) is complex.
2560 (kind %acosh $increasing)
2561 (kind %atanh $increasing) (kind %atanh $oddfun)
2562 (kind $li $complex)
2563 (kind $lambert_w $complex)
2564 (kind %cabs $complex)))
2566 ;; Create an initial context for the user which is a subcontext of $global.
2567 ($newcontext '$initial))