Mercurial > emacs
annotate lisp/calc/calc-cplx.el @ 112218:376148b31b5e
Add 2011 to FSF/AIST copyright years.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Sun, 02 Jan 2011 15:50:46 -0800 |
parents | 1d1d5d9bd884 |
children | ef719132ddfa |
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, |
112218
376148b31b5e
Add 2011 to FSF/AIST copyright years.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 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> |
77465
1154f082efd9
Update maintainer's address.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
76595
diff
changeset
|
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com> |
40785 | 8 |
9 ;; This file is part of GNU Emacs. | |
10 | |
94654
6c9af2bfcfee
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
11 ;; GNU Emacs is free software: you can redistribute it and/or modify |
76595
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
12 ;; it under the terms of the GNU General Public License as published by |
94654
6c9af2bfcfee
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; the Free Software Foundation, either version 3 of the License, or |
6c9af2bfcfee
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; (at your option) any later version. |
40785 | 15 |
76595
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
16 ;; GNU Emacs is distributed in the hope that it will be useful, |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
19 ;; GNU General Public License for more details. |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
20 |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
21 ;; You should have received a copy of the GNU General Public License |
94654
6c9af2bfcfee
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
40785 | 23 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
24 ;;; Commentary: |
40785 | 25 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
26 ;;; Code: |
40785 | 27 |
28 ;; 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
|
29 |
40785 | 30 (require 'calc-ext) |
31 (require 'calc-macs) | |
32 | |
33 (defun calc-argument (arg) | |
34 (interactive "P") | |
35 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
36 (calc-unary-op "arg" 'calcFunc-arg arg))) |
40785 | 37 |
38 (defun calc-re (arg) | |
39 (interactive "P") | |
40 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
41 (calc-unary-op "re" 'calcFunc-re arg))) |
40785 | 42 |
43 (defun calc-im (arg) | |
44 (interactive "P") | |
45 (calc-slow-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
46 (calc-unary-op "im" 'calcFunc-im arg))) |
40785 | 47 |
48 | |
49 (defun calc-polar () | |
50 (interactive) | |
51 (calc-slow-wrapper | |
52 (let ((arg (calc-top-n 1))) | |
53 (if (or (calc-is-inverse) | |
54 (eq (car-safe arg) 'polar)) | |
55 (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
|
56 (calc-enter-result 1 "r-p" (list 'calcFunc-polar arg)))))) |
40785 | 57 |
58 | |
59 | |
60 | |
61 (defun calc-complex-notation () | |
62 (interactive) | |
63 (calc-wrapper | |
64 (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
|
65 (message "Displaying complex numbers in (X,Y) format"))) |
40785 | 66 |
67 (defun calc-i-notation () | |
68 (interactive) | |
69 (calc-wrapper | |
70 (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
|
71 (message "Displaying complex numbers in X+Yi format"))) |
40785 | 72 |
73 (defun calc-j-notation () | |
74 (interactive) | |
75 (calc-wrapper | |
76 (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
|
77 (message "Displaying complex numbers in X+Yj format"))) |
40785 | 78 |
79 | |
80 (defun calc-polar-mode (n) | |
81 (interactive "P") | |
82 (calc-wrapper | |
83 (if (if n | |
84 (> (prefix-numeric-value n) 0) | |
85 (eq calc-complex-mode 'cplx)) | |
86 (progn | |
87 (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
|
88 (message "Preferred complex form is polar")) |
40785 | 89 (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
|
90 (message "Preferred complex form is rectangular")))) |
40785 | 91 |
92 | |
93 ;;;; Complex numbers. | |
94 | |
95 (defun math-normalize-polar (a) | |
96 (let ((r (math-normalize (nth 1 a))) | |
97 (th (math-normalize (nth 2 a)))) | |
98 (cond ((math-zerop r) | |
99 '(polar 0 0)) | |
100 ((or (math-zerop th)) | |
101 r) | |
102 ((and (not (eq calc-angle-mode 'rad)) | |
103 (or (equal th '(float 18 1)) | |
104 (equal th 180))) | |
105 (math-neg r)) | |
106 ((math-negp r) | |
107 (math-neg (list 'polar (math-neg r) th))) | |
108 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
109 (list 'polar r th))))) |
40785 | 110 |
111 | |
112 ;;; Coerce A to be complex (rectangular form). [c N] | |
113 (defun math-complex (a) | |
114 (cond ((eq (car-safe a) 'cplx) a) | |
115 ((eq (car-safe a) 'polar) | |
116 (if (math-zerop (nth 1 a)) | |
117 (nth 1 a) | |
118 (let ((sc (calcFunc-sincos (nth 2 a)))) | |
119 (list 'cplx | |
120 (math-mul (nth 1 a) (nth 1 sc)) | |
121 (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
|
122 (t (list 'cplx a 0)))) |
40785 | 123 |
124 ;;; Coerce A to be complex (polar form). [c N] | |
125 (defun math-polar (a) | |
126 (cond ((eq (car-safe a) 'polar) a) | |
127 ((math-zerop a) '(polar 0 0)) | |
128 (t | |
129 (list 'polar | |
130 (math-abs a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
131 (calcFunc-arg a))))) |
40785 | 132 |
133 ;;; Multiply A by the imaginary constant i. [N N] [Public] | |
134 (defun math-imaginary (a) | |
135 (if (and (or (Math-objvecp a) (math-infinitep a)) | |
136 (not calc-symbolic-mode)) | |
137 (math-mul a | |
138 (if (or (eq (car-safe a) 'polar) | |
139 (and (not (eq (car-safe a) 'cplx)) | |
140 (eq calc-complex-mode 'polar))) | |
141 (list 'polar 1 (math-quarter-circle nil)) | |
142 '(cplx 0 1))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
143 (math-mul a '(var i var-i)))) |
40785 | 144 |
145 | |
146 | |
147 | |
148 (defun math-want-polar (a b) | |
149 (cond ((eq (car-safe a) 'polar) | |
150 (if (eq (car-safe b) 'cplx) | |
151 (eq calc-complex-mode 'polar) | |
152 t)) | |
153 ((eq (car-safe a) 'cplx) | |
154 (if (eq (car-safe b) 'polar) | |
155 (eq calc-complex-mode 'polar) | |
156 nil)) | |
157 ((eq (car-safe b) 'polar) | |
158 t) | |
159 ((eq (car-safe b) 'cplx) | |
160 nil) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
161 (t (eq calc-complex-mode 'polar)))) |
40785 | 162 |
163 ;;; Force A to be in the (-pi,pi] or (-180,180] range. | |
164 (defun math-fix-circular (a &optional dir) ; [R R] | |
165 (cond ((eq (car-safe a) 'hms) | |
166 (cond ((and (Math-lessp 180 (nth 1 a)) (not (eq dir 1))) | |
167 (math-fix-circular (math-add a '(float -36 1)) -1)) | |
168 ((or (Math-lessp -180 (nth 1 a)) (eq dir -1)) | |
169 a) | |
170 (t | |
171 (math-fix-circular (math-add a '(float 36 1)) 1)))) | |
172 ((eq calc-angle-mode 'rad) | |
173 (cond ((and (Math-lessp (math-pi) a) (not (eq dir 1))) | |
174 (math-fix-circular (math-sub a (math-two-pi)) -1)) | |
175 ((or (Math-lessp (math-neg (math-pi)) a) (eq dir -1)) | |
176 a) | |
177 (t | |
178 (math-fix-circular (math-add a (math-two-pi)) 1)))) | |
179 (t | |
180 (cond ((and (Math-lessp '(float 18 1) a) (not (eq dir 1))) | |
181 (math-fix-circular (math-add a '(float -36 1)) -1)) | |
182 ((or (Math-lessp '(float -18 1) a) (eq dir -1)) | |
183 a) | |
184 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
185 (math-fix-circular (math-add a '(float 36 1)) 1)))))) |
40785 | 186 |
187 | |
188 ;;;; Complex numbers. | |
189 | |
190 (defun calcFunc-polar (a) ; [C N] [Public] | |
191 (cond ((Math-vectorp a) | |
192 (math-map-vec 'calcFunc-polar a)) | |
193 ((Math-realp a) a) | |
194 ((Math-numberp a) | |
195 (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
|
196 (t (list 'calcFunc-polar a)))) |
40785 | 197 |
198 (defun calcFunc-rect (a) ; [N N] [Public] | |
199 (cond ((Math-vectorp a) | |
200 (math-map-vec 'calcFunc-rect a)) | |
201 ((Math-realp a) a) | |
202 ((Math-numberp a) | |
203 (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
|
204 (t (list 'calcFunc-rect a)))) |
40785 | 205 |
206 ;;; Compute the complex conjugate of A. [O O] [Public] | |
207 (defun calcFunc-conj (a) | |
208 (let (aa bb) | |
209 (cond ((Math-realp a) | |
210 a) | |
211 ((eq (car a) 'cplx) | |
212 (list 'cplx (nth 1 a) (math-neg (nth 2 a)))) | |
213 ((eq (car a) 'polar) | |
214 (list 'polar (nth 1 a) (math-neg (nth 2 a)))) | |
215 ((eq (car a) 'vec) | |
216 (math-map-vec 'calcFunc-conj a)) | |
217 ((eq (car a) 'calcFunc-conj) | |
218 (nth 1 a)) | |
219 ((math-known-realp a) | |
220 a) | |
221 ((and (equal a '(var i var-i)) | |
222 (math-imaginary-i)) | |
223 (math-neg a)) | |
224 ((and (memq (car a) '(+ - * /)) | |
225 (progn | |
226 (setq aa (calcFunc-conj (nth 1 a)) | |
227 bb (calcFunc-conj (nth 2 a))) | |
228 (or (not (eq (car-safe aa) 'calcFunc-conj)) | |
229 (not (eq (car-safe bb) 'calcFunc-conj))))) | |
230 (if (eq (car a) '+) | |
231 (math-add aa bb) | |
232 (if (eq (car a) '-) | |
233 (math-sub aa bb) | |
234 (if (eq (car a) '*) | |
235 (math-mul aa bb) | |
236 (math-div aa bb))))) | |
237 ((eq (car a) 'neg) | |
238 (math-neg (calcFunc-conj (nth 1 a)))) | |
239 ((let ((inf (math-infinitep a))) | |
240 (and inf | |
241 (math-mul (calcFunc-conj (math-infinite-dir a inf)) inf)))) | |
242 (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
|
243 (list 'calcFunc-conj a))))) |
40785 | 244 |
245 | |
246 ;;; Compute the complex argument of A. [F N] [Public] | |
247 (defun calcFunc-arg (a) | |
248 (cond ((Math-anglep a) | |
249 (if (math-negp a) (math-half-circle nil) 0)) | |
250 ((eq (car-safe a) 'cplx) | |
251 (calcFunc-arctan2 (nth 2 a) (nth 1 a))) | |
252 ((eq (car-safe a) 'polar) | |
253 (nth 2 a)) | |
254 ((eq (car a) 'vec) | |
255 (math-map-vec 'calcFunc-arg a)) | |
256 ((and (equal a '(var i var-i)) | |
257 (math-imaginary-i)) | |
258 (math-quarter-circle t)) | |
259 ((and (equal a '(neg (var i var-i))) | |
260 (math-imaginary-i)) | |
261 (math-neg (math-quarter-circle t))) | |
262 ((let ((signs (math-possible-signs a))) | |
263 (or (and (memq signs '(2 4 6)) 0) | |
264 (and (eq signs 1) (math-half-circle nil))))) | |
265 ((math-infinitep a) | |
266 (if (or (equal a '(var uinf var-uinf)) | |
267 (equal a '(var nan var-nan))) | |
268 '(var nan var-nan) | |
269 (calcFunc-arg (math-infinite-dir a)))) | |
270 (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
|
271 (list 'calcFunc-arg a)))) |
40785 | 272 |
273 (defun math-imaginary-i () | |
274 (let ((val (calc-var-value 'var-i))) | |
275 (or (eq (car-safe val) 'special-const) | |
276 (equal val '(cplx 0 1)) | |
277 (and (eq (car-safe val) 'polar) | |
278 (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
|
279 (Math-equal (nth 1 val) (math-quarter-circle nil)))))) |
40785 | 280 |
281 ;;; Extract the real or complex part of a complex number. [R N] [Public] | |
282 ;;; Also extracts the real part of a modulo form. | |
283 (defun calcFunc-re (a) | |
284 (let (aa bb) | |
285 (cond ((Math-realp a) a) | |
286 ((memq (car a) '(mod cplx)) | |
287 (nth 1 a)) | |
288 ((eq (car a) 'polar) | |
289 (math-mul (nth 1 a) (calcFunc-cos (nth 2 a)))) | |
290 ((eq (car a) 'vec) | |
291 (math-map-vec 'calcFunc-re a)) | |
292 ((math-known-realp a) a) | |
293 ((eq (car a) 'calcFunc-conj) | |
294 (calcFunc-re (nth 1 a))) | |
295 ((and (equal a '(var i var-i)) | |
296 (math-imaginary-i)) | |
297 0) | |
298 ((and (memq (car a) '(+ - *)) | |
299 (progn | |
300 (setq aa (calcFunc-re (nth 1 a)) | |
301 bb (calcFunc-re (nth 2 a))) | |
302 (or (not (eq (car-safe aa) 'calcFunc-re)) | |
303 (not (eq (car-safe bb) 'calcFunc-re))))) | |
304 (if (eq (car a) '+) | |
305 (math-add aa bb) | |
306 (if (eq (car a) '-) | |
307 (math-sub aa bb) | |
308 (math-sub (math-mul aa bb) | |
309 (math-mul (calcFunc-im (nth 1 a)) | |
310 (calcFunc-im (nth 2 a))))))) | |
311 ((and (eq (car a) '/) | |
312 (math-known-realp (nth 2 a))) | |
313 (math-div (calcFunc-re (nth 1 a)) (nth 2 a))) | |
314 ((eq (car a) 'neg) | |
315 (math-neg (calcFunc-re (nth 1 a)))) | |
316 (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
|
317 (list 'calcFunc-re a))))) |
40785 | 318 |
319 (defun calcFunc-im (a) | |
320 (let (aa bb) | |
321 (cond ((Math-realp a) | |
322 (if (math-floatp a) '(float 0 0) 0)) | |
323 ((eq (car a) 'cplx) | |
324 (nth 2 a)) | |
325 ((eq (car a) 'polar) | |
326 (math-mul (nth 1 a) (calcFunc-sin (nth 2 a)))) | |
327 ((eq (car a) 'vec) | |
328 (math-map-vec 'calcFunc-im a)) | |
329 ((math-known-realp a) | |
330 0) | |
331 ((eq (car a) 'calcFunc-conj) | |
332 (math-neg (calcFunc-im (nth 1 a)))) | |
333 ((and (equal a '(var i var-i)) | |
334 (math-imaginary-i)) | |
335 1) | |
336 ((and (memq (car a) '(+ - *)) | |
337 (progn | |
338 (setq aa (calcFunc-im (nth 1 a)) | |
339 bb (calcFunc-im (nth 2 a))) | |
340 (or (not (eq (car-safe aa) 'calcFunc-im)) | |
341 (not (eq (car-safe bb) 'calcFunc-im))))) | |
342 (if (eq (car a) '+) | |
343 (math-add aa bb) | |
344 (if (eq (car a) '-) | |
345 (math-sub aa bb) | |
346 (math-add (math-mul (calcFunc-re (nth 1 a)) bb) | |
347 (math-mul aa (calcFunc-re (nth 2 a))))))) | |
348 ((and (eq (car a) '/) | |
349 (math-known-realp (nth 2 a))) | |
350 (math-div (calcFunc-im (nth 1 a)) (nth 2 a))) | |
351 ((eq (car a) 'neg) | |
352 (math-neg (calcFunc-im (nth 1 a)))) | |
353 (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
|
354 (list 'calcFunc-im a))))) |
40785 | 355 |
58650
d0909149f67b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
356 (provide 'calc-cplx) |
d0909149f67b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
357 |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
79702
diff
changeset
|
358 ;; 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
|
359 ;;; calc-cplx.el ends here |