Mercurial > emacs
annotate lisp/calc/calc-arith.el @ 58627:fee84e9454db
*** empty log message ***
author | Jay Belanger <jay.p.belanger@gmail.com> |
---|---|
date | Mon, 29 Nov 2004 22:50:05 +0000 |
parents | f6af195898b4 |
children | e655efbeebac |
rev | line source |
---|---|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
1 ;;; calc-arith.el --- arithmetic functions for Calc |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
2 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
4 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
6 ;; Maintainer: Jay Belanger <belanger@truman.edu> |
40785 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 ;; accepts responsibility to anyone for the consequences of using it | |
13 ;; or for whether it serves any particular purpose or works at all, | |
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 ;; License for full details. | |
16 | |
17 ;; Everyone is granted permission to copy, modify and redistribute | |
18 ;; GNU Emacs, but only under the conditions described in the | |
19 ;; GNU Emacs General Public License. A copy of this license is | |
20 ;; supposed to have been given to you along with GNU Emacs so you | |
21 ;; can know your rights and responsibilities. It should be in a | |
22 ;; file named COPYING. Among other things, the copyright notice | |
23 ;; and this notice must be preserved on all copies. | |
24 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
25 ;;; Commentary: |
40785 | 26 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
27 ;;; Code: |
40785 | 28 |
29 ;; This file is autoloaded from calc-ext.el. | |
30 (require 'calc-ext) | |
31 | |
32 (require 'calc-macs) | |
33 | |
34 (defun calc-Need-calc-arith () nil) | |
35 | |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
36 ;;; The following lists are not exhaustive. |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
37 (defvar math-scalar-functions '(calcFunc-det |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
38 calcFunc-cnorm calcFunc-rnorm |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
39 calcFunc-vlen calcFunc-vcount |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
40 calcFunc-vsum calcFunc-vprod |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
41 calcFunc-vmin calcFunc-vmax)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
42 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
43 (defvar math-nonscalar-functions '(vec calcFunc-idn calcFunc-diag |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
44 calcFunc-cvec calcFunc-index |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
45 calcFunc-trn |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
46 | calcFunc-append |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
47 calcFunc-cons calcFunc-rcons |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
48 calcFunc-tail calcFunc-rhead)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
49 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
50 (defvar math-scalar-if-args-functions '(+ - * / neg)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
51 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
52 (defvar math-real-functions '(calcFunc-arg |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
53 calcFunc-re calcFunc-im |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
54 calcFunc-floor calcFunc-ceil |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
55 calcFunc-trunc calcFunc-round |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
56 calcFunc-rounde calcFunc-roundu |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
57 calcFunc-ffloor calcFunc-fceil |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
58 calcFunc-ftrunc calcFunc-fround |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
59 calcFunc-frounde calcFunc-froundu)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
60 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
61 (defvar math-positive-functions '()) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
62 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
63 (defvar math-nonnegative-functions '(calcFunc-cnorm calcFunc-rnorm |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
64 calcFunc-vlen calcFunc-vcount)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
65 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
66 (defvar math-real-scalar-functions '(% calcFunc-idiv calcFunc-abs |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
67 calcFunc-choose calcFunc-perm |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
68 calcFunc-eq calcFunc-neq |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
69 calcFunc-lt calcFunc-gt |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
70 calcFunc-leq calcFunc-geq |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
71 calcFunc-lnot |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
72 calcFunc-max calcFunc-min)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
73 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
74 (defvar math-real-if-arg-functions '(calcFunc-sin calcFunc-cos |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
75 calcFunc-tan calcFunc-arctan |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
76 calcFunc-sinh calcFunc-cosh |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
77 calcFunc-tanh calcFunc-exp |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
78 calcFunc-gamma calcFunc-fact)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
79 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
80 (defvar math-integer-functions '(calcFunc-idiv |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
81 calcFunc-isqrt calcFunc-ilog |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
82 calcFunc-vlen calcFunc-vcount)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
83 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
84 (defvar math-num-integer-functions '()) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
85 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
86 (defvar math-rounding-functions '(calcFunc-floor |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
87 calcFunc-ceil |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
88 calcFunc-round calcFunc-trunc |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
89 calcFunc-rounde calcFunc-roundu)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
90 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
91 (defvar math-float-rounding-functions '(calcFunc-ffloor |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
92 calcFunc-fceil |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
93 calcFunc-fround calcFunc-ftrunc |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
94 calcFunc-frounde calcFunc-froundu)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
95 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
96 (defvar math-integer-if-args-functions '(+ - * % neg calcFunc-abs |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
97 calcFunc-min calcFunc-max |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
98 calcFunc-choose calcFunc-perm)) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
99 |
40785 | 100 |
101 ;;; Arithmetic. | |
102 | |
103 (defun calc-min (arg) | |
104 (interactive "P") | |
105 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
106 (calc-binary-op "min" 'calcFunc-min arg '(var inf var-inf)))) |
40785 | 107 |
108 (defun calc-max (arg) | |
109 (interactive "P") | |
110 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
111 (calc-binary-op "max" 'calcFunc-max arg '(neg (var inf var-inf))))) |
40785 | 112 |
113 (defun calc-abs (arg) | |
114 (interactive "P") | |
115 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
116 (calc-unary-op "abs" 'calcFunc-abs arg))) |
40785 | 117 |
118 | |
119 (defun calc-idiv (arg) | |
120 (interactive "P") | |
121 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
122 (calc-binary-op "\\" 'calcFunc-idiv arg 1))) |
40785 | 123 |
124 | |
125 (defun calc-floor (arg) | |
126 (interactive "P") | |
127 (calc-slow-wrapper | |
128 (if (calc-is-inverse) | |
129 (if (calc-is-hyperbolic) | |
130 (calc-unary-op "ceil" 'calcFunc-fceil arg) | |
131 (calc-unary-op "ceil" 'calcFunc-ceil arg)) | |
132 (if (calc-is-hyperbolic) | |
133 (calc-unary-op "flor" 'calcFunc-ffloor arg) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
134 (calc-unary-op "flor" 'calcFunc-floor arg))))) |
40785 | 135 |
136 (defun calc-ceiling (arg) | |
137 (interactive "P") | |
138 (calc-invert-func) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
139 (calc-floor arg)) |
40785 | 140 |
141 (defun calc-round (arg) | |
142 (interactive "P") | |
143 (calc-slow-wrapper | |
144 (if (calc-is-inverse) | |
145 (if (calc-is-hyperbolic) | |
146 (calc-unary-op "trnc" 'calcFunc-ftrunc arg) | |
147 (calc-unary-op "trnc" 'calcFunc-trunc arg)) | |
148 (if (calc-is-hyperbolic) | |
149 (calc-unary-op "rond" 'calcFunc-fround arg) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
150 (calc-unary-op "rond" 'calcFunc-round arg))))) |
40785 | 151 |
152 (defun calc-trunc (arg) | |
153 (interactive "P") | |
154 (calc-invert-func) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
155 (calc-round arg)) |
40785 | 156 |
157 (defun calc-mant-part (arg) | |
158 (interactive "P") | |
159 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
160 (calc-unary-op "mant" 'calcFunc-mant arg))) |
40785 | 161 |
162 (defun calc-xpon-part (arg) | |
163 (interactive "P") | |
164 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
165 (calc-unary-op "xpon" 'calcFunc-xpon arg))) |
40785 | 166 |
167 (defun calc-scale-float (arg) | |
168 (interactive "P") | |
169 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
170 (calc-binary-op "scal" 'calcFunc-scf arg))) |
40785 | 171 |
172 (defun calc-abssqr (arg) | |
173 (interactive "P") | |
174 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
175 (calc-unary-op "absq" 'calcFunc-abssqr arg))) |
40785 | 176 |
177 (defun calc-sign (arg) | |
178 (interactive "P") | |
179 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
180 (calc-unary-op "sign" 'calcFunc-sign arg))) |
40785 | 181 |
182 (defun calc-increment (arg) | |
183 (interactive "p") | |
184 (calc-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
185 (calc-enter-result 1 "incr" (list 'calcFunc-incr (calc-top-n 1) arg)))) |
40785 | 186 |
187 (defun calc-decrement (arg) | |
188 (interactive "p") | |
189 (calc-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
190 (calc-enter-result 1 "decr" (list 'calcFunc-decr (calc-top-n 1) arg)))) |
40785 | 191 |
192 | |
193 (defun math-abs-approx (a) | |
194 (cond ((Math-negp a) | |
195 (math-neg a)) | |
196 ((Math-anglep a) | |
197 a) | |
198 ((eq (car a) 'cplx) | |
199 (math-add (math-abs (nth 1 a)) (math-abs (nth 2 a)))) | |
200 ((eq (car a) 'polar) | |
201 (nth 1 a)) | |
202 ((eq (car a) 'sdev) | |
203 (math-abs-approx (nth 1 a))) | |
204 ((eq (car a) 'intv) | |
205 (math-max (math-abs (nth 2 a)) (math-abs (nth 3 a)))) | |
206 ((eq (car a) 'date) | |
207 a) | |
208 ((eq (car a) 'vec) | |
209 (math-reduce-vec 'math-add-abs-approx a)) | |
210 ((eq (car a) 'calcFunc-abs) | |
211 (car a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
212 (t a))) |
40785 | 213 |
214 (defun math-add-abs-approx (a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
215 (math-add (math-abs-approx a) (math-abs-approx b))) |
40785 | 216 |
217 | |
218 ;;;; Declarations. | |
219 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
220 (defvar math-decls-cache-tag nil) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
221 (defvar math-decls-cache nil) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
222 (defvar math-decls-all nil) |
40785 | 223 |
224 ;;; Math-decls-cache is an a-list where each entry is a list of the form: | |
225 ;;; (VAR TYPES RANGE) | |
226 ;;; where VAR is a variable name (with var- prefix) or function name; | |
227 ;;; TYPES is a list of type symbols (any, int, frac, ...) | |
228 ;;; RANGE is a sorted vector of intervals describing the range. | |
229 | |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
230 (defvar math-super-types |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
231 '((int numint rat real number) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
232 (numint real number) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
233 (frac rat real number) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
234 (rat real number) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
235 (float real number) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
236 (real number) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
237 (number) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
238 (scalar) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
239 (matrix vector) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
240 (vector) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
241 (const))) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
242 |
40785 | 243 (defun math-setup-declarations () |
244 (or (eq math-decls-cache-tag (calc-var-value 'var-Decls)) | |
245 (let ((p (calc-var-value 'var-Decls)) | |
246 vec type range) | |
247 (setq math-decls-cache-tag p | |
248 math-decls-cache nil) | |
249 (and (eq (car-safe p) 'vec) | |
250 (while (setq p (cdr p)) | |
251 (and (eq (car-safe (car p)) 'vec) | |
252 (setq vec (nth 2 (car p))) | |
253 (condition-case err | |
254 (let ((v (nth 1 (car p)))) | |
255 (setq type nil range nil) | |
256 (or (eq (car-safe vec) 'vec) | |
257 (setq vec (list 'vec vec))) | |
258 (while (and (setq vec (cdr vec)) | |
259 (not (Math-objectp (car vec)))) | |
260 (and (eq (car-safe (car vec)) 'var) | |
261 (let ((st (assq (nth 1 (car vec)) | |
262 math-super-types))) | |
263 (cond (st (setq type (append type st))) | |
264 ((eq (nth 1 (car vec)) 'pos) | |
265 (setq type (append type | |
266 '(real number)) | |
267 range | |
268 '(intv 1 0 (var inf var-inf)))) | |
269 ((eq (nth 1 (car vec)) 'nonneg) | |
270 (setq type (append type | |
271 '(real number)) | |
272 range | |
273 '(intv 3 0 | |
274 (var inf var-inf)))))))) | |
275 (if vec | |
276 (setq type (append type '(real number)) | |
277 range (math-prepare-set (cons 'vec vec)))) | |
278 (setq type (list type range)) | |
279 (or (eq (car-safe v) 'vec) | |
280 (setq v (list 'vec v))) | |
281 (while (setq v (cdr v)) | |
282 (if (or (eq (car-safe (car v)) 'var) | |
283 (not (Math-primp (car v)))) | |
284 (setq math-decls-cache | |
285 (cons (cons (if (eq (car (car v)) 'var) | |
286 (nth 2 (car v)) | |
287 (car (car v))) | |
288 type) | |
289 math-decls-cache))))) | |
290 (error nil))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
291 (setq math-decls-all (assq 'var-All math-decls-cache))))) |
40785 | 292 |
293 (defun math-known-scalarp (a &optional assume-scalar) | |
294 (math-setup-declarations) | |
295 (if (if calc-matrix-mode | |
296 (eq calc-matrix-mode 'scalar) | |
297 assume-scalar) | |
298 (not (math-check-known-matrixp a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
299 (math-check-known-scalarp a))) |
40785 | 300 |
301 (defun math-known-matrixp (a) | |
302 (and (not (Math-scalarp a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
303 (not (math-known-scalarp a t)))) |
40785 | 304 |
305 ;;; Try to prove that A is a scalar (i.e., a non-vector). | |
306 (defun math-check-known-scalarp (a) | |
307 (cond ((Math-objectp a) t) | |
308 ((memq (car a) math-scalar-functions) | |
309 t) | |
310 ((memq (car a) math-real-scalar-functions) | |
311 t) | |
312 ((memq (car a) math-scalar-if-args-functions) | |
313 (while (and (setq a (cdr a)) | |
314 (math-check-known-scalarp (car a)))) | |
315 (null a)) | |
316 ((eq (car a) '^) | |
317 (math-check-known-scalarp (nth 1 a))) | |
318 ((math-const-var a) t) | |
319 (t | |
320 (let ((decl (if (eq (car a) 'var) | |
321 (or (assq (nth 2 a) math-decls-cache) | |
322 math-decls-all) | |
323 (assq (car a) math-decls-cache)))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
324 (memq 'scalar (nth 1 decl)))))) |
40785 | 325 |
326 ;;; Try to prove that A is *not* a scalar. | |
327 (defun math-check-known-matrixp (a) | |
328 (cond ((Math-objectp a) nil) | |
329 ((memq (car a) math-nonscalar-functions) | |
330 t) | |
331 ((memq (car a) math-scalar-if-args-functions) | |
332 (while (and (setq a (cdr a)) | |
333 (not (math-check-known-matrixp (car a))))) | |
334 a) | |
335 ((eq (car a) '^) | |
336 (math-check-known-matrixp (nth 1 a))) | |
337 ((math-const-var a) nil) | |
338 (t | |
339 (let ((decl (if (eq (car a) 'var) | |
340 (or (assq (nth 2 a) math-decls-cache) | |
341 math-decls-all) | |
342 (assq (car a) math-decls-cache)))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
343 (memq 'vector (nth 1 decl)))))) |
40785 | 344 |
345 | |
346 ;;; Try to prove that A is a real (i.e., not complex). | |
347 (defun math-known-realp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
348 (< (math-possible-signs a) 8)) |
40785 | 349 |
350 ;;; Try to prove that A is real and positive. | |
351 (defun math-known-posp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
352 (eq (math-possible-signs a) 4)) |
40785 | 353 |
354 ;;; Try to prove that A is real and negative. | |
355 (defun math-known-negp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
356 (eq (math-possible-signs a) 1)) |
40785 | 357 |
358 ;;; Try to prove that A is real and nonnegative. | |
359 (defun math-known-nonnegp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
360 (memq (math-possible-signs a) '(2 4 6))) |
40785 | 361 |
362 ;;; Try to prove that A is real and nonpositive. | |
363 (defun math-known-nonposp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
364 (memq (math-possible-signs a) '(1 2 3))) |
40785 | 365 |
366 ;;; Try to prove that A is nonzero. | |
367 (defun math-known-nonzerop (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
368 (memq (math-possible-signs a) '(1 4 5 8 9 12 13))) |
40785 | 369 |
370 ;;; Return true if A is negative, or looks negative but we don't know. | |
371 (defun math-guess-if-neg (a) | |
372 (let ((sgn (math-possible-signs a))) | |
373 (if (memq sgn '(1 3)) | |
374 t | |
375 (if (memq sgn '(2 4 6)) | |
376 nil | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
377 (math-looks-negp a))))) |
40785 | 378 |
379 ;;; Find the possible signs of A, assuming A is a number of some kind. | |
380 ;;; Returns an integer with bits: 1 may be negative, | |
381 ;;; 2 may be zero, | |
382 ;;; 4 may be positive, | |
383 ;;; 8 may be nonreal. | |
384 | |
385 (defun math-possible-signs (a &optional origin) | |
386 (cond ((Math-objectp a) | |
387 (if origin (setq a (math-sub a origin))) | |
388 (cond ((Math-posp a) 4) | |
389 ((Math-negp a) 1) | |
390 ((Math-zerop a) 2) | |
391 ((eq (car a) 'intv) | |
392 (cond ((Math-zerop (nth 2 a)) 6) | |
393 ((Math-zerop (nth 3 a)) 3) | |
394 (t 7))) | |
395 ((eq (car a) 'sdev) | |
396 (if (math-known-realp (nth 1 a)) 7 15)) | |
397 (t 8))) | |
398 ((memq (car a) '(+ -)) | |
399 (cond ((Math-realp (nth 1 a)) | |
400 (if (eq (car a) '-) | |
401 (math-neg-signs | |
402 (math-possible-signs (nth 2 a) | |
403 (if origin | |
404 (math-add origin (nth 1 a)) | |
405 (nth 1 a)))) | |
406 (math-possible-signs (nth 2 a) | |
407 (if origin | |
408 (math-sub origin (nth 1 a)) | |
409 (math-neg (nth 1 a)))))) | |
410 ((Math-realp (nth 2 a)) | |
411 (let ((org (if (eq (car a) '-) | |
412 (nth 2 a) | |
413 (math-neg (nth 2 a))))) | |
414 (math-possible-signs (nth 1 a) | |
415 (if origin | |
416 (math-add origin org) | |
417 org)))) | |
418 (t | |
419 (let ((s1 (math-possible-signs (nth 1 a) origin)) | |
420 (s2 (math-possible-signs (nth 2 a)))) | |
421 (if (eq (car a) '-) (setq s2 (math-neg-signs s2))) | |
422 (cond ((eq s1 s2) s1) | |
423 ((eq s1 2) s2) | |
424 ((eq s2 2) s1) | |
425 ((>= s1 8) 15) | |
426 ((>= s2 8) 15) | |
427 ((and (eq s1 4) (eq s2 6)) 4) | |
428 ((and (eq s2 4) (eq s1 6)) 4) | |
429 ((and (eq s1 1) (eq s2 3)) 1) | |
430 ((and (eq s2 1) (eq s1 3)) 1) | |
431 (t 7)))))) | |
432 ((eq (car a) 'neg) | |
433 (math-neg-signs (math-possible-signs | |
434 (nth 1 a) | |
435 (and origin (math-neg origin))))) | |
436 ((and origin (Math-zerop origin) (setq origin nil) | |
437 nil)) | |
438 ((and (or (eq (car a) '*) | |
439 (and (eq (car a) '/) origin)) | |
440 (Math-realp (nth 1 a))) | |
441 (let ((s (if (eq (car a) '*) | |
442 (if (Math-zerop (nth 1 a)) | |
443 (math-possible-signs 0 origin) | |
444 (math-possible-signs (nth 2 a) | |
445 (math-div (or origin 0) | |
446 (nth 1 a)))) | |
447 (math-neg-signs | |
448 (math-possible-signs (nth 2 a) | |
449 (math-div (nth 1 a) | |
450 origin)))))) | |
451 (if (Math-negp (nth 1 a)) (math-neg-signs s) s))) | |
452 ((and (memq (car a) '(* /)) (Math-realp (nth 2 a))) | |
453 (let ((s (math-possible-signs (nth 1 a) | |
454 (if (eq (car a) '*) | |
455 (math-mul (or origin 0) (nth 2 a)) | |
456 (math-div (or origin 0) (nth 2 a)))))) | |
457 (if (Math-negp (nth 2 a)) (math-neg-signs s) s))) | |
458 ((eq (car a) 'vec) | |
459 (let ((signs 0)) | |
460 (while (and (setq a (cdr a)) (< signs 15)) | |
461 (setq signs (logior signs (math-possible-signs | |
462 (car a) origin)))) | |
463 signs)) | |
464 (t (let ((sign | |
465 (cond | |
466 ((memq (car a) '(* /)) | |
467 (let ((s1 (math-possible-signs (nth 1 a))) | |
468 (s2 (math-possible-signs (nth 2 a)))) | |
469 (cond ((>= s1 8) 15) | |
470 ((>= s2 8) 15) | |
471 ((and (eq (car a) '/) (memq s2 '(2 3 6 7))) 15) | |
472 (t | |
473 (logior (if (memq s1 '(4 5 6 7)) s2 0) | |
474 (if (memq s1 '(2 3 6 7)) 2 0) | |
475 (if (memq s1 '(1 3 5 7)) | |
476 (math-neg-signs s2) 0)))))) | |
477 ((eq (car a) '^) | |
478 (let ((s1 (math-possible-signs (nth 1 a))) | |
479 (s2 (math-possible-signs (nth 2 a)))) | |
480 (cond ((>= s1 8) 15) | |
481 ((>= s2 8) 15) | |
482 ((eq s1 4) 4) | |
483 ((eq s1 2) (if (eq s2 4) 2 15)) | |
484 ((eq s2 2) (if (memq s1 '(1 5)) 2 15)) | |
485 ((Math-integerp (nth 2 a)) | |
486 (if (math-evenp (nth 2 a)) | |
487 (if (memq s1 '(3 6 7)) 6 4) | |
488 s1)) | |
489 ((eq s1 6) (if (eq s2 4) 6 15)) | |
490 (t 7)))) | |
491 ((eq (car a) '%) | |
492 (let ((s2 (math-possible-signs (nth 2 a)))) | |
493 (cond ((>= s2 8) 7) | |
494 ((eq s2 2) 2) | |
495 ((memq s2 '(4 6)) 6) | |
496 ((memq s2 '(1 3)) 3) | |
497 (t 7)))) | |
498 ((and (memq (car a) '(calcFunc-abs calcFunc-abssqr)) | |
499 (= (length a) 2)) | |
500 (let ((s1 (math-possible-signs (nth 1 a)))) | |
501 (cond ((eq s1 2) 2) | |
502 ((memq s1 '(1 4 5)) 4) | |
503 (t 6)))) | |
504 ((and (eq (car a) 'calcFunc-exp) (= (length a) 2)) | |
505 (let ((s1 (math-possible-signs (nth 1 a)))) | |
506 (if (>= s1 8) | |
507 15 | |
508 (if (or (not origin) (math-negp origin)) | |
509 4 | |
510 (setq origin (math-sub (or origin 0) 1)) | |
511 (if (Math-zerop origin) (setq origin nil)) | |
512 s1)))) | |
513 ((or (and (memq (car a) '(calcFunc-ln calcFunc-log10)) | |
514 (= (length a) 2)) | |
515 (and (eq (car a) 'calcFunc-log) | |
516 (= (length a) 3) | |
517 (math-known-posp (nth 2 a)))) | |
518 (if (math-known-nonnegp (nth 1 a)) | |
519 (math-possible-signs (nth 1 a) 1) | |
520 15)) | |
521 ((and (eq (car a) 'calcFunc-sqrt) (= (length a) 2)) | |
522 (let ((s1 (math-possible-signs (nth 1 a)))) | |
523 (if (memq s1 '(2 4 6)) s1 15))) | |
524 ((memq (car a) math-nonnegative-functions) 6) | |
525 ((memq (car a) math-positive-functions) 4) | |
526 ((memq (car a) math-real-functions) 7) | |
527 ((memq (car a) math-real-scalar-functions) 7) | |
528 ((and (memq (car a) math-real-if-arg-functions) | |
529 (= (length a) 2)) | |
530 (if (math-known-realp (nth 1 a)) 7 15))))) | |
531 (cond (sign | |
532 (if origin | |
533 (+ (logand sign 8) | |
534 (if (Math-posp origin) | |
535 (if (memq sign '(1 2 3 8 9 10 11)) 1 7) | |
536 (if (memq sign '(2 4 6 8 10 12 14)) 4 7))) | |
537 sign)) | |
538 ((math-const-var a) | |
539 (cond ((eq (nth 2 a) 'var-pi) | |
540 (if origin | |
541 (math-possible-signs (math-pi) origin) | |
542 4)) | |
543 ((eq (nth 2 a) 'var-e) | |
544 (if origin | |
545 (math-possible-signs (math-e) origin) | |
546 4)) | |
547 ((eq (nth 2 a) 'var-inf) 4) | |
548 ((eq (nth 2 a) 'var-uinf) 13) | |
549 ((eq (nth 2 a) 'var-i) 8) | |
550 (t 15))) | |
551 (t | |
552 (math-setup-declarations) | |
553 (let ((decl (if (eq (car a) 'var) | |
554 (or (assq (nth 2 a) math-decls-cache) | |
555 math-decls-all) | |
556 (assq (car a) math-decls-cache)))) | |
557 (if (and origin | |
558 (memq 'int (nth 1 decl)) | |
559 (not (Math-num-integerp origin))) | |
560 5 | |
561 (if (nth 2 decl) | |
562 (math-possible-signs (nth 2 decl) origin) | |
563 (if (memq 'real (nth 1 decl)) | |
564 7 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
565 15)))))))))) |
40785 | 566 |
567 (defun math-neg-signs (s1) | |
568 (if (>= s1 8) | |
569 (+ 8 (math-neg-signs (- s1 8))) | |
570 (+ (if (memq s1 '(1 3 5 7)) 4 0) | |
571 (if (memq s1 '(2 3 6 7)) 2 0) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
572 (if (memq s1 '(4 5 6 7)) 1 0)))) |
40785 | 573 |
574 | |
575 ;;; Try to prove that A is an integer. | |
576 (defun math-known-integerp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
577 (eq (math-possible-types a) 1)) |
40785 | 578 |
579 (defun math-known-num-integerp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
580 (<= (math-possible-types a t) 3)) |
40785 | 581 |
582 (defun math-known-imagp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
583 (= (math-possible-types a) 16)) |
40785 | 584 |
585 | |
586 ;;; Find the possible types of A. | |
587 ;;; Returns an integer with bits: 1 may be integer. | |
588 ;;; 2 may be integer-valued float. | |
589 ;;; 4 may be fraction. | |
590 ;;; 8 may be non-integer-valued float. | |
591 ;;; 16 may be imaginary. | |
592 ;;; 32 may be non-real, non-imaginary. | |
593 ;;; Real infinities count as integers for the purposes of this function. | |
594 (defun math-possible-types (a &optional num) | |
595 (cond ((Math-objectp a) | |
596 (cond ((Math-integerp a) (if num 3 1)) | |
597 ((Math-messy-integerp a) (if num 3 2)) | |
598 ((eq (car a) 'frac) (if num 12 4)) | |
599 ((eq (car a) 'float) (if num 12 8)) | |
600 ((eq (car a) 'intv) | |
601 (if (equal (nth 2 a) (nth 3 a)) | |
602 (math-possible-types (nth 2 a)) | |
603 15)) | |
604 ((eq (car a) 'sdev) | |
605 (if (math-known-realp (nth 1 a)) 15 63)) | |
606 ((eq (car a) 'cplx) | |
607 (if (math-zerop (nth 1 a)) 16 32)) | |
608 ((eq (car a) 'polar) | |
609 (if (or (Math-equal (nth 2 a) (math-quarter-circle nil)) | |
610 (Math-equal (nth 2 a) | |
611 (math-neg (math-quarter-circle nil)))) | |
612 16 48)) | |
613 (t 63))) | |
614 ((eq (car a) '/) | |
615 (let* ((t1 (math-possible-types (nth 1 a) num)) | |
616 (t2 (math-possible-types (nth 2 a) num)) | |
617 (t12 (logior t1 t2))) | |
618 (if (< t12 16) | |
619 (if (> (logand t12 10) 0) | |
620 10 | |
621 (if (or (= t1 4) (= t2 4) calc-prefer-frac) | |
622 5 | |
623 15)) | |
624 (if (< t12 32) | |
625 (if (= t1 16) | |
626 (if (= t2 16) 15 | |
627 (if (< t2 16) 16 31)) | |
628 (if (= t2 16) | |
629 (if (< t1 16) 16 31) | |
630 31)) | |
631 63)))) | |
632 ((memq (car a) '(+ - * %)) | |
633 (let* ((t1 (math-possible-types (nth 1 a) num)) | |
634 (t2 (math-possible-types (nth 2 a) num)) | |
635 (t12 (logior t1 t2))) | |
636 (if (eq (car a) '%) | |
637 (setq t1 (logand t1 15) t2 (logand t2 15) t12 (logand t12 15))) | |
638 (if (< t12 16) | |
639 (let ((mask (if (<= t12 3) | |
640 1 | |
641 (if (and (or (and (<= t1 3) (= (logand t2 3) 0)) | |
642 (and (<= t2 3) (= (logand t1 3) 0))) | |
643 (memq (car a) '(+ -))) | |
644 4 | |
645 5)))) | |
646 (if num | |
647 (* mask 3) | |
648 (logior (if (and (> (logand t1 5) 0) (> (logand t2 5) 0)) | |
649 mask 0) | |
650 (if (> (logand t12 10) 0) | |
651 (* mask 2) 0)))) | |
652 (if (< t12 32) | |
653 (if (eq (car a) '*) | |
654 (if (= t1 16) | |
655 (if (= t2 16) 15 | |
656 (if (< t2 16) 16 31)) | |
657 (if (= t2 16) | |
658 (if (< t1 16) 16 31) | |
659 31)) | |
660 (if (= t12 16) 16 | |
661 (if (or (and (= t1 16) (< t2 16)) | |
662 (and (= t2 16) (< t1 16))) 32 63))) | |
663 63)))) | |
664 ((eq (car a) 'neg) | |
665 (math-possible-types (nth 1 a))) | |
666 ((eq (car a) '^) | |
667 (let* ((t1 (math-possible-types (nth 1 a) num)) | |
668 (t2 (math-possible-types (nth 2 a) num)) | |
669 (t12 (logior t1 t2))) | |
670 (if (and (<= t2 3) (math-known-nonnegp (nth 2 a)) (< t1 16)) | |
671 (let ((mask (logior (if (> (logand t1 3) 0) 1 0) | |
672 (logand t1 4) | |
673 (if (> (logand t1 12) 0) 5 0)))) | |
674 (if num | |
675 (* mask 3) | |
676 (logior (if (and (> (logand t1 5) 0) (> (logand t2 5) 0)) | |
677 mask 0) | |
678 (if (> (logand t12 10) 0) | |
679 (* mask 2) 0)))) | |
680 (if (and (math-known-nonnegp (nth 1 a)) | |
681 (math-known-posp (nth 2 a))) | |
682 15 | |
683 63)))) | |
684 ((eq (car a) 'calcFunc-sqrt) | |
685 (let ((t1 (math-possible-signs (nth 1 a)))) | |
686 (logior (if (> (logand t1 2) 0) 3 0) | |
687 (if (> (logand t1 1) 0) 16 0) | |
688 (if (> (logand t1 4) 0) 15 0) | |
689 (if (> (logand t1 8) 0) 32 0)))) | |
690 ((eq (car a) 'vec) | |
691 (let ((types 0)) | |
692 (while (and (setq a (cdr a)) (< types 63)) | |
693 (setq types (logior types (math-possible-types (car a) t)))) | |
694 types)) | |
695 ((or (memq (car a) math-integer-functions) | |
696 (and (memq (car a) math-rounding-functions) | |
697 (math-known-nonnegp (or (nth 2 a) 0)))) | |
698 1) | |
699 ((or (memq (car a) math-num-integer-functions) | |
700 (and (memq (car a) math-float-rounding-functions) | |
701 (math-known-nonnegp (or (nth 2 a) 0)))) | |
702 2) | |
703 ((eq (car a) 'calcFunc-frac) | |
704 5) | |
705 ((and (eq (car a) 'calcFunc-float) (= (length a) 2)) | |
706 (let ((t1 (math-possible-types (nth 1 a)))) | |
707 (logior (if (> (logand t1 3) 0) 2 0) | |
708 (if (> (logand t1 12) 0) 8 0) | |
709 (logand t1 48)))) | |
710 ((and (memq (car a) '(calcFunc-abs calcFunc-abssqr)) | |
711 (= (length a) 2)) | |
712 (let ((t1 (math-possible-types (nth 1 a)))) | |
713 (if (>= t1 16) | |
714 15 | |
715 t1))) | |
716 ((math-const-var a) | |
717 (cond ((memq (nth 2 a) '(var-e var-pi var-phi var-gamma)) 8) | |
718 ((eq (nth 2 a) 'var-inf) 1) | |
719 ((eq (nth 2 a) 'var-i) 16) | |
720 (t 63))) | |
721 (t | |
722 (math-setup-declarations) | |
723 (let ((decl (if (eq (car a) 'var) | |
724 (or (assq (nth 2 a) math-decls-cache) | |
725 math-decls-all) | |
726 (assq (car a) math-decls-cache)))) | |
727 (cond ((memq 'int (nth 1 decl)) | |
728 1) | |
729 ((memq 'numint (nth 1 decl)) | |
730 3) | |
731 ((memq 'frac (nth 1 decl)) | |
732 4) | |
733 ((memq 'rat (nth 1 decl)) | |
734 5) | |
735 ((memq 'float (nth 1 decl)) | |
736 10) | |
737 ((nth 2 decl) | |
738 (math-possible-types (nth 2 decl))) | |
739 ((memq 'real (nth 1 decl)) | |
740 15) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
741 (t 63)))))) |
40785 | 742 |
743 (defun math-known-evenp (a) | |
744 (cond ((Math-integerp a) | |
745 (math-evenp a)) | |
746 ((Math-messy-integerp a) | |
747 (or (> (nth 2 a) 0) | |
748 (math-evenp (math-trunc a)))) | |
749 ((eq (car a) '*) | |
750 (if (math-known-evenp (nth 1 a)) | |
751 (math-known-num-integerp (nth 2 a)) | |
752 (if (math-known-num-integerp (nth 1 a)) | |
753 (math-known-evenp (nth 2 a))))) | |
754 ((memq (car a) '(+ -)) | |
755 (or (and (math-known-evenp (nth 1 a)) | |
756 (math-known-evenp (nth 2 a))) | |
757 (and (math-known-oddp (nth 1 a)) | |
758 (math-known-oddp (nth 2 a))))) | |
759 ((eq (car a) 'neg) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
760 (math-known-evenp (nth 1 a))))) |
40785 | 761 |
762 (defun math-known-oddp (a) | |
763 (cond ((Math-integerp a) | |
764 (math-oddp a)) | |
765 ((Math-messy-integerp a) | |
766 (and (<= (nth 2 a) 0) | |
767 (math-oddp (math-trunc a)))) | |
768 ((memq (car a) '(+ -)) | |
769 (or (and (math-known-evenp (nth 1 a)) | |
770 (math-known-oddp (nth 2 a))) | |
771 (and (math-known-oddp (nth 1 a)) | |
772 (math-known-evenp (nth 2 a))))) | |
773 ((eq (car a) 'neg) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
774 (math-known-oddp (nth 1 a))))) |
40785 | 775 |
776 | |
777 (defun calcFunc-dreal (expr) | |
778 (let ((types (math-possible-types expr))) | |
779 (if (< types 16) 1 | |
780 (if (= (logand types 15) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
781 (math-reject-arg expr 'realp 'quiet))))) |
40785 | 782 |
783 (defun calcFunc-dimag (expr) | |
784 (let ((types (math-possible-types expr))) | |
785 (if (= types 16) 1 | |
786 (if (= (logand types 16) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
787 (math-reject-arg expr "Expected an imaginary number"))))) |
40785 | 788 |
789 (defun calcFunc-dpos (expr) | |
790 (let ((signs (math-possible-signs expr))) | |
791 (if (eq signs 4) 1 | |
792 (if (memq signs '(1 2 3)) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
793 (math-reject-arg expr 'posp 'quiet))))) |
40785 | 794 |
795 (defun calcFunc-dneg (expr) | |
796 (let ((signs (math-possible-signs expr))) | |
797 (if (eq signs 1) 1 | |
798 (if (memq signs '(2 4 6)) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
799 (math-reject-arg expr 'negp 'quiet))))) |
40785 | 800 |
801 (defun calcFunc-dnonneg (expr) | |
802 (let ((signs (math-possible-signs expr))) | |
803 (if (memq signs '(2 4 6)) 1 | |
804 (if (eq signs 1) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
805 (math-reject-arg expr 'posp 'quiet))))) |
40785 | 806 |
807 (defun calcFunc-dnonzero (expr) | |
808 (let ((signs (math-possible-signs expr))) | |
809 (if (memq signs '(1 4 5 8 9 12 13)) 1 | |
810 (if (eq signs 2) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
811 (math-reject-arg expr 'nonzerop 'quiet))))) |
40785 | 812 |
813 (defun calcFunc-dint (expr) | |
814 (let ((types (math-possible-types expr))) | |
815 (if (= types 1) 1 | |
816 (if (= (logand types 1) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
817 (math-reject-arg expr 'integerp 'quiet))))) |
40785 | 818 |
819 (defun calcFunc-dnumint (expr) | |
820 (let ((types (math-possible-types expr t))) | |
821 (if (<= types 3) 1 | |
822 (if (= (logand types 3) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
823 (math-reject-arg expr 'integerp 'quiet))))) |
40785 | 824 |
825 (defun calcFunc-dnatnum (expr) | |
826 (let ((res (calcFunc-dint expr))) | |
827 (if (eq res 1) | |
828 (calcFunc-dnonneg expr) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
829 res))) |
40785 | 830 |
831 (defun calcFunc-deven (expr) | |
832 (if (math-known-evenp expr) | |
833 1 | |
834 (if (or (math-known-oddp expr) | |
835 (= (logand (math-possible-types expr) 3) 0)) | |
836 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
837 (math-reject-arg expr "Can't tell if expression is odd or even")))) |
40785 | 838 |
839 (defun calcFunc-dodd (expr) | |
840 (if (math-known-oddp expr) | |
841 1 | |
842 (if (or (math-known-evenp expr) | |
843 (= (logand (math-possible-types expr) 3) 0)) | |
844 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
845 (math-reject-arg expr "Can't tell if expression is odd or even")))) |
40785 | 846 |
847 (defun calcFunc-drat (expr) | |
848 (let ((types (math-possible-types expr))) | |
849 (if (memq types '(1 4 5)) 1 | |
850 (if (= (logand types 5) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
851 (math-reject-arg expr "Rational number expected"))))) |
40785 | 852 |
853 (defun calcFunc-drange (expr) | |
854 (math-setup-declarations) | |
855 (let (range) | |
856 (if (Math-realp expr) | |
857 (list 'vec expr) | |
858 (if (eq (car-safe expr) 'intv) | |
859 expr | |
860 (if (eq (car-safe expr) 'var) | |
861 (setq range (nth 2 (or (assq (nth 2 expr) math-decls-cache) | |
862 math-decls-all))) | |
863 (setq range (nth 2 (assq (car-safe expr) math-decls-cache)))) | |
864 (if range | |
865 (math-clean-set (copy-sequence range)) | |
866 (setq range (math-possible-signs expr)) | |
867 (if (< range 8) | |
868 (aref [(vec) | |
869 (intv 2 (neg (var inf var-inf)) 0) | |
870 (vec 0) | |
871 (intv 3 (neg (var inf var-inf)) 0) | |
872 (intv 1 0 (var inf var-inf)) | |
873 (vec (intv 2 (neg (var inf var-inf)) 0) | |
874 (intv 1 0 (var inf var-inf))) | |
875 (intv 3 0 (var inf var-inf)) | |
876 (intv 3 (neg (var inf var-inf)) (var inf var-inf))] range) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
877 (math-reject-arg expr 'realp 'quiet))))))) |
40785 | 878 |
879 (defun calcFunc-dscalar (a) | |
880 (if (math-known-scalarp a) 1 | |
881 (if (math-known-matrixp a) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
882 (math-reject-arg a 'objectp 'quiet)))) |
40785 | 883 |
884 | |
885 ;;;; Arithmetic. | |
886 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
887 (defsubst calcFunc-neg (a) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
888 (math-normalize (list 'neg a))) |
40785 | 889 |
890 (defun math-neg-fancy (a) | |
891 (cond ((eq (car a) 'polar) | |
892 (list 'polar | |
893 (nth 1 a) | |
894 (if (math-posp (nth 2 a)) | |
895 (math-sub (nth 2 a) (math-half-circle nil)) | |
896 (math-add (nth 2 a) (math-half-circle nil))))) | |
897 ((eq (car a) 'mod) | |
898 (if (math-zerop (nth 1 a)) | |
899 a | |
900 (list 'mod (math-sub (nth 2 a) (nth 1 a)) (nth 2 a)))) | |
901 ((eq (car a) 'sdev) | |
902 (list 'sdev (math-neg (nth 1 a)) (nth 2 a))) | |
903 ((eq (car a) 'intv) | |
904 (math-make-intv (aref [0 2 1 3] (nth 1 a)) | |
905 (math-neg (nth 3 a)) | |
906 (math-neg (nth 2 a)))) | |
907 ((and math-simplify-only | |
908 (not (equal a math-simplify-only))) | |
909 (list 'neg a)) | |
910 ((eq (car a) '+) | |
911 (math-sub (math-neg (nth 1 a)) (nth 2 a))) | |
912 ((eq (car a) '-) | |
913 (math-sub (nth 2 a) (nth 1 a))) | |
914 ((and (memq (car a) '(* /)) | |
915 (math-okay-neg (nth 1 a))) | |
916 (list (car a) (math-neg (nth 1 a)) (nth 2 a))) | |
917 ((and (memq (car a) '(* /)) | |
918 (math-okay-neg (nth 2 a))) | |
919 (list (car a) (nth 1 a) (math-neg (nth 2 a)))) | |
920 ((and (memq (car a) '(* /)) | |
921 (or (math-objectp (nth 1 a)) | |
922 (and (eq (car (nth 1 a)) '*) | |
923 (math-objectp (nth 1 (nth 1 a)))))) | |
924 (list (car a) (math-neg (nth 1 a)) (nth 2 a))) | |
925 ((and (eq (car a) '/) | |
926 (or (math-objectp (nth 2 a)) | |
927 (and (eq (car (nth 2 a)) '*) | |
928 (math-objectp (nth 1 (nth 2 a)))))) | |
929 (list (car a) (nth 1 a) (math-neg (nth 2 a)))) | |
930 ((and (eq (car a) 'var) (memq (nth 2 a) '(var-uinf var-nan))) | |
931 a) | |
932 ((eq (car a) 'neg) | |
933 (nth 1 a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
934 (t (list 'neg a)))) |
40785 | 935 |
936 (defun math-okay-neg (a) | |
937 (or (math-looks-negp a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
938 (eq (car-safe a) '-))) |
40785 | 939 |
940 (defun math-neg-float (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
941 (list 'float (Math-integer-neg (nth 1 a)) (nth 2 a))) |
40785 | 942 |
943 | |
944 (defun calcFunc-add (&rest rest) | |
945 (if rest | |
946 (let ((a (car rest))) | |
947 (while (setq rest (cdr rest)) | |
948 (setq a (list '+ a (car rest)))) | |
949 (math-normalize a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
950 0)) |
40785 | 951 |
952 (defun calcFunc-sub (&rest rest) | |
953 (if rest | |
954 (let ((a (car rest))) | |
955 (while (setq rest (cdr rest)) | |
956 (setq a (list '- a (car rest)))) | |
957 (math-normalize a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
958 0)) |
40785 | 959 |
960 (defun math-add-objects-fancy (a b) | |
961 (cond ((and (Math-numberp a) (Math-numberp b)) | |
962 (let ((aa (math-complex a)) | |
963 (bb (math-complex b))) | |
964 (math-normalize | |
965 (let ((res (list 'cplx | |
966 (math-add (nth 1 aa) (nth 1 bb)) | |
967 (math-add (nth 2 aa) (nth 2 bb))))) | |
968 (if (math-want-polar a b) | |
969 (math-polar res) | |
970 res))))) | |
971 ((or (Math-vectorp a) (Math-vectorp b)) | |
972 (math-map-vec-2 'math-add a b)) | |
973 ((eq (car-safe a) 'sdev) | |
974 (if (eq (car-safe b) 'sdev) | |
975 (math-make-sdev (math-add (nth 1 a) (nth 1 b)) | |
976 (math-hypot (nth 2 a) (nth 2 b))) | |
977 (and (or (Math-scalarp b) | |
978 (not (Math-objvecp b))) | |
979 (math-make-sdev (math-add (nth 1 a) b) (nth 2 a))))) | |
980 ((and (eq (car-safe b) 'sdev) | |
981 (or (Math-scalarp a) | |
982 (not (Math-objvecp a)))) | |
983 (math-make-sdev (math-add a (nth 1 b)) (nth 2 b))) | |
984 ((eq (car-safe a) 'intv) | |
985 (if (eq (car-safe b) 'intv) | |
986 (math-make-intv (logior (logand (nth 1 a) (nth 1 b)) | |
987 (if (equal (nth 2 a) | |
988 '(neg (var inf var-inf))) | |
989 (logand (nth 1 a) 2) 0) | |
990 (if (equal (nth 2 b) | |
991 '(neg (var inf var-inf))) | |
992 (logand (nth 1 b) 2) 0) | |
993 (if (equal (nth 3 a) '(var inf var-inf)) | |
994 (logand (nth 1 a) 1) 0) | |
995 (if (equal (nth 3 b) '(var inf var-inf)) | |
996 (logand (nth 1 b) 1) 0)) | |
997 (math-add (nth 2 a) (nth 2 b)) | |
998 (math-add (nth 3 a) (nth 3 b))) | |
999 (and (or (Math-anglep b) | |
1000 (eq (car b) 'date) | |
1001 (not (Math-objvecp b))) | |
1002 (math-make-intv (nth 1 a) | |
1003 (math-add (nth 2 a) b) | |
1004 (math-add (nth 3 a) b))))) | |
1005 ((and (eq (car-safe b) 'intv) | |
1006 (or (Math-anglep a) | |
1007 (eq (car a) 'date) | |
1008 (not (Math-objvecp a)))) | |
1009 (math-make-intv (nth 1 b) | |
1010 (math-add a (nth 2 b)) | |
1011 (math-add a (nth 3 b)))) | |
1012 ((eq (car-safe a) 'date) | |
1013 (cond ((eq (car-safe b) 'date) | |
1014 (math-add (nth 1 a) (nth 1 b))) | |
1015 ((eq (car-safe b) 'hms) | |
1016 (let ((parts (math-date-parts (nth 1 a)))) | |
1017 (list 'date | |
1018 (math-add (car parts) ; this minimizes roundoff | |
1019 (math-div (math-add | |
1020 (math-add (nth 1 parts) | |
1021 (nth 2 parts)) | |
1022 (math-add | |
1023 (math-mul (nth 1 b) 3600) | |
1024 (math-add (math-mul (nth 2 b) 60) | |
1025 (nth 3 b)))) | |
1026 86400))))) | |
1027 ((Math-realp b) | |
1028 (list 'date (math-add (nth 1 a) b))) | |
1029 (t nil))) | |
1030 ((eq (car-safe b) 'date) | |
1031 (math-add-objects-fancy b a)) | |
1032 ((and (eq (car-safe a) 'mod) | |
1033 (eq (car-safe b) 'mod) | |
1034 (equal (nth 2 a) (nth 2 b))) | |
1035 (math-make-mod (math-add (nth 1 a) (nth 1 b)) (nth 2 a))) | |
1036 ((and (eq (car-safe a) 'mod) | |
1037 (Math-anglep b)) | |
1038 (math-make-mod (math-add (nth 1 a) b) (nth 2 a))) | |
1039 ((and (eq (car-safe b) 'mod) | |
1040 (Math-anglep a)) | |
1041 (math-make-mod (math-add a (nth 1 b)) (nth 2 b))) | |
1042 ((and (or (eq (car-safe a) 'hms) (eq (car-safe b) 'hms)) | |
1043 (and (Math-anglep a) (Math-anglep b))) | |
1044 (or (eq (car-safe a) 'hms) (setq a (math-to-hms a))) | |
1045 (or (eq (car-safe b) 'hms) (setq b (math-to-hms b))) | |
1046 (math-normalize | |
1047 (if (math-negp a) | |
1048 (math-neg (math-add (math-neg a) (math-neg b))) | |
1049 (if (math-negp b) | |
1050 (let* ((s (math-add (nth 3 a) (nth 3 b))) | |
1051 (m (math-add (nth 2 a) (nth 2 b))) | |
1052 (h (math-add (nth 1 a) (nth 1 b)))) | |
1053 (if (math-negp s) | |
1054 (setq s (math-add s 60) | |
1055 m (math-add m -1))) | |
1056 (if (math-negp m) | |
1057 (setq m (math-add m 60) | |
1058 h (math-add h -1))) | |
1059 (if (math-negp h) | |
1060 (math-add b a) | |
1061 (list 'hms h m s))) | |
1062 (let* ((s (math-add (nth 3 a) (nth 3 b))) | |
1063 (m (math-add (nth 2 a) (nth 2 b))) | |
1064 (h (math-add (nth 1 a) (nth 1 b)))) | |
1065 (list 'hms h m s)))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1066 (t (calc-record-why "*Incompatible arguments for +" a b)))) |
40785 | 1067 |
1068 (defun math-add-symb-fancy (a b) | |
1069 (or (and math-simplify-only | |
1070 (not (equal a math-simplify-only)) | |
1071 (list '+ a b)) | |
1072 (and (eq (car-safe b) '+) | |
1073 (math-add (math-add a (nth 1 b)) | |
1074 (nth 2 b))) | |
1075 (and (eq (car-safe b) '-) | |
1076 (math-sub (math-add a (nth 1 b)) | |
1077 (nth 2 b))) | |
1078 (and (eq (car-safe b) 'neg) | |
1079 (eq (car-safe (nth 1 b)) '+) | |
1080 (math-sub (math-sub a (nth 1 (nth 1 b))) | |
1081 (nth 2 (nth 1 b)))) | |
1082 (and (or (and (Math-vectorp a) (math-known-scalarp b)) | |
1083 (and (Math-vectorp b) (math-known-scalarp a))) | |
1084 (math-map-vec-2 'math-add a b)) | |
1085 (let ((inf (math-infinitep a))) | |
1086 (cond | |
1087 (inf | |
1088 (let ((inf2 (math-infinitep b))) | |
1089 (if inf2 | |
1090 (if (or (memq (nth 2 inf) '(var-uinf var-nan)) | |
1091 (memq (nth 2 inf2) '(var-uinf var-nan))) | |
1092 '(var nan var-nan) | |
1093 (let ((dir (math-infinite-dir a inf)) | |
1094 (dir2 (math-infinite-dir b inf2))) | |
1095 (if (and (Math-objectp dir) (Math-objectp dir2)) | |
1096 (if (Math-equal dir dir2) | |
1097 a | |
1098 '(var nan var-nan))))) | |
1099 (if (and (equal a '(var inf var-inf)) | |
1100 (eq (car-safe b) 'intv) | |
1101 (memq (nth 1 b) '(2 3)) | |
1102 (equal (nth 2 b) '(neg (var inf var-inf)))) | |
1103 (list 'intv 3 (nth 2 b) a) | |
1104 (if (and (equal a '(neg (var inf var-inf))) | |
1105 (eq (car-safe b) 'intv) | |
1106 (memq (nth 1 b) '(1 3)) | |
1107 (equal (nth 3 b) '(var inf var-inf))) | |
1108 (list 'intv 3 a (nth 3 b)) | |
1109 a))))) | |
1110 ((math-infinitep b) | |
1111 (if (eq (car-safe a) 'intv) | |
1112 (math-add b a) | |
1113 b)) | |
1114 ((eq (car-safe a) '+) | |
1115 (let ((temp (math-combine-sum (nth 2 a) b nil nil t))) | |
1116 (and temp | |
1117 (math-add (nth 1 a) temp)))) | |
1118 ((eq (car-safe a) '-) | |
1119 (let ((temp (math-combine-sum (nth 2 a) b t nil t))) | |
1120 (and temp | |
1121 (math-add (nth 1 a) temp)))) | |
1122 ((and (Math-objectp a) (Math-objectp b)) | |
1123 nil) | |
1124 (t | |
1125 (math-combine-sum a b nil nil nil)))) | |
1126 (and (Math-looks-negp b) | |
1127 (list '- a (math-neg b))) | |
1128 (and (Math-looks-negp a) | |
1129 (list '- b (math-neg a))) | |
1130 (and (eq (car-safe a) 'calcFunc-idn) | |
1131 (= (length a) 2) | |
1132 (or (and (eq (car-safe b) 'calcFunc-idn) | |
1133 (= (length b) 2) | |
1134 (list 'calcFunc-idn (math-add (nth 1 a) (nth 1 b)))) | |
1135 (and (math-square-matrixp b) | |
1136 (math-add (math-mimic-ident (nth 1 a) b) b)) | |
1137 (and (math-known-scalarp b) | |
1138 (math-add (nth 1 a) b)))) | |
1139 (and (eq (car-safe b) 'calcFunc-idn) | |
1140 (= (length a) 2) | |
1141 (or (and (math-square-matrixp a) | |
1142 (math-add a (math-mimic-ident (nth 1 b) a))) | |
1143 (and (math-known-scalarp a) | |
1144 (math-add a (nth 1 b))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1145 (list '+ a b))) |
40785 | 1146 |
1147 | |
1148 (defun calcFunc-mul (&rest rest) | |
1149 (if rest | |
1150 (let ((a (car rest))) | |
1151 (while (setq rest (cdr rest)) | |
1152 (setq a (list '* a (car rest)))) | |
1153 (math-normalize a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1154 1)) |
40785 | 1155 |
1156 (defun math-mul-objects-fancy (a b) | |
1157 (cond ((and (Math-numberp a) (Math-numberp b)) | |
1158 (math-normalize | |
1159 (if (math-want-polar a b) | |
1160 (let ((a (math-polar a)) | |
1161 (b (math-polar b))) | |
1162 (list 'polar | |
1163 (math-mul (nth 1 a) (nth 1 b)) | |
1164 (math-fix-circular (math-add (nth 2 a) (nth 2 b))))) | |
1165 (setq a (math-complex a) | |
1166 b (math-complex b)) | |
1167 (list 'cplx | |
1168 (math-sub (math-mul (nth 1 a) (nth 1 b)) | |
1169 (math-mul (nth 2 a) (nth 2 b))) | |
1170 (math-add (math-mul (nth 1 a) (nth 2 b)) | |
1171 (math-mul (nth 2 a) (nth 1 b))))))) | |
1172 ((Math-vectorp a) | |
1173 (if (Math-vectorp b) | |
1174 (if (math-matrixp a) | |
1175 (if (math-matrixp b) | |
1176 (if (= (length (nth 1 a)) (length b)) | |
1177 (math-mul-mats a b) | |
1178 (math-dimension-error)) | |
1179 (if (= (length (nth 1 a)) 2) | |
1180 (if (= (length a) (length b)) | |
1181 (math-mul-mats a (list 'vec b)) | |
1182 (math-dimension-error)) | |
1183 (if (= (length (nth 1 a)) (length b)) | |
1184 (math-mul-mat-vec a b) | |
1185 (math-dimension-error)))) | |
1186 (if (math-matrixp b) | |
1187 (if (= (length a) (length b)) | |
1188 (nth 1 (math-mul-mats (list 'vec a) b)) | |
1189 (math-dimension-error)) | |
1190 (if (= (length a) (length b)) | |
1191 (math-dot-product a b) | |
1192 (math-dimension-error)))) | |
1193 (math-map-vec-2 'math-mul a b))) | |
1194 ((Math-vectorp b) | |
1195 (math-map-vec-2 'math-mul a b)) | |
1196 ((eq (car-safe a) 'sdev) | |
1197 (if (eq (car-safe b) 'sdev) | |
1198 (math-make-sdev (math-mul (nth 1 a) (nth 1 b)) | |
1199 (math-hypot (math-mul (nth 2 a) (nth 1 b)) | |
1200 (math-mul (nth 2 b) (nth 1 a)))) | |
1201 (and (or (Math-scalarp b) | |
1202 (not (Math-objvecp b))) | |
1203 (math-make-sdev (math-mul (nth 1 a) b) | |
1204 (math-mul (nth 2 a) b))))) | |
1205 ((and (eq (car-safe b) 'sdev) | |
1206 (or (Math-scalarp a) | |
1207 (not (Math-objvecp a)))) | |
1208 (math-make-sdev (math-mul a (nth 1 b)) (math-mul a (nth 2 b)))) | |
1209 ((and (eq (car-safe a) 'intv) (Math-anglep b)) | |
1210 (if (Math-negp b) | |
1211 (math-neg (math-mul a (math-neg b))) | |
1212 (math-make-intv (nth 1 a) | |
1213 (math-mul (nth 2 a) b) | |
1214 (math-mul (nth 3 a) b)))) | |
1215 ((and (eq (car-safe b) 'intv) (Math-anglep a)) | |
1216 (math-mul b a)) | |
1217 ((and (eq (car-safe a) 'intv) (math-intv-constp a) | |
1218 (eq (car-safe b) 'intv) (math-intv-constp b)) | |
1219 (let ((lo (math-mul a (nth 2 b))) | |
1220 (hi (math-mul a (nth 3 b)))) | |
1221 (or (eq (car-safe lo) 'intv) | |
1222 (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) lo lo))) | |
1223 (or (eq (car-safe hi) 'intv) | |
1224 (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) hi hi))) | |
1225 (math-combine-intervals | |
1226 (nth 2 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1227 (math-infinitep (nth 2 lo))) | |
1228 (memq (nth 1 lo) '(2 3))) | |
1229 (nth 3 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1230 (math-infinitep (nth 3 lo))) | |
1231 (memq (nth 1 lo) '(1 3))) | |
1232 (nth 2 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1233 (math-infinitep (nth 2 hi))) | |
1234 (memq (nth 1 hi) '(2 3))) | |
1235 (nth 3 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1236 (math-infinitep (nth 3 hi))) | |
1237 (memq (nth 1 hi) '(1 3)))))) | |
1238 ((and (eq (car-safe a) 'mod) | |
1239 (eq (car-safe b) 'mod) | |
1240 (equal (nth 2 a) (nth 2 b))) | |
1241 (math-make-mod (math-mul (nth 1 a) (nth 1 b)) (nth 2 a))) | |
1242 ((and (eq (car-safe a) 'mod) | |
1243 (Math-anglep b)) | |
1244 (math-make-mod (math-mul (nth 1 a) b) (nth 2 a))) | |
1245 ((and (eq (car-safe b) 'mod) | |
1246 (Math-anglep a)) | |
1247 (math-make-mod (math-mul a (nth 1 b)) (nth 2 b))) | |
1248 ((and (eq (car-safe a) 'hms) (Math-realp b)) | |
1249 (math-with-extra-prec 2 | |
1250 (math-to-hms (math-mul (math-from-hms a 'deg) b) 'deg))) | |
1251 ((and (eq (car-safe b) 'hms) (Math-realp a)) | |
1252 (math-mul b a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1253 (t (calc-record-why "*Incompatible arguments for *" a b)))) |
40785 | 1254 |
1255 ;;; Fast function to multiply floating-point numbers. | |
1256 (defun math-mul-float (a b) ; [F F F] | |
1257 (math-make-float (math-mul (nth 1 a) (nth 1 b)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1258 (+ (nth 2 a) (nth 2 b)))) |
40785 | 1259 |
1260 (defun math-sqr-float (a) ; [F F] | |
1261 (math-make-float (math-mul (nth 1 a) (nth 1 a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1262 (+ (nth 2 a) (nth 2 a)))) |
40785 | 1263 |
1264 (defun math-intv-constp (a &optional finite) | |
1265 (and (or (Math-anglep (nth 2 a)) | |
1266 (and (equal (nth 2 a) '(neg (var inf var-inf))) | |
1267 (or (not finite) | |
1268 (memq (nth 1 a) '(0 1))))) | |
1269 (or (Math-anglep (nth 3 a)) | |
1270 (and (equal (nth 3 a) '(var inf var-inf)) | |
1271 (or (not finite) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1272 (memq (nth 1 a) '(0 2))))))) |
40785 | 1273 |
1274 (defun math-mul-zero (a b) | |
1275 (if (math-known-matrixp b) | |
1276 (if (math-vectorp b) | |
1277 (math-map-vec-2 'math-mul a b) | |
1278 (math-mimic-ident 0 b)) | |
1279 (if (math-infinitep b) | |
1280 '(var nan var-nan) | |
1281 (let ((aa nil) (bb nil)) | |
1282 (if (and (eq (car-safe b) 'intv) | |
1283 (progn | |
1284 (and (equal (nth 2 b) '(neg (var inf var-inf))) | |
1285 (memq (nth 1 b) '(2 3)) | |
1286 (setq aa (nth 2 b))) | |
1287 (and (equal (nth 3 b) '(var inf var-inf)) | |
1288 (memq (nth 1 b) '(1 3)) | |
1289 (setq bb (nth 3 b))) | |
1290 (or aa bb))) | |
1291 (if (or (math-posp a) | |
1292 (and (math-zerop a) | |
1293 (or (memq calc-infinite-mode '(-1 1)) | |
1294 (setq aa '(neg (var inf var-inf)) | |
1295 bb '(var inf var-inf))))) | |
1296 (list 'intv 3 (or aa 0) (or bb 0)) | |
1297 (if (math-negp a) | |
1298 (math-neg (list 'intv 3 (or aa 0) (or bb 0))) | |
1299 '(var nan var-nan))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1300 (if (or (math-floatp a) (math-floatp b)) '(float 0 0) 0)))))) |
40785 | 1301 |
1302 | |
1303 (defun math-mul-symb-fancy (a b) | |
1304 (or (and math-simplify-only | |
1305 (not (equal a math-simplify-only)) | |
1306 (list '* a b)) | |
1307 (and (Math-equal-int a 1) | |
1308 b) | |
1309 (and (Math-equal-int a -1) | |
1310 (math-neg b)) | |
1311 (and (or (and (Math-vectorp a) (math-known-scalarp b)) | |
1312 (and (Math-vectorp b) (math-known-scalarp a))) | |
1313 (math-map-vec-2 'math-mul a b)) | |
1314 (and (Math-objectp b) (not (Math-objectp a)) | |
1315 (math-mul b a)) | |
1316 (and (eq (car-safe a) 'neg) | |
1317 (math-neg (math-mul (nth 1 a) b))) | |
1318 (and (eq (car-safe b) 'neg) | |
1319 (math-neg (math-mul a (nth 1 b)))) | |
1320 (and (eq (car-safe a) '*) | |
1321 (math-mul (nth 1 a) | |
1322 (math-mul (nth 2 a) b))) | |
1323 (and (eq (car-safe a) '^) | |
1324 (Math-looks-negp (nth 2 a)) | |
1325 (not (and (eq (car-safe b) '^) (Math-looks-negp (nth 2 b)))) | |
1326 (math-known-scalarp b t) | |
1327 (math-div b (math-normalize | |
1328 (list '^ (nth 1 a) (math-neg (nth 2 a)))))) | |
1329 (and (eq (car-safe b) '^) | |
1330 (Math-looks-negp (nth 2 b)) | |
1331 (not (and (eq (car-safe a) '^) (Math-looks-negp (nth 2 a)))) | |
1332 (math-div a (math-normalize | |
1333 (list '^ (nth 1 b) (math-neg (nth 2 b)))))) | |
1334 (and (eq (car-safe a) '/) | |
1335 (or (math-known-scalarp a t) (math-known-scalarp b t)) | |
1336 (let ((temp (math-combine-prod (nth 2 a) b t nil t))) | |
1337 (if temp | |
1338 (math-mul (nth 1 a) temp) | |
1339 (math-div (math-mul (nth 1 a) b) (nth 2 a))))) | |
1340 (and (eq (car-safe b) '/) | |
1341 (math-div (math-mul a (nth 1 b)) (nth 2 b))) | |
1342 (and (eq (car-safe b) '+) | |
1343 (Math-numberp a) | |
1344 (or (Math-numberp (nth 1 b)) | |
1345 (Math-numberp (nth 2 b))) | |
1346 (math-add (math-mul a (nth 1 b)) | |
1347 (math-mul a (nth 2 b)))) | |
1348 (and (eq (car-safe b) '-) | |
1349 (Math-numberp a) | |
1350 (or (Math-numberp (nth 1 b)) | |
1351 (Math-numberp (nth 2 b))) | |
1352 (math-sub (math-mul a (nth 1 b)) | |
1353 (math-mul a (nth 2 b)))) | |
1354 (and (eq (car-safe b) '*) | |
1355 (Math-numberp (nth 1 b)) | |
1356 (not (Math-numberp a)) | |
1357 (math-mul (nth 1 b) (math-mul a (nth 2 b)))) | |
1358 (and (eq (car-safe a) 'calcFunc-idn) | |
1359 (= (length a) 2) | |
1360 (or (and (eq (car-safe b) 'calcFunc-idn) | |
1361 (= (length b) 2) | |
1362 (list 'calcFunc-idn (math-mul (nth 1 a) (nth 1 b)))) | |
1363 (and (math-known-scalarp b) | |
1364 (list 'calcFunc-idn (math-mul (nth 1 a) b))) | |
1365 (and (math-known-matrixp b) | |
1366 (math-mul (nth 1 a) b)))) | |
1367 (and (eq (car-safe b) 'calcFunc-idn) | |
1368 (= (length b) 2) | |
1369 (or (and (math-known-scalarp a) | |
1370 (list 'calcFunc-idn (math-mul a (nth 1 b)))) | |
1371 (and (math-known-matrixp a) | |
1372 (math-mul a (nth 1 b))))) | |
1373 (and (math-looks-negp b) | |
1374 (math-mul (math-neg a) (math-neg b))) | |
1375 (and (eq (car-safe b) '-) | |
1376 (math-looks-negp a) | |
1377 (math-mul (math-neg a) (math-neg b))) | |
1378 (cond | |
1379 ((eq (car-safe b) '*) | |
1380 (let ((temp (math-combine-prod a (nth 1 b) nil nil t))) | |
1381 (and temp | |
1382 (math-mul temp (nth 2 b))))) | |
1383 (t | |
1384 (math-combine-prod a b nil nil nil))) | |
1385 (and (equal a '(var nan var-nan)) | |
1386 a) | |
1387 (and (equal b '(var nan var-nan)) | |
1388 b) | |
1389 (and (equal a '(var uinf var-uinf)) | |
1390 a) | |
1391 (and (equal b '(var uinf var-uinf)) | |
1392 b) | |
1393 (and (equal b '(var inf var-inf)) | |
1394 (let ((s1 (math-possible-signs a))) | |
1395 (cond ((eq s1 4) | |
1396 b) | |
1397 ((eq s1 6) | |
1398 '(intv 3 0 (var inf var-inf))) | |
1399 ((eq s1 1) | |
1400 (math-neg b)) | |
1401 ((eq s1 3) | |
1402 '(intv 3 (neg (var inf var-inf)) 0)) | |
1403 ((and (eq (car a) 'intv) (math-intv-constp a)) | |
1404 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))) | |
1405 ((and (eq (car a) 'cplx) | |
1406 (math-zerop (nth 1 a))) | |
1407 (list '* (list 'cplx 0 (calcFunc-sign (nth 2 a))) b)) | |
1408 ((eq (car a) 'polar) | |
1409 (list '* (list 'polar 1 (nth 2 a)) b))))) | |
1410 (and (equal a '(var inf var-inf)) | |
1411 (math-mul b a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1412 (list '* a b))) |
40785 | 1413 |
1414 | |
1415 (defun calcFunc-div (a &rest rest) | |
1416 (while rest | |
1417 (setq a (list '/ a (car rest)) | |
1418 rest (cdr rest))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1419 (math-normalize a)) |
40785 | 1420 |
1421 (defun math-div-objects-fancy (a b) | |
1422 (cond ((and (Math-numberp a) (Math-numberp b)) | |
1423 (math-normalize | |
1424 (cond ((math-want-polar a b) | |
1425 (let ((a (math-polar a)) | |
1426 (b (math-polar b))) | |
1427 (list 'polar | |
1428 (math-div (nth 1 a) (nth 1 b)) | |
1429 (math-fix-circular (math-sub (nth 2 a) | |
1430 (nth 2 b)))))) | |
1431 ((Math-realp b) | |
1432 (setq a (math-complex a)) | |
1433 (list 'cplx (math-div (nth 1 a) b) | |
1434 (math-div (nth 2 a) b))) | |
1435 (t | |
1436 (setq a (math-complex a) | |
1437 b (math-complex b)) | |
1438 (math-div | |
1439 (list 'cplx | |
1440 (math-add (math-mul (nth 1 a) (nth 1 b)) | |
1441 (math-mul (nth 2 a) (nth 2 b))) | |
1442 (math-sub (math-mul (nth 2 a) (nth 1 b)) | |
1443 (math-mul (nth 1 a) (nth 2 b)))) | |
1444 (math-add (math-sqr (nth 1 b)) | |
1445 (math-sqr (nth 2 b)))))))) | |
1446 ((math-matrixp b) | |
1447 (if (math-square-matrixp b) | |
1448 (let ((n1 (length b))) | |
1449 (if (Math-vectorp a) | |
1450 (if (math-matrixp a) | |
1451 (if (= (length a) n1) | |
1452 (math-lud-solve (math-matrix-lud b) a b) | |
1453 (if (= (length (nth 1 a)) n1) | |
1454 (math-transpose | |
1455 (math-lud-solve (math-matrix-lud | |
1456 (math-transpose b)) | |
1457 (math-transpose a) b)) | |
1458 (math-dimension-error))) | |
1459 (if (= (length a) n1) | |
1460 (math-mat-col (math-lud-solve (math-matrix-lud b) | |
1461 (math-col-matrix a) b) | |
1462 1) | |
1463 (math-dimension-error))) | |
1464 (if (Math-equal-int a 1) | |
1465 (calcFunc-inv b) | |
1466 (math-mul a (calcFunc-inv b))))) | |
1467 (math-reject-arg b 'square-matrixp))) | |
1468 ((and (Math-vectorp a) (Math-objectp b)) | |
1469 (math-map-vec-2 'math-div a b)) | |
1470 ((eq (car-safe a) 'sdev) | |
1471 (if (eq (car-safe b) 'sdev) | |
1472 (let ((x (math-div (nth 1 a) (nth 1 b)))) | |
1473 (math-make-sdev x | |
1474 (math-div (math-hypot (nth 2 a) | |
1475 (math-mul (nth 2 b) x)) | |
1476 (nth 1 b)))) | |
1477 (if (or (Math-scalarp b) | |
1478 (not (Math-objvecp b))) | |
1479 (math-make-sdev (math-div (nth 1 a) b) (math-div (nth 2 a) b)) | |
1480 (math-reject-arg 'realp b)))) | |
1481 ((and (eq (car-safe b) 'sdev) | |
1482 (or (Math-scalarp a) | |
1483 (not (Math-objvecp a)))) | |
1484 (let ((x (math-div a (nth 1 b)))) | |
1485 (math-make-sdev x | |
1486 (math-div (math-mul (nth 2 b) x) (nth 1 b))))) | |
1487 ((and (eq (car-safe a) 'intv) (Math-anglep b)) | |
1488 (if (Math-negp b) | |
1489 (math-neg (math-div a (math-neg b))) | |
1490 (math-make-intv (nth 1 a) | |
1491 (math-div (nth 2 a) b) | |
1492 (math-div (nth 3 a) b)))) | |
1493 ((and (eq (car-safe b) 'intv) (Math-anglep a)) | |
1494 (if (or (Math-posp (nth 2 b)) | |
1495 (and (Math-zerop (nth 2 b)) (or (memq (nth 1 b) '(0 1)) | |
1496 calc-infinite-mode))) | |
1497 (if (Math-negp a) | |
1498 (math-neg (math-div (math-neg a) b)) | |
1499 (let ((calc-infinite-mode 1)) | |
1500 (math-make-intv (aref [0 2 1 3] (nth 1 b)) | |
1501 (math-div a (nth 3 b)) | |
1502 (math-div a (nth 2 b))))) | |
1503 (if (or (Math-negp (nth 3 b)) | |
1504 (and (Math-zerop (nth 3 b)) (or (memq (nth 1 b) '(0 2)) | |
1505 calc-infinite-mode))) | |
1506 (math-neg (math-div a (math-neg b))) | |
1507 (if calc-infinite-mode | |
1508 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1509 (math-reject-arg b "*Division by zero"))))) | |
1510 ((and (eq (car-safe a) 'intv) (math-intv-constp a) | |
1511 (eq (car-safe b) 'intv) (math-intv-constp b)) | |
1512 (if (or (Math-posp (nth 2 b)) | |
1513 (and (Math-zerop (nth 2 b)) (or (memq (nth 1 b) '(0 1)) | |
1514 calc-infinite-mode))) | |
1515 (let* ((calc-infinite-mode 1) | |
1516 (lo (math-div a (nth 2 b))) | |
1517 (hi (math-div a (nth 3 b)))) | |
1518 (or (eq (car-safe lo) 'intv) | |
1519 (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) | |
1520 lo lo))) | |
1521 (or (eq (car-safe hi) 'intv) | |
1522 (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) | |
1523 hi hi))) | |
1524 (math-combine-intervals | |
1525 (nth 2 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1526 (and (math-infinitep (nth 2 lo)) | |
1527 (not (math-zerop (nth 2 b))))) | |
1528 (memq (nth 1 lo) '(2 3))) | |
1529 (nth 3 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1530 (and (math-infinitep (nth 3 lo)) | |
1531 (not (math-zerop (nth 2 b))))) | |
1532 (memq (nth 1 lo) '(1 3))) | |
1533 (nth 2 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1534 (and (math-infinitep (nth 2 hi)) | |
1535 (not (math-zerop (nth 3 b))))) | |
1536 (memq (nth 1 hi) '(2 3))) | |
1537 (nth 3 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1538 (and (math-infinitep (nth 3 hi)) | |
1539 (not (math-zerop (nth 3 b))))) | |
1540 (memq (nth 1 hi) '(1 3))))) | |
1541 (if (or (Math-negp (nth 3 b)) | |
1542 (and (Math-zerop (nth 3 b)) (or (memq (nth 1 b) '(0 2)) | |
1543 calc-infinite-mode))) | |
1544 (math-neg (math-div a (math-neg b))) | |
1545 (if calc-infinite-mode | |
1546 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1547 (math-reject-arg b "*Division by zero"))))) | |
1548 ((and (eq (car-safe a) 'mod) | |
1549 (eq (car-safe b) 'mod) | |
1550 (equal (nth 2 a) (nth 2 b))) | |
1551 (math-make-mod (math-div-mod (nth 1 a) (nth 1 b) (nth 2 a)) | |
1552 (nth 2 a))) | |
1553 ((and (eq (car-safe a) 'mod) | |
1554 (Math-anglep b)) | |
1555 (math-make-mod (math-div-mod (nth 1 a) b (nth 2 a)) (nth 2 a))) | |
1556 ((and (eq (car-safe b) 'mod) | |
1557 (Math-anglep a)) | |
1558 (math-make-mod (math-div-mod a (nth 1 b) (nth 2 b)) (nth 2 b))) | |
1559 ((eq (car-safe a) 'hms) | |
1560 (if (eq (car-safe b) 'hms) | |
1561 (math-with-extra-prec 1 | |
1562 (math-div (math-from-hms a 'deg) | |
1563 (math-from-hms b 'deg))) | |
1564 (math-with-extra-prec 2 | |
1565 (math-to-hms (math-div (math-from-hms a 'deg) b) 'deg)))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1566 (t (calc-record-why "*Incompatible arguments for /" a b)))) |
40785 | 1567 |
1568 (defun math-div-by-zero (a b) | |
1569 (if (math-infinitep a) | |
1570 (if (or (equal a '(var nan var-nan)) | |
1571 (equal b '(var uinf var-uinf)) | |
1572 (memq calc-infinite-mode '(-1 1))) | |
1573 a | |
1574 '(var uinf var-uinf)) | |
1575 (if calc-infinite-mode | |
1576 (if (math-zerop a) | |
1577 '(var nan var-nan) | |
1578 (if (eq calc-infinite-mode 1) | |
1579 (math-mul a '(var inf var-inf)) | |
1580 (if (eq calc-infinite-mode -1) | |
1581 (math-mul a '(neg (var inf var-inf))) | |
1582 (if (eq (car-safe a) 'intv) | |
1583 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1584 '(var uinf var-uinf))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1585 (math-reject-arg a "*Division by zero")))) |
40785 | 1586 |
1587 (defun math-div-zero (a b) | |
1588 (if (math-known-matrixp b) | |
1589 (if (math-vectorp b) | |
1590 (math-map-vec-2 'math-div a b) | |
1591 (math-mimic-ident 0 b)) | |
1592 (if (equal b '(var nan var-nan)) | |
1593 b | |
1594 (if (and (eq (car-safe b) 'intv) (math-intv-constp b) | |
1595 (not (math-posp b)) (not (math-negp b))) | |
1596 (if calc-infinite-mode | |
1597 (list 'intv 3 | |
1598 (if (and (math-zerop (nth 2 b)) | |
1599 (memq calc-infinite-mode '(1 -1))) | |
1600 (nth 2 b) '(neg (var inf var-inf))) | |
1601 (if (and (math-zerop (nth 3 b)) | |
1602 (memq calc-infinite-mode '(1 -1))) | |
1603 (nth 3 b) '(var inf var-inf))) | |
1604 (math-reject-arg b "*Division by zero")) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1605 a)))) |
40785 | 1606 |
1607 (defun math-div-symb-fancy (a b) | |
1608 (or (and math-simplify-only | |
1609 (not (equal a math-simplify-only)) | |
1610 (list '/ a b)) | |
1611 (and (Math-equal-int b 1) a) | |
1612 (and (Math-equal-int b -1) (math-neg a)) | |
1613 (and (Math-vectorp a) (math-known-scalarp b) | |
1614 (math-map-vec-2 'math-div a b)) | |
1615 (and (eq (car-safe b) '^) | |
1616 (or (Math-looks-negp (nth 2 b)) (Math-equal-int a 1)) | |
1617 (math-mul a (math-normalize | |
1618 (list '^ (nth 1 b) (math-neg (nth 2 b)))))) | |
1619 (and (eq (car-safe a) 'neg) | |
1620 (math-neg (math-div (nth 1 a) b))) | |
1621 (and (eq (car-safe b) 'neg) | |
1622 (math-neg (math-div a (nth 1 b)))) | |
1623 (and (eq (car-safe a) '/) | |
1624 (math-div (nth 1 a) (math-mul (nth 2 a) b))) | |
1625 (and (eq (car-safe b) '/) | |
1626 (or (math-known-scalarp (nth 1 b) t) | |
1627 (math-known-scalarp (nth 2 b) t)) | |
1628 (math-div (math-mul a (nth 2 b)) (nth 1 b))) | |
1629 (and (eq (car-safe b) 'frac) | |
1630 (math-mul (math-make-frac (nth 2 b) (nth 1 b)) a)) | |
1631 (and (eq (car-safe a) '+) | |
1632 (or (Math-numberp (nth 1 a)) | |
1633 (Math-numberp (nth 2 a))) | |
1634 (Math-numberp b) | |
1635 (math-add (math-div (nth 1 a) b) | |
1636 (math-div (nth 2 a) b))) | |
1637 (and (eq (car-safe a) '-) | |
1638 (or (Math-numberp (nth 1 a)) | |
1639 (Math-numberp (nth 2 a))) | |
1640 (Math-numberp b) | |
1641 (math-sub (math-div (nth 1 a) b) | |
1642 (math-div (nth 2 a) b))) | |
1643 (and (or (eq (car-safe a) '-) | |
1644 (math-looks-negp a)) | |
1645 (math-looks-negp b) | |
1646 (math-div (math-neg a) (math-neg b))) | |
1647 (and (eq (car-safe b) '-) | |
1648 (math-looks-negp a) | |
1649 (math-div (math-neg a) (math-neg b))) | |
1650 (and (eq (car-safe a) 'calcFunc-idn) | |
1651 (= (length a) 2) | |
1652 (or (and (eq (car-safe b) 'calcFunc-idn) | |
1653 (= (length b) 2) | |
1654 (list 'calcFunc-idn (math-div (nth 1 a) (nth 1 b)))) | |
1655 (and (math-known-scalarp b) | |
1656 (list 'calcFunc-idn (math-div (nth 1 a) b))) | |
1657 (and (math-known-matrixp b) | |
1658 (math-div (nth 1 a) b)))) | |
1659 (and (eq (car-safe b) 'calcFunc-idn) | |
1660 (= (length b) 2) | |
1661 (or (and (math-known-scalarp a) | |
1662 (list 'calcFunc-idn (math-div a (nth 1 b)))) | |
1663 (and (math-known-matrixp a) | |
1664 (math-div a (nth 1 b))))) | |
1665 (if (and calc-matrix-mode | |
1666 (or (math-known-matrixp a) (math-known-matrixp b))) | |
1667 (math-combine-prod a b nil t nil) | |
1668 (if (eq (car-safe a) '*) | |
1669 (if (eq (car-safe b) '*) | |
1670 (let ((c (math-combine-prod (nth 1 a) (nth 1 b) nil t t))) | |
1671 (and c | |
1672 (math-div (math-mul c (nth 2 a)) (nth 2 b)))) | |
1673 (let ((c (math-combine-prod (nth 1 a) b nil t t))) | |
1674 (and c | |
1675 (math-mul c (nth 2 a))))) | |
1676 (if (eq (car-safe b) '*) | |
1677 (let ((c (math-combine-prod a (nth 1 b) nil t t))) | |
1678 (and c | |
1679 (math-div c (nth 2 b)))) | |
1680 (math-combine-prod a b nil t nil)))) | |
1681 (and (math-infinitep a) | |
1682 (if (math-infinitep b) | |
1683 '(var nan var-nan) | |
1684 (if (or (equal a '(var nan var-nan)) | |
1685 (equal a '(var uinf var-uinf))) | |
1686 a | |
1687 (if (equal a '(var inf var-inf)) | |
1688 (if (or (math-posp b) | |
1689 (and (eq (car-safe b) 'intv) | |
1690 (math-zerop (nth 2 b)))) | |
1691 (if (and (eq (car-safe b) 'intv) | |
1692 (not (math-intv-constp b t))) | |
1693 '(intv 3 0 (var inf var-inf)) | |
1694 a) | |
1695 (if (or (math-negp b) | |
1696 (and (eq (car-safe b) 'intv) | |
1697 (math-zerop (nth 3 b)))) | |
1698 (if (and (eq (car-safe b) 'intv) | |
1699 (not (math-intv-constp b t))) | |
1700 '(intv 3 (neg (var inf var-inf)) 0) | |
1701 (math-neg a)) | |
1702 (if (and (eq (car-safe b) 'intv) | |
1703 (math-negp (nth 2 b)) (math-posp (nth 3 b))) | |
1704 '(intv 3 (neg (var inf var-inf)) | |
1705 (var inf var-inf))))))))) | |
1706 (and (math-infinitep b) | |
1707 (if (equal b '(var nan var-nan)) | |
1708 b | |
1709 (let ((calc-infinite-mode 1)) | |
1710 (math-mul-zero b a)))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1711 (list '/ a b))) |
40785 | 1712 |
1713 | |
1714 (defun calcFunc-mod (a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1715 (math-normalize (list '% a b))) |
40785 | 1716 |
1717 (defun math-mod-fancy (a b) | |
1718 (cond ((equal b '(var inf var-inf)) | |
1719 (if (or (math-posp a) (math-zerop a)) | |
1720 a | |
1721 (if (math-negp a) | |
1722 b | |
1723 (if (eq (car-safe a) 'intv) | |
1724 (if (math-negp (nth 2 a)) | |
1725 '(intv 3 0 (var inf var-inf)) | |
1726 a) | |
1727 (list '% a b))))) | |
1728 ((and (eq (car-safe a) 'mod) (Math-realp b) (math-posp b)) | |
1729 (math-make-mod (nth 1 a) b)) | |
1730 ((and (eq (car-safe a) 'intv) (math-intv-constp a t) (math-posp b)) | |
1731 (math-mod-intv a b)) | |
1732 (t | |
1733 (if (Math-anglep a) | |
1734 (calc-record-why 'anglep b) | |
1735 (calc-record-why 'anglep a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1736 (list '% a b)))) |
40785 | 1737 |
1738 | |
1739 (defun calcFunc-pow (a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1740 (math-normalize (list '^ a b))) |
40785 | 1741 |
1742 (defun math-pow-of-zero (a b) | |
58626
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1743 "Raise A to the power of B, where A is a form of zero." |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1744 (if (math-floatp b) (setq a (math-float a))) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1745 (cond |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1746 ;; 0^0 = 1 |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1747 ((eq b 0) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1748 1) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1749 ;; 0^0.0, etc., are undetermined |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1750 ((Math-zerop b) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1751 (if calc-infinite-mode |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1752 '(var nan var-nan) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1753 (math-reject-arg (list '^ a b) "*Indeterminate form"))) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1754 ;; 0^positive = 0 |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1755 ((math-posp b) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1756 a) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1757 ;; 0^negative is undefined (let math-div handle it) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1758 ((math-negp b) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1759 (math-div 1 a)) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1760 ;; 0^infinity is undefined |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1761 ((math-infinitep b) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1762 '(var nan var-nan)) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1763 ;; Some intervals |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1764 ((and (eq (car b) 'intv) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1765 calc-infinite-mode |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1766 (math-negp (nth 2 b)) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1767 (math-posp (nth 3 b))) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1768 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))) |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1769 ;; If none of the above, leave it alone. |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1770 (t |
f6af195898b4
(math-pow-of-zero): Take into account different cases.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58510
diff
changeset
|
1771 (list '^ a b)))) |
40785 | 1772 |
1773 (defun math-pow-zero (a b) | |
1774 (if (eq (car-safe a) 'mod) | |
1775 (math-make-mod 1 (nth 2 a)) | |
1776 (if (math-known-matrixp a) | |
1777 (math-mimic-ident 1 a) | |
1778 (if (math-infinitep a) | |
1779 '(var nan var-nan) | |
1780 (if (and (eq (car a) 'intv) (math-intv-constp a) | |
1781 (or (and (not (math-posp a)) (not (math-negp a))) | |
1782 (not (math-intv-constp a t)))) | |
1783 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1784 (if (or (math-floatp a) (math-floatp b)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1785 '(float 1 0) 1)))))) |
40785 | 1786 |
1787 (defun math-pow-fancy (a b) | |
1788 (cond ((and (Math-numberp a) (Math-numberp b)) | |
1789 (or (if (memq (math-quarter-integer b) '(1 2 3)) | |
1790 (let ((sqrt (math-sqrt (if (math-floatp b) | |
1791 (math-float a) a)))) | |
1792 (and (Math-numberp sqrt) | |
1793 (math-pow sqrt (math-mul 2 b)))) | |
1794 (and (eq (car b) 'frac) | |
1795 (integerp (nth 2 b)) | |
1796 (<= (nth 2 b) 10) | |
1797 (let ((root (math-nth-root a (nth 2 b)))) | |
1798 (and root (math-ipow root (nth 1 b)))))) | |
1799 (and (or (eq a 10) (equal a '(float 1 1))) | |
1800 (math-num-integerp b) | |
1801 (calcFunc-scf '(float 1 0) b)) | |
1802 (and calc-symbolic-mode | |
1803 (list '^ a b)) | |
1804 (math-with-extra-prec 2 | |
1805 (math-exp-raw | |
1806 (math-float (math-mul b (math-ln-raw (math-float a)))))))) | |
1807 ((or (not (Math-objvecp a)) | |
1808 (not (Math-objectp b))) | |
1809 (let (temp) | |
1810 (cond ((and math-simplify-only | |
1811 (not (equal a math-simplify-only))) | |
1812 (list '^ a b)) | |
1813 ((and (eq (car-safe a) '*) | |
1814 (or (math-known-num-integerp b) | |
1815 (math-known-nonnegp (nth 1 a)) | |
1816 (math-known-nonnegp (nth 2 a)))) | |
1817 (math-mul (math-pow (nth 1 a) b) | |
1818 (math-pow (nth 2 a) b))) | |
1819 ((and (eq (car-safe a) '/) | |
1820 (or (math-known-num-integerp b) | |
1821 (math-known-nonnegp (nth 2 a)))) | |
1822 (math-div (math-pow (nth 1 a) b) | |
1823 (math-pow (nth 2 a) b))) | |
1824 ((and (eq (car-safe a) '/) | |
1825 (math-known-nonnegp (nth 1 a)) | |
1826 (not (math-equal-int (nth 1 a) 1))) | |
1827 (math-mul (math-pow (nth 1 a) b) | |
1828 (math-pow (math-div 1 (nth 2 a)) b))) | |
1829 ((and (eq (car-safe a) '^) | |
1830 (or (math-known-num-integerp b) | |
1831 (math-known-nonnegp (nth 1 a)))) | |
1832 (math-pow (nth 1 a) (math-mul (nth 2 a) b))) | |
1833 ((and (eq (car-safe a) 'calcFunc-sqrt) | |
1834 (or (math-known-num-integerp b) | |
1835 (math-known-nonnegp (nth 1 a)))) | |
1836 (math-pow (nth 1 a) (math-div b 2))) | |
1837 ((and (eq (car-safe a) '^) | |
1838 (math-known-evenp (nth 2 a)) | |
1839 (memq (math-quarter-integer b) '(1 2 3)) | |
1840 (math-known-realp (nth 1 a))) | |
1841 (math-abs (math-pow (nth 1 a) (math-mul (nth 2 a) b)))) | |
1842 ((and (math-looks-negp a) | |
1843 (math-known-integerp b) | |
1844 (setq temp (or (and (math-known-evenp b) | |
1845 (math-pow (math-neg a) b)) | |
1846 (and (math-known-oddp b) | |
1847 (math-neg (math-pow (math-neg a) | |
1848 b)))))) | |
1849 temp) | |
1850 ((and (eq (car-safe a) 'calcFunc-abs) | |
1851 (math-known-realp (nth 1 a)) | |
1852 (math-known-evenp b)) | |
1853 (math-pow (nth 1 a) b)) | |
1854 ((math-infinitep a) | |
1855 (cond ((equal a '(var nan var-nan)) | |
1856 a) | |
1857 ((eq (car a) 'neg) | |
1858 (math-mul (math-pow -1 b) (math-pow (nth 1 a) b))) | |
1859 ((math-posp b) | |
1860 a) | |
1861 ((math-negp b) | |
1862 (if (math-floatp b) '(float 0 0) 0)) | |
1863 ((and (eq (car-safe b) 'intv) | |
1864 (math-intv-constp b)) | |
1865 '(intv 3 0 (var inf var-inf))) | |
1866 (t | |
1867 '(var nan var-nan)))) | |
1868 ((math-infinitep b) | |
1869 (let (scale) | |
1870 (cond ((math-negp b) | |
1871 (math-pow (math-div 1 a) (math-neg b))) | |
1872 ((not (math-posp b)) | |
1873 '(var nan var-nan)) | |
1874 ((math-equal-int (setq scale (calcFunc-abssqr a)) 1) | |
1875 '(var nan var-nan)) | |
1876 ((Math-lessp scale 1) | |
1877 (if (math-floatp a) '(float 0 0) 0)) | |
1878 ((Math-lessp 1 a) | |
1879 b) | |
1880 ((Math-lessp a -1) | |
1881 '(var uinf var-uinf)) | |
1882 ((and (eq (car a) 'intv) | |
1883 (math-intv-constp a)) | |
1884 (if (Math-lessp -1 a) | |
1885 (if (math-equal-int (nth 3 a) 1) | |
1886 '(intv 3 0 1) | |
1887 '(intv 3 0 (var inf var-inf))) | |
1888 '(intv 3 (neg (var inf var-inf)) | |
1889 (var inf var-inf)))) | |
1890 (t (list '^ a b))))) | |
1891 ((and (eq (car-safe a) 'calcFunc-idn) | |
1892 (= (length a) 2) | |
1893 (math-known-num-integerp b)) | |
1894 (list 'calcFunc-idn (math-pow (nth 1 a) b))) | |
1895 (t (if (Math-objectp a) | |
1896 (calc-record-why 'objectp b) | |
1897 (calc-record-why 'objectp a)) | |
1898 (list '^ a b))))) | |
1899 ((and (eq (car-safe a) 'sdev) (eq (car-safe b) 'sdev)) | |
1900 (if (and (math-constp a) (math-constp b)) | |
1901 (math-with-extra-prec 2 | |
1902 (let* ((ln (math-ln-raw (math-float (nth 1 a)))) | |
1903 (pow (math-exp-raw | |
1904 (math-float (math-mul (nth 1 b) ln))))) | |
1905 (math-make-sdev | |
1906 pow | |
1907 (math-mul | |
1908 pow | |
1909 (math-hypot (math-mul (nth 2 a) | |
1910 (math-div (nth 1 b) (nth 1 a))) | |
1911 (math-mul (nth 2 b) ln)))))) | |
1912 (let ((pow (math-pow (nth 1 a) (nth 1 b)))) | |
1913 (math-make-sdev | |
1914 pow | |
1915 (math-mul pow | |
1916 (math-hypot (math-mul (nth 2 a) | |
1917 (math-div (nth 1 b) (nth 1 a))) | |
1918 (math-mul (nth 2 b) (calcFunc-ln | |
1919 (nth 1 a))))))))) | |
1920 ((and (eq (car-safe a) 'sdev) (Math-numberp b)) | |
1921 (if (math-constp a) | |
1922 (math-with-extra-prec 2 | |
1923 (let ((pow (math-pow (nth 1 a) (math-sub b 1)))) | |
1924 (math-make-sdev (math-mul pow (nth 1 a)) | |
1925 (math-mul pow (math-mul (nth 2 a) b))))) | |
1926 (math-make-sdev (math-pow (nth 1 a) b) | |
1927 (math-mul (math-pow (nth 1 a) (math-add b -1)) | |
1928 (math-mul (nth 2 a) b))))) | |
1929 ((and (eq (car-safe b) 'sdev) (Math-numberp a)) | |
1930 (math-with-extra-prec 2 | |
1931 (let* ((ln (math-ln-raw (math-float a))) | |
1932 (pow (calcFunc-exp (math-mul (nth 1 b) ln)))) | |
1933 (math-make-sdev pow (math-mul pow (math-mul (nth 2 b) ln)))))) | |
1934 ((and (eq (car-safe a) 'intv) (math-intv-constp a) | |
1935 (Math-realp b) | |
1936 (or (Math-natnump b) | |
1937 (Math-posp (nth 2 a)) | |
1938 (and (math-zerop (nth 2 a)) | |
1939 (or (Math-posp b) | |
1940 (and (Math-integerp b) calc-infinite-mode))) | |
1941 (Math-negp (nth 3 a)) | |
1942 (and (math-zerop (nth 3 a)) | |
1943 (or (Math-posp b) | |
1944 (and (Math-integerp b) calc-infinite-mode))))) | |
1945 (if (math-evenp b) | |
1946 (setq a (math-abs a))) | |
1947 (let ((calc-infinite-mode (if (math-zerop (nth 3 a)) -1 1))) | |
1948 (math-sort-intv (nth 1 a) | |
1949 (math-pow (nth 2 a) b) | |
1950 (math-pow (nth 3 a) b)))) | |
1951 ((and (eq (car-safe b) 'intv) (math-intv-constp b) | |
1952 (Math-realp a) (Math-posp a)) | |
1953 (math-sort-intv (nth 1 b) | |
1954 (math-pow a (nth 2 b)) | |
1955 (math-pow a (nth 3 b)))) | |
1956 ((and (eq (car-safe a) 'intv) (math-intv-constp a) | |
1957 (eq (car-safe b) 'intv) (math-intv-constp b) | |
1958 (or (and (not (Math-negp (nth 2 a))) | |
1959 (not (Math-negp (nth 2 b)))) | |
1960 (and (Math-posp (nth 2 a)) | |
1961 (not (Math-posp (nth 3 b)))))) | |
1962 (let ((lo (math-pow a (nth 2 b))) | |
1963 (hi (math-pow a (nth 3 b)))) | |
1964 (or (eq (car-safe lo) 'intv) | |
1965 (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) lo lo))) | |
1966 (or (eq (car-safe hi) 'intv) | |
1967 (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) hi hi))) | |
1968 (math-combine-intervals | |
1969 (nth 2 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1970 (math-infinitep (nth 2 lo))) | |
1971 (memq (nth 1 lo) '(2 3))) | |
1972 (nth 3 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1973 (math-infinitep (nth 3 lo))) | |
1974 (memq (nth 1 lo) '(1 3))) | |
1975 (nth 2 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1976 (math-infinitep (nth 2 hi))) | |
1977 (memq (nth 1 hi) '(2 3))) | |
1978 (nth 3 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1979 (math-infinitep (nth 3 hi))) | |
1980 (memq (nth 1 hi) '(1 3)))))) | |
1981 ((and (eq (car-safe a) 'mod) (eq (car-safe b) 'mod) | |
1982 (equal (nth 2 a) (nth 2 b))) | |
1983 (math-make-mod (math-pow-mod (nth 1 a) (nth 1 b) (nth 2 a)) | |
1984 (nth 2 a))) | |
1985 ((and (eq (car-safe a) 'mod) (Math-anglep b)) | |
1986 (math-make-mod (math-pow-mod (nth 1 a) b (nth 2 a)) (nth 2 a))) | |
1987 ((and (eq (car-safe b) 'mod) (Math-anglep a)) | |
1988 (math-make-mod (math-pow-mod a (nth 1 b) (nth 2 b)) (nth 2 b))) | |
1989 ((not (Math-numberp a)) | |
1990 (math-reject-arg a 'numberp)) | |
1991 (t | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1992 (math-reject-arg b 'numberp)))) |
40785 | 1993 |
1994 (defun math-quarter-integer (x) | |
1995 (if (Math-integerp x) | |
1996 0 | |
1997 (if (math-negp x) | |
1998 (progn | |
1999 (setq x (math-quarter-integer (math-neg x))) | |
2000 (and x (- 4 x))) | |
2001 (if (eq (car x) 'frac) | |
2002 (if (eq (nth 2 x) 2) | |
2003 2 | |
2004 (and (eq (nth 2 x) 4) | |
2005 (progn | |
2006 (setq x (nth 1 x)) | |
2007 (% (if (consp x) (nth 1 x) x) 4)))) | |
2008 (if (eq (car x) 'float) | |
2009 (if (>= (nth 2 x) 0) | |
2010 0 | |
2011 (if (= (nth 2 x) -1) | |
2012 (progn | |
2013 (setq x (nth 1 x)) | |
2014 (and (= (% (if (consp x) (nth 1 x) x) 10) 5) 2)) | |
2015 (if (= (nth 2 x) -2) | |
2016 (progn | |
2017 (setq x (nth 1 x) | |
2018 x (% (if (consp x) (nth 1 x) x) 100)) | |
2019 (if (= x 25) 1 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2020 (if (= x 75) 3))))))))))) |
40785 | 2021 |
2022 ;;; This assumes A < M and M > 0. | |
2023 (defun math-pow-mod (a b m) ; [R R R R] | |
2024 (if (and (Math-integerp a) (Math-integerp b) (Math-integerp m)) | |
2025 (if (Math-negp b) | |
2026 (math-div-mod 1 (math-pow-mod a (Math-integer-neg b) m) m) | |
2027 (if (eq m 1) | |
2028 0 | |
2029 (math-pow-mod-step a b m))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2030 (math-mod (math-pow a b) m))) |
40785 | 2031 |
2032 (defun math-pow-mod-step (a n m) ; [I I I I] | |
2033 (math-working "pow" a) | |
2034 (let ((val (cond | |
2035 ((eq n 0) 1) | |
2036 ((eq n 1) a) | |
2037 (t | |
2038 (let ((rest (math-pow-mod-step | |
2039 (math-imod (math-mul a a) m) | |
2040 (math-div2 n) | |
2041 m))) | |
2042 (if (math-evenp n) | |
2043 rest | |
2044 (math-mod (math-mul a rest) m))))))) | |
2045 (math-working "pow" val) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2046 val)) |
40785 | 2047 |
2048 | |
2049 ;;; Compute the minimum of two real numbers. [R R R] [Public] | |
2050 (defun math-min (a b) | |
2051 (if (and (consp a) (eq (car a) 'intv)) | |
2052 (if (and (consp b) (eq (car b) 'intv)) | |
2053 (let ((lo (nth 2 a)) | |
2054 (lom (memq (nth 1 a) '(2 3))) | |
2055 (hi (nth 3 a)) | |
2056 (him (memq (nth 1 a) '(1 3))) | |
2057 res) | |
2058 (if (= (setq res (math-compare (nth 2 b) lo)) -1) | |
2059 (setq lo (nth 2 b) lom (memq (nth 1 b) '(2 3))) | |
2060 (if (= res 0) | |
2061 (setq lom (or lom (memq (nth 1 b) '(2 3)))))) | |
2062 (if (= (setq res (math-compare (nth 3 b) hi)) -1) | |
2063 (setq hi (nth 3 b) him (memq (nth 1 b) '(1 3))) | |
2064 (if (= res 0) | |
2065 (setq him (or him (memq (nth 1 b) '(1 3)))))) | |
2066 (math-make-intv (+ (if lom 2 0) (if him 1 0)) lo hi)) | |
2067 (math-min a (list 'intv 3 b b))) | |
2068 (if (and (consp b) (eq (car b) 'intv)) | |
2069 (math-min (list 'intv 3 a a) b) | |
2070 (let ((res (math-compare a b))) | |
2071 (if (= res 1) | |
2072 b | |
2073 (if (= res 2) | |
2074 '(var nan var-nan) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2075 a)))))) |
40785 | 2076 |
2077 (defun calcFunc-min (&optional a &rest b) | |
2078 (if (not a) | |
2079 '(var inf var-inf) | |
2080 (if (not (or (Math-anglep a) (eq (car a) 'date) | |
2081 (and (eq (car a) 'intv) (math-intv-constp a)) | |
2082 (math-infinitep a))) | |
2083 (math-reject-arg a 'anglep)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2084 (math-min-list a b))) |
40785 | 2085 |
2086 (defun math-min-list (a b) | |
2087 (if b | |
2088 (if (or (Math-anglep (car b)) (eq (car b) 'date) | |
2089 (and (eq (car (car b)) 'intv) (math-intv-constp (car b))) | |
2090 (math-infinitep (car b))) | |
2091 (math-min-list (math-min a (car b)) (cdr b)) | |
2092 (math-reject-arg (car b) 'anglep)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2093 a)) |
40785 | 2094 |
2095 ;;; Compute the maximum of two real numbers. [R R R] [Public] | |
2096 (defun math-max (a b) | |
2097 (if (or (and (consp a) (eq (car a) 'intv)) | |
2098 (and (consp b) (eq (car b) 'intv))) | |
2099 (math-neg (math-min (math-neg a) (math-neg b))) | |
2100 (let ((res (math-compare a b))) | |
2101 (if (= res -1) | |
2102 b | |
2103 (if (= res 2) | |
2104 '(var nan var-nan) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2105 a))))) |
40785 | 2106 |
2107 (defun calcFunc-max (&optional a &rest b) | |
2108 (if (not a) | |
2109 '(neg (var inf var-inf)) | |
2110 (if (not (or (Math-anglep a) (eq (car a) 'date) | |
2111 (and (eq (car a) 'intv) (math-intv-constp a)) | |
2112 (math-infinitep a))) | |
2113 (math-reject-arg a 'anglep)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2114 (math-max-list a b))) |
40785 | 2115 |
2116 (defun math-max-list (a b) | |
2117 (if b | |
2118 (if (or (Math-anglep (car b)) (eq (car b) 'date) | |
2119 (and (eq (car (car b)) 'intv) (math-intv-constp (car b))) | |
2120 (math-infinitep (car b))) | |
2121 (math-max-list (math-max a (car b)) (cdr b)) | |
2122 (math-reject-arg (car b) 'anglep)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2123 a)) |
40785 | 2124 |
2125 | |
2126 ;;; Compute the absolute value of A. [O O; r r] [Public] | |
2127 (defun math-abs (a) | |
2128 (cond ((Math-negp a) | |
2129 (math-neg a)) | |
2130 ((Math-anglep a) | |
2131 a) | |
2132 ((eq (car a) 'cplx) | |
2133 (math-hypot (nth 1 a) (nth 2 a))) | |
2134 ((eq (car a) 'polar) | |
2135 (nth 1 a)) | |
2136 ((eq (car a) 'vec) | |
2137 (if (cdr (cdr (cdr a))) | |
2138 (math-sqrt (calcFunc-abssqr a)) | |
2139 (if (cdr (cdr a)) | |
2140 (math-hypot (nth 1 a) (nth 2 a)) | |
2141 (if (cdr a) | |
2142 (math-abs (nth 1 a)) | |
2143 a)))) | |
2144 ((eq (car a) 'sdev) | |
2145 (list 'sdev (math-abs (nth 1 a)) (nth 2 a))) | |
2146 ((and (eq (car a) 'intv) (math-intv-constp a)) | |
2147 (if (Math-posp a) | |
2148 a | |
2149 (let* ((nlo (math-neg (nth 2 a))) | |
2150 (res (math-compare nlo (nth 3 a)))) | |
2151 (cond ((= res 1) | |
2152 (math-make-intv (if (memq (nth 1 a) '(0 1)) 2 3) 0 nlo)) | |
2153 ((= res 0) | |
2154 (math-make-intv (if (eq (nth 1 a) 0) 2 3) 0 nlo)) | |
2155 (t | |
2156 (math-make-intv (if (memq (nth 1 a) '(0 2)) 2 3) | |
2157 0 (nth 3 a))))))) | |
2158 ((math-looks-negp a) | |
2159 (list 'calcFunc-abs (math-neg a))) | |
2160 ((let ((signs (math-possible-signs a))) | |
2161 (or (and (memq signs '(2 4 6)) a) | |
2162 (and (memq signs '(1 3)) (math-neg a))))) | |
2163 ((let ((inf (math-infinitep a))) | |
2164 (and inf | |
2165 (if (equal inf '(var nan var-nan)) | |
2166 inf | |
2167 '(var inf var-inf))))) | |
2168 (t (calc-record-why 'numvecp a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2169 (list 'calcFunc-abs a)))) |
40785 | 2170 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2171 (defalias 'calcFunc-abs 'math-abs) |
40785 | 2172 |
2173 (defun math-float-fancy (a) | |
2174 (cond ((eq (car a) 'intv) | |
2175 (cons (car a) (cons (nth 1 a) (mapcar 'math-float (nthcdr 2 a))))) | |
2176 ((and (memq (car a) '(* /)) | |
2177 (math-numberp (nth 1 a))) | |
2178 (list (car a) (math-float (nth 1 a)) | |
2179 (list 'calcFunc-float (nth 2 a)))) | |
2180 ((and (eq (car a) '/) | |
2181 (eq (car (nth 1 a)) '*) | |
2182 (math-numberp (nth 1 (nth 1 a)))) | |
2183 (list '* (math-float (nth 1 (nth 1 a))) | |
2184 (list 'calcFunc-float (list '/ (nth 2 (nth 1 a)) (nth 2 a))))) | |
2185 ((math-infinitep a) a) | |
2186 ((eq (car a) 'calcFunc-float) a) | |
2187 ((let ((func (assq (car a) '((calcFunc-floor . calcFunc-ffloor) | |
2188 (calcFunc-ceil . calcFunc-fceil) | |
2189 (calcFunc-trunc . calcFunc-ftrunc) | |
2190 (calcFunc-round . calcFunc-fround) | |
2191 (calcFunc-rounde . calcFunc-frounde) | |
2192 (calcFunc-roundu . calcFunc-froundu))))) | |
2193 (and func (cons (cdr func) (cdr a))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2194 (t (math-reject-arg a 'objectp)))) |
40785 | 2195 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2196 (defalias 'calcFunc-float 'math-float) |
40785 | 2197 |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2198 ;; The variable math-trunc-prec is local to math-trunc in calc-misc.el, |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2199 ;; but used by math-trunc-fancy which is called by math-trunc. |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2200 (defvar math-trunc-prec) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2201 |
40785 | 2202 (defun math-trunc-fancy (a) |
2203 (cond ((eq (car a) 'frac) (math-quotient (nth 1 a) (nth 2 a))) | |
2204 ((eq (car a) 'cplx) (math-trunc (nth 1 a))) | |
2205 ((eq (car a) 'polar) (math-trunc (math-complex a))) | |
2206 ((eq (car a) 'hms) (list 'hms (nth 1 a) 0 0)) | |
2207 ((eq (car a) 'date) (list 'date (math-trunc (nth 1 a)))) | |
2208 ((eq (car a) 'mod) | |
2209 (if (math-messy-integerp (nth 2 a)) | |
2210 (math-trunc (math-make-mod (nth 1 a) (math-trunc (nth 2 a)))) | |
2211 (math-make-mod (math-trunc (nth 1 a)) (nth 2 a)))) | |
2212 ((eq (car a) 'intv) | |
2213 (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf))) | |
2214 (memq (nth 1 a) '(0 1))) | |
2215 0 2) | |
2216 (if (and (equal (nth 3 a) '(var inf var-inf)) | |
2217 (memq (nth 1 a) '(0 2))) | |
2218 0 1)) | |
2219 (if (and (Math-negp (nth 2 a)) | |
2220 (Math-num-integerp (nth 2 a)) | |
2221 (memq (nth 1 a) '(0 1))) | |
2222 (math-add (math-trunc (nth 2 a)) 1) | |
2223 (math-trunc (nth 2 a))) | |
2224 (if (and (Math-posp (nth 3 a)) | |
2225 (Math-num-integerp (nth 3 a)) | |
2226 (memq (nth 1 a) '(0 2))) | |
2227 (math-add (math-trunc (nth 3 a)) -1) | |
2228 (math-trunc (nth 3 a))))) | |
2229 ((math-provably-integerp a) a) | |
2230 ((Math-vectorp a) | |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2231 (math-map-vec (function (lambda (x) (math-trunc x math-trunc-prec))) a)) |
40785 | 2232 ((math-infinitep a) |
2233 (if (or (math-posp a) (math-negp a)) | |
2234 a | |
2235 '(var nan var-nan))) | |
2236 ((math-to-integer a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2237 (t (math-reject-arg a 'numberp)))) |
40785 | 2238 |
2239 (defun math-trunc-special (a prec) | |
2240 (if (Math-messy-integerp prec) | |
2241 (setq prec (math-trunc prec))) | |
2242 (or (integerp prec) | |
2243 (math-reject-arg prec 'fixnump)) | |
2244 (if (and (<= prec 0) | |
2245 (math-provably-integerp a)) | |
2246 a | |
2247 (calcFunc-scf (math-trunc (let ((calc-prefer-frac t)) | |
2248 (calcFunc-scf a prec))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2249 (- prec)))) |
40785 | 2250 |
2251 (defun math-to-integer (a) | |
2252 (let ((func (assq (car-safe a) '((calcFunc-ffloor . calcFunc-floor) | |
2253 (calcFunc-fceil . calcFunc-ceil) | |
2254 (calcFunc-ftrunc . calcFunc-trunc) | |
2255 (calcFunc-fround . calcFunc-round) | |
2256 (calcFunc-frounde . calcFunc-rounde) | |
2257 (calcFunc-froundu . calcFunc-roundu))))) | |
2258 (and func (= (length a) 2) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2259 (cons (cdr func) (cdr a))))) |
40785 | 2260 |
2261 (defun calcFunc-ftrunc (a &optional prec) | |
2262 (if (and (Math-messy-integerp a) | |
2263 (or (not prec) (and (integerp prec) | |
2264 (<= prec 0)))) | |
2265 a | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2266 (math-float (math-trunc a prec)))) |
40785 | 2267 |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2268 ;; The variable math-floor-prec is local to math-floor in calc-misc.el, |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2269 ;; but used by math-floor-fancy which is called by math-floor. |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2270 (defvar math-floor-prec) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2271 |
40785 | 2272 (defun math-floor-fancy (a) |
2273 (cond ((math-provably-integerp a) a) | |
2274 ((eq (car a) 'hms) | |
2275 (if (or (math-posp a) | |
2276 (and (math-zerop (nth 2 a)) | |
2277 (math-zerop (nth 3 a)))) | |
2278 (math-trunc a) | |
2279 (math-add (math-trunc a) -1))) | |
2280 ((eq (car a) 'date) (list 'date (math-floor (nth 1 a)))) | |
2281 ((eq (car a) 'intv) | |
2282 (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf))) | |
2283 (memq (nth 1 a) '(0 1))) | |
2284 0 2) | |
2285 (if (and (equal (nth 3 a) '(var inf var-inf)) | |
2286 (memq (nth 1 a) '(0 2))) | |
2287 0 1)) | |
2288 (math-floor (nth 2 a)) | |
2289 (if (and (Math-num-integerp (nth 3 a)) | |
2290 (memq (nth 1 a) '(0 2))) | |
2291 (math-add (math-floor (nth 3 a)) -1) | |
2292 (math-floor (nth 3 a))))) | |
2293 ((Math-vectorp a) | |
58510
b1042b68790c
Finish making previous change.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58479
diff
changeset
|
2294 (math-map-vec (function (lambda (x) (math-floor x math-floor-prec))) a)) |
40785 | 2295 ((math-infinitep a) |
2296 (if (or (math-posp a) (math-negp a)) | |
2297 a | |
2298 '(var nan var-nan))) | |
2299 ((math-to-integer a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2300 (t (math-reject-arg a 'anglep)))) |
40785 | 2301 |
2302 (defun math-floor-special (a prec) | |
2303 (if (Math-messy-integerp prec) | |
2304 (setq prec (math-trunc prec))) | |
2305 (or (integerp prec) | |
2306 (math-reject-arg prec 'fixnump)) | |
2307 (if (and (<= prec 0) | |
2308 (math-provably-integerp a)) | |
2309 a | |
2310 (calcFunc-scf (math-floor (let ((calc-prefer-frac t)) | |
2311 (calcFunc-scf a prec))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2312 (- prec)))) |
40785 | 2313 |
2314 (defun calcFunc-ffloor (a &optional prec) | |
2315 (if (and (Math-messy-integerp a) | |
2316 (or (not prec) (and (integerp prec) | |
2317 (<= prec 0)))) | |
2318 a | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2319 (math-float (math-floor a prec)))) |
40785 | 2320 |
2321 ;;; Coerce A to be an integer (by truncation toward plus infinity). [I N] | |
2322 (defun math-ceiling (a &optional prec) ; [Public] | |
2323 (cond (prec | |
2324 (if (Math-messy-integerp prec) | |
2325 (setq prec (math-trunc prec))) | |
2326 (or (integerp prec) | |
2327 (math-reject-arg prec 'fixnump)) | |
2328 (if (and (<= prec 0) | |
2329 (math-provably-integerp a)) | |
2330 a | |
2331 (calcFunc-scf (math-ceiling (let ((calc-prefer-frac t)) | |
2332 (calcFunc-scf a prec))) | |
2333 (- prec)))) | |
2334 ((Math-integerp a) a) | |
2335 ((Math-messy-integerp a) (math-trunc a)) | |
2336 ((Math-realp a) | |
2337 (if (Math-posp a) | |
2338 (math-add (math-trunc a) 1) | |
2339 (math-trunc a))) | |
2340 ((math-provably-integerp a) a) | |
2341 ((eq (car a) 'hms) | |
2342 (if (or (math-negp a) | |
2343 (and (math-zerop (nth 2 a)) | |
2344 (math-zerop (nth 3 a)))) | |
2345 (math-trunc a) | |
2346 (math-add (math-trunc a) 1))) | |
2347 ((eq (car a) 'date) (list 'date (math-ceiling (nth 1 a)))) | |
2348 ((eq (car a) 'intv) | |
2349 (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf))) | |
2350 (memq (nth 1 a) '(0 1))) | |
2351 0 2) | |
2352 (if (and (equal (nth 3 a) '(var inf var-inf)) | |
2353 (memq (nth 1 a) '(0 2))) | |
2354 0 1)) | |
2355 (if (and (Math-num-integerp (nth 2 a)) | |
2356 (memq (nth 1 a) '(0 1))) | |
2357 (math-add (math-floor (nth 2 a)) 1) | |
2358 (math-ceiling (nth 2 a))) | |
2359 (math-ceiling (nth 3 a)))) | |
2360 ((Math-vectorp a) | |
2361 (math-map-vec (function (lambda (x) (math-ceiling x prec))) a)) | |
2362 ((math-infinitep a) | |
2363 (if (or (math-posp a) (math-negp a)) | |
2364 a | |
2365 '(var nan var-nan))) | |
2366 ((math-to-integer a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2367 (t (math-reject-arg a 'anglep)))) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2368 |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2369 (defalias 'calcFunc-ceil 'math-ceiling) |
40785 | 2370 |
2371 (defun calcFunc-fceil (a &optional prec) | |
2372 (if (and (Math-messy-integerp a) | |
2373 (or (not prec) (and (integerp prec) | |
2374 (<= prec 0)))) | |
2375 a | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2376 (math-float (math-ceiling a prec)))) |
40785 | 2377 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
2378 (defvar math-rounding-mode nil) |
40785 | 2379 |
2380 ;;; Coerce A to be an integer (by rounding to nearest integer). [I N] [Public] | |
2381 (defun math-round (a &optional prec) | |
2382 (cond (prec | |
2383 (if (Math-messy-integerp prec) | |
2384 (setq prec (math-trunc prec))) | |
2385 (or (integerp prec) | |
2386 (math-reject-arg prec 'fixnump)) | |
2387 (if (and (<= prec 0) | |
2388 (math-provably-integerp a)) | |
2389 a | |
2390 (calcFunc-scf (math-round (let ((calc-prefer-frac t)) | |
2391 (calcFunc-scf a prec))) | |
2392 (- prec)))) | |
2393 ((Math-anglep a) | |
2394 (if (Math-num-integerp a) | |
2395 (math-trunc a) | |
2396 (if (and (Math-negp a) (not (eq math-rounding-mode 'up))) | |
2397 (math-neg (math-round (math-neg a))) | |
2398 (setq a (let ((calc-angle-mode 'deg)) ; in case of HMS forms | |
2399 (math-add a (if (Math-ratp a) | |
2400 '(frac 1 2) | |
2401 '(float 5 -1))))) | |
2402 (if (and (Math-num-integerp a) (eq math-rounding-mode 'even)) | |
2403 (progn | |
2404 (setq a (math-floor a)) | |
2405 (or (math-evenp a) | |
2406 (setq a (math-sub a 1))) | |
2407 a) | |
2408 (math-floor a))))) | |
2409 ((math-provably-integerp a) a) | |
2410 ((eq (car a) 'date) (list 'date (math-round (nth 1 a)))) | |
2411 ((eq (car a) 'intv) | |
2412 (math-floor (math-add a '(frac 1 2)))) | |
2413 ((Math-vectorp a) | |
2414 (math-map-vec (function (lambda (x) (math-round x prec))) a)) | |
2415 ((math-infinitep a) | |
2416 (if (or (math-posp a) (math-negp a)) | |
2417 a | |
2418 '(var nan var-nan))) | |
2419 ((math-to-integer a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2420 (t (math-reject-arg a 'anglep)))) |
40785 | 2421 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2422 (defalias 'calcFunc-round 'math-round) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2423 |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2424 (defsubst calcFunc-rounde (a &optional prec) |
40785 | 2425 (let ((math-rounding-mode 'even)) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2426 (math-round a prec))) |
40785 | 2427 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2428 (defsubst calcFunc-roundu (a &optional prec) |
40785 | 2429 (let ((math-rounding-mode 'up)) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2430 (math-round a prec))) |
40785 | 2431 |
2432 (defun calcFunc-fround (a &optional prec) | |
2433 (if (and (Math-messy-integerp a) | |
2434 (or (not prec) (and (integerp prec) | |
2435 (<= prec 0)))) | |
2436 a | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2437 (math-float (math-round a prec)))) |
40785 | 2438 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2439 (defsubst calcFunc-frounde (a &optional prec) |
40785 | 2440 (let ((math-rounding-mode 'even)) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2441 (calcFunc-fround a prec))) |
40785 | 2442 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2443 (defsubst calcFunc-froundu (a &optional prec) |
40785 | 2444 (let ((math-rounding-mode 'up)) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2445 (calcFunc-fround a prec))) |
40785 | 2446 |
2447 ;;; Pull floating-point values apart into mantissa and exponent. | |
2448 (defun calcFunc-mant (x) | |
2449 (if (Math-realp x) | |
2450 (if (or (Math-ratp x) | |
2451 (eq (nth 1 x) 0)) | |
2452 x | |
2453 (list 'float (nth 1 x) (- 1 (math-numdigs (nth 1 x))))) | |
2454 (calc-record-why 'realp x) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2455 (list 'calcFunc-mant x))) |
40785 | 2456 |
2457 (defun calcFunc-xpon (x) | |
2458 (if (Math-realp x) | |
2459 (if (or (Math-ratp x) | |
2460 (eq (nth 1 x) 0)) | |
2461 0 | |
2462 (math-normalize (+ (nth 2 x) (1- (math-numdigs (nth 1 x)))))) | |
2463 (calc-record-why 'realp x) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2464 (list 'calcFunc-xpon x))) |
40785 | 2465 |
2466 (defun calcFunc-scf (x n) | |
2467 (if (integerp n) | |
2468 (cond ((eq n 0) | |
2469 x) | |
2470 ((Math-integerp x) | |
2471 (if (> n 0) | |
2472 (math-scale-int x n) | |
2473 (math-div x (math-scale-int 1 (- n))))) | |
2474 ((eq (car x) 'frac) | |
2475 (if (> n 0) | |
2476 (math-make-frac (math-scale-int (nth 1 x) n) (nth 2 x)) | |
2477 (math-make-frac (nth 1 x) (math-scale-int (nth 2 x) (- n))))) | |
2478 ((eq (car x) 'float) | |
2479 (math-make-float (nth 1 x) (+ (nth 2 x) n))) | |
2480 ((memq (car x) '(cplx sdev)) | |
2481 (math-normalize | |
2482 (list (car x) | |
2483 (calcFunc-scf (nth 1 x) n) | |
2484 (calcFunc-scf (nth 2 x) n)))) | |
2485 ((memq (car x) '(polar mod)) | |
2486 (math-normalize | |
2487 (list (car x) | |
2488 (calcFunc-scf (nth 1 x) n) | |
2489 (nth 2 x)))) | |
2490 ((eq (car x) 'intv) | |
2491 (math-normalize | |
2492 (list (car x) | |
2493 (nth 1 x) | |
2494 (calcFunc-scf (nth 2 x) n) | |
2495 (calcFunc-scf (nth 3 x) n)))) | |
2496 ((eq (car x) 'vec) | |
2497 (math-map-vec (function (lambda (x) (calcFunc-scf x n))) x)) | |
2498 ((math-infinitep x) | |
2499 x) | |
2500 (t | |
2501 (calc-record-why 'realp x) | |
2502 (list 'calcFunc-scf x n))) | |
2503 (if (math-messy-integerp n) | |
2504 (if (< (nth 2 n) 10) | |
2505 (calcFunc-scf x (math-trunc n)) | |
2506 (math-overflow n)) | |
2507 (if (math-integerp n) | |
2508 (math-overflow n) | |
2509 (calc-record-why 'integerp n) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2510 (list 'calcFunc-scf x n))))) |
40785 | 2511 |
2512 | |
2513 (defun calcFunc-incr (x &optional step relative-to) | |
2514 (or step (setq step 1)) | |
2515 (cond ((not (Math-integerp step)) | |
2516 (math-reject-arg step 'integerp)) | |
2517 ((Math-integerp x) | |
2518 (math-add x step)) | |
2519 ((eq (car x) 'float) | |
2520 (if (and (math-zerop x) | |
2521 (eq (car-safe relative-to) 'float)) | |
2522 (math-mul step | |
2523 (calcFunc-scf relative-to (- 1 calc-internal-prec))) | |
2524 (math-add-float x (math-make-float | |
2525 step | |
2526 (+ (nth 2 x) | |
2527 (- (math-numdigs (nth 1 x)) | |
2528 calc-internal-prec)))))) | |
2529 ((eq (car x) 'date) | |
2530 (if (Math-integerp (nth 1 x)) | |
2531 (math-add x step) | |
2532 (math-add x (list 'hms 0 0 step)))) | |
2533 (t | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2534 (math-reject-arg x 'realp)))) |
40785 | 2535 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2536 (defsubst calcFunc-decr (x &optional step relative-to) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2537 (calcFunc-incr x (math-neg (or step 1)) relative-to)) |
40785 | 2538 |
2539 (defun calcFunc-percent (x) | |
2540 (if (math-objectp x) | |
2541 (let ((calc-prefer-frac nil)) | |
2542 (math-div x 100)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2543 (list 'calcFunc-percent x))) |
40785 | 2544 |
2545 (defun calcFunc-relch (x y) | |
2546 (if (and (math-objectp x) (math-objectp y)) | |
2547 (math-div (math-sub y x) x) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2548 (list 'calcFunc-relch x y))) |
40785 | 2549 |
2550 ;;; Compute the absolute value squared of A. [F N] [Public] | |
2551 (defun calcFunc-abssqr (a) | |
2552 (cond ((Math-realp a) | |
2553 (math-mul a a)) | |
2554 ((eq (car a) 'cplx) | |
2555 (math-add (math-sqr (nth 1 a)) | |
2556 (math-sqr (nth 2 a)))) | |
2557 ((eq (car a) 'polar) | |
2558 (math-sqr (nth 1 a))) | |
2559 ((and (memq (car a) '(sdev intv)) (math-constp a)) | |
2560 (math-sqr (math-abs a))) | |
2561 ((eq (car a) 'vec) | |
2562 (math-reduce-vec 'math-add (math-map-vec 'calcFunc-abssqr a))) | |
2563 ((math-known-realp a) | |
2564 (math-pow a 2)) | |
2565 ((let ((inf (math-infinitep a))) | |
2566 (and inf | |
2567 (math-mul (calcFunc-abssqr (math-infinite-dir a inf)) inf)))) | |
2568 (t (calc-record-why 'numvecp a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2569 (list 'calcFunc-abssqr a)))) |
40785 | 2570 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2571 (defsubst math-sqr (a) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2572 (math-mul a a)) |
40785 | 2573 |
2574 ;;;; Number theory. | |
2575 | |
2576 (defun calcFunc-idiv (a b) ; [I I I] [Public] | |
2577 (cond ((and (Math-natnump a) (Math-natnump b) (not (eq b 0))) | |
2578 (math-quotient a b)) | |
2579 ((Math-realp a) | |
2580 (if (Math-realp b) | |
2581 (let ((calc-prefer-frac t)) | |
2582 (math-floor (math-div a b))) | |
2583 (math-reject-arg b 'realp))) | |
2584 ((eq (car-safe a) 'hms) | |
2585 (if (eq (car-safe b) 'hms) | |
2586 (let ((calc-prefer-frac t)) | |
2587 (math-floor (math-div a b))) | |
2588 (math-reject-arg b 'hmsp))) | |
2589 ((and (or (eq (car-safe a) 'intv) (Math-realp a)) | |
2590 (or (eq (car-safe b) 'intv) (Math-realp b))) | |
2591 (math-floor (math-div a b))) | |
2592 ((or (math-infinitep a) | |
2593 (math-infinitep b)) | |
2594 (math-div a b)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2595 (t (math-reject-arg a 'anglep)))) |
40785 | 2596 |
2597 | |
2598 ;;; Combine two terms being added, if possible. | |
2599 (defun math-combine-sum (a b nega negb scalar-okay) | |
2600 (if (and scalar-okay (Math-objvecp a) (Math-objvecp b)) | |
2601 (math-add-or-sub a b nega negb) | |
2602 (let ((amult 1) (bmult 1)) | |
2603 (and (consp a) | |
2604 (cond ((and (eq (car a) '*) | |
2605 (Math-objectp (nth 1 a))) | |
2606 (setq amult (nth 1 a) | |
2607 a (nth 2 a))) | |
2608 ((and (eq (car a) '/) | |
2609 (Math-objectp (nth 2 a))) | |
2610 (setq amult (if (Math-integerp (nth 2 a)) | |
2611 (list 'frac 1 (nth 2 a)) | |
2612 (math-div 1 (nth 2 a))) | |
2613 a (nth 1 a))) | |
2614 ((eq (car a) 'neg) | |
2615 (setq amult -1 | |
2616 a (nth 1 a))))) | |
2617 (and (consp b) | |
2618 (cond ((and (eq (car b) '*) | |
2619 (Math-objectp (nth 1 b))) | |
2620 (setq bmult (nth 1 b) | |
2621 b (nth 2 b))) | |
2622 ((and (eq (car b) '/) | |
2623 (Math-objectp (nth 2 b))) | |
2624 (setq bmult (if (Math-integerp (nth 2 b)) | |
2625 (list 'frac 1 (nth 2 b)) | |
2626 (math-div 1 (nth 2 b))) | |
2627 b (nth 1 b))) | |
2628 ((eq (car b) 'neg) | |
2629 (setq bmult -1 | |
2630 b (nth 1 b))))) | |
2631 (and (if math-simplifying | |
2632 (Math-equal a b) | |
2633 (equal a b)) | |
2634 (progn | |
2635 (if nega (setq amult (math-neg amult))) | |
2636 (if negb (setq bmult (math-neg bmult))) | |
2637 (setq amult (math-add amult bmult)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2638 (math-mul amult a)))))) |
40785 | 2639 |
2640 (defun math-add-or-sub (a b aneg bneg) | |
2641 (if aneg (setq a (math-neg a))) | |
2642 (if bneg (setq b (math-neg b))) | |
2643 (if (or (Math-vectorp a) (Math-vectorp b)) | |
2644 (math-normalize (list '+ a b)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2645 (math-add a b))) |
40785 | 2646 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
2647 (defvar math-combine-prod-e '(var e var-e)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41041
diff
changeset
|
2648 |
40785 | 2649 ;;; The following is expanded out four ways for speed. |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2650 |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2651 ;; math-unit-prefixes is defined in calc-units.el, |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2652 ;; but used here. |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2653 (defvar math-unit-prefixes) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2654 |
40785 | 2655 (defun math-combine-prod (a b inva invb scalar-okay) |
2656 (cond | |
2657 ((or (and inva (Math-zerop a)) | |
2658 (and invb (Math-zerop b))) | |
2659 nil) | |
2660 ((and scalar-okay (Math-objvecp a) (Math-objvecp b)) | |
2661 (setq a (math-mul-or-div a b inva invb)) | |
2662 (and (Math-objvecp a) | |
2663 a)) | |
2664 ((and (eq (car-safe a) '^) | |
2665 inva | |
2666 (math-looks-negp (nth 2 a))) | |
2667 (math-mul (math-pow (nth 1 a) (math-neg (nth 2 a))) b)) | |
2668 ((and (eq (car-safe b) '^) | |
2669 invb | |
2670 (math-looks-negp (nth 2 b))) | |
2671 (math-mul a (math-pow (nth 1 b) (math-neg (nth 2 b))))) | |
2672 (t (let ((apow 1) (bpow 1)) | |
2673 (and (consp a) | |
2674 (cond ((and (eq (car a) '^) | |
2675 (or math-simplifying | |
2676 (Math-numberp (nth 2 a)))) | |
2677 (setq apow (nth 2 a) | |
2678 a (nth 1 a))) | |
2679 ((eq (car a) 'calcFunc-sqrt) | |
2680 (setq apow '(frac 1 2) | |
2681 a (nth 1 a))) | |
2682 ((and (eq (car a) 'calcFunc-exp) | |
2683 (or math-simplifying | |
2684 (Math-numberp (nth 1 a)))) | |
2685 (setq apow (nth 1 a) | |
2686 a math-combine-prod-e)))) | |
2687 (and (consp a) (eq (car a) 'frac) | |
2688 (Math-lessp (nth 1 a) (nth 2 a)) | |
2689 (setq a (math-div 1 a) apow (math-neg apow))) | |
2690 (and (consp b) | |
2691 (cond ((and (eq (car b) '^) | |
2692 (or math-simplifying | |
2693 (Math-numberp (nth 2 b)))) | |
2694 (setq bpow (nth 2 b) | |
2695 b (nth 1 b))) | |
2696 ((eq (car b) 'calcFunc-sqrt) | |
2697 (setq bpow '(frac 1 2) | |
2698 b (nth 1 b))) | |
2699 ((and (eq (car b) 'calcFunc-exp) | |
2700 (or math-simplifying | |
2701 (Math-numberp (nth 1 b)))) | |
2702 (setq bpow (nth 1 b) | |
2703 b math-combine-prod-e)))) | |
2704 (and (consp b) (eq (car b) 'frac) | |
2705 (Math-lessp (nth 1 b) (nth 2 b)) | |
2706 (setq b (math-div 1 b) bpow (math-neg bpow))) | |
2707 (if inva (setq apow (math-neg apow))) | |
2708 (if invb (setq bpow (math-neg bpow))) | |
2709 (or (and (if math-simplifying | |
2710 (math-commutative-equal a b) | |
2711 (equal a b)) | |
2712 (let ((sumpow (math-add apow bpow))) | |
2713 (and (or (not (Math-integerp a)) | |
2714 (Math-zerop sumpow) | |
2715 (eq (eq (car-safe apow) 'frac) | |
2716 (eq (car-safe bpow) 'frac))) | |
2717 (progn | |
2718 (and (math-looks-negp sumpow) | |
2719 (Math-ratp a) (Math-posp a) | |
2720 (setq a (math-div 1 a) | |
2721 sumpow (math-neg sumpow))) | |
2722 (cond ((equal sumpow '(frac 1 2)) | |
2723 (list 'calcFunc-sqrt a)) | |
2724 ((equal sumpow '(frac -1 2)) | |
2725 (math-div 1 (list 'calcFunc-sqrt a))) | |
2726 ((and (eq a math-combine-prod-e) | |
2727 (eq a b)) | |
2728 (list 'calcFunc-exp sumpow)) | |
2729 (t | |
2730 (condition-case err | |
2731 (math-pow a sumpow) | |
2732 (inexact-result (list '^ a sumpow))))))))) | |
2733 (and math-simplifying-units | |
2734 math-combining-units | |
2735 (let* ((ua (math-check-unit-name a)) | |
2736 ub) | |
2737 (and ua | |
2738 (eq ua (setq ub (math-check-unit-name b))) | |
2739 (progn | |
2740 (setq ua (if (eq (nth 1 a) (car ua)) | |
2741 1 | |
2742 (nth 1 (assq (aref (symbol-name (nth 1 a)) | |
2743 0) | |
2744 math-unit-prefixes))) | |
2745 ub (if (eq (nth 1 b) (car ub)) | |
2746 1 | |
2747 (nth 1 (assq (aref (symbol-name (nth 1 b)) | |
2748 0) | |
2749 math-unit-prefixes)))) | |
2750 (if (Math-lessp ua ub) | |
2751 (let (temp) | |
2752 (setq temp a a b b temp | |
2753 temp ua ua ub ub temp | |
2754 temp apow apow bpow bpow temp))) | |
2755 (math-mul (math-pow (math-div ua ub) apow) | |
2756 (math-pow b (math-add apow bpow))))))) | |
2757 (and (equal apow bpow) | |
2758 (Math-natnump a) (Math-natnump b) | |
2759 (cond ((equal apow '(frac 1 2)) | |
2760 (list 'calcFunc-sqrt (math-mul a b))) | |
2761 ((equal apow '(frac -1 2)) | |
2762 (math-div 1 (list 'calcFunc-sqrt (math-mul a b)))) | |
2763 (t | |
2764 (setq a (math-mul a b)) | |
2765 (condition-case err | |
2766 (math-pow a apow) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2767 (inexact-result (list '^ a apow))))))))))) |
40785 | 2768 |
2769 (defun math-mul-or-div (a b ainv binv) | |
2770 (if (or (Math-vectorp a) (Math-vectorp b)) | |
2771 (math-normalize | |
2772 (if ainv | |
2773 (if binv | |
2774 (list '/ (math-div 1 a) b) | |
2775 (list '/ b a)) | |
2776 (if binv | |
2777 (list '/ a b) | |
2778 (list '* a b)))) | |
2779 (if ainv | |
2780 (if binv | |
2781 (math-div (math-div 1 a) b) | |
2782 (math-div b a)) | |
2783 (if binv | |
2784 (math-div a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2785 (math-mul a b))))) |
40785 | 2786 |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2787 ;; The variable math-com-bterms is local to math-commutative-equal, |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2788 ;; but is used by math-commutative collect, which is called by |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2789 ;; math-commutative-equal. |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2790 (defvar math-com-bterms) |
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2791 |
40785 | 2792 (defun math-commutative-equal (a b) |
2793 (if (memq (car-safe a) '(+ -)) | |
2794 (and (memq (car-safe b) '(+ -)) | |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2795 (let ((math-com-bterms nil) aterms p) |
40785 | 2796 (math-commutative-collect b nil) |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2797 (setq aterms math-com-bterms math-com-bterms nil) |
40785 | 2798 (math-commutative-collect a nil) |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2799 (and (= (length aterms) (length math-com-bterms)) |
40785 | 2800 (progn |
2801 (while (and aterms | |
2802 (progn | |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2803 (setq p math-com-bterms) |
40785 | 2804 (while (and p (not (equal (car aterms) |
2805 (car p)))) | |
2806 (setq p (cdr p))) | |
2807 p)) | |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2808 (setq math-com-bterms (delq (car p) math-com-bterms) |
40785 | 2809 aterms (cdr aterms))) |
2810 (not aterms))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2811 (equal a b))) |
40785 | 2812 |
2813 (defun math-commutative-collect (b neg) | |
2814 (if (eq (car-safe b) '+) | |
2815 (progn | |
2816 (math-commutative-collect (nth 1 b) neg) | |
2817 (math-commutative-collect (nth 2 b) neg)) | |
2818 (if (eq (car-safe b) '-) | |
2819 (progn | |
2820 (math-commutative-collect (nth 1 b) neg) | |
2821 (math-commutative-collect (nth 2 b) (not neg))) | |
58479
562c6a62c99e
(math-scalar-functions, math-nonscalar-functions)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2822 (setq math-com-bterms (cons (if neg (math-neg b) b) math-com-bterms))))) |
40785 | 2823 |
52401 | 2824 ;;; arch-tag: 6c396b5b-14c6-40ed-bb2a-7cc2e8111465 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2825 ;;; calc-arith.el ends here |