Fix bug #4260: translate fails with go tag in final position
[maxima.git] / src / compar.lisp
blob1c3af36855b1f2e38cf86ab95aebcefeef717c08
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 (when (and $useminmax (or (minmaxp xlhs) (minmaxp xrhs)))
1494 (setq sgn (signdiff-minmax xlhs xrhs)))
1495 (when sgn (setq sign sgn minus nil odds nil evens nil)
1496 t)))
1498 ;;; Look for symbols with an assumption a > n or a < -n, where n is a number.
1499 ;;; For this case shift the symbol a -> a+n in a summation and multiplication.
1500 ;;; This handles cases like a>1 and b>1 gives sign(a+b-2) -> pos.
1502 (defun sign-shift (expr)
1503 (do ((l (cdr expr) (cdr l))
1504 (fl nil)
1505 (acc nil))
1506 ((null l) (addn acc nil))
1507 (cond ((symbolp (car l))
1508 ;; Get the facts related to the symbol (car l)
1509 ;; Reverse the order to test the newest facts first.
1510 (setq fl (reverse (cdr (facts1 (car l)))))
1511 (push (car l) acc)
1512 (dolist (f fl)
1513 (cond ((and (eq (caar f) 'mgreaterp)
1514 (mnump (caddr f))
1515 (eq ($sign (caddr f)) '$pos))
1516 ;; The case a > n, where a is a symbol and n a number.
1517 ;; Add the number to the list of terms.
1518 (return (push (caddr f) acc)))
1519 ((and (eq (caar f) 'mgreaterp)
1520 (mnump (cadr f))
1521 (eq ($sign (cadr f)) '$neg))
1522 ;; The case a < -n, where a is a symbol and n a number.
1523 ;; Add the number to the list of terms.
1524 (return (push (cadr f) acc))))))
1525 ((mtimesp (car l))
1526 (let ((acctimes) (flag))
1527 ;; Go through the factors of the multiplication.
1528 (dolist (ll (cdar l))
1529 (cond ((symbolp ll)
1530 ;; Get the facts related to the symbol (car l)
1531 ;; Reverse the order to test the newest facts first.
1532 (setq fl (reverse (cdr (facts1 ll))))
1533 (dolist (f fl)
1534 (cond ((and (eq (caar f) 'mgreaterp)
1535 (mnump (caddr f))
1536 (eq ($sign (caddr f)) '$pos))
1537 ;; The case a > n, where a is a symbol and n a
1538 ;; number. Add the number to the list of terms.
1539 (setq flag t)
1540 (return (push (add ll (caddr f)) acctimes)))
1541 ((and (eq (caar f) 'mgreaterp)
1542 (mnump (cadr f))
1543 (eq ($sign (cadr f)) '$neg))
1544 ;; The case a < -n, where a is a symbol and n a
1545 ;; number. Add the number to the list of terms.
1546 (setq flag t)
1547 (return (push (add ll (cadr f)) acctimes)))))
1548 (when (not flag) (push ll acctimes)))
1550 (push ll acctimes))))
1551 (if flag
1552 ;; If a shift has been done expand the factors.
1553 (push ($multthru (muln acctimes nil)) acc)
1554 (push (muln acctimes nil) acc))))
1556 (push (car l) acc)))))
1558 (defun signsum (x)
1559 (setq x (sign-shift x))
1560 ;; x might be simplified to an atom in sign-shift.
1561 (when (atom x) (setq x (cons '(mplus) (list x))))
1562 (do ((l (cdr x) (cdr l)) (s '$zero))
1563 ((null l) (setq sign s minus nil odds (list x) evens nil)
1564 (cond (*complexsign*
1565 ;; Because we have continued the loop in Complex Mode
1566 ;; we have to look for the sign '$pnz and return nil.
1567 (if (eq s '$pnz) nil t))
1568 (t t))) ; in Real Mode return T
1569 ;; Call sign1 and not sign, because sign1 handles constant expressions.
1570 (sign1 (car l))
1571 (cond ((and *complexsign*
1572 (or (eq sign '$complex) (eq sign '$imaginary)))
1573 ;; Found a complex or imaginary expression. The sign is $complex.
1574 (setq sign '$complex odds nil evens nil minus nil)
1575 (return t))
1576 ((or (and (eq sign '$zero)
1577 (setq x (sub x (car l))))
1578 (and (eq s sign) (not (eq s '$pn))) ; $PN + $PN = $PNZ
1579 (and (eq s '$pos) (eq sign '$pz))
1580 (and (eq s '$neg) (eq sign '$nz))))
1581 ((or (and (member sign '($pz $pos) :test #'eq) (member s '($zero $pz) :test #'eq))
1582 (and (member sign '($nz $neg) :test #'eq) (member s '($zero $nz) :test #'eq))
1583 (and (eq sign '$pn) (eq s '$zero)))
1584 (setq s sign))
1586 (cond (*complexsign*
1587 ;; In Complex Mode we have to continue the loop to look further
1588 ;; for a complex or imaginay expression.
1589 (setq s '$pnz))
1591 ;; In Real mode the loop stops when the sign is 'pnz.
1592 (setq sign '$pnz odds (list x) evens nil minus nil)
1593 (return nil)))))))
1595 (defun signfactor (x)
1596 (let (y (factored t))
1597 (setq y (factor-if-small x))
1598 (cond ((or (mplusp y) (> (conssize y) 50.))
1599 (setq sign '$pnz)
1600 nil)
1601 (t (sign y)))))
1603 (defun factor-if-small (x)
1604 (if (< (conssize x) 51.)
1605 (let ($ratprint)
1606 (declare (special $ratprint))
1607 (factor x)) x))
1609 (defun sign-mexpt (x)
1610 (let* ((expt (caddr x)) (base1 (cadr x))
1611 (sign-expt (sign1 expt)) (sign-base (sign1 base1))
1612 (evod (evod expt)))
1613 ;; The variable sign is now equal to sign-base. This is used below
1614 ;; in some places to avoid an assignment operation for sign.
1615 (cond ((and (eq sign-base '$zero)
1616 (member sign-expt '($zero $neg $nz) :test #'eq))
1617 (dbzs-err x))
1618 ((eq sign-expt '$zero) (setq sign '$pos))
1619 ((eq sign-base '$zero))
1621 ((and *complexsign* (or (eq sign-expt '$complex)
1622 (eq sign-expt '$imaginary)
1623 (eq sign-base '$complex)))
1624 ;; Base is complex or exponent is complex or imaginary.
1625 ;; The sign is $complex.
1626 (when *debug-compar*
1627 (format t "~&in SIGN-MEXPT for ~A, sign is complex.~%" x))
1628 (setq sign '$complex))
1630 ((and *complexsign*
1631 (eq sign-base '$neg)
1632 (eq (evod ($expand (mul 2 expt))) '$odd))
1633 ;; Base is negative and the double of the exponent is odd.
1634 ;; Result is imaginary.
1635 (when *debug-compar*
1636 (format t "~&in SIGN-MEXPT for ~A, sign is $imaginary.~%" x))
1637 (setq sign '$imaginary))
1639 ((and *complexsign*
1640 (eq sign-base '$imaginary))
1641 ;; An imaginary base. Look for even or odd exponent.
1642 (when *debug-compar*
1643 (format t "~&in SIGN-MEXPT for ~A, base is $imaginary.~%" x))
1644 (cond
1645 ((and (integerp expt) (eq evod '$even))
1646 (setq sign (if (eql (mod expt 4) 0) '$pz '$nz)))
1647 ((and (integerp expt) (eq evod '$odd))
1648 (setq sign '$imaginary
1649 minus (if (eql (mod (- expt 1) 4) 0) t nil)))
1650 (t (setq sign '$complex))))
1652 ((and *complexsign*
1653 (not evod)
1654 (not (ratnump expt))
1655 (not (member sign-base '($pos $pz $zero) :test #'eq)))
1656 (when *debug-compar*
1657 (format t "~&in SIGN-MEXPT for ~A, base is not $pos, $pz or $zero.~%" x))
1658 (setq sign (if (maxima-integerp expt) '$pnz '$complex)))
1659 ((eq sign-base '$pos))
1660 ((eq evod '$even)
1661 (cond ((eq sign-expt '$neg)
1662 (setq sign '$pos minus nil evens (ncons base1) odds nil))
1663 ((member sign-base '($pn $neg) :test #'eq)
1664 (setq sign '$pos minus nil
1665 evens (nconc odds evens)
1666 odds nil))
1667 (t (setq sign '$pz minus nil
1668 evens (nconc odds evens)
1669 odds nil))))
1670 ((and (member sign-expt '($neg $nz) :test #'eq)
1671 (member sign-base '($nz $pz $pnz) :test #'eq))
1672 (setq sign (if (eq sign-base '$pz)
1673 '$pos
1674 '$pn)))
1675 ((member sign-expt '($pz $nz $pnz) :test #'eq)
1676 (cond ((member sign-base '($neg $nz) :test #'eq)
1677 (setq odds (ncons x) sign (if (eq sign-base '$neg) '$pn '$pnz)))))
1678 ((eq sign-expt '$pn)
1679 (cond ((eq sign-base '$neg)
1680 (setq sign '$pn))
1681 ((eq sign-base '$nz)
1682 (setq sign '$pnz))))
1683 ((ratnump expt)
1684 (cond ((mevenp (cadr expt))
1685 (cond ((member sign-base '($pn $neg) :test #'eq)
1686 (setq sign-base '$pos))
1687 ((member sign-base '($pnz $nz) :test #'eq)
1688 (setq sign-base '$pz)))
1689 (setq evens (nconc odds evens)
1690 odds nil minus nil))
1691 ((mevenp (caddr expt))
1692 (cond (*complexsign*
1693 (when (not (member sign-base '($pos $pz)))
1694 ;; In Complex Mode the sign is $complex.
1695 (setq sign-base (setq sign-expt '$complex))))
1696 (complexsign
1697 ;; The only place the variable complexsign
1698 ;; is used. Unfortunately, one routine in
1699 ;; to_poly.lisp in /share/to_poly_solve depends on
1700 ;; this piece of code. Perhaps we can remove
1701 ;; the dependency. (12/2008)
1702 (setq sign-base (setq sign-expt '$pnz)))
1703 ((eq sign-base '$neg) (imag-err x))
1704 ((eq sign-base '$pn)
1705 (setq sign-base '$pos))
1706 ((eq sign-base '$nz)
1707 (setq sign-base '$zero))
1708 (t (setq sign-base '$pz)))))
1709 (cond ((eq sign-expt '$neg)
1710 (cond ((eq sign-base '$zero) (dbzs-err x))
1711 ((eq sign-base '$pz)
1712 (setq sign-base '$pos))
1713 ((eq sign-base '$nz)
1714 (setq sign-base '$neg))
1715 ((eq sign-base '$pnz)
1716 (setq sign-base '$pn)))))
1717 (setq sign sign-base))
1718 ((member sign-base '($neg $nz) :test #'eq)
1719 (cond ((eq evod '$odd))
1720 (*complexsign*
1721 (setq sign '$complex))
1722 ((eq sign-base '$neg)
1723 (setq sign '$pn))
1724 (t (setq sign '$pnz)))))))
1726 ;;; Determine the sign of log(expr). This function changes the special variable sign.
1728 (defun sign-log (x)
1729 (setq x (cadr x))
1730 (setq sign
1731 (cond ((eq t (mgrp x 0))
1732 (cond ((eq t (mgrp 1 x)) '$neg)
1733 ((eq t (meqp x 1)) '$zero);; log(1) = 0.
1734 ((eq t (mgqp 1 x)) '$nz)
1735 ((eq t (mgrp x 1)) '$pos)
1736 ((eq t (mgqp x 1)) '$pz)
1737 ((eq t (mnqp x 1)) '$pn)
1738 (t '$pnz)))
1739 ((and *complexsign* (eql 1 (cabs x))) '$imaginary)
1740 (*complexsign* '$complex)
1741 (t '$pnz))))
1743 (defun sign-mabs (x)
1744 (let ((*complexsign* t))
1745 (sign (cadr x))
1746 (cond ((member sign '($pos $zero) :test #'eq))
1747 ((member sign '($neg $pn) :test #'eq) (setq sign '$pos))
1748 (t (setq sign '$pz minus nil evens (nconc odds evens) odds nil)))))
1750 (defun sign-asin/acos/atanh (x)
1751 (cond ((and *complexsign*
1752 (or (not (eq t (mgqp (cadr x) -1)))
1753 (not (eq t (mgqp 1 (cadr x)))))) ; x < -1 or x > 1
1754 (setq sign '$complex))
1755 ((not (eq (caar x) '%acos))
1756 (sign-oddfun x))
1757 ((eq t (mlsp (cadr x) 1)) ; x < 1
1758 (sign-posfun x))
1759 (t ; x <= 1
1760 (setq sign '$pz))))
1762 (defun sign-acosh (x)
1763 (cond ((and *complexsign* (not (eq t (mgqp (cadr x) 1)))) ; x < 1
1764 (setq sign '$complex))
1765 ((eq t (mgrp (cadr x) 1)) ; x > 1
1766 (sign-posfun x))
1767 (t ; x >= 1
1768 (setq sign '$pz))))
1770 ;; This code handles sign(sin(x)), where -%pi <= x <= %pi. Of course, at the
1771 ;; expense of a great deal of additional complexity, this code could catch far
1772 ;; more cases.
1774 ;; Like all sign functions, it works by setting the value of the special variable
1775 ;; sign.
1777 ;; The sign of sin(constant expression) is supposed to be handled elsewhere,
1778 ;; so we don't consider that case here. But running the testsuite,
1779 ;; sometimes sign-sin receives a constant expression. This code could
1780 ;; handle this case, but I think that's fixing a bug in the wrong place.
1782 ;; The general simplifier converts sin(%i*a) to %i*sinh(a), so I don't
1783 ;; think this code needs to handle the case of sin(x), where x is purely imaginary.
1785 ;; This code could handle sin({zerob, zeroa, ind}), but it doesn't.
1787 ;; Finally, sometimes the argument to sin is not simplified; for example,
1788 ;; running the testsuite sometimes this code recieves the expression sin(1/(1/x)).
1789 ;; We could simplify it, but I think that it would be better to fix the code
1790 ;; so that the input to sign-sin is always simplified.
1791 (defun sign-sin (e) ; e = sin(x)
1792 (let ((x (cadr e)) (y 0))
1793 ;; When *complexsign* is true, find the rectangular form of
1794 ;; the argument to sin.
1795 (when *complexsign*
1796 (setq x (risplit x))
1797 (setq y (cdr x)
1798 x (car x)))
1799 (cond
1800 ;; When y = 0 and -%pi <= x <= %pi, sign(sin(x)) = sign(x)
1801 ((and (eql y 0)
1802 (eq t (mgqp x (mul -1 '$%pi)))
1803 (eq t (mgqp '$%pi x)))
1804 (sign x))
1805 ;; When *complexsign* is true & y # 0, set sign to complex.
1806 ;; To test y # 0, we'll use (not (eql y 0)))
1807 ((and *complexsign* (not (eql y 0)))
1808 (setf sign '$complex))
1809 (t (setf sign '$pnz))))
1810 nil)
1811 (putprop '%sin 'sign-sin 'sign-function)
1813 (defun sign-cos (e) ; e = cos(x)
1814 (let ((x (cadr e)) (y 0))
1815 ;; When *complexsign* is true, find the rectangular form of
1816 ;; the argument to cos.
1817 (when *complexsign*
1818 (setq x (risplit x))
1819 (setq y (cdr x)
1820 x (car x)))
1821 (cond
1822 ;; When y = 0 and -%pi/2 <= x <= 3 %pi/2, sign(cos(x)) = sign(%pi/2-x)
1823 ((and (eql y 0)
1824 (eq t (mgqp x (div '$%pi -2)))
1825 (eq t (mgqp (div (mul 3 '$%pi) 2) x)))
1826 (sign (sub (div '$%pi 2) x)))
1827 ;; When *complexsign* is true & y # 0, set sign to complex.
1828 ;; To test y # 0, we'll use (not (eql y 0)))
1829 ((and *complexsign* (not (eql y 0)))
1830 (setf sign '$complex))
1831 (t (setf sign '$pnz))))
1832 nil)
1833 (putprop '%cos 'sign-cos 'sign-function)
1835 ;; When Maxima can deduce that when -1<x<=0, then ceiling(x) simplifies to 0. So this
1836 ;; case is simplified away before it gets to this function.
1837 (defun sign-ceiling (x)
1838 (setq x (cadr x)) ; ceiling(x) --> x
1839 (setf sign (cond ((eq t (mgrp x 0)) '$pos) ; ceiling(> 0) = pos
1840 ((eq t (mgrp x -1)) '$pz) ; ceiling(> -1) = pz
1841 ((eq t (mgqp -1 x)) '$neg) ; ceiling(<= -1) = neg
1842 ((eq t (mgrp 0 x)) '$nz) ; ceiling( < 0) = nz
1843 (t '$pnz))))
1844 (putprop '$ceiling 'sign-ceiling 'sign-function)
1846 (defun sign-floor (x)
1847 (setq x (cadr x)) ; floor(x) --> x
1848 (setf sign (cond ((eq t (mgqp x 1)) '$pos) ;floor(>= 1) = pos
1849 ((eq t (mgqp x 0)) '$pz) ;floor(>= 0) = pz
1850 ((eq t (mgrp 0 x)) '$neg) ;floor(< 0) = neg
1851 ((eq t (mgqp 0 x)) '$nz) ;floor(<= 0) = nz
1852 (t '$pnz))))
1853 (putprop '$floor 'sign-floor 'sign-function)
1855 ;;; Compare min/max
1857 ;;; Macros used in simp min/max
1858 ;;; If op is min, use body; if not, negate sign constants in body
1859 ;;; Used to avoid writing min and max code separately: just write the min code
1860 ;;; in such a way that its dual works for max
1861 (defmacro minmaxforms (op &rest body)
1862 `(if (eq ,op '$min)
1863 ,@body
1864 ,@(sublis '(($neg . $pos)
1865 ($nz . $pz)
1866 ($pz . $nz)
1867 ($pos . $neg)
1868 ;;($zero . $zero)
1869 ;;($pn . $pn)
1870 ;;($pnz . $pnz)
1872 ($max . $min)
1873 ($min . $max)
1875 ($inf . $minf)
1876 ($minf . $inf))
1877 body)))
1879 (defun sign-minmax (op args)
1880 (do ((sgn (minmaxforms op '$pos) ;identity element for min
1881 (sminmax op sgn (sign* (car l))))
1882 (end (minmaxforms op '$neg))
1883 (l args (cdr l)))
1884 ((or (null l) (eq sgn end))
1885 (setq minus nil
1886 odds (if (not (member sgn '($pos $neg $zero) :test #'eq))
1887 (ncons (cons (list op) args)))
1888 evens nil
1889 sign sgn))))
1891 ;; sign(op(a,b)) = sminmax(sign(a),sign(b))
1892 ;; op is $min/$max; s1/s2 in neg, nz, zero, pz, pos, pn, pnz
1893 (defun sminmax (op s1 s2)
1894 (minmaxforms
1896 ;; Many of these cases don't come up in simplified expressions,
1897 ;; since e.g. sign(a)=neg and sign(b)=pos implies min(a,b)=a
1898 ;; the order of these clauses is important
1899 (cond ((eq s1 '$pos) s2)
1900 ((eq s2 '$pos) s1)
1901 ((eq s1 s2) s1)
1902 ((or (eq s1 '$neg) (eq s2 '$neg)) '$neg)
1903 ((or (eq s1 '$nz) (eq s2 '$nz)) '$nz)
1904 ((eq s1 '$zero) (if (eq s2 '$pz) '$zero '$nz))
1905 ((eq s2 '$zero) (if (eq s1 '$pz) '$zero '$nz))
1906 (t '$pnz))))
1908 (defun minmaxp (ex)
1909 (cond ((atom ex) nil)
1910 ((member (caar ex) '($min $max) :test #'eq) (caar ex))
1911 (t nil)))
1913 (defun signdiff-minmax (l r)
1914 ;; sign of l-r; nil if unknown (not PNZ)
1915 (let* ((lm (minmaxp l))
1916 (rm (minmaxp r))
1917 (ll (if lm (cdr l)))
1918 (rr (if rm (cdr r)))) ;distinguish between < and <= argument lists of min/max
1919 (minmaxforms
1920 (or rm lm)
1921 (cond ((eq lm rm) ; min(a,...) - min(b,...)
1922 (multiple-value-bind (both onlyl onlyr) (intersect-info ll rr)
1923 (declare (ignore both))
1924 (cond ((null onlyl) '$pz) ; min(a,b) - min(a,b,c)
1925 ((null onlyr) '$nz) ; min(a,b,c) - min(a,b)
1926 ;; TBD: add processing for full onlyl/onlyr case
1927 (t nil))))
1928 ;; TBD: memalike and set-disjointp are crude approx.
1929 ((null lm) (if (memalike l rr) '$pz)) ; a - min(a,b)
1930 ((null rm) (if (memalike r ll) '$nz)) ; min(a,b) - a
1931 (t ; min/max or max/min
1932 (if (not (set-disjointp ll rr)) '$pz)))))) ; max(a,q,r) - min(a,s,t)
1934 (defun intersect-info (a b)
1935 (let ((both nil)
1936 (onlya nil)
1937 (onlyb nil))
1938 (do-merge-asym
1940 #'like
1941 #'$orderlessp
1942 #'(lambda (x) (push x both))
1943 #'(lambda (x) (push x onlya))
1944 #'(lambda (x) (push x onlyb)))
1945 (values
1946 (reverse both)
1947 (reverse onlya)
1948 (reverse onlyb))))
1950 ;;; end compare min/max
1952 (defun sign-posfun (xx)
1953 (declare (ignore xx))
1954 (setq sign '$pos
1955 minus nil
1956 odds nil
1957 evens nil))
1959 (defun sign-oddfun (x)
1960 (cond ((kindp (caar x) '$increasing)
1961 ; Take the sign of the argument
1962 (sign (cadr x)))
1963 ((kindp (caar x) '$decreasing)
1964 ; Take the sign of negative of the argument
1965 (sign (neg (cadr x))))
1967 ; If the sign of the argument is zero, then we're done (the sign of
1968 ; the function value is the same). Otherwise, punt to SIGN-ANY.
1969 (sign (cadr x))
1970 (unless (eq sign '$zero)
1971 (sign-any x)))))
1973 (defun imag-err (x)
1974 (if sign-imag-errp
1975 (merror (intl:gettext "sign: argument cannot be imaginary; found ~M") x)
1976 (throw 'sign-imag-err t)))
1978 (defun dbzs-err (x)
1979 (merror (intl:gettext "sign: division by zero in ~M") x))
1981 ;; Return true iff e is an expression with operator op1, op2,...,or opn.
1983 (defun op-equalp (e &rest op)
1984 (and (consp e) (consp (car e)) (some #'(lambda (s) (equal (caar e) s)) op)))
1986 ;; Return true iff the operator of a is a Maxima relation operator.
1988 (defun mrelationp (a)
1989 (op-equalp a 'mlessp 'mleqp 'mequal 'mnotequal 'mgeqp 'mgreaterp))
1991 ;; This version of featurep applies ratdisrep to the first argument. This
1992 ;; change allows things like featurep(rat(n),integer) --> true when n has
1993 ;; been declared an integer.
1995 (defmfun $featurep (e ind)
1996 (setq e ($ratdisrep e))
1997 (cond ((not (symbolp ind))
1998 (merror
1999 (intl:gettext "featurep: second argument must be a symbol; found ~M")
2000 ind))
2001 ;; Properties not related to the assume database.
2002 ((and (member ind opers) (safe-get e ind)))
2003 ((and (member ind '($evfun $evflag $bindtest $nonarray))
2004 (safe-get e (stripdollar ind))))
2005 ((and (eq ind '$noun)
2006 (safe-get e (stripdollar ind))
2008 ((and (member ind '($scalar $nonscalar $mainvar))
2009 (mget e ind)))
2010 ((and (eq ind '$feature)
2011 (member e $features)
2013 ((eq ind '$alphabetic)
2014 (dolist (l (coerce e 'list) t)
2015 (when (not (member l *alphabet*)) (return nil))))
2016 ;; Properties related to the assume database.
2017 ((eq ind '$integer) (maxima-integerp e))
2018 ((eq ind '$noninteger) (nonintegerp e))
2019 ((eq ind '$even) (mevenp e))
2020 ((eq ind '$odd) (moddp e))
2021 ((eq ind '$real)
2022 (if (atom e)
2023 (or (numberp e) (kindp e '$real) (numberp (numer e)))
2024 (free ($rectform e) '$%i)))
2025 ((symbolp e) (kindp e ind))))
2027 ;; Give a function the maps-integers-to-integers property when it is integer
2028 ;; valued on the integers; give it the integer-valued property when its
2029 ;; range is a subset of the integers. What have I missed?
2031 (setf (get 'mplus 'maps-integers-to-integers) t)
2032 (setf (get 'mtimes 'maps-integers-to-integers) t)
2033 (setf (get 'mabs 'maps-integers-to-integers) t)
2034 (setf (get '$max 'maps-integers-to-integers) t)
2035 (setf (get '$min 'maps-integers-to-integers) t)
2037 (setf (get '$floor 'integer-valued) t)
2038 (setf (get '$ceiling 'integer-valued) t)
2039 (setf (get '$charfun 'integer-valued) t)
2041 (defun maxima-integerp (x)
2042 (cond ((integerp x))
2043 ((mnump x) nil)
2044 ((and (symbolp x)
2045 (or (kindp x '$integer)
2046 (kindp x '$even)
2047 (kindp x '$odd)
2048 (check-integer-facts x))))
2049 (t (let ((x-op (and (consp x) (consp (car x)) (caar x))) ($prederror nil))
2050 (cond ((null x-op) nil)
2051 ((not (symbolp x-op)) nil) ; fix for mqapply at some point?
2052 ((eq x-op 'mrat) (and (integerp (cadr x)) (equal (cddr x) 1)))
2053 ;; mtimes and mplus are generally handled by this clause
2054 ((and (get x-op 'maps-integers-to-integers) (every #'maxima-integerp (margs x))))
2055 ;; Special case for 1/2*...*even
2056 ((eq x-op 'mtimes)
2057 (and (mnump (cadr x))
2058 (integerp (mul 2 (cadr x)))
2059 (every 'maxima-integerp (cddr x))
2060 (some #'(lambda (s) ($featurep s '$even)) (rest (margs x)))))
2061 ((eq x-op 'mexpt)
2062 (and (every #'maxima-integerp (margs x))
2063 (null (mevalp (mlsp (caddr x) 0)))))
2064 ;; ! in Maxima allows real arguments
2065 ((eq x-op 'mfactorial)
2066 (and (maxima-integerp (cadr x))
2067 (not (mevalp (mlsp (cadr x) 0)))))
2068 ((eq x-op '%gamma)
2069 (and (maxima-integerp (cadr x))
2070 (not (mevalp (mlsp (cadr x) 1)))))
2071 ;; other x-ops
2072 ((or ($featurep ($verbify x-op) '$integervalued)
2073 (get x-op 'integer-valued))))))))
2075 ;; When called with mode 'integer look into the database for symbols which are
2076 ;; declared to be equal to an integer or an expression which is an integer.
2077 ;; In mode 'evod look for odd and even expressions.
2078 (defun check-integer-facts (x &optional (mode 'integer))
2079 (do ((factsl (cdr (facts1 x)) (cdr factsl))
2080 fact)
2081 ((null factsl) nil)
2082 (setq fact (car factsl))
2083 (cond ((and (not (atom fact))
2084 (eq (caar fact) '$equal))
2085 (cond ((and (symbolp (cadr fact))
2086 (eq (cadr fact) x))
2087 ;; Case equal(x,expr): Test expr to be an integer.
2088 (cond ((symbolp (caddr fact))
2089 (cond ((and (eq mode 'integer)
2090 (or (kindp (caddr fact) '$integer)
2091 (kindp (caddr fact) '$odd)
2092 (kindp (caddr fact) '$even)))
2093 (return t))
2094 ((eq mode 'evod)
2095 (cond ((kindp (caddr fact) '$odd)
2096 (return '$odd))
2097 ((kindp (caddr fact) '$even)
2098 (return '$even))
2099 (t (return nil))))
2100 (t (return nil))))
2102 (cond ((eq mode 'integer)
2103 (return (maxima-integerp (caddr fact))))
2104 ((eq mode 'evod)
2105 (return (evod (caddr fact))))
2106 (t (return nil))))))
2107 ((and (symbolp (caddr fact))
2108 (eq (caddr fact) x))
2109 ;; Case equal(expr,x): Test expr to be an integer.
2110 (cond ((symbolp (caddr fact))
2111 (cond ((and (eq mode 'integer)
2112 (or (kindp (cadr fact) '$integer)
2113 (kindp (cadr fact) '$odd)
2114 (kindp (cadr fact) '$even)))
2115 (return t))
2116 ((eq mode 'evod)
2117 (cond ((kindp (cadr fact) '$odd)
2118 (return '$odd))
2119 ((kindp (cadr fact) '$even)
2120 (return '$even))
2121 (t (return nil))))
2122 (t (return nil))))
2124 (cond ((eq mode 'integer)
2125 (return (maxima-integerp (cadr fact))))
2126 ((eq mode 'evod)
2127 (return (evod (cadr fact))))
2128 (t (return nil)))))))))))
2130 (defun nonintegerp (e)
2131 (cond ((and (symbolp e) (or (kindp e '$noninteger) (check-noninteger-facts e) (kindp e '$irrational)))) ;declared noninteger
2132 ((mnump e)
2133 (if (integerp e) nil t)) ;all floats are noninteger and integers are not nonintegers
2134 (($ratp e)
2135 (nonintegerp ($ratdisrep e)))
2136 (t (eq t (mgrp e (take '($floor) e))))))
2139 ;; Look into the database for symbols which are declared to be equal
2140 ;; to a noninteger or an expression which is a noninteger.
2141 (defun check-noninteger-facts (x)
2142 (do ((factsl (cdr (facts1 x)) (cdr factsl)))
2143 ((null factsl) nil)
2144 (cond ((and (not (atom (car factsl)))
2145 (eq (caar (car factsl)) '$equal))
2146 (cond ((and (symbolp (cadr (car factsl)))
2147 (eq (cadr (car factsl)) x))
2148 ;; Case equal(x,expr): Test expr to be a noninteger.
2149 (cond ((symbolp (caddr (car factsl)))
2150 (if (kindp (caddr (car factsl)) '$noninteger)
2151 (return t)))
2153 (return (nonintegerp (caddr (car factsl)))))))
2154 ((and (symbolp (caddr (car factsl)))
2155 (eq (caddr (car factsl)) x))
2156 ;; Case equal(expr,x): Test expr to be a noninteger.
2157 (cond ((symbolp (cadr (car factsl)))
2158 (if (kindp (cadr (car factsl)) '$noninteger)
2159 (return t)))
2161 (return (nonintegerp (cadr (car factsl))))))))))))
2163 (defun intp (l)
2164 (every #'maxima-integerp (cdr l)))
2166 (defun mevenp (e)
2167 (cond ((integerp e) (not (oddp e)))
2168 ((mnump e) nil)
2169 (t (eq '$even (evod e)))))
2171 (defun moddp (e)
2172 (cond ((integerp e) (oddp e))
2173 ((mnump e) nil)
2174 (t (eq '$odd (evod e)))))
2176 ;; An extended evod that recognizes that abs(even) is even and
2177 ;; abs(odd) is odd.
2179 (defun evod (e)
2180 (cond ((integerp e) (if (oddp e) '$odd '$even))
2181 ((mnump e) nil)
2182 ((atom e)
2183 (cond ((kindp e '$odd) '$odd)
2184 ((kindp e '$even) '$even)
2185 ;; Check the database for facts.
2186 ((symbolp e) (check-integer-facts e 'evod))))
2187 ((eq 'mtimes (caar e)) (evod-mtimes e))
2188 ((eq 'mplus (caar e)) (evod-mplus e))
2189 ((eq 'mabs (caar e)) (evod (cadr e))) ;; extra code
2190 ((eq 'mexpt (caar e)) (evod-mexpt e))))
2192 (defun evod-mtimes (x)
2193 (do ((l (cdr x) (cdr l)) (flag '$odd))
2194 ((null l) flag)
2195 (setq x (evod (car l)))
2196 (cond ((eq '$odd x))
2197 ((eq '$even x) (setq flag '$even))
2198 ((maxima-integerp (car l)) (cond ((eq '$odd flag) (setq flag nil))))
2199 (t (return nil)))))
2201 (defun evod-mplus (x)
2202 (do ((l (cdr x) (cdr l)) (flag))
2203 ((null l) (cond (flag '$odd) (t '$even)))
2204 (setq x (evod (car l)))
2205 (cond ((eq '$odd x) (setq flag (not flag)))
2206 ((eq '$even x))
2207 (t (return nil)))))
2209 (defun evod-mexpt (x)
2210 (when (and (integerp (caddr x)) (not (minusp (caddr x))))
2211 (evod (cadr x))))
2213 (declare-top (special mgqp mlqp))
2215 (defmode cl ()
2216 (atom (selector +labs) (selector -labs) (selector data)))
2218 (defmacro c-dobj (&rest x)
2219 `(list ,@x))
2221 (defun dcompare (x y)
2222 (setq odds (list (sub x y)) evens nil minus nil
2223 sign (cond ((eq x y) '$zero)
2224 ((or (eq '$inf x) (eq '$minf y)) '$pos)
2225 ((or (eq '$minf x) (eq '$inf y)) '$neg)
2226 (t (dcomp x y)))))
2228 (defun dcomp (x y)
2229 (let (mgqp mlqp)
2230 (setq x (dinternp x) y (dinternp y))
2231 (cond ((or (null x) (null y)) '$pnz)
2232 ((progn (clear) (deq x y) (sel y +labs)))
2233 (t '$pnz))))
2235 (defun deq (x y)
2236 (cond ((dmark x '$zero) nil)
2237 ((eq x y))
2238 (t (do ((l (sel x data) (cdr l))) ((null l))
2239 (if (and (visiblep (car l)) (deqf x y (car l))) (return t))))))
2241 (defun deqf (x y f)
2242 (cond ((eq 'meqp (caar f))
2243 (if (eq x (cadar f)) (deq (caddar f) y) (deq (cadar f) y)))
2244 ((eq 'mgrp (caar f))
2245 (if (eq x (cadar f)) (dgr (caddar f) y) (dls (cadar f) y)))
2246 ((eq 'mgqp (caar f))
2247 (if (eq x (cadar f)) (dgq (caddar f) y) (dlq (cadar f) y)))
2248 ((eq 'mnqp (caar f))
2249 (if (eq x (cadar f)) (dnq (caddar f) y) (dnq (cadar f) y)))))
2251 (defun dgr (x y)
2252 (cond ((dmark x '$pos) nil)
2253 ((eq x y))
2254 (t (do ((l (sel x data) (cdr l)))
2255 ((null l))
2256 (when (or mlqp (and (visiblep (car l)) (dgrf x y (car l))))
2257 (return t))))))
2259 (defun dgrf (x y f)
2260 (cond ((eq 'mgrp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2261 ((eq 'mgqp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2262 ((eq 'meqp (caar f))
2263 (if (eq x (cadar f))
2264 (dgr (caddar f) y)
2265 (dgr (cadar f) y)))))
2267 (defun dls (x y)
2268 (cond ((dmark x '$neg) nil)
2269 ((eq x y))
2270 (t (do ((l (sel x data) (cdr l)))
2271 ((null l))
2272 (when (or mgqp (and (visiblep (car l)) (dlsf x y (car l))))
2273 (return t))))))
2275 (defun dlsf (x y f)
2276 (cond ((eq 'mgrp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2277 ((eq 'mgqp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2278 ((eq 'meqp (caar f))
2279 (if (eq x (cadar f)) (dls (caddar f) y) (dls (cadar f) y)))))
2281 (defun dgq (x y)
2282 (cond ((member (sel x +labs) '($pos $zero) :test #'eq) nil)
2283 ((eq '$nz (sel x +labs)) (deq x y))
2284 ((eq '$pn (sel x +labs)) (dgr x y))
2285 ((dmark x '$pz) nil)
2286 ((eq x y) (setq mgqp t) nil)
2287 (t (do ((l (sel x data) (cdr l))) ((null l))
2288 (if (and (visiblep (car l)) (dgqf x y (car l))) (return t))))))
2290 (defun dgqf (x y f)
2291 (cond ((eq 'mgrp (caar f)) (if (eq x (cadar f)) (dgr (caddar f) y)))
2292 ((eq 'mgqp (caar f)) (if (eq x (cadar f)) (dgq (caddar f) y)))
2293 ((eq 'meqp (caar f))
2294 (if (eq x (cadar f)) (dgq (caddar f) y) (dgq (cadar f) y)))))
2296 (defun dlq (x y)
2297 (cond ((member (sel x +labs) '($neg $zero) :test #'eq) nil)
2298 ((eq '$pz (sel x +labs)) (deq x y))
2299 ((eq '$pn (sel x +labs)) (dls x y))
2300 ((dmark x '$nz) nil)
2301 ((eq x y) (setq mlqp t) nil)
2302 (t (do ((l (sel x data) (cdr l))) ((null l))
2303 (if (and (visiblep (car l)) (dlqf x y (car l))) (return t))))))
2305 (defun dlqf (x y f)
2306 (cond ((eq 'mgrp (caar f)) (if (eq x (caddar f)) (dls (cadar f) y)))
2307 ((eq 'mgqp (caar f)) (if (eq x (caddar f)) (dlq (cadar f) y)))
2308 ((eq 'meqp (caar f))
2309 (if (eq x (cadar f)) (dlq (caddar f) y) (dlq (cadar f) y)))))
2311 (defun dnq (x y)
2312 (cond ((member (sel x +labs) '($pos $neg) :test #'eq) nil)
2313 ((eq '$pz (sel x +labs)) (dgr x y))
2314 ((eq '$nz (sel x +labs)) (dls x y))
2315 ((dmark x '$pn) nil)
2316 ((eq x y) nil)
2317 (t (do ((l (sel x data) (cdr l))) ((null l))
2318 (if (and (visiblep (car l)) (dnqf x y (car l))) (return t))))))
2320 (defun dnqf (x y f)
2321 (cond ((eq 'meqp (caar f))
2322 (if (eq x (cadar f)) (dnq (caddar f) y) (dnq (cadar f) y)))))
2324 ;; mark sign of x to be m, relative to current comparison point for dcomp.
2325 ;; returns true if this fact is already known, nil otherwise.
2326 (defun dmark (x m)
2327 (cond ((eq m (sel x +labs)))
2328 ((and dbtrace (prog1
2330 (mtell (intl:gettext "DMARK: marking ~M ~M") (if (atom x) x (car x)) m))
2331 nil))
2333 (push x +labs)
2334 (push+sto (sel x +labs) m)
2335 nil)))
2337 (defun daddgr (flag x)
2338 (with-compsplt (lhs rhs x)
2339 (mdata flag 'mgrp (dintern lhs) (dintern rhs))
2340 (if (or (mnump lhs) (constant lhs))
2341 (list '(mlessp) rhs lhs)
2342 (list '(mgreaterp) lhs rhs))))
2344 (defun daddgq (flag x)
2345 (with-compsplt (lhs rhs x)
2346 (mdata flag 'mgqp (dintern lhs) (dintern rhs))
2347 (if (or (mnump lhs) (constant lhs))
2348 (list '(mleqp) rhs lhs)
2349 (list '(mgeqp) lhs rhs))))
2351 (defun daddeq (flag x)
2352 (with-compsplt-eq (lhs rhs x)
2353 (mdata flag 'meqp (dintern lhs) (dintern rhs))
2354 (list '($equal) lhs rhs)))
2356 (defun daddnq (flag x)
2357 (with-compsplt-eq (lhs rhs x)
2358 (cond ((and (mtimesp lhs) (equal rhs 0))
2359 (dolist (term (cdr lhs)) (daddnq flag term)))
2360 ((and (mexptp lhs) (mexptp rhs)
2361 (integerp (caddr lhs)) (integerp (caddr rhs))
2362 (equal (caddr lhs) (caddr rhs)))
2363 (mdata flag 'mnqp (dintern (cadr lhs)) (dintern (cadr rhs)))
2364 (cond ((not (oddp (caddr lhs)))
2365 (mdata flag 'mnqp (dintern (cadr lhs))
2366 (dintern (neg (cadr rhs)))))))
2367 (t (mdata flag 'mnqp (dintern lhs) (dintern rhs))))
2368 (list '(mnot) (list '($equal) lhs rhs))))
2370 ;; The following functions are used by asksign to write answers into the
2371 ;; database. We make sure that these answers are written into the global
2372 ;; context '$initial and not in a local context which might be generated during
2373 ;; the evaluation phase and which will be destroyed before the evaluation has
2374 ;; finshed.
2375 ;; The additional facts are removed from the global context '$initial after
2376 ;; finishing the evaluation phase of meval with a call to clearsign.
2378 (defun tdpos (x)
2379 (let ((context '$initial))
2380 (daddgr t x)
2381 (push (cons x '$pos) *local-signs*)))
2383 (defun tdneg (x)
2384 (let ((context '$initial))
2385 (daddgr t (neg x))
2386 (push (cons x '$neg) *local-signs*)))
2388 (defun tdzero (x)
2389 (let ((context '$initial))
2390 (daddeq t x)
2391 (push (cons x '$zero) *local-signs*)))
2393 (defun tdpn (x)
2394 (let ((context '$initial))
2395 (daddnq t x)
2396 (push (cons x '$pn) *local-signs*)))
2398 (defun tdpz (x)
2399 (let ((context '$initial))
2400 (daddgq t x)
2401 (push (cons x '$pz) *local-signs*)))
2403 (defun compsplt-eq (x)
2404 (with-compsplt (lhs rhs x)
2405 (when (equal lhs 0)
2406 (setq lhs rhs rhs 0))
2407 (if (and (equal rhs 0)
2408 (or (mexptp lhs)
2409 (and (not (atom lhs))
2410 (kindp (caar lhs) '$oddfun)
2411 (kindp (caar lhs) '$increasing))))
2412 (setq lhs (cadr lhs)))
2413 (values lhs rhs)))
2415 (defun mdata (flag r x y)
2416 (if flag
2417 (mfact r x y)
2418 (mkill r x y)))
2420 (defun mfact (r x y)
2421 (let ((f (datum (list r x y))))
2422 (cntxt f context)
2423 (addf f x)
2424 (addf f y)))
2426 (defun mkill (r x y)
2427 (let ((f (car (datum (list r x y)))))
2428 (kcntxt f context)
2429 (maxima-remf f x)
2430 (maxima-remf f y)))
2432 (defun mkind (x y)
2433 (kind (dintern x) (dintern y)))
2435 ;; To guess from the previous incarnation of this code,
2436 ;; each argument is assumed to be a float, bigfloat, integer, or Maxima rational.
2437 ;; Convert Maxima rationals to Lisp rationals to make them comparable to others.
2439 (defun rgrp (x y)
2440 (when (or ($bfloatp x) ($bfloatp y))
2441 (setq
2442 x (let (($float2bf t))
2443 (declare (special $float2bf))
2444 (cadr ($bfloat (sub x y))))
2445 y 0))
2446 (if (not (numberp x))
2447 (setq x (/ (cadr x) (caddr x))))
2448 (if (not (numberp y))
2449 (setq y (/ (cadr y) (caddr y))))
2450 (cond
2451 (#-ecl (> x y) #+ecl (> (- x y) 0) '$pos)
2452 (#-ecl (> y x) #+ecl (> (- y x) 0) '$neg)
2453 (t '$zero)))
2455 (defun mcons (x l)
2456 (cons (car l) (cons x (cdr l))))
2458 (defun flip (s)
2459 (cond ((eq '$pos s) '$neg)
2460 ((eq '$neg s) '$pos)
2461 ((eq '$pz s) '$nz)
2462 ((eq '$nz s) '$pz)
2463 (t s)))
2465 (defun strongp (x y)
2466 (cond ((eq '$pnz y))
2467 ((eq '$pnz x) nil)
2468 ((member y '($pz $nz $pn) :test #'eq))))
2470 (defun munformat (form)
2471 (if (atom form)
2472 form
2473 (cons (caar form) (mapcar #'munformat (cdr form)))))
2475 (defun declarekind (var prop) ; This function is for $DECLARE to use.
2476 (let (prop2)
2477 (cond ((truep (list 'kind var prop)) t)
2478 ((or (falsep (list 'kind var prop))
2479 (and (setq prop2 (assoc prop '(($integer . $noninteger)
2480 ($noninteger . $integer)
2481 ($increasing . $decreasing)
2482 ($decreasing . $increasing)
2483 ($symmetric . $antisymmetric)
2484 ($antisymmetric . $symmetric)
2485 ($rational . $irrational)
2486 ($irrational . $rational)
2487 ($oddfun . $evenfun)
2488 ($evenfun . $oddfun)) :test #'eq))
2489 (truep (list 'kind var (cdr prop2)))))
2490 (merror (intl:gettext "declare: inconsistent declaration ~:M") `(($declare) ,var ,prop)))
2491 (t (mkind var prop) t))))
2493 ;;; These functions reformat expressions to be stored in the data base.
2495 ;; Return a list of all the atoms in X that aren't either numbers or constants
2496 ;; whose numerical value we know.
2497 (defun unknown-atoms (x)
2498 (let (($listconstvars t))
2499 (remove-if (lambda (sym) (mget sym '$numer))
2500 (cdr ($listofvars x)))))
2502 ;; Rewrite a^b to a simpler expression that has the same sign:
2503 ;; If b is odd or 1/b is odd, remove the exponent, e.g. x^3 becomes x.
2504 ;; If b has a negative sign, return a^-b, e.g. 1/x^a becomes x^a.
2505 ;; Otherwise, do nothing.
2506 (defun rewrite-mexpt-retaining-sign (x)
2507 (if (mexptp x)
2508 (let ((base (cadr x)) (exponent (caddr x)))
2509 (cond ((or (eq (evod exponent) '$odd) (eq (evod (inv exponent)) '$odd)) base)
2510 ((negp exponent) (inv x))
2511 (t x)))
2514 ;; COMPSPLT
2516 ;; Split X into (values LHS RHS) so that X>0 <=> LHS > RHS. This is supposed to
2517 ;; be a canonical form for X that can be stored in the database and then looked
2518 ;; up in future.
2520 ;; This uses two worker routines: COMPSPLT-SINGLE and COMPSPLT-GENERAL. The
2521 ;; former assumes that X only contains one symbol whose value is not known (eg not %e,
2522 ;; %pi etc.). The latter tries to deal with arbitrary collections of variables.
2523 (defun compsplt (x)
2524 (multiple-value-bind (lhs rhs)
2525 (cond
2526 ((or (atom x) (atom (car x))) (values x 0))
2527 ((null (cdr (unknown-atoms x))) (compsplt-single x))
2528 (t (compsplt-general x)))
2529 (let ((swapped nil))
2530 ;; If lhs is zero, swap lhs and rhs to make the following code simpler.
2531 ;; Remember that they were swapped to swap them back afterwards.
2532 (when (equal lhs 0)
2533 (setq lhs rhs rhs 0 swapped t))
2534 (when (equal rhs 0)
2535 ;; Rewrite mexpts in factors so that e.g. x^3/y>0 becomes x*y>0. */
2536 (setq lhs
2537 (if (mtimesp lhs)
2538 (cons (car lhs) (mapcar #'rewrite-mexpt-retaining-sign (cdr lhs)))
2539 (rewrite-mexpt-retaining-sign lhs))))
2540 ;; Undo swapping lhs and rhs.
2541 (if swapped
2542 (values rhs lhs)
2543 (values lhs rhs)))))
2545 (defun compsplt-single (x)
2546 (do ((exp (list x 0)) (success nil))
2547 ((or success (symbols (cadr exp))) (values (car exp) (cadr exp)))
2548 (cond ((atom (car exp)) (setq success t))
2549 ((eq (caaar exp) 'mplus) (setq exp (splitsum exp)))
2550 ((eq (caaar exp) 'mtimes) (setq exp (splitprod exp)))
2551 (t (setq success t)))))
2553 (defun compsplt-general (x)
2554 ;; Let compsplt-single work on it first to treat constant factors/terms.
2555 (multiple-value-bind (lhs rhs) (compsplt-single x) (setq x (sub lhs rhs)))
2556 (cond
2557 ;; If x is an atom or a single level list then we won't change it any.
2558 ((or (atom x) (atom (car x)))
2559 (values x 0))
2560 ;; If x is a negative expression but not a sum, then get rid of the
2561 ;; negative sign.
2562 ((negp x) (values 0 (neg x)))
2563 ;; If x is not a sum, or is a sum with more than 2 terms, or has some
2564 ;; symbols common to both summands, then do nothing.
2565 ((or (cdddr x)
2566 (not (eq (caar x) 'mplus))
2567 (intersect* (symbols (cadr x)) (symbols (caddr x))))
2568 (values x 0))
2569 ;; -x + y gives (y, x)
2570 ((and (or (negp (cadr x)) (mnump (cadr x)))
2571 (not (negp (caddr x))))
2572 (values (caddr x) (neg (cadr x))))
2573 ;; x - y gives (x, y)
2574 ((and (not (negp (cadr x)))
2575 (or (negp (caddr x)) (mnump (caddr x))))
2576 (values (cadr x) (neg (caddr x))))
2577 ;; - x - y gives (0, x+y)
2578 ((and (negp (cadr x)) (negp (caddr x)))
2579 (values 0 (neg x)))
2580 ;; Give up! (x, 0)
2582 (values x 0))))
2584 (defun negp (x)
2585 (and (mtimesp x) (mnegp (cadr x))))
2587 (defun splitsum (exp)
2588 (do ((llist (cdar exp) (cdr llist))
2589 (lhs1 (car exp))
2590 (rhs1 (cadr exp)))
2591 ((null llist)
2592 (if (mplusp lhs1) (setq success t))
2593 (list lhs1 rhs1))
2594 (cond ((member '$inf llist :test #'eq)
2595 (setq rhs1 (add2 '$inf (sub* rhs1 (addn llist t)))
2596 lhs1 (add2 '$inf (sub* lhs1 (addn llist t)))
2597 llist nil))
2598 ((member '$minf llist :test #'eq)
2599 (setq rhs1 (add2 '$minf (sub* rhs1 (addn llist t)))
2600 lhs1 (add2 '$minf (sub* lhs1 (addn llist t)))
2601 llist nil))
2602 ((null (symbols (car llist)))
2603 (setq lhs1 (sub lhs1 (car llist))
2604 rhs1 (sub rhs1 (car llist)))))))
2606 (defun splitprod (exp)
2607 (do ((flipsign)
2608 (lhs1 (car exp))
2609 (rhs1 (cadr exp))
2610 (llist (cdar exp) (cdr llist))
2611 (sign)
2612 (minus)
2613 (evens)
2614 (odds))
2615 ((null llist)
2616 (if (mtimesp lhs1) (setq success t))
2617 (cond (flipsign
2618 (setq success t)
2619 (list rhs1 lhs1))
2620 (t (list lhs1 rhs1))))
2621 (when (null (symbols (car llist)))
2622 (sign (car llist))
2623 (if (eq sign '$neg) (setq flipsign (not flipsign)))
2624 (if (member sign '($pos $neg) :test #'eq)
2625 (setq lhs1 (div lhs1 (car llist))
2626 rhs1 (div rhs1 (car llist)))))))
2628 (defun symbols (x)
2629 (let (($listconstvars %initiallearnflag))
2630 (cdr ($listofvars x))))
2632 ;; %initiallearnflag is only necessary so that %PI, %E, etc. can be LEARNed.
2634 (defun initialize-numeric-constant (c)
2635 (setq %initiallearnflag t)
2636 (let ((context '$global))
2637 (learn `((mequal) ,c ,(mget c '$numer)) t))
2638 (setq %initiallearnflag nil))
2640 (eval-when (:load-toplevel :execute)
2641 (mapc #'true*
2642 '(;; even and odd are integer
2643 (par ($even $odd) $integer)
2645 ; Cutting out inferences for integer, rational, real, complex (DK 10/2009).
2646 ; (kind $integer $rational)
2647 ; (par ($rational $irrational) $real)
2648 ; (par ($real $imaginary) $complex)
2650 ;; imaginary is complex
2651 (kind $imaginary $complex)
2653 ;; Declarations for constants
2654 (kind $%i $noninteger)
2655 (kind $%i $imaginary)
2656 (kind $%e $noninteger)
2657 (kind $%e $real)
2658 (kind $%pi $noninteger)
2659 (kind $%pi $real)
2660 (kind $%gamma $noninteger)
2661 (kind $%gamma $real)
2662 (kind $%phi $noninteger)
2663 (kind $%phi $real)
2664 (kind $%pi $irrational)
2665 (kind $%e $irrational)
2666 (kind $%phi $irrational)
2667 (kind $%catalan $noninteger)
2668 (kind $%catalan $real)
2670 ;; Declarations for functions
2671 (kind %log $increasing)
2672 (kind %asin $increasing) (kind %asin $oddfun)
2673 (kind %atan $increasing) (kind %atan $oddfun)
2674 (kind $delta $evenfun)
2675 (kind %sinh $increasing) (kind %sinh $oddfun)
2676 (kind %cosh $posfun)
2677 (kind %tanh $increasing) (kind %tanh $oddfun)
2678 (kind %coth $oddfun)
2679 (kind %csch $oddfun)
2680 (kind %sech $posfun)
2681 (kind %asinh $increasing) (kind %asinh $oddfun)
2682 ;; It would be nice to say %acosh is $posfun, but then
2683 ;; assume(xn<0); abs(acosh(xn)) -> acosh(xn), which is wrong
2684 ;; since acosh(xn) is complex.
2685 (kind %acosh $increasing) (kind %acosh $complex)
2686 (kind %atanh $increasing) (kind %atanh $oddfun)
2687 (kind $li $complex)
2688 (kind $lambert_w $complex)
2689 (kind %cabs $complex)))
2691 ;; Create an initial context for the user which is a subcontext of $global.
2692 ($newcontext '$initial))