Mercurial > emacs
annotate lisp/calc/calc-cplx.el @ 45920:e475ce899bf1
Comment corrected.
author | Francesco Potortì <pot@gnu.org> |
---|---|
date | Thu, 20 Jun 2002 10:05:11 +0000 |
parents | fcd507927105 |
children | f4d68f97221e |
rev | line source |
---|---|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1 ;;; calc-cplx.el --- Complex number functions for Calc |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
4 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
6 ;; Maintainer: Colin Walters <walters@debian.org> |
40785 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 ;; accepts responsibility to anyone for the consequences of using it | |
13 ;; or for whether it serves any particular purpose or works at all, | |
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 ;; License for full details. | |
16 | |
17 ;; Everyone is granted permission to copy, modify and redistribute | |
18 ;; GNU Emacs, but only under the conditions described in the | |
19 ;; GNU Emacs General Public License. A copy of this license is | |
20 ;; supposed to have been given to you along with GNU Emacs so you | |
21 ;; can know your rights and responsibilities. It should be in a | |
22 ;; file named COPYING. Among other things, the copyright notice | |
23 ;; and this notice must be preserved on all copies. | |
24 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
25 ;;; Commentary: |
40785 | 26 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
27 ;;; Code: |
40785 | 28 |
29 ;; This file is autoloaded from calc-ext.el. | |
30 (require 'calc-ext) | |
31 | |
32 (require 'calc-macs) | |
33 | |
34 (defun calc-Need-calc-cplx () nil) | |
35 | |
36 | |
37 (defun calc-argument (arg) | |
38 (interactive "P") | |
39 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
40 (calc-unary-op "arg" 'calcFunc-arg arg))) |
40785 | 41 |
42 (defun calc-re (arg) | |
43 (interactive "P") | |
44 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
45 (calc-unary-op "re" 'calcFunc-re arg))) |
40785 | 46 |
47 (defun calc-im (arg) | |
48 (interactive "P") | |
49 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
50 (calc-unary-op "im" 'calcFunc-im arg))) |
40785 | 51 |
52 | |
53 (defun calc-polar () | |
54 (interactive) | |
55 (calc-slow-wrapper | |
56 (let ((arg (calc-top-n 1))) | |
57 (if (or (calc-is-inverse) | |
58 (eq (car-safe arg) 'polar)) | |
59 (calc-enter-result 1 "p-r" (list 'calcFunc-rect arg)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
60 (calc-enter-result 1 "r-p" (list 'calcFunc-polar arg)))))) |
40785 | 61 |
62 | |
63 | |
64 | |
65 (defun calc-complex-notation () | |
66 (interactive) | |
67 (calc-wrapper | |
68 (calc-change-mode 'calc-complex-format nil t) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
69 (message "Displaying complex numbers in (X,Y) format"))) |
40785 | 70 |
71 (defun calc-i-notation () | |
72 (interactive) | |
73 (calc-wrapper | |
74 (calc-change-mode 'calc-complex-format 'i t) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
75 (message "Displaying complex numbers in X+Yi format"))) |
40785 | 76 |
77 (defun calc-j-notation () | |
78 (interactive) | |
79 (calc-wrapper | |
80 (calc-change-mode 'calc-complex-format 'j t) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
81 (message "Displaying complex numbers in X+Yj format"))) |
40785 | 82 |
83 | |
84 (defun calc-polar-mode (n) | |
85 (interactive "P") | |
86 (calc-wrapper | |
87 (if (if n | |
88 (> (prefix-numeric-value n) 0) | |
89 (eq calc-complex-mode 'cplx)) | |
90 (progn | |
91 (calc-change-mode 'calc-complex-mode 'polar) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
92 (message "Preferred complex form is polar")) |
40785 | 93 (calc-change-mode 'calc-complex-mode 'cplx) |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
94 (message "Preferred complex form is rectangular")))) |
40785 | 95 |
96 | |
97 ;;;; Complex numbers. | |
98 | |
99 (defun math-normalize-polar (a) | |
100 (let ((r (math-normalize (nth 1 a))) | |
101 (th (math-normalize (nth 2 a)))) | |
102 (cond ((math-zerop r) | |
103 '(polar 0 0)) | |
104 ((or (math-zerop th)) | |
105 r) | |
106 ((and (not (eq calc-angle-mode 'rad)) | |
107 (or (equal th '(float 18 1)) | |
108 (equal th 180))) | |
109 (math-neg r)) | |
110 ((math-negp r) | |
111 (math-neg (list 'polar (math-neg r) th))) | |
112 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
113 (list 'polar r th))))) |
40785 | 114 |
115 | |
116 ;;; Coerce A to be complex (rectangular form). [c N] | |
117 (defun math-complex (a) | |
118 (cond ((eq (car-safe a) 'cplx) a) | |
119 ((eq (car-safe a) 'polar) | |
120 (if (math-zerop (nth 1 a)) | |
121 (nth 1 a) | |
122 (let ((sc (calcFunc-sincos (nth 2 a)))) | |
123 (list 'cplx | |
124 (math-mul (nth 1 a) (nth 1 sc)) | |
125 (math-mul (nth 1 a) (nth 2 sc)))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
126 (t (list 'cplx a 0)))) |
40785 | 127 |
128 ;;; Coerce A to be complex (polar form). [c N] | |
129 (defun math-polar (a) | |
130 (cond ((eq (car-safe a) 'polar) a) | |
131 ((math-zerop a) '(polar 0 0)) | |
132 (t | |
133 (list 'polar | |
134 (math-abs a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
135 (calcFunc-arg a))))) |
40785 | 136 |
137 ;;; Multiply A by the imaginary constant i. [N N] [Public] | |
138 (defun math-imaginary (a) | |
139 (if (and (or (Math-objvecp a) (math-infinitep a)) | |
140 (not calc-symbolic-mode)) | |
141 (math-mul a | |
142 (if (or (eq (car-safe a) 'polar) | |
143 (and (not (eq (car-safe a) 'cplx)) | |
144 (eq calc-complex-mode 'polar))) | |
145 (list 'polar 1 (math-quarter-circle nil)) | |
146 '(cplx 0 1))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
147 (math-mul a '(var i var-i)))) |
40785 | 148 |
149 | |
150 | |
151 | |
152 (defun math-want-polar (a b) | |
153 (cond ((eq (car-safe a) 'polar) | |
154 (if (eq (car-safe b) 'cplx) | |
155 (eq calc-complex-mode 'polar) | |
156 t)) | |
157 ((eq (car-safe a) 'cplx) | |
158 (if (eq (car-safe b) 'polar) | |
159 (eq calc-complex-mode 'polar) | |
160 nil)) | |
161 ((eq (car-safe b) 'polar) | |
162 t) | |
163 ((eq (car-safe b) 'cplx) | |
164 nil) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
165 (t (eq calc-complex-mode 'polar)))) |
40785 | 166 |
167 ;;; Force A to be in the (-pi,pi] or (-180,180] range. | |
168 (defun math-fix-circular (a &optional dir) ; [R R] | |
169 (cond ((eq (car-safe a) 'hms) | |
170 (cond ((and (Math-lessp 180 (nth 1 a)) (not (eq dir 1))) | |
171 (math-fix-circular (math-add a '(float -36 1)) -1)) | |
172 ((or (Math-lessp -180 (nth 1 a)) (eq dir -1)) | |
173 a) | |
174 (t | |
175 (math-fix-circular (math-add a '(float 36 1)) 1)))) | |
176 ((eq calc-angle-mode 'rad) | |
177 (cond ((and (Math-lessp (math-pi) a) (not (eq dir 1))) | |
178 (math-fix-circular (math-sub a (math-two-pi)) -1)) | |
179 ((or (Math-lessp (math-neg (math-pi)) a) (eq dir -1)) | |
180 a) | |
181 (t | |
182 (math-fix-circular (math-add a (math-two-pi)) 1)))) | |
183 (t | |
184 (cond ((and (Math-lessp '(float 18 1) a) (not (eq dir 1))) | |
185 (math-fix-circular (math-add a '(float -36 1)) -1)) | |
186 ((or (Math-lessp '(float -18 1) a) (eq dir -1)) | |
187 a) | |
188 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
189 (math-fix-circular (math-add a '(float 36 1)) 1)))))) |
40785 | 190 |
191 | |
192 ;;;; Complex numbers. | |
193 | |
194 (defun calcFunc-polar (a) ; [C N] [Public] | |
195 (cond ((Math-vectorp a) | |
196 (math-map-vec 'calcFunc-polar a)) | |
197 ((Math-realp a) a) | |
198 ((Math-numberp a) | |
199 (math-normalize (math-polar a))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
200 (t (list 'calcFunc-polar a)))) |
40785 | 201 |
202 (defun calcFunc-rect (a) ; [N N] [Public] | |
203 (cond ((Math-vectorp a) | |
204 (math-map-vec 'calcFunc-rect a)) | |
205 ((Math-realp a) a) | |
206 ((Math-numberp a) | |
207 (math-normalize (math-complex a))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
208 (t (list 'calcFunc-rect a)))) |
40785 | 209 |
210 ;;; Compute the complex conjugate of A. [O O] [Public] | |
211 (defun calcFunc-conj (a) | |
212 (let (aa bb) | |
213 (cond ((Math-realp a) | |
214 a) | |
215 ((eq (car a) 'cplx) | |
216 (list 'cplx (nth 1 a) (math-neg (nth 2 a)))) | |
217 ((eq (car a) 'polar) | |
218 (list 'polar (nth 1 a) (math-neg (nth 2 a)))) | |
219 ((eq (car a) 'vec) | |
220 (math-map-vec 'calcFunc-conj a)) | |
221 ((eq (car a) 'calcFunc-conj) | |
222 (nth 1 a)) | |
223 ((math-known-realp a) | |
224 a) | |
225 ((and (equal a '(var i var-i)) | |
226 (math-imaginary-i)) | |
227 (math-neg a)) | |
228 ((and (memq (car a) '(+ - * /)) | |
229 (progn | |
230 (setq aa (calcFunc-conj (nth 1 a)) | |
231 bb (calcFunc-conj (nth 2 a))) | |
232 (or (not (eq (car-safe aa) 'calcFunc-conj)) | |
233 (not (eq (car-safe bb) 'calcFunc-conj))))) | |
234 (if (eq (car a) '+) | |
235 (math-add aa bb) | |
236 (if (eq (car a) '-) | |
237 (math-sub aa bb) | |
238 (if (eq (car a) '*) | |
239 (math-mul aa bb) | |
240 (math-div aa bb))))) | |
241 ((eq (car a) 'neg) | |
242 (math-neg (calcFunc-conj (nth 1 a)))) | |
243 ((let ((inf (math-infinitep a))) | |
244 (and inf | |
245 (math-mul (calcFunc-conj (math-infinite-dir a inf)) inf)))) | |
246 (t (calc-record-why 'numberp a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
247 (list 'calcFunc-conj a))))) |
40785 | 248 |
249 | |
250 ;;; Compute the complex argument of A. [F N] [Public] | |
251 (defun calcFunc-arg (a) | |
252 (cond ((Math-anglep a) | |
253 (if (math-negp a) (math-half-circle nil) 0)) | |
254 ((eq (car-safe a) 'cplx) | |
255 (calcFunc-arctan2 (nth 2 a) (nth 1 a))) | |
256 ((eq (car-safe a) 'polar) | |
257 (nth 2 a)) | |
258 ((eq (car a) 'vec) | |
259 (math-map-vec 'calcFunc-arg a)) | |
260 ((and (equal a '(var i var-i)) | |
261 (math-imaginary-i)) | |
262 (math-quarter-circle t)) | |
263 ((and (equal a '(neg (var i var-i))) | |
264 (math-imaginary-i)) | |
265 (math-neg (math-quarter-circle t))) | |
266 ((let ((signs (math-possible-signs a))) | |
267 (or (and (memq signs '(2 4 6)) 0) | |
268 (and (eq signs 1) (math-half-circle nil))))) | |
269 ((math-infinitep a) | |
270 (if (or (equal a '(var uinf var-uinf)) | |
271 (equal a '(var nan var-nan))) | |
272 '(var nan var-nan) | |
273 (calcFunc-arg (math-infinite-dir a)))) | |
274 (t (calc-record-why 'numvecp a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
275 (list 'calcFunc-arg a)))) |
40785 | 276 |
277 (defun math-imaginary-i () | |
278 (let ((val (calc-var-value 'var-i))) | |
279 (or (eq (car-safe val) 'special-const) | |
280 (equal val '(cplx 0 1)) | |
281 (and (eq (car-safe val) 'polar) | |
282 (eq (nth 1 val) 0) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
283 (Math-equal (nth 1 val) (math-quarter-circle nil)))))) |
40785 | 284 |
285 ;;; Extract the real or complex part of a complex number. [R N] [Public] | |
286 ;;; Also extracts the real part of a modulo form. | |
287 (defun calcFunc-re (a) | |
288 (let (aa bb) | |
289 (cond ((Math-realp a) a) | |
290 ((memq (car a) '(mod cplx)) | |
291 (nth 1 a)) | |
292 ((eq (car a) 'polar) | |
293 (math-mul (nth 1 a) (calcFunc-cos (nth 2 a)))) | |
294 ((eq (car a) 'vec) | |
295 (math-map-vec 'calcFunc-re a)) | |
296 ((math-known-realp a) a) | |
297 ((eq (car a) 'calcFunc-conj) | |
298 (calcFunc-re (nth 1 a))) | |
299 ((and (equal a '(var i var-i)) | |
300 (math-imaginary-i)) | |
301 0) | |
302 ((and (memq (car a) '(+ - *)) | |
303 (progn | |
304 (setq aa (calcFunc-re (nth 1 a)) | |
305 bb (calcFunc-re (nth 2 a))) | |
306 (or (not (eq (car-safe aa) 'calcFunc-re)) | |
307 (not (eq (car-safe bb) 'calcFunc-re))))) | |
308 (if (eq (car a) '+) | |
309 (math-add aa bb) | |
310 (if (eq (car a) '-) | |
311 (math-sub aa bb) | |
312 (math-sub (math-mul aa bb) | |
313 (math-mul (calcFunc-im (nth 1 a)) | |
314 (calcFunc-im (nth 2 a))))))) | |
315 ((and (eq (car a) '/) | |
316 (math-known-realp (nth 2 a))) | |
317 (math-div (calcFunc-re (nth 1 a)) (nth 2 a))) | |
318 ((eq (car a) 'neg) | |
319 (math-neg (calcFunc-re (nth 1 a)))) | |
320 (t (calc-record-why 'numberp a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
321 (list 'calcFunc-re a))))) |
40785 | 322 |
323 (defun calcFunc-im (a) | |
324 (let (aa bb) | |
325 (cond ((Math-realp a) | |
326 (if (math-floatp a) '(float 0 0) 0)) | |
327 ((eq (car a) 'cplx) | |
328 (nth 2 a)) | |
329 ((eq (car a) 'polar) | |
330 (math-mul (nth 1 a) (calcFunc-sin (nth 2 a)))) | |
331 ((eq (car a) 'vec) | |
332 (math-map-vec 'calcFunc-im a)) | |
333 ((math-known-realp a) | |
334 0) | |
335 ((eq (car a) 'calcFunc-conj) | |
336 (math-neg (calcFunc-im (nth 1 a)))) | |
337 ((and (equal a '(var i var-i)) | |
338 (math-imaginary-i)) | |
339 1) | |
340 ((and (memq (car a) '(+ - *)) | |
341 (progn | |
342 (setq aa (calcFunc-im (nth 1 a)) | |
343 bb (calcFunc-im (nth 2 a))) | |
344 (or (not (eq (car-safe aa) 'calcFunc-im)) | |
345 (not (eq (car-safe bb) 'calcFunc-im))))) | |
346 (if (eq (car a) '+) | |
347 (math-add aa bb) | |
348 (if (eq (car a) '-) | |
349 (math-sub aa bb) | |
350 (math-add (math-mul (calcFunc-re (nth 1 a)) bb) | |
351 (math-mul aa (calcFunc-re (nth 2 a))))))) | |
352 ((and (eq (car a) '/) | |
353 (math-known-realp (nth 2 a))) | |
354 (math-div (calcFunc-im (nth 1 a)) (nth 2 a))) | |
355 ((eq (car a) 'neg) | |
356 (math-neg (calcFunc-im (nth 1 a)))) | |
357 (t (calc-record-why 'numberp a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
358 (list 'calcFunc-im a))))) |
40785 | 359 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
360 ;;; calc-cplx.el ends here |