calc-arith.el 90.7 KB
Newer Older
1
;;; calc-arith.el --- arithmetic functions for Calc
2

Jay Belanger's avatar
Jay Belanger committed
3 4
;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
;;   2005 Free Software Foundation, Inc.
5 6

;; Author: David Gillespie <daveg@synaptics.com>
7
;; Maintainer: Jay Belanger <belanger@truman.edu>
Eli Zaretskii's avatar
Eli Zaretskii committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

;; This file is part of GNU Emacs.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.

;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.

26
;;; Commentary:
Eli Zaretskii's avatar
Eli Zaretskii committed
27

28
;;; Code:
Eli Zaretskii's avatar
Eli Zaretskii committed
29 30 31

;; This file is autoloaded from calc-ext.el.

Jay Belanger's avatar
Jay Belanger committed
32
(require 'calc-ext)
Eli Zaretskii's avatar
Eli Zaretskii committed
33 34
(require 'calc-macs)

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
;;; The following lists are not exhaustive.
(defvar math-scalar-functions '(calcFunc-det
				calcFunc-cnorm calcFunc-rnorm
				calcFunc-vlen calcFunc-vcount
				calcFunc-vsum calcFunc-vprod
				calcFunc-vmin calcFunc-vmax))

(defvar math-nonscalar-functions '(vec calcFunc-idn calcFunc-diag
				       calcFunc-cvec calcFunc-index
				       calcFunc-trn
				       | calcFunc-append
				       calcFunc-cons calcFunc-rcons
				       calcFunc-tail calcFunc-rhead))

(defvar math-scalar-if-args-functions '(+ - * / neg))

(defvar math-real-functions '(calcFunc-arg
			      calcFunc-re calcFunc-im
			      calcFunc-floor calcFunc-ceil
			      calcFunc-trunc calcFunc-round
			      calcFunc-rounde calcFunc-roundu
			      calcFunc-ffloor calcFunc-fceil
			      calcFunc-ftrunc calcFunc-fround
			      calcFunc-frounde calcFunc-froundu))

(defvar math-positive-functions '())

(defvar math-nonnegative-functions '(calcFunc-cnorm calcFunc-rnorm
				     calcFunc-vlen calcFunc-vcount))

(defvar math-real-scalar-functions '(% calcFunc-idiv calcFunc-abs
				       calcFunc-choose calcFunc-perm
				       calcFunc-eq calcFunc-neq
				       calcFunc-lt calcFunc-gt
				       calcFunc-leq calcFunc-geq
				       calcFunc-lnot
				       calcFunc-max calcFunc-min))

(defvar math-real-if-arg-functions '(calcFunc-sin calcFunc-cos
74 75 76
				     calcFunc-tan calcFunc-sec
                                     calcFunc-csc calcFunc-cot
                                     calcFunc-arctan
77
				     calcFunc-sinh calcFunc-cosh
78 79 80
				     calcFunc-tanh calcFunc-sech
                                     calcFunc-csch calcFunc-coth
                                     calcFunc-exp
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
				     calcFunc-gamma calcFunc-fact))

(defvar math-integer-functions '(calcFunc-idiv
				 calcFunc-isqrt calcFunc-ilog
				 calcFunc-vlen calcFunc-vcount))

(defvar math-num-integer-functions '())

(defvar math-rounding-functions '(calcFunc-floor
				  calcFunc-ceil
				  calcFunc-round calcFunc-trunc
				  calcFunc-rounde calcFunc-roundu))

(defvar math-float-rounding-functions '(calcFunc-ffloor
					calcFunc-fceil
					calcFunc-fround calcFunc-ftrunc
					calcFunc-frounde calcFunc-froundu))

(defvar math-integer-if-args-functions '(+ - * % neg calcFunc-abs
					   calcFunc-min calcFunc-max
					   calcFunc-choose calcFunc-perm))

Eli Zaretskii's avatar
Eli Zaretskii committed
103 104 105 106 107 108

;;; Arithmetic.

(defun calc-min (arg)
  (interactive "P")
  (calc-slow-wrapper
109
   (calc-binary-op "min" 'calcFunc-min arg '(var inf var-inf))))
Eli Zaretskii's avatar
Eli Zaretskii committed
110 111 112 113

(defun calc-max (arg)
  (interactive "P")
  (calc-slow-wrapper
114
   (calc-binary-op "max" 'calcFunc-max arg '(neg (var inf var-inf)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
115 116 117 118

(defun calc-abs (arg)
  (interactive "P")
  (calc-slow-wrapper
119
   (calc-unary-op "abs" 'calcFunc-abs arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
120 121 122 123 124


(defun calc-idiv (arg)
  (interactive "P")
  (calc-slow-wrapper
125
   (calc-binary-op "\\" 'calcFunc-idiv arg 1)))
Eli Zaretskii's avatar
Eli Zaretskii committed
126 127 128 129 130 131 132 133 134 135 136


(defun calc-floor (arg)
  (interactive "P")
  (calc-slow-wrapper
   (if (calc-is-inverse)
       (if (calc-is-hyperbolic)
	   (calc-unary-op "ceil" 'calcFunc-fceil arg)
	 (calc-unary-op "ceil" 'calcFunc-ceil arg))
     (if (calc-is-hyperbolic)
	 (calc-unary-op "flor" 'calcFunc-ffloor arg)
137
       (calc-unary-op "flor" 'calcFunc-floor arg)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
138 139 140 141

(defun calc-ceiling (arg)
  (interactive "P")
  (calc-invert-func)
142
  (calc-floor arg))
Eli Zaretskii's avatar
Eli Zaretskii committed
143 144 145 146 147 148 149 150 151 152

(defun calc-round (arg)
  (interactive "P")
  (calc-slow-wrapper
   (if (calc-is-inverse)
       (if (calc-is-hyperbolic)
	   (calc-unary-op "trnc" 'calcFunc-ftrunc arg)
	 (calc-unary-op "trnc" 'calcFunc-trunc arg))
     (if (calc-is-hyperbolic)
	 (calc-unary-op "rond" 'calcFunc-fround arg)
153
       (calc-unary-op "rond" 'calcFunc-round arg)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
154 155 156 157

(defun calc-trunc (arg)
  (interactive "P")
  (calc-invert-func)
158
  (calc-round arg))
Eli Zaretskii's avatar
Eli Zaretskii committed
159 160 161 162

(defun calc-mant-part (arg)
  (interactive "P")
  (calc-slow-wrapper
163
   (calc-unary-op "mant" 'calcFunc-mant arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
164 165 166 167

(defun calc-xpon-part (arg)
  (interactive "P")
  (calc-slow-wrapper
168
   (calc-unary-op "xpon" 'calcFunc-xpon arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
169 170 171 172

(defun calc-scale-float (arg)
  (interactive "P")
  (calc-slow-wrapper
173
   (calc-binary-op "scal" 'calcFunc-scf arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
174 175 176 177

(defun calc-abssqr (arg)
  (interactive "P")
  (calc-slow-wrapper
178
   (calc-unary-op "absq" 'calcFunc-abssqr arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
179 180 181 182

(defun calc-sign (arg)
  (interactive "P")
  (calc-slow-wrapper
183
   (calc-unary-op "sign" 'calcFunc-sign arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
184 185 186 187

(defun calc-increment (arg)
  (interactive "p")
  (calc-wrapper
188
   (calc-enter-result 1 "incr" (list 'calcFunc-incr (calc-top-n 1) arg))))
Eli Zaretskii's avatar
Eli Zaretskii committed
189 190 191 192

(defun calc-decrement (arg)
  (interactive "p")
  (calc-wrapper
193
   (calc-enter-result 1 "decr" (list 'calcFunc-decr (calc-top-n 1) arg))))
Eli Zaretskii's avatar
Eli Zaretskii committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214


(defun math-abs-approx (a)
  (cond ((Math-negp a)
	 (math-neg a))
	((Math-anglep a)
	 a)
	((eq (car a) 'cplx)
	 (math-add (math-abs (nth 1 a)) (math-abs (nth 2 a))))
	((eq (car a) 'polar)
	 (nth 1 a))
	((eq (car a) 'sdev)
	 (math-abs-approx (nth 1 a)))
	((eq (car a) 'intv)
	 (math-max (math-abs (nth 2 a)) (math-abs (nth 3 a))))
	((eq (car a) 'date)
	 a)
	((eq (car a) 'vec)
	 (math-reduce-vec 'math-add-abs-approx a))
	((eq (car a) 'calcFunc-abs)
	 (car a))
215
	(t a)))
Eli Zaretskii's avatar
Eli Zaretskii committed
216 217

(defun math-add-abs-approx (a b)
218
  (math-add (math-abs-approx a) (math-abs-approx b)))
Eli Zaretskii's avatar
Eli Zaretskii committed
219 220 221 222


;;;; Declarations.

223 224 225
(defvar math-decls-cache-tag nil)
(defvar math-decls-cache nil)
(defvar math-decls-all nil)
Eli Zaretskii's avatar
Eli Zaretskii committed
226 227 228 229 230 231 232

;;; Math-decls-cache is an a-list where each entry is a list of the form:
;;;   (VAR TYPES RANGE)
;;; where VAR is a variable name (with var- prefix) or function name;
;;;       TYPES is a list of type symbols (any, int, frac, ...)
;;;	  RANGE is a sorted vector of intervals describing the range.

233 234 235 236 237 238 239 240 241 242 243 244 245
(defvar math-super-types
  '((int numint rat real number)
    (numint real number)
    (frac rat real number)
    (rat real number)
    (float real number)
    (real number)
    (number)
    (scalar)
    (matrix vector)
    (vector)
    (const)))

Eli Zaretskii's avatar
Eli Zaretskii committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
(defun math-setup-declarations ()
  (or (eq math-decls-cache-tag (calc-var-value 'var-Decls))
      (let ((p (calc-var-value 'var-Decls))
	    vec type range)
	(setq math-decls-cache-tag p
	      math-decls-cache nil)
	(and (eq (car-safe p) 'vec)
	     (while (setq p (cdr p))
	       (and (eq (car-safe (car p)) 'vec)
		    (setq vec (nth 2 (car p)))
		    (condition-case err
			(let ((v (nth 1 (car p))))
			  (setq type nil range nil)
			  (or (eq (car-safe vec) 'vec)
			      (setq vec (list 'vec vec)))
			  (while (and (setq vec (cdr vec))
				      (not (Math-objectp (car vec))))
			    (and (eq (car-safe (car vec)) 'var)
				 (let ((st (assq (nth 1 (car vec))
						 math-super-types)))
				   (cond (st (setq type (append type st)))
					 ((eq (nth 1 (car vec)) 'pos)
					  (setq type (append type
							     '(real number))
						range
						'(intv 1 0 (var inf var-inf))))
					 ((eq (nth 1 (car vec)) 'nonneg)
					  (setq type (append type
							     '(real number))
						range
						'(intv 3 0
						       (var inf var-inf))))))))
			  (if vec
			      (setq type (append type '(real number))
				    range (math-prepare-set (cons 'vec vec))))
			  (setq type (list type range))
			  (or (eq (car-safe v) 'vec)
			      (setq v (list 'vec v)))
			  (while (setq v (cdr v))
			    (if (or (eq (car-safe (car v)) 'var)
				    (not (Math-primp (car v))))
				(setq math-decls-cache
				      (cons (cons (if (eq (car (car v)) 'var)
						      (nth 2 (car v))
						    (car (car v)))
						  type)
					    math-decls-cache)))))
		      (error nil)))))
294
	(setq math-decls-all (assq 'var-All math-decls-cache)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
295 296 297 298 299 300 301

(defun math-known-scalarp (a &optional assume-scalar)
  (math-setup-declarations)
  (if (if calc-matrix-mode
	  (eq calc-matrix-mode 'scalar)
	assume-scalar)
      (not (math-check-known-matrixp a))
302
    (math-check-known-scalarp a)))
Eli Zaretskii's avatar
Eli Zaretskii committed
303 304 305

(defun math-known-matrixp (a)
  (and (not (Math-scalarp a))
306
       (not (math-known-scalarp a t))))
Eli Zaretskii's avatar
Eli Zaretskii committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

;;; Try to prove that A is a scalar (i.e., a non-vector).
(defun math-check-known-scalarp (a)
  (cond ((Math-objectp a) t)
	((memq (car a) math-scalar-functions)
	 t)
	((memq (car a) math-real-scalar-functions)
	 t)
	((memq (car a) math-scalar-if-args-functions)
	 (while (and (setq a (cdr a))
		     (math-check-known-scalarp (car a))))
	 (null a))
	((eq (car a) '^)
	 (math-check-known-scalarp (nth 1 a)))
	((math-const-var a) t)
	(t
	 (let ((decl (if (eq (car a) 'var)
			 (or (assq (nth 2 a) math-decls-cache)
			     math-decls-all)
		       (assq (car a) math-decls-cache))))
327
	   (memq 'scalar (nth 1 decl))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

;;; Try to prove that A is *not* a scalar.
(defun math-check-known-matrixp (a)
  (cond ((Math-objectp a) nil)
	((memq (car a) math-nonscalar-functions)
	 t)
	((memq (car a) math-scalar-if-args-functions)
	 (while (and (setq a (cdr a))
		     (not (math-check-known-matrixp (car a)))))
	 a)
	((eq (car a) '^)
	 (math-check-known-matrixp (nth 1 a)))
	((math-const-var a) nil)
	(t
	 (let ((decl (if (eq (car a) 'var)
			 (or (assq (nth 2 a) math-decls-cache)
			     math-decls-all)
		       (assq (car a) math-decls-cache))))
346
	   (memq 'vector (nth 1 decl))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
347 348 349 350


;;; Try to prove that A is a real (i.e., not complex).
(defun math-known-realp (a)
351
  (< (math-possible-signs a) 8))
Eli Zaretskii's avatar
Eli Zaretskii committed
352 353 354

;;; Try to prove that A is real and positive.
(defun math-known-posp (a)
355
  (eq (math-possible-signs a) 4))
Eli Zaretskii's avatar
Eli Zaretskii committed
356 357 358

;;; Try to prove that A is real and negative.
(defun math-known-negp (a)
359
  (eq (math-possible-signs a) 1))
Eli Zaretskii's avatar
Eli Zaretskii committed
360 361 362

;;; Try to prove that A is real and nonnegative.
(defun math-known-nonnegp (a)
363
  (memq (math-possible-signs a) '(2 4 6)))
Eli Zaretskii's avatar
Eli Zaretskii committed
364 365 366

;;; Try to prove that A is real and nonpositive.
(defun math-known-nonposp (a)
367
  (memq (math-possible-signs a) '(1 2 3)))
Eli Zaretskii's avatar
Eli Zaretskii committed
368 369 370

;;; Try to prove that A is nonzero.
(defun math-known-nonzerop (a)
371
  (memq (math-possible-signs a) '(1 4 5 8 9 12 13)))
Eli Zaretskii's avatar
Eli Zaretskii committed
372 373 374 375 376 377 378 379

;;; Return true if A is negative, or looks negative but we don't know.
(defun math-guess-if-neg (a)
  (let ((sgn (math-possible-signs a)))
    (if (memq sgn '(1 3))
	t
      (if (memq sgn '(2 4 6))
	  nil
380
	(math-looks-negp a)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394

;;; Find the possible signs of A, assuming A is a number of some kind.
;;; Returns an integer with bits:  1  may be negative,
;;;				   2  may be zero,
;;;				   4  may be positive,
;;;				   8  may be nonreal.

(defun math-possible-signs (a &optional origin)
  (cond ((Math-objectp a)
	 (if origin (setq a (math-sub a origin)))
	 (cond ((Math-posp a) 4)
	       ((Math-negp a) 1)
	       ((Math-zerop a) 2)
	       ((eq (car a) 'intv)
395 396 397 398 399 400
		(cond 
                 ((math-known-posp (nth 2 a)) 4)
                 ((math-known-negp (nth 3 a)) 1)
                 ((Math-zerop (nth 2 a)) 6)
                 ((Math-zerop (nth 3 a)) 3)
                 (t 7)))
Eli Zaretskii's avatar
Eli Zaretskii committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	       ((eq (car a) 'sdev)
		(if (math-known-realp (nth 1 a)) 7 15))
	       (t 8)))
	((memq (car a) '(+ -))
	 (cond ((Math-realp (nth 1 a))
		(if (eq (car a) '-)
		    (math-neg-signs
		     (math-possible-signs (nth 2 a)
					  (if origin
					      (math-add origin (nth 1 a))
					    (nth 1 a))))
		  (math-possible-signs (nth 2 a)
				       (if origin
					   (math-sub origin (nth 1 a))
					 (math-neg (nth 1 a))))))
	       ((Math-realp (nth 2 a))
		(let ((org (if (eq (car a) '-)
			       (nth 2 a)
			     (math-neg (nth 2 a)))))
		  (math-possible-signs (nth 1 a)
				       (if origin
					   (math-add origin org)
					 org))))
	       (t
		(let ((s1 (math-possible-signs (nth 1 a) origin))
		      (s2 (math-possible-signs (nth 2 a))))
		  (if (eq (car a) '-) (setq s2 (math-neg-signs s2)))
		  (cond ((eq s1 s2) s1)
			((eq s1 2) s2)
			((eq s2 2) s1)
			((>= s1 8) 15)
			((>= s2 8) 15)
			((and (eq s1 4) (eq s2 6)) 4)
			((and (eq s2 4) (eq s1 6)) 4)
			((and (eq s1 1) (eq s2 3)) 1)
			((and (eq s2 1) (eq s1 3)) 1)
			(t 7))))))
	((eq (car a) 'neg)
	 (math-neg-signs (math-possible-signs
			  (nth 1 a)
			  (and origin (math-neg origin)))))
	((and origin (Math-zerop origin) (setq origin nil)
	      nil))
	((and (or (eq (car a) '*)
		  (and (eq (car a) '/) origin))
	      (Math-realp (nth 1 a)))
	 (let ((s (if (eq (car a) '*)
		      (if (Math-zerop (nth 1 a))
			  (math-possible-signs 0 origin)
			(math-possible-signs (nth 2 a)
					     (math-div (or origin 0)
						       (nth 1 a))))
		    (math-neg-signs
		     (math-possible-signs (nth 2 a)
					  (math-div (nth 1 a)
						    origin))))))
	   (if (Math-negp (nth 1 a)) (math-neg-signs s) s)))
	((and (memq (car a) '(* /)) (Math-realp (nth 2 a)))
	 (let ((s (math-possible-signs (nth 1 a)
				       (if (eq (car a) '*)
					   (math-mul (or origin 0) (nth 2 a))
					 (math-div (or origin 0) (nth 2 a))))))
	   (if (Math-negp (nth 2 a)) (math-neg-signs s) s)))
	((eq (car a) 'vec)
	 (let ((signs 0))
	   (while (and (setq a (cdr a)) (< signs 15))
	     (setq signs (logior signs (math-possible-signs
					(car a) origin))))
	   signs))
	(t (let ((sign
		  (cond
		   ((memq (car a) '(* /))
		    (let ((s1 (math-possible-signs (nth 1 a)))
			  (s2 (math-possible-signs (nth 2 a))))
		      (cond ((>= s1 8) 15)
			    ((>= s2 8) 15)
			    ((and (eq (car a) '/) (memq s2 '(2 3 6 7))) 15)
			    (t
			     (logior (if (memq s1 '(4 5 6 7)) s2 0)
				     (if (memq s1 '(2 3 6 7)) 2 0)
				     (if (memq s1 '(1 3 5 7))
					 (math-neg-signs s2) 0))))))
		   ((eq (car a) '^)
		    (let ((s1 (math-possible-signs (nth 1 a)))
			  (s2 (math-possible-signs (nth 2 a))))
		      (cond ((>= s1 8) 15)
			    ((>= s2 8) 15)
			    ((eq s1 4) 4)
			    ((eq s1 2) (if (eq s2 4) 2 15))
			    ((eq s2 2) (if (memq s1 '(1 5)) 2 15))
			    ((Math-integerp (nth 2 a))
			     (if (math-evenp (nth 2 a))
				 (if (memq s1 '(3 6 7)) 6 4)
			       s1))
			    ((eq s1 6) (if (eq s2 4) 6 15))
			    (t 7))))
		   ((eq (car a) '%)
		    (let ((s2 (math-possible-signs (nth 2 a))))
		      (cond ((>= s2 8) 7)
			    ((eq s2 2) 2)
			    ((memq s2 '(4 6)) 6)
			    ((memq s2 '(1 3)) 3)
			    (t 7))))
		   ((and (memq (car a) '(calcFunc-abs calcFunc-abssqr))
			 (= (length a) 2))
		    (let ((s1 (math-possible-signs (nth 1 a))))
		      (cond ((eq s1 2) 2)
			    ((memq s1 '(1 4 5)) 4)
			    (t 6))))
		   ((and (eq (car a) 'calcFunc-exp) (= (length a) 2))
		    (let ((s1 (math-possible-signs (nth 1 a))))
		      (if (>= s1 8)
			  15
			(if (or (not origin) (math-negp origin))
			    4
			  (setq origin (math-sub (or origin 0) 1))
			  (if (Math-zerop origin) (setq origin nil))
			  s1))))
		   ((or (and (memq (car a) '(calcFunc-ln calcFunc-log10))
			     (= (length a) 2))
			(and (eq (car a) 'calcFunc-log)
			     (= (length a) 3)
			     (math-known-posp (nth 2 a))))
		    (if (math-known-nonnegp (nth 1 a))
			(math-possible-signs (nth 1 a) 1)
		      15))
		   ((and (eq (car a) 'calcFunc-sqrt) (= (length a) 2))
		    (let ((s1 (math-possible-signs (nth 1 a))))
		      (if (memq s1 '(2 4 6)) s1 15)))
		   ((memq (car a) math-nonnegative-functions) 6)
		   ((memq (car a) math-positive-functions) 4)
		   ((memq (car a) math-real-functions) 7)
		   ((memq (car a) math-real-scalar-functions) 7)
		   ((and (memq (car a) math-real-if-arg-functions)
			 (= (length a) 2))
		    (if (math-known-realp (nth 1 a)) 7 15)))))
	     (cond (sign
		    (if origin
			(+ (logand sign 8)
			   (if (Math-posp origin)
			       (if (memq sign '(1 2 3 8 9 10 11)) 1 7)
			     (if (memq sign '(2 4 6 8 10 12 14)) 4 7)))
		      sign))
		   ((math-const-var a)
		    (cond ((eq (nth 2 a) 'var-pi)
			   (if origin
			       (math-possible-signs (math-pi) origin)
			     4))
			  ((eq (nth 2 a) 'var-e)
			   (if origin
			       (math-possible-signs (math-e) origin)
			     4))
			  ((eq (nth 2 a) 'var-inf) 4)
			  ((eq (nth 2 a) 'var-uinf) 13)
			  ((eq (nth 2 a) 'var-i) 8)
			  (t 15)))
		   (t
		    (math-setup-declarations)
		    (let ((decl (if (eq (car a) 'var)
				    (or (assq (nth 2 a) math-decls-cache)
					math-decls-all)
				  (assq (car a) math-decls-cache))))
		      (if (and origin
			       (memq 'int (nth 1 decl))
			       (not (Math-num-integerp origin)))
			  5
			(if (nth 2 decl)
			    (math-possible-signs (nth 2 decl) origin)
			  (if (memq 'real (nth 1 decl))
			      7
571
			    15))))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
572 573 574 575 576 577

(defun math-neg-signs (s1)
  (if (>= s1 8)
      (+ 8 (math-neg-signs (- s1 8)))
    (+ (if (memq s1 '(1 3 5 7)) 4 0)
       (if (memq s1 '(2 3 6 7)) 2 0)
578
       (if (memq s1 '(4 5 6 7)) 1 0))))
Eli Zaretskii's avatar
Eli Zaretskii committed
579 580 581 582


;;; Try to prove that A is an integer.
(defun math-known-integerp (a)
583
  (eq (math-possible-types a) 1))
Eli Zaretskii's avatar
Eli Zaretskii committed
584 585

(defun math-known-num-integerp (a)
586
  (<= (math-possible-types a t) 3))
Eli Zaretskii's avatar
Eli Zaretskii committed
587 588

(defun math-known-imagp (a)
589
  (= (math-possible-types a) 16))
Eli Zaretskii's avatar
Eli Zaretskii committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746


;;; Find the possible types of A.
;;; Returns an integer with bits:  1  may be integer.
;;;				   2  may be integer-valued float.
;;;				   4  may be fraction.
;;;				   8  may be non-integer-valued float.
;;;				  16  may be imaginary.
;;;				  32  may be non-real, non-imaginary.
;;; Real infinities count as integers for the purposes of this function.
(defun math-possible-types (a &optional num)
  (cond ((Math-objectp a)
	 (cond ((Math-integerp a) (if num 3 1))
	       ((Math-messy-integerp a) (if num 3 2))
	       ((eq (car a) 'frac) (if num 12 4))
	       ((eq (car a) 'float) (if num 12 8))
	       ((eq (car a) 'intv)
		(if (equal (nth 2 a) (nth 3 a))
		    (math-possible-types (nth 2 a))
		  15))
	       ((eq (car a) 'sdev)
		(if (math-known-realp (nth 1 a)) 15 63))
	       ((eq (car a) 'cplx)
		(if (math-zerop (nth 1 a)) 16 32))
	       ((eq (car a) 'polar)
		(if (or (Math-equal (nth 2 a) (math-quarter-circle nil))
			(Math-equal (nth 2 a)
				    (math-neg (math-quarter-circle nil))))
		    16 48))
	       (t 63)))
	((eq (car a) '/)
	 (let* ((t1 (math-possible-types (nth 1 a) num))
		(t2 (math-possible-types (nth 2 a) num))
		(t12 (logior t1 t2)))
	   (if (< t12 16)
	       (if (> (logand t12 10) 0)
		   10
		 (if (or (= t1 4) (= t2 4) calc-prefer-frac)
		     5
		   15))
	     (if (< t12 32)
		 (if (= t1 16)
		     (if (= t2 16) 15
		       (if (< t2 16) 16 31))
		   (if (= t2 16)
		       (if (< t1 16) 16 31)
		     31))
	       63))))
	((memq (car a) '(+ - * %))
	 (let* ((t1 (math-possible-types (nth 1 a) num))
		(t2 (math-possible-types (nth 2 a) num))
		(t12 (logior t1 t2)))
	   (if (eq (car a) '%)
	       (setq t1 (logand t1 15) t2 (logand t2 15) t12 (logand t12 15)))
	   (if (< t12 16)
	       (let ((mask (if (<= t12 3)
			       1
			     (if (and (or (and (<= t1 3) (= (logand t2 3) 0))
					  (and (<= t2 3) (= (logand t1 3) 0)))
				      (memq (car a) '(+ -)))
				 4
			       5))))
		 (if num
		     (* mask 3)
		   (logior (if (and (> (logand t1 5) 0) (> (logand t2 5) 0))
			       mask 0)
			   (if (> (logand t12 10) 0)
			       (* mask 2) 0))))
	     (if (< t12 32)
		 (if (eq (car a) '*)
		     (if (= t1 16)
			 (if (= t2 16) 15
			   (if (< t2 16) 16 31))
		       (if (= t2 16)
			   (if (< t1 16) 16 31)
			 31))
		   (if (= t12 16) 16
		     (if (or (and (= t1 16) (< t2 16))
			     (and (= t2 16) (< t1 16))) 32 63)))
	       63))))
	((eq (car a) 'neg)
	 (math-possible-types (nth 1 a)))
	((eq (car a) '^)
	 (let* ((t1 (math-possible-types (nth 1 a) num))
		(t2 (math-possible-types (nth 2 a) num))
		(t12 (logior t1 t2)))
	   (if (and (<= t2 3) (math-known-nonnegp (nth 2 a)) (< t1 16))
	       (let ((mask (logior (if (> (logand t1 3) 0) 1 0)
				   (logand t1 4)
				   (if (> (logand t1 12) 0) 5 0))))
		 (if num
		     (* mask 3)
		   (logior (if (and (> (logand t1 5) 0) (> (logand t2 5) 0))
			       mask 0)
			   (if (> (logand t12 10) 0)
			       (* mask 2) 0))))
	     (if (and (math-known-nonnegp (nth 1 a))
		      (math-known-posp (nth 2 a)))
		 15
	       63))))
	((eq (car a) 'calcFunc-sqrt)
	 (let ((t1 (math-possible-signs (nth 1 a))))
	   (logior (if (> (logand t1 2) 0) 3 0)
		   (if (> (logand t1 1) 0) 16 0)
		   (if (> (logand t1 4) 0) 15 0)
		   (if (> (logand t1 8) 0) 32 0))))
	((eq (car a) 'vec)
	 (let ((types 0))
	   (while (and (setq a (cdr a)) (< types 63))
	     (setq types (logior types (math-possible-types (car a) t))))
	   types))
	((or (memq (car a) math-integer-functions)
	     (and (memq (car a) math-rounding-functions)
		  (math-known-nonnegp (or (nth 2 a) 0))))
	 1)
	((or (memq (car a) math-num-integer-functions)
	     (and (memq (car a) math-float-rounding-functions)
		  (math-known-nonnegp (or (nth 2 a) 0))))
	 2)
	((eq (car a) 'calcFunc-frac)
	 5)
	((and (eq (car a) 'calcFunc-float) (= (length a) 2))
	 (let ((t1 (math-possible-types (nth 1 a))))
	   (logior (if (> (logand t1 3) 0) 2 0)
		   (if (> (logand t1 12) 0) 8 0)
		   (logand t1 48))))
	((and (memq (car a) '(calcFunc-abs calcFunc-abssqr))
	      (= (length a) 2))
	 (let ((t1 (math-possible-types (nth 1 a))))
	   (if (>= t1 16)
	       15
	     t1)))
	((math-const-var a)
	 (cond ((memq (nth 2 a) '(var-e var-pi var-phi var-gamma)) 8)
	       ((eq (nth 2 a) 'var-inf) 1)
	       ((eq (nth 2 a) 'var-i) 16)
	       (t 63)))
	(t
	 (math-setup-declarations)
	 (let ((decl (if (eq (car a) 'var)
			 (or (assq (nth 2 a) math-decls-cache)
			     math-decls-all)
		       (assq (car a) math-decls-cache))))
	   (cond ((memq 'int (nth 1 decl))
		  1)
		 ((memq 'numint (nth 1 decl))
		  3)
		 ((memq 'frac (nth 1 decl))
		  4)
		 ((memq 'rat (nth 1 decl))
		  5)
		 ((memq 'float (nth 1 decl))
		  10)
		 ((nth 2 decl)
		  (math-possible-types (nth 2 decl)))
		 ((memq 'real (nth 1 decl))
		  15)
747
		 (t 63))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765

(defun math-known-evenp (a)
  (cond ((Math-integerp a)
	 (math-evenp a))
	((Math-messy-integerp a)
	 (or (> (nth 2 a) 0)
	     (math-evenp (math-trunc a))))
	((eq (car a) '*)
	 (if (math-known-evenp (nth 1 a))
	     (math-known-num-integerp (nth 2 a))
	   (if (math-known-num-integerp (nth 1 a))
	       (math-known-evenp (nth 2 a)))))
	((memq (car a) '(+ -))
	 (or (and (math-known-evenp (nth 1 a))
		  (math-known-evenp (nth 2 a)))
	     (and (math-known-oddp (nth 1 a))
		  (math-known-oddp (nth 2 a)))))
	((eq (car a) 'neg)
766
	 (math-known-evenp (nth 1 a)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
767 768 769 770 771 772 773 774 775 776 777 778 779

(defun math-known-oddp (a)
  (cond ((Math-integerp a)
	 (math-oddp a))
	((Math-messy-integerp a)
	 (and (<= (nth 2 a) 0)
	      (math-oddp (math-trunc a))))
	((memq (car a) '(+ -))
	 (or (and (math-known-evenp (nth 1 a))
		  (math-known-oddp (nth 2 a)))
	     (and (math-known-oddp (nth 1 a))
		  (math-known-evenp (nth 2 a)))))
	((eq (car a) 'neg)
780
	 (math-known-oddp (nth 1 a)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
781 782 783 784 785 786


(defun calcFunc-dreal (expr)
  (let ((types (math-possible-types expr)))
    (if (< types 16) 1
      (if (= (logand types 15) 0) 0
787
	(math-reject-arg expr 'realp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
788 789 790 791 792

(defun calcFunc-dimag (expr)
  (let ((types (math-possible-types expr)))
    (if (= types 16) 1
      (if (= (logand types 16) 0) 0
793
	(math-reject-arg expr "Expected an imaginary number")))))
Eli Zaretskii's avatar
Eli Zaretskii committed
794 795 796 797 798

(defun calcFunc-dpos (expr)
  (let ((signs (math-possible-signs expr)))
    (if (eq signs 4) 1
      (if (memq signs '(1 2 3)) 0
799
	(math-reject-arg expr 'posp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
800 801 802 803 804

(defun calcFunc-dneg (expr)
  (let ((signs (math-possible-signs expr)))
    (if (eq signs 1) 1
      (if (memq signs '(2 4 6)) 0
805
	(math-reject-arg expr 'negp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
806 807 808 809 810

(defun calcFunc-dnonneg (expr)
  (let ((signs (math-possible-signs expr)))
    (if (memq signs '(2 4 6)) 1
      (if (eq signs 1) 0
811
	(math-reject-arg expr 'posp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
812 813 814 815 816

(defun calcFunc-dnonzero (expr)
  (let ((signs (math-possible-signs expr)))
    (if (memq signs '(1 4 5 8 9 12 13)) 1
      (if (eq signs 2) 0
817
	(math-reject-arg expr 'nonzerop 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
818 819 820 821 822

(defun calcFunc-dint (expr)
  (let ((types (math-possible-types expr)))
    (if (= types 1) 1
      (if (= (logand types 1) 0) 0
823
	(math-reject-arg expr 'integerp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
824 825 826 827 828

(defun calcFunc-dnumint (expr)
  (let ((types (math-possible-types expr t)))
    (if (<= types 3) 1
      (if (= (logand types 3) 0) 0
829
	(math-reject-arg expr 'integerp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
830 831 832 833 834

(defun calcFunc-dnatnum (expr)
  (let ((res (calcFunc-dint expr)))
    (if (eq res 1)
	(calcFunc-dnonneg expr)
835
      res)))
Eli Zaretskii's avatar
Eli Zaretskii committed
836 837 838 839 840 841 842

(defun calcFunc-deven (expr)
  (if (math-known-evenp expr)
      1
    (if (or (math-known-oddp expr)
	    (= (logand (math-possible-types expr) 3) 0))
	0
843
      (math-reject-arg expr "Can't tell if expression is odd or even"))))
Eli Zaretskii's avatar
Eli Zaretskii committed
844 845 846 847 848 849 850

(defun calcFunc-dodd (expr)
  (if (math-known-oddp expr)
      1
    (if (or (math-known-evenp expr)
	    (= (logand (math-possible-types expr) 3) 0))
	0
851
      (math-reject-arg expr "Can't tell if expression is odd or even"))))
Eli Zaretskii's avatar
Eli Zaretskii committed
852 853 854 855 856

(defun calcFunc-drat (expr)
  (let ((types (math-possible-types expr)))
    (if (memq types '(1 4 5)) 1
      (if (= (logand types 5) 0) 0
857
	(math-reject-arg expr "Rational number expected")))))
Eli Zaretskii's avatar
Eli Zaretskii committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

(defun calcFunc-drange (expr)
  (math-setup-declarations)
  (let (range)
    (if (Math-realp expr)
	(list 'vec expr)
      (if (eq (car-safe expr) 'intv)
	  expr
	(if (eq (car-safe expr) 'var)
	    (setq range (nth 2 (or (assq (nth 2 expr) math-decls-cache)
				   math-decls-all)))
	  (setq range (nth 2 (assq (car-safe expr) math-decls-cache))))
	(if range
	    (math-clean-set (copy-sequence range))
	  (setq range (math-possible-signs expr))
	  (if (< range 8)
	      (aref [(vec)
		     (intv 2 (neg (var inf var-inf)) 0)
		     (vec 0)
		     (intv 3 (neg (var inf var-inf)) 0)
		     (intv 1 0 (var inf var-inf))
		     (vec (intv 2 (neg (var inf var-inf)) 0)
			  (intv 1 0 (var inf var-inf)))
		     (intv 3 0 (var inf var-inf))
		     (intv 3 (neg (var inf var-inf)) (var inf var-inf))] range)
883
	    (math-reject-arg expr 'realp 'quiet)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
884 885 886 887

(defun calcFunc-dscalar (a)
  (if (math-known-scalarp a) 1
    (if (math-known-matrixp a) 0
888
      (math-reject-arg a 'objectp 'quiet))))
Eli Zaretskii's avatar
Eli Zaretskii committed
889 890 891 892


;;;; Arithmetic.

893
(defsubst calcFunc-neg (a)
894
  (math-normalize (list 'neg a)))
Eli Zaretskii's avatar
Eli Zaretskii committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939

(defun math-neg-fancy (a)
  (cond ((eq (car a) 'polar)
	 (list 'polar
	       (nth 1 a)
	       (if (math-posp (nth 2 a))
		   (math-sub (nth 2 a) (math-half-circle nil))
		 (math-add (nth 2 a) (math-half-circle nil)))))
	((eq (car a) 'mod)
	 (if (math-zerop (nth 1 a))
	     a
	   (list 'mod (math-sub (nth 2 a) (nth 1 a)) (nth 2 a))))
	((eq (car a) 'sdev)
	 (list 'sdev (math-neg (nth 1 a)) (nth 2 a)))
	((eq (car a) 'intv)
	 (math-make-intv (aref [0 2 1 3] (nth 1 a))
			 (math-neg (nth 3 a))
			 (math-neg (nth 2 a))))
	((and math-simplify-only
	      (not (equal a math-simplify-only)))
	 (list 'neg a))
	((eq (car a) '+)
	 (math-sub (math-neg (nth 1 a)) (nth 2 a)))
	((eq (car a) '-)
	 (math-sub (nth 2 a) (nth 1 a)))
	((and (memq (car a) '(* /))
	      (math-okay-neg (nth 1 a)))
	 (list (car a) (math-neg (nth 1 a)) (nth 2 a)))
	((and (memq (car a) '(* /))
	      (math-okay-neg (nth 2 a)))
	 (list (car a) (nth 1 a) (math-neg (nth 2 a))))
	((and (memq (car a) '(* /))
	      (or (math-objectp (nth 1 a))
		  (and (eq (car (nth 1 a)) '*)
		       (math-objectp (nth 1 (nth 1 a))))))
	 (list (car a) (math-neg (nth 1 a)) (nth 2 a)))
	((and (eq (car a) '/)
	      (or (math-objectp (nth 2 a))
		  (and (eq (car (nth 2 a)) '*)
		       (math-objectp (nth 1 (nth 2 a))))))
	 (list (car a) (nth 1 a) (math-neg (nth 2 a))))
	((and (eq (car a) 'var) (memq (nth 2 a) '(var-uinf var-nan)))
	 a)
	((eq (car a) 'neg)
	 (nth 1 a))
940
	(t (list 'neg a))))
Eli Zaretskii's avatar
Eli Zaretskii committed
941 942 943

(defun math-okay-neg (a)
  (or (math-looks-negp a)
944
      (eq (car-safe a) '-)))
Eli Zaretskii's avatar
Eli Zaretskii committed
945 946

(defun math-neg-float (a)
947
  (list 'float (Math-integer-neg (nth 1 a)) (nth 2 a)))
Eli Zaretskii's avatar
Eli Zaretskii committed
948 949 950 951 952 953 954 955


(defun calcFunc-add (&rest rest)
  (if rest
      (let ((a (car rest)))
	(while (setq rest (cdr rest))
	  (setq a (list '+ a (car rest))))
	(math-normalize a))
956
    0))
Eli Zaretskii's avatar
Eli Zaretskii committed
957 958 959 960 961 962 963

(defun calcFunc-sub (&rest rest)
  (if rest
      (let ((a (car rest)))
	(while (setq rest (cdr rest))
	  (setq a (list '- a (car rest))))
	(math-normalize a))
964
    0))
Eli Zaretskii's avatar
Eli Zaretskii committed
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

(defun math-add-objects-fancy (a b)
  (cond ((and (Math-numberp a) (Math-numberp b))
	 (let ((aa (math-complex a))
	       (bb (math-complex b)))
	   (math-normalize
	    (let ((res (list 'cplx
			     (math-add (nth 1 aa) (nth 1 bb))
			     (math-add (nth 2 aa) (nth 2 bb)))))
	      (if (math-want-polar a b)
		  (math-polar res)
		res)))))
	((or (Math-vectorp a) (Math-vectorp b))
	 (math-map-vec-2 'math-add a b))
	((eq (car-safe a) 'sdev)
	 (if (eq (car-safe b) 'sdev)
	     (math-make-sdev (math-add (nth 1 a) (nth 1 b))
			     (math-hypot (nth 2 a) (nth 2 b)))
	   (and (or (Math-scalarp b)
		    (not (Math-objvecp b)))
		(math-make-sdev (math-add (nth 1 a) b) (nth 2 a)))))
	((and (eq (car-safe b) 'sdev)
	      (or (Math-scalarp a)
		  (not (Math-objvecp a))))
	 (math-make-sdev (math-add a (nth 1 b)) (nth 2 b)))
	((eq (car-safe a) 'intv)
	 (if (eq (car-safe b) 'intv)
	     (math-make-intv (logior (logand (nth 1 a) (nth 1 b))
				     (if (equal (nth 2 a)
						'(neg (var inf var-inf)))
					 (logand (nth 1 a) 2) 0)
				     (if (equal (nth 2 b)
						'(neg (var inf var-inf)))
					 (logand (nth 1 b) 2) 0)
				     (if (equal (nth 3 a) '(var inf var-inf))
					 (logand (nth 1 a) 1) 0)
				     (if (equal (nth 3 b) '(var inf var-inf))
					 (logand (nth 1 b) 1) 0))
			     (math-add (nth 2 a) (nth 2 b))
			     (math-add (nth 3 a) (nth 3 b)))
	   (and (or (Math-anglep b)
		    (eq (car b) 'date)
		    (not (Math-objvecp b)))
		(math-make-intv (nth 1 a)
				(math-add (nth 2 a) b)
				(math-add (nth 3 a) b)))))
	((and (eq (car-safe b) 'intv)
	      (or (Math-anglep a)
		  (eq (car a) 'date)
		  (not (Math-objvecp a))))
	 (math-make-intv (nth 1 b)
			 (math-add a (nth 2 b))
			 (math-add a (nth 3 b))))
	((eq (car-safe a) 'date)
	 (cond ((eq (car-safe b) 'date)
		(math-add (nth 1 a) (nth 1 b)))
	       ((eq (car-safe b) 'hms)
		(let ((parts (math-date-parts (nth 1 a))))
		  (list 'date
			(math-add (car parts)   ; this minimizes roundoff
				  (math-div (math-add
					     (math-add (nth 1 parts)
						       (nth 2 parts))
					     (math-add
					      (math-mul (nth 1 b) 3600)
					      (math-add (math-mul (nth 2 b) 60)
							(nth 3 b))))
					    86400)))))
	       ((Math-realp b)
		(list 'date (math-add (nth 1 a) b)))
	       (t nil)))
	((eq (car-safe b) 'date)
	 (math-add-objects-fancy b a))
	((and (eq (car-safe a) 'mod)
	      (eq (car-safe b) 'mod)
	      (equal (nth 2 a) (nth 2 b)))
	 (math-make-mod (math-add (nth 1 a) (nth 1 b)) (nth 2 a)))
	((and (eq (car-safe a) 'mod)
	      (Math-anglep b))
	 (math-make-mod (math-add (nth 1 a) b) (nth 2 a)))
	((and (eq (car-safe b) 'mod)
	      (Math-anglep a))
	 (math-make-mod (math-add a (nth 1 b)) (nth 2 b)))
	((and (or (eq (car-safe a) 'hms) (eq (car-safe b) 'hms))
	      (and (Math-anglep a) (Math-anglep b)))
	 (or (eq (car-safe a) 'hms) (setq a (math-to-hms a)))
	 (or (eq (car-safe b) 'hms) (setq b (math-to-hms b)))
	 (math-normalize
	  (if (math-negp a)
	      (math-neg (math-add (math-neg a) (math-neg b)))
	    (if (math-negp b)
		(let* ((s (math-add (nth 3 a) (nth 3 b)))
		       (m (math-add (nth 2 a) (nth 2 b)))
		       (h (math-add (nth 1 a) (nth 1 b))))
		  (if (math-negp s)
		      (setq s (math-add s 60)
			    m (math-add m -1)))
		  (if (math-negp m)
		      (setq m (math-add m 60)
			    h (math-add h -1)))
		  (if (math-negp h)
		      (math-add b a)
		    (list 'hms h m s)))
	      (let* ((s (math-add (nth 3 a) (nth 3 b)))
		     (m (math-add (nth 2 a) (nth 2 b)))
		     (h (math-add (nth 1 a) (nth 1 b))))
		(list 'hms h m s))))))
1072
	(t (calc-record-why "*Incompatible arguments for +" a b))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150

(defun math-add-symb-fancy (a b)
  (or (and math-simplify-only
	   (not (equal a math-simplify-only))
	   (list '+ a b))
      (and (eq (car-safe b) '+)
	   (math-add (math-add a (nth 1 b))
		     (nth 2 b)))
      (and (eq (car-safe b) '-)
	   (math-sub (math-add a (nth 1 b))
		     (nth 2 b)))
      (and (eq (car-safe b) 'neg)
	   (eq (car-safe (nth 1 b)) '+)
	   (math-sub (math-sub a (nth 1 (nth 1 b)))
		     (nth 2 (nth 1 b))))
      (and (or (and (Math-vectorp a) (math-known-scalarp b))
	       (and (Math-vectorp b) (math-known-scalarp a)))
	   (math-map-vec-2 'math-add a b))
      (let ((inf (math-infinitep a)))
	(cond
	 (inf
	  (let ((inf2 (math-infinitep b)))
	    (if inf2
		(if (or (memq (nth 2 inf) '(var-uinf var-nan))
			(memq (nth 2 inf2) '(var-uinf var-nan)))
		    '(var nan var-nan)
		  (let ((dir (math-infinite-dir a inf))
			(dir2 (math-infinite-dir b inf2)))
		    (if (and (Math-objectp dir) (Math-objectp dir2))
			(if (Math-equal dir dir2)
			    a
			  '(var nan var-nan)))))
	      (if (and (equal a '(var inf var-inf))
		       (eq (car-safe b) 'intv)
		       (memq (nth 1 b) '(2 3))
		       (equal (nth 2 b) '(neg (var inf var-inf))))
		  (list 'intv 3 (nth 2 b) a)
		(if (and (equal a '(neg (var inf var-inf)))
			 (eq (car-safe b) 'intv)
			 (memq (nth 1 b) '(1 3))
			 (equal (nth 3 b) '(var inf var-inf)))
		    (list 'intv 3 a (nth 3 b))
		  a)))))
	 ((math-infinitep b)
	  (if (eq (car-safe a) 'intv)
	      (math-add b a)
	    b))
	 ((eq (car-safe a) '+)
	  (let ((temp (math-combine-sum (nth 2 a) b nil nil t)))
	    (and temp
		 (math-add (nth 1 a) temp))))
	 ((eq (car-safe a) '-)
	  (let ((temp (math-combine-sum (nth 2 a) b t nil t)))
	    (and temp
		 (math-add (nth 1 a) temp))))
	 ((and (Math-objectp a) (Math-objectp b))
	  nil)
	 (t
	  (math-combine-sum a b nil nil nil))))
      (and (Math-looks-negp b)
	   (list '- a (math-neg b)))
      (and (Math-looks-negp a)
	   (list '- b (math-neg a)))
      (and (eq (car-safe a) 'calcFunc-idn)
	   (= (length a) 2)
	   (or (and (eq (car-safe b) 'calcFunc-idn)
		    (= (length b) 2)
		    (list 'calcFunc-idn (math-add (nth 1 a) (nth 1 b))))
	       (and (math-square-matrixp b)
		    (math-add (math-mimic-ident (nth 1 a) b) b))
	       (and (math-known-scalarp b)
		    (math-add (nth 1 a) b))))
      (and (eq (car-safe b) 'calcFunc-idn)
	   (= (length a) 2)
	   (or (and (math-square-matrixp a)
		    (math-add a (math-mimic-ident (nth 1 b) a)))
	       (and (math-known-scalarp a)
		    (math-add a (nth 1 b)))))
1151
      (list '+ a b)))
Eli Zaretskii's avatar
Eli Zaretskii committed
1152 1153 1154 1155 1156 1157 1158 1159


(defun calcFunc-mul (&rest rest)
  (if rest
      (let ((a (car rest)))
	(while (setq rest (cdr rest))
	  (setq a (list '* a (car rest))))
	(math-normalize a))
1160
    1))
Eli Zaretskii's avatar
Eli Zaretskii committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258

(defun math-mul-objects-fancy (a b)
  (cond ((and (Math-numberp a) (Math-numberp b))
	 (math-normalize
	  (if (math-want-polar a b)
	      (let ((a (math-polar a))
		    (b (math-polar b)))
		(list 'polar
		      (math-mul (nth 1 a) (nth 1 b))
		      (math-fix-circular (math-add (nth 2 a) (nth 2 b)))))
	    (setq a (math-complex a)
		  b (math-complex b))
	    (list 'cplx
		  (math-sub (math-mul (nth 1 a) (nth 1 b))
			    (math-mul (nth 2 a) (nth 2 b)))
		  (math-add (math-mul (nth 1 a) (nth 2 b))
			    (math-mul (nth 2 a) (nth 1 b)))))))
	((Math-vectorp a)
	 (if (Math-vectorp b)
	     (if (math-matrixp a)
		 (if (math-matrixp b)
		     (if (= (length (nth 1 a)) (length b))
			 (math-mul-mats a b)
		       (math-dimension-error))
		   (if (= (length (nth 1 a)) 2)
		       (if (= (length a) (length b))
			   (math-mul-mats a (list 'vec b))
			 (math-dimension-error))
		     (if (= (length (nth 1 a)) (length b))
			 (math-mul-mat-vec a b)
		       (math-dimension-error))))
	       (if (math-matrixp b)
		   (if (= (length a) (length b))
		       (nth 1 (math-mul-mats (list 'vec a) b))
		     (math-dimension-error))
		 (if (= (length a) (length b))
		     (math-dot-product a b)
		   (math-dimension-error))))
	   (math-map-vec-2 'math-mul a b)))
	((Math-vectorp b)
	 (math-map-vec-2 'math-mul a b))
	((eq (car-safe a) 'sdev)
	 (if (eq (car-safe b) 'sdev)
	     (math-make-sdev (math-mul (nth 1 a) (nth 1 b))
			     (math-hypot (math-mul (nth 2 a) (nth 1 b))
					 (math-mul (nth 2 b) (nth 1 a))))
	   (and (or (Math-scalarp b)
		    (not (Math-objvecp b)))
		(math-make-sdev (math-mul (nth 1 a) b)
				(math-mul (nth 2 a) b)))))
	((and (eq (car-safe b) 'sdev)
	      (or (Math-scalarp a)
		  (not (Math-objvecp a))))
	 (math-make-sdev (math-mul a (nth 1 b)) (math-mul a (nth 2 b))))
	((and (eq (car-safe a) 'intv) (Math-anglep b))
	 (if (Math-negp b)
	     (math-neg (math-mul a (math-neg b)))
	   (math-make-intv (nth 1 a)
			   (math-mul (nth 2 a) b)
			   (math-mul (nth 3 a) b))))
	((and (eq (car-safe b) 'intv) (Math-anglep a))
	 (math-mul b a))
	((and (eq (car-safe a) 'intv) (math-intv-constp a)
	      (eq (car-safe b) 'intv) (math-intv-constp b))
	 (let ((lo (math-mul a (nth 2 b)))
	       (hi (math-mul a (nth 3 b))))
	   (or (eq (car-safe lo) 'intv)
	       (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) lo lo)))
	   (or (eq (car-safe hi) 'intv)
	       (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) hi hi)))
	   (math-combine-intervals
	    (nth 2 lo) (and (or (memq (nth 1 b) '(2 3))
				(math-infinitep (nth 2 lo)))
			    (memq (nth 1 lo) '(2 3)))
	    (nth 3 lo) (and (or (memq (nth 1 b) '(2 3))
				(math-infinitep (nth 3 lo)))
			    (memq (nth 1 lo) '(1 3)))
	    (nth 2 hi) (and (or (memq (nth 1 b) '(1 3))
				(math-infinitep (nth 2 hi)))
			    (memq (nth 1 hi) '(2 3)))
	    (nth 3 hi) (and (or (memq (nth 1 b) '(1 3))
				(math-infinitep (nth 3 hi)))
			    (memq (nth 1 hi) '(1 3))))))
	((and (eq (car-safe a) 'mod)
	      (eq (car-safe b) 'mod)
	      (equal (nth 2 a) (nth 2 b)))
	 (math-make-mod (math-mul (nth 1 a) (nth 1 b)) (nth 2 a)))
	((and (eq (car-safe a) 'mod)
	      (Math-anglep b))
	 (math-make-mod (math-mul (nth 1 a) b) (nth 2 a)))
	((and (eq (car-safe b) 'mod)
	      (Math-anglep a))
	 (math-make-mod (math-mul a (nth 1 b)) (nth 2 b)))
	((and (eq (car-safe a) 'hms) (Math-realp b))
	 (math-with-extra-prec 2
	   (math-to-hms (math-mul (math-from-hms a 'deg) b) 'deg)))
	((and (eq (car-safe b) 'hms) (Math-realp a))
	 (math-mul b a))
1259
	(t (calc-record-why "*Incompatible arguments for *" a b))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1260 1261 1262 1263

;;; Fast function to multiply floating-point numbers.
(defun math-mul-float (a b)   ; [F F F]
  (math-make-float (math-mul (nth 1 a) (nth 1 b))
1264
		   (+ (nth 2 a) (nth 2 b))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1265 1266 1267

(defun math-sqr-float (a)   ; [F F]
  (math-make-float (math-mul (nth 1 a) (nth 1 a))
1268
		   (+ (nth 2 a) (nth 2 a))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1269 1270 1271 1272 1273 1274 1275 1276 1277

(defun math-intv-constp (a &optional finite)
  (and (or (Math-anglep (nth 2 a))
	   (and (equal (nth 2 a) '(neg (var inf var-inf)))
		(or (not finite)
		    (memq (nth 1 a) '(0 1)))))
       (or (Math-anglep (nth 3 a))
	   (and (equal (nth 3 a) '(var inf var-inf))
		(or (not finite)
1278
		    (memq (nth 1 a) '(0 2)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

(defun math-mul-zero (a b)
  (if (math-known-matrixp b)
      (if (math-vectorp b)
	  (math-map-vec-2 'math-mul a b)
	(math-mimic-ident 0 b))
    (if (math-infinitep b)
	'(var nan var-nan)
      (let ((aa nil) (bb nil))
	(if (and (eq (car-safe b) 'intv)
		 (progn
		   (and (equal (nth 2 b) '(neg (var inf var-inf)))
			(memq (nth 1 b) '(2 3))
			(setq aa (nth 2 b)))
		   (and (equal (nth 3 b) '(var inf var-inf))
			(memq (nth 1 b) '(1 3))
			(setq bb (nth 3 b)))
		   (or aa bb)))
	    (if (or (math-posp a)
		    (and (math-zerop a)
			 (or (memq calc-infinite-mode '(-1 1))
			     (setq aa '(neg (var inf var-inf))
				   bb '(var inf var-inf)))))
		(list 'intv 3 (or aa 0) (or bb 0))
	      (if (math-negp a)
		  (math-neg (list 'intv 3 (or aa 0) (or bb 0)))
		'(var nan var-nan)))
1306
	  (if (or (math-floatp a) (math-floatp b)) '(float 0 0) 0))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417


(defun math-mul-symb-fancy (a b)
  (or (and math-simplify-only
	   (not (equal a math-simplify-only))
	   (list '* a b))
      (and (Math-equal-int a 1)
	   b)
      (and (Math-equal-int a -1)
	   (math-neg b))
      (and (or (and (Math-vectorp a) (math-known-scalarp b))
	       (and (Math-vectorp b) (math-known-scalarp a)))
	   (math-map-vec-2 'math-mul a b))
      (and (Math-objectp b) (not (Math-objectp a))
	   (math-mul b a))
      (and (eq (car-safe a) 'neg)
	   (math-neg (math-mul (nth 1 a) b)))
      (and (eq (car-safe b) 'neg)
	   (math-neg (math-mul a (nth 1 b))))
      (and (eq (car-safe a) '*)
	   (math-mul (nth 1 a)
		     (math-mul (nth 2 a) b)))
      (and (eq (car-safe a) '^)
	   (Math-looks-negp (nth 2 a))
	   (not (and (eq (car-safe b) '^) (Math-looks-negp (nth 2 b))))
	   (math-known-scalarp b t)
	   (math-div b (math-normalize
			(list '^ (nth 1 a) (math-neg (nth 2 a))))))
      (and (eq (car-safe b) '^)
	   (Math-looks-negp (nth 2 b))
	   (not (and (eq (car-safe a) '^) (Math-looks-negp (nth 2 a))))
	   (math-div a (math-normalize
			(list '^ (nth 1 b) (math-neg (nth 2 b))))))
      (and (eq (car-safe a) '/)
	   (or (math-known-scalarp a t) (math-known-scalarp b t))
	   (let ((temp (math-combine-prod (nth 2 a) b t nil t)))
	     (if temp
		 (math-mul (nth 1 a) temp)
	       (math-div (math-mul (nth 1 a) b) (nth 2 a)))))
      (and (eq (car-safe b) '/)
	   (math-div (math-mul a (nth 1 b)) (nth 2 b)))
      (and (eq (car-safe b) '+)
	   (Math-numberp a)
	   (or (Math-numberp (nth 1 b))
	       (Math-numberp (nth 2 b)))
	   (math-add (math-mul a (nth 1 b))
		     (math-mul a (nth 2 b))))
      (and (eq (car-safe b) '-)
	   (Math-numberp a)
	   (or (Math-numberp (nth 1 b))
	       (Math-numberp (nth 2 b)))
	   (math-sub (math-mul a (nth 1 b))
		     (math-mul a (nth 2 b))))
      (and (eq (car-safe b) '*)
	   (Math-numberp (nth 1 b))
	   (not (Math-numberp a))
	   (math-mul (nth 1 b) (math-mul a (nth 2 b))))
      (and (eq (car-safe a) 'calcFunc-idn)
	   (= (length a) 2)
	   (or (and (eq (car-safe b) 'calcFunc-idn)
		    (= (length b) 2)
		    (list 'calcFunc-idn (math-mul (nth 1 a) (nth 1 b))))
	       (and (math-known-scalarp b)
		    (list 'calcFunc-idn (math-mul (nth 1 a) b)))
	       (