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

3
;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4 5

;; Author: David Gillespie <daveg@synaptics.com>
6
;; Maintainers: D. Goel <deego@gnufans.org>
Deepak Goel's avatar
Deepak Goel committed
7
;;              Colin Walters <walters@debian.org>
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 32 33 34 35 36 37 38 39 40 41 42

;; This file is autoloaded from calc-ext.el.
(require 'calc-ext)

(require 'calc-macs)

(defun calc-Need-calc-arith () nil)


;;; Arithmetic.

(defun calc-min (arg)
  (interactive "P")
  (calc-slow-wrapper
43
   (calc-binary-op "min" 'calcFunc-min arg '(var inf var-inf))))
Eli Zaretskii's avatar
Eli Zaretskii committed
44 45 46 47

(defun calc-max (arg)
  (interactive "P")
  (calc-slow-wrapper
48
   (calc-binary-op "max" 'calcFunc-max arg '(neg (var inf var-inf)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
49 50 51 52

(defun calc-abs (arg)
  (interactive "P")
  (calc-slow-wrapper
53
   (calc-unary-op "abs" 'calcFunc-abs arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
54 55 56 57 58


(defun calc-idiv (arg)
  (interactive "P")
  (calc-slow-wrapper
59
   (calc-binary-op "\\" 'calcFunc-idiv arg 1)))
Eli Zaretskii's avatar
Eli Zaretskii committed
60 61 62 63 64 65 66 67 68 69 70


(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)
71
       (calc-unary-op "flor" 'calcFunc-floor arg)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
72 73 74 75

(defun calc-ceiling (arg)
  (interactive "P")
  (calc-invert-func)
76
  (calc-floor arg))
Eli Zaretskii's avatar
Eli Zaretskii committed
77 78 79 80 81 82 83 84 85 86

(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)
87
       (calc-unary-op "rond" 'calcFunc-round arg)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
88 89 90 91

(defun calc-trunc (arg)
  (interactive "P")
  (calc-invert-func)
92
  (calc-round arg))
Eli Zaretskii's avatar
Eli Zaretskii committed
93 94 95 96

(defun calc-mant-part (arg)
  (interactive "P")
  (calc-slow-wrapper
97
   (calc-unary-op "mant" 'calcFunc-mant arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
98 99 100 101

(defun calc-xpon-part (arg)
  (interactive "P")
  (calc-slow-wrapper
102
   (calc-unary-op "xpon" 'calcFunc-xpon arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
103 104 105 106

(defun calc-scale-float (arg)
  (interactive "P")
  (calc-slow-wrapper
107
   (calc-binary-op "scal" 'calcFunc-scf arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
108 109 110 111

(defun calc-abssqr (arg)
  (interactive "P")
  (calc-slow-wrapper
112
   (calc-unary-op "absq" 'calcFunc-abssqr arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
113 114 115 116

(defun calc-sign (arg)
  (interactive "P")
  (calc-slow-wrapper
117
   (calc-unary-op "sign" 'calcFunc-sign arg)))
Eli Zaretskii's avatar
Eli Zaretskii committed
118 119 120 121

(defun calc-increment (arg)
  (interactive "p")
  (calc-wrapper
122
   (calc-enter-result 1 "incr" (list 'calcFunc-incr (calc-top-n 1) arg))))
Eli Zaretskii's avatar
Eli Zaretskii committed
123 124 125 126

(defun calc-decrement (arg)
  (interactive "p")
  (calc-wrapper
127
   (calc-enter-result 1 "decr" (list 'calcFunc-decr (calc-top-n 1) arg))))
Eli Zaretskii's avatar
Eli Zaretskii committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148


(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))
149
	(t a)))
Eli Zaretskii's avatar
Eli Zaretskii committed
150 151

(defun math-add-abs-approx (a b)
152
  (math-add (math-abs-approx a) (math-abs-approx b)))
Eli Zaretskii's avatar
Eli Zaretskii committed
153 154 155 156


;;;; Declarations.

157 158 159
(defvar math-decls-cache-tag nil)
(defvar math-decls-cache nil)
(defvar math-decls-all nil)
Eli Zaretskii's avatar
Eli Zaretskii committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

;;; 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.

(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)))))
215
	(setq math-decls-all (assq 'var-All math-decls-cache)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
216 217

(defvar math-super-types
218 219 220 221 222 223 224 225 226 227 228
  '((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
229 230 231 232 233 234 235

(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))
236
    (math-check-known-scalarp a)))
Eli Zaretskii's avatar
Eli Zaretskii committed
237 238 239

(defun math-known-matrixp (a)
  (and (not (Math-scalarp a))
240
       (not (math-known-scalarp a t))))
Eli Zaretskii's avatar
Eli Zaretskii committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

;;; 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))))
261
	   (memq 'scalar (nth 1 decl))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

;;; 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))))
280
	   (memq 'vector (nth 1 decl))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
281 282 283 284


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

;;; Try to prove that A is real and positive.
(defun math-known-posp (a)
289
  (eq (math-possible-signs a) 4))
Eli Zaretskii's avatar
Eli Zaretskii committed
290 291 292

;;; Try to prove that A is real and negative.
(defun math-known-negp (a)
293
  (eq (math-possible-signs a) 1))
Eli Zaretskii's avatar
Eli Zaretskii committed
294 295 296

;;; Try to prove that A is real and nonnegative.
(defun math-known-nonnegp (a)
297
  (memq (math-possible-signs a) '(2 4 6)))
Eli Zaretskii's avatar
Eli Zaretskii committed
298 299 300

;;; Try to prove that A is real and nonpositive.
(defun math-known-nonposp (a)
301
  (memq (math-possible-signs a) '(1 2 3)))
Eli Zaretskii's avatar
Eli Zaretskii committed
302 303 304

;;; Try to prove that A is nonzero.
(defun math-known-nonzerop (a)
305
  (memq (math-possible-signs a) '(1 4 5 8 9 12 13)))
Eli Zaretskii's avatar
Eli Zaretskii committed
306 307 308 309 310 311 312 313

;;; 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
314
	(math-looks-negp a)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 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

;;; 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)
		(cond ((Math-zerop (nth 2 a)) 6)
		      ((Math-zerop (nth 3 a)) 3)
		      (t 7)))
	       ((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
502
			    15))))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
503 504 505 506 507 508

(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)
509
       (if (memq s1 '(4 5 6 7)) 1 0))))
Eli Zaretskii's avatar
Eli Zaretskii committed
510 511 512 513


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

(defun math-known-num-integerp (a)
517
  (<= (math-possible-types a t) 3))
Eli Zaretskii's avatar
Eli Zaretskii committed
518 519

(defun math-known-imagp (a)
520
  (= (math-possible-types a) 16))
Eli Zaretskii's avatar
Eli Zaretskii committed
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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 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


;;; 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)
678
		 (t 63))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

(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)
697
	 (math-known-evenp (nth 1 a)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
698 699 700 701 702 703 704 705 706 707 708 709 710

(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)
711
	 (math-known-oddp (nth 1 a)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
712 713 714 715 716 717


(defun calcFunc-dreal (expr)
  (let ((types (math-possible-types expr)))
    (if (< types 16) 1
      (if (= (logand types 15) 0) 0
718
	(math-reject-arg expr 'realp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
719 720 721 722 723

(defun calcFunc-dimag (expr)
  (let ((types (math-possible-types expr)))
    (if (= types 16) 1
      (if (= (logand types 16) 0) 0
724
	(math-reject-arg expr "Expected an imaginary number")))))
Eli Zaretskii's avatar
Eli Zaretskii committed
725 726 727 728 729

(defun calcFunc-dpos (expr)
  (let ((signs (math-possible-signs expr)))
    (if (eq signs 4) 1
      (if (memq signs '(1 2 3)) 0
730
	(math-reject-arg expr 'posp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
731 732 733 734 735

(defun calcFunc-dneg (expr)
  (let ((signs (math-possible-signs expr)))
    (if (eq signs 1) 1
      (if (memq signs '(2 4 6)) 0
736
	(math-reject-arg expr 'negp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
737 738 739 740 741

(defun calcFunc-dnonneg (expr)
  (let ((signs (math-possible-signs expr)))
    (if (memq signs '(2 4 6)) 1
      (if (eq signs 1) 0
742
	(math-reject-arg expr 'posp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
743 744 745 746 747

(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
748
	(math-reject-arg expr 'nonzerop 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
749 750 751 752 753

(defun calcFunc-dint (expr)
  (let ((types (math-possible-types expr)))
    (if (= types 1) 1
      (if (= (logand types 1) 0) 0
754
	(math-reject-arg expr 'integerp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
755 756 757 758 759

(defun calcFunc-dnumint (expr)
  (let ((types (math-possible-types expr t)))
    (if (<= types 3) 1
      (if (= (logand types 3) 0) 0
760
	(math-reject-arg expr 'integerp 'quiet)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
761 762 763 764 765

(defun calcFunc-dnatnum (expr)
  (let ((res (calcFunc-dint expr)))
    (if (eq res 1)
	(calcFunc-dnonneg expr)
766
      res)))
Eli Zaretskii's avatar
Eli Zaretskii committed
767 768 769 770 771 772 773

(defun calcFunc-deven (expr)
  (if (math-known-evenp expr)
      1
    (if (or (math-known-oddp expr)
	    (= (logand (math-possible-types expr) 3) 0))
	0
774
      (math-reject-arg expr "Can't tell if expression is odd or even"))))
Eli Zaretskii's avatar
Eli Zaretskii committed
775 776 777 778 779 780 781

(defun calcFunc-dodd (expr)
  (if (math-known-oddp expr)
      1
    (if (or (math-known-evenp expr)
	    (= (logand (math-possible-types expr) 3) 0))
	0
782
      (math-reject-arg expr "Can't tell if expression is odd or even"))))
Eli Zaretskii's avatar
Eli Zaretskii committed
783 784 785 786 787

(defun calcFunc-drat (expr)
  (let ((types (math-possible-types expr)))
    (if (memq types '(1 4 5)) 1
      (if (= (logand types 5) 0) 0
788
	(math-reject-arg expr "Rational number expected")))))
Eli Zaretskii's avatar
Eli Zaretskii committed
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

(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)
814
	    (math-reject-arg expr 'realp 'quiet)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
815 816 817 818

(defun calcFunc-dscalar (a)
  (if (math-known-scalarp a) 1
    (if (math-known-matrixp a) 0
819
      (math-reject-arg a 'objectp 'quiet))))
Eli Zaretskii's avatar
Eli Zaretskii committed
820 821 822 823 824 825 826


;;; The following lists are not exhaustive.
(defvar math-scalar-functions '(calcFunc-det
				calcFunc-cnorm calcFunc-rnorm
				calcFunc-vlen calcFunc-vcount
				calcFunc-vsum calcFunc-vprod
827
				calcFunc-vmin calcFunc-vmax))
Eli Zaretskii's avatar
Eli Zaretskii committed
828 829 830 831 832 833

(defvar math-nonscalar-functions '(vec calcFunc-idn calcFunc-diag
				       calcFunc-cvec calcFunc-index
				       calcFunc-trn
				       | calcFunc-append
				       calcFunc-cons calcFunc-rcons
834
				       calcFunc-tail calcFunc-rhead))
Eli Zaretskii's avatar
Eli Zaretskii committed
835 836 837 838 839 840 841 842 843 844

(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
845
			      calcFunc-frounde calcFunc-froundu))
Eli Zaretskii's avatar
Eli Zaretskii committed
846

847
(defvar math-positive-functions '())
Eli Zaretskii's avatar
Eli Zaretskii committed
848 849

(defvar math-nonnegative-functions '(calcFunc-cnorm calcFunc-rnorm
850
				     calcFunc-vlen calcFunc-vcount))
Eli Zaretskii's avatar
Eli Zaretskii committed
851 852 853 854 855 856 857

(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
858
				       calcFunc-max calcFunc-min))
Eli Zaretskii's avatar
Eli Zaretskii committed
859 860 861 862 863

(defvar math-real-if-arg-functions '(calcFunc-sin calcFunc-cos
				     calcFunc-tan calcFunc-arctan
				     calcFunc-sinh calcFunc-cosh
				     calcFunc-tanh calcFunc-exp
864
				     calcFunc-gamma calcFunc-fact))
Eli Zaretskii's avatar
Eli Zaretskii committed
865 866 867

(defvar math-integer-functions '(calcFunc-idiv
				 calcFunc-isqrt calcFunc-ilog
868
				 calcFunc-vlen calcFunc-vcount))
Eli Zaretskii's avatar
Eli Zaretskii committed
869

870
(defvar math-num-integer-functions '())
Eli Zaretskii's avatar
Eli Zaretskii committed
871 872 873 874

(defvar math-rounding-functions '(calcFunc-floor
				  calcFunc-ceil
				  calcFunc-round calcFunc-trunc
875
				  calcFunc-rounde calcFunc-roundu))
Eli Zaretskii's avatar
Eli Zaretskii committed
876 877 878 879

(defvar math-float-rounding-functions '(calcFunc-ffloor
					calcFunc-fceil
					calcFunc-fround calcFunc-ftrunc
880
					calcFunc-frounde calcFunc-froundu))
Eli Zaretskii's avatar
Eli Zaretskii committed
881 882 883

(defvar math-integer-if-args-functions '(+ - * % neg calcFunc-abs
					   calcFunc-min calcFunc-max
884
					   calcFunc-choose calcFunc-perm))
Eli Zaretskii's avatar
Eli Zaretskii committed
885 886 887 888


;;;; Arithmetic.

889
(defsubst calcFunc-neg (a)
890
  (math-normalize (list 'neg a)))
Eli Zaretskii's avatar
Eli Zaretskii committed
891 892 893 894 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

(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))
936
	(t (list 'neg a))))
Eli Zaretskii's avatar
Eli Zaretskii committed
937 938 939

(defun math-okay-neg (a)
  (or (math-looks-negp a)
940
      (eq (car-safe a) '-)))
Eli Zaretskii's avatar
Eli Zaretskii committed
941 942

(defun math-neg-float (a)
943
  (list 'float (Math-integer-neg (nth 1 a)) (nth 2 a)))
Eli Zaretskii's avatar
Eli Zaretskii committed
944 945 946 947 948 949 950 951


(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))
952
    0))
Eli Zaretskii's avatar
Eli Zaretskii committed
953 954 955 956 957 958 959

(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))
960
    0))
Eli Zaretskii's avatar
Eli Zaretskii committed
961 962 963 964 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

(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))))))
1068
	(t (calc-record-why "*Incompatible arguments for +" a b))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1069 1070 1071 1072 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

(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)))))
1147
      (list '+ a b)))
Eli Zaretskii's avatar
Eli Zaretskii committed
1148 1149 1150 1151 1152 1153 1154 1155


(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))
1156
    1))
Eli Zaretskii's avatar
Eli Zaretskii committed
1157 1158 1159 1160 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

(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))
1255
	(t (calc-record-why "*Incompatible arguments for *" a b))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1256 1257 1258 1259

;;; 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))
1260
		   (+ (nth 2 a) (nth 2 b))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1261 1262 1263

(defun math-sqr-float (a)   ; [F F]
  (math-make-float (math-mul (nth 1 a) (nth 1 a))
1264
		   (+ (nth 2 a) (nth 2 a))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1265 1266 1267 1268 1269 1270 1271 1272 1273

(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)
1274
		    (memq (nth 1 a) '(0 2)))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301

(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)))
1302
	  (if (or (math-floatp a) (math-floatp b)) '(float 0 0) 0))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
1303 1304 1305 1306 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