Mercurial > emacs
annotate lisp/calc/calc-cplx.el @ 76260:3823acfb56c7
Comment fix.
author | Vinicius Jose Latorre <viniciusjl@ig.com.br> |
---|---|
date | Thu, 01 Mar 2007 20:02:32 +0000 |
parents | 7a3f13e2dd57 |
children | 497d17a80bb8 95d0cdf160ea |
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 |
64325
1db49616ce05
Update copyright information.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62442
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004, |
75346 | 4 ;; 2005, 2006, 2007 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
|
5 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
6 ;; Author: David Gillespie <daveg@synaptics.com> |
58650
d0909149f67b
Add a provide statement.
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:
41047
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:
41047
diff
changeset
|
28 ;;; Code: |
40785 | 29 |
30 ;; This file is autoloaded from calc-ext.el. | |
58650
d0909149f67b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
31 |
40785 | 32 (require 'calc-ext) |
33 (require 'calc-macs) | |
34 | |
35 (defun calc-argument (arg) | |
36 (interactive "P") | |
37 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
38 (calc-unary-op "arg" 'calcFunc-arg arg))) |
40785 | 39 |
40 (defun calc-re (arg) | |
41 (interactive "P") | |
42 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
43 (calc-unary-op "re" 'calcFunc-re arg))) |
40785 | 44 |
45 (defun calc-im (arg) | |
46 (interactive "P") | |
47 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
48 (calc-unary-op "im" 'calcFunc-im arg))) |
40785 | 49 |
50 | |
51 (defun calc-polar () | |
52 (interactive) | |
53 (calc-slow-wrapper | |
54 (let ((arg (calc-top-n 1))) | |
55 (if (or (calc-is-inverse) | |
56 (eq (car-safe arg) 'polar)) | |
57 (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
|
58 (calc-enter-result 1 "r-p" (list 'calcFunc-polar arg)))))) |
40785 | 59 |
60 | |
61 | |
62 | |
63 (defun calc-complex-notation () | |
64 (interactive) | |
65 (calc-wrapper | |
66 (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
|
67 (message "Displaying complex numbers in (X,Y) format"))) |
40785 | 68 |
69 (defun calc-i-notation () | |
70 (interactive) | |
71 (calc-wrapper | |
72 (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
|
73 (message "Displaying complex numbers in X+Yi format"))) |
40785 | 74 |
75 (defun calc-j-notation () | |
76 (interactive) | |
77 (calc-wrapper | |
78 (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
|
79 (message "Displaying complex numbers in X+Yj format"))) |
40785 | 80 |
81 | |
82 (defun calc-polar-mode (n) | |
83 (interactive "P") | |
84 (calc-wrapper | |
85 (if (if n | |
86 (> (prefix-numeric-value n) 0) | |
87 (eq calc-complex-mode 'cplx)) | |
88 (progn | |
89 (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
|
90 (message "Preferred complex form is polar")) |
40785 | 91 (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
|
92 (message "Preferred complex form is rectangular")))) |
40785 | 93 |
94 | |
95 ;;;; Complex numbers. | |
96 | |
97 (defun math-normalize-polar (a) | |
98 (let ((r (math-normalize (nth 1 a))) | |
99 (th (math-normalize (nth 2 a)))) | |
100 (cond ((math-zerop r) | |
101 '(polar 0 0)) | |
102 ((or (math-zerop th)) | |
103 r) | |
104 ((and (not (eq calc-angle-mode 'rad)) | |
105 (or (equal th '(float 18 1)) | |
106 (equal th 180))) | |
107 (math-neg r)) | |
108 ((math-negp r) | |
109 (math-neg (list 'polar (math-neg r) th))) | |
110 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
111 (list 'polar r th))))) |
40785 | 112 |
113 | |
114 ;;; Coerce A to be complex (rectangular form). [c N] | |
115 (defun math-complex (a) | |
116 (cond ((eq (car-safe a) 'cplx) a) | |
117 ((eq (car-safe a) 'polar) | |
118 (if (math-zerop (nth 1 a)) | |
119 (nth 1 a) | |
120 (let ((sc (calcFunc-sincos (nth 2 a)))) | |
121 (list 'cplx | |
122 (math-mul (nth 1 a) (nth 1 sc)) | |
123 (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
|
124 (t (list 'cplx a 0)))) |
40785 | 125 |
126 ;;; Coerce A to be complex (polar form). [c N] | |
127 (defun math-polar (a) | |
128 (cond ((eq (car-safe a) 'polar) a) | |
129 ((math-zerop a) '(polar 0 0)) | |
130 (t | |
131 (list 'polar | |
132 (math-abs a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
133 (calcFunc-arg a))))) |
40785 | 134 |
135 ;;; Multiply A by the imaginary constant i. [N N] [Public] | |
136 (defun math-imaginary (a) | |
137 (if (and (or (Math-objvecp a) (math-infinitep a)) | |
138 (not calc-symbolic-mode)) | |
139 (math-mul a | |
140 (if (or (eq (car-safe a) 'polar) | |
141 (and (not (eq (car-safe a) 'cplx)) | |
142 (eq calc-complex-mode 'polar))) | |
143 (list 'polar 1 (math-quarter-circle nil)) | |
144 '(cplx 0 1))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
145 (math-mul a '(var i var-i)))) |
40785 | 146 |
147 | |
148 | |
149 | |
150 (defun math-want-polar (a b) | |
151 (cond ((eq (car-safe a) 'polar) | |
152 (if (eq (car-safe b) 'cplx) | |
153 (eq calc-complex-mode 'polar) | |
154 t)) | |
155 ((eq (car-safe a) 'cplx) | |
156 (if (eq (car-safe b) 'polar) | |
157 (eq calc-complex-mode 'polar) | |
158 nil)) | |
159 ((eq (car-safe b) 'polar) | |
160 t) | |
161 ((eq (car-safe b) 'cplx) | |
162 nil) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
163 (t (eq calc-complex-mode 'polar)))) |
40785 | 164 |
165 ;;; Force A to be in the (-pi,pi] or (-180,180] range. | |
166 (defun math-fix-circular (a &optional dir) ; [R R] | |
167 (cond ((eq (car-safe a) 'hms) | |
168 (cond ((and (Math-lessp 180 (nth 1 a)) (not (eq dir 1))) | |
169 (math-fix-circular (math-add a '(float -36 1)) -1)) | |
170 ((or (Math-lessp -180 (nth 1 a)) (eq dir -1)) | |
171 a) | |
172 (t | |
173 (math-fix-circular (math-add a '(float 36 1)) 1)))) | |
174 ((eq calc-angle-mode 'rad) | |
175 (cond ((and (Math-lessp (math-pi) a) (not (eq dir 1))) | |
176 (math-fix-circular (math-sub a (math-two-pi)) -1)) | |
177 ((or (Math-lessp (math-neg (math-pi)) a) (eq dir -1)) | |
178 a) | |
179 (t | |
180 (math-fix-circular (math-add a (math-two-pi)) 1)))) | |
181 (t | |
182 (cond ((and (Math-lessp '(float 18 1) a) (not (eq dir 1))) | |
183 (math-fix-circular (math-add a '(float -36 1)) -1)) | |
184 ((or (Math-lessp '(float -18 1) a) (eq dir -1)) | |
185 a) | |
186 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
187 (math-fix-circular (math-add a '(float 36 1)) 1)))))) |
40785 | 188 |
189 | |
190 ;;;; Complex numbers. | |
191 | |
192 (defun calcFunc-polar (a) ; [C N] [Public] | |
193 (cond ((Math-vectorp a) | |
194 (math-map-vec 'calcFunc-polar a)) | |
195 ((Math-realp a) a) | |
196 ((Math-numberp a) | |
197 (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
|
198 (t (list 'calcFunc-polar a)))) |
40785 | 199 |
200 (defun calcFunc-rect (a) ; [N N] [Public] | |
201 (cond ((Math-vectorp a) | |
202 (math-map-vec 'calcFunc-rect a)) | |
203 ((Math-realp a) a) | |
204 ((Math-numberp a) | |
205 (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
|
206 (t (list 'calcFunc-rect a)))) |
40785 | 207 |
208 ;;; Compute the complex conjugate of A. [O O] [Public] | |
209 (defun calcFunc-conj (a) | |
210 (let (aa bb) | |
211 (cond ((Math-realp a) | |
212 a) | |
213 ((eq (car a) 'cplx) | |
214 (list 'cplx (nth 1 a) (math-neg (nth 2 a)))) | |
215 ((eq (car a) 'polar) | |
216 (list 'polar (nth 1 a) (math-neg (nth 2 a)))) | |
217 ((eq (car a) 'vec) | |
218 (math-map-vec 'calcFunc-conj a)) | |
219 ((eq (car a) 'calcFunc-conj) | |
220 (nth 1 a)) | |
221 ((math-known-realp a) | |
222 a) | |
223 ((and (equal a '(var i var-i)) | |
224 (math-imaginary-i)) | |
225 (math-neg a)) | |
226 ((and (memq (car a) '(+ - * /)) | |
227 (progn | |
228 (setq aa (calcFunc-conj (nth 1 a)) | |
229 bb (calcFunc-conj (nth 2 a))) | |
230 (or (not (eq (car-safe aa) 'calcFunc-conj)) | |
231 (not (eq (car-safe bb) 'calcFunc-conj))))) | |
232 (if (eq (car a) '+) | |
233 (math-add aa bb) | |
234 (if (eq (car a) '-) | |
235 (math-sub aa bb) | |
236 (if (eq (car a) '*) | |
237 (math-mul aa bb) | |
238 (math-div aa bb))))) | |
239 ((eq (car a) 'neg) | |
240 (math-neg (calcFunc-conj (nth 1 a)))) | |
241 ((let ((inf (math-infinitep a))) | |
242 (and inf | |
243 (math-mul (calcFunc-conj (math-infinite-dir a inf)) inf)))) | |
244 (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
|
245 (list 'calcFunc-conj a))))) |
40785 | 246 |
247 | |
248 ;;; Compute the complex argument of A. [F N] [Public] | |
249 (defun calcFunc-arg (a) | |
250 (cond ((Math-anglep a) | |
251 (if (math-negp a) (math-half-circle nil) 0)) | |
252 ((eq (car-safe a) 'cplx) | |
253 (calcFunc-arctan2 (nth 2 a) (nth 1 a))) | |
254 ((eq (car-safe a) 'polar) | |
255 (nth 2 a)) | |
256 ((eq (car a) 'vec) | |
257 (math-map-vec 'calcFunc-arg a)) | |
258 ((and (equal a '(var i var-i)) | |
259 (math-imaginary-i)) | |
260 (math-quarter-circle t)) | |
261 ((and (equal a '(neg (var i var-i))) | |
262 (math-imaginary-i)) | |
263 (math-neg (math-quarter-circle t))) | |
264 ((let ((signs (math-possible-signs a))) | |
265 (or (and (memq signs '(2 4 6)) 0) | |
266 (and (eq signs 1) (math-half-circle nil))))) | |
267 ((math-infinitep a) | |
268 (if (or (equal a '(var uinf var-uinf)) | |
269 (equal a '(var nan var-nan))) | |
270 '(var nan var-nan) | |
271 (calcFunc-arg (math-infinite-dir a)))) | |
272 (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
|
273 (list 'calcFunc-arg a)))) |
40785 | 274 |
275 (defun math-imaginary-i () | |
276 (let ((val (calc-var-value 'var-i))) | |
277 (or (eq (car-safe val) 'special-const) | |
278 (equal val '(cplx 0 1)) | |
279 (and (eq (car-safe val) 'polar) | |
280 (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
|
281 (Math-equal (nth 1 val) (math-quarter-circle nil)))))) |
40785 | 282 |
283 ;;; Extract the real or complex part of a complex number. [R N] [Public] | |
284 ;;; Also extracts the real part of a modulo form. | |
285 (defun calcFunc-re (a) | |
286 (let (aa bb) | |
287 (cond ((Math-realp a) a) | |
288 ((memq (car a) '(mod cplx)) | |
289 (nth 1 a)) | |
290 ((eq (car a) 'polar) | |
291 (math-mul (nth 1 a) (calcFunc-cos (nth 2 a)))) | |
292 ((eq (car a) 'vec) | |
293 (math-map-vec 'calcFunc-re a)) | |
294 ((math-known-realp a) a) | |
295 ((eq (car a) 'calcFunc-conj) | |
296 (calcFunc-re (nth 1 a))) | |
297 ((and (equal a '(var i var-i)) | |
298 (math-imaginary-i)) | |
299 0) | |
300 ((and (memq (car a) '(+ - *)) | |
301 (progn | |
302 (setq aa (calcFunc-re (nth 1 a)) | |
303 bb (calcFunc-re (nth 2 a))) | |
304 (or (not (eq (car-safe aa) 'calcFunc-re)) | |
305 (not (eq (car-safe bb) 'calcFunc-re))))) | |
306 (if (eq (car a) '+) | |
307 (math-add aa bb) | |
308 (if (eq (car a) '-) | |
309 (math-sub aa bb) | |
310 (math-sub (math-mul aa bb) | |
311 (math-mul (calcFunc-im (nth 1 a)) | |
312 (calcFunc-im (nth 2 a))))))) | |
313 ((and (eq (car a) '/) | |
314 (math-known-realp (nth 2 a))) | |
315 (math-div (calcFunc-re (nth 1 a)) (nth 2 a))) | |
316 ((eq (car a) 'neg) | |
317 (math-neg (calcFunc-re (nth 1 a)))) | |
318 (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
|
319 (list 'calcFunc-re a))))) |
40785 | 320 |
321 (defun calcFunc-im (a) | |
322 (let (aa bb) | |
323 (cond ((Math-realp a) | |
324 (if (math-floatp a) '(float 0 0) 0)) | |
325 ((eq (car a) 'cplx) | |
326 (nth 2 a)) | |
327 ((eq (car a) 'polar) | |
328 (math-mul (nth 1 a) (calcFunc-sin (nth 2 a)))) | |
329 ((eq (car a) 'vec) | |
330 (math-map-vec 'calcFunc-im a)) | |
331 ((math-known-realp a) | |
332 0) | |
333 ((eq (car a) 'calcFunc-conj) | |
334 (math-neg (calcFunc-im (nth 1 a)))) | |
335 ((and (equal a '(var i var-i)) | |
336 (math-imaginary-i)) | |
337 1) | |
338 ((and (memq (car a) '(+ - *)) | |
339 (progn | |
340 (setq aa (calcFunc-im (nth 1 a)) | |
341 bb (calcFunc-im (nth 2 a))) | |
342 (or (not (eq (car-safe aa) 'calcFunc-im)) | |
343 (not (eq (car-safe bb) 'calcFunc-im))))) | |
344 (if (eq (car a) '+) | |
345 (math-add aa bb) | |
346 (if (eq (car a) '-) | |
347 (math-sub aa bb) | |
348 (math-add (math-mul (calcFunc-re (nth 1 a)) bb) | |
349 (math-mul aa (calcFunc-re (nth 2 a))))))) | |
350 ((and (eq (car a) '/) | |
351 (math-known-realp (nth 2 a))) | |
352 (math-div (calcFunc-im (nth 1 a)) (nth 2 a))) | |
353 ((eq (car a) 'neg) | |
354 (math-neg (calcFunc-im (nth 1 a)))) | |
355 (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
|
356 (list 'calcFunc-im a))))) |
40785 | 357 |
58650
d0909149f67b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
358 (provide 'calc-cplx) |
d0909149f67b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
359 |
52401 | 360 ;;; arch-tag: de73a331-941c-4507-ae76-46c76adc70dd |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
361 ;;; calc-cplx.el ends here |