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