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