(visit-tags-table-buffer): Add autoload cookie;
[emacs.git] / lisp / calc / calc-cplx.el
blob3730254403af7f1946bd5d2abe27487650a2cfbb
1 ;;; calc-cplx.el --- Complex number functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
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.
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.
26 ;;; Commentary:
28 ;;; Code:
30 ;; This file is autoloaded from calc-ext.el.
31 (require 'calc-ext)
33 (require 'calc-macs)
35 (defun calc-Need-calc-cplx () nil)
38 (defun calc-argument (arg)
39 (interactive "P")
40 (calc-slow-wrapper
41 (calc-unary-op "arg" 'calcFunc-arg arg)))
43 (defun calc-re (arg)
44 (interactive "P")
45 (calc-slow-wrapper
46 (calc-unary-op "re" 'calcFunc-re arg)))
48 (defun calc-im (arg)
49 (interactive "P")
50 (calc-slow-wrapper
51 (calc-unary-op "im" 'calcFunc-im arg)))
54 (defun calc-polar ()
55 (interactive)
56 (calc-slow-wrapper
57 (let ((arg (calc-top-n 1)))
58 (if (or (calc-is-inverse)
59 (eq (car-safe arg) 'polar))
60 (calc-enter-result 1 "p-r" (list 'calcFunc-rect arg))
61 (calc-enter-result 1 "r-p" (list 'calcFunc-polar arg))))))
66 (defun calc-complex-notation ()
67 (interactive)
68 (calc-wrapper
69 (calc-change-mode 'calc-complex-format nil t)
70 (message "Displaying complex numbers in (X,Y) format")))
72 (defun calc-i-notation ()
73 (interactive)
74 (calc-wrapper
75 (calc-change-mode 'calc-complex-format 'i t)
76 (message "Displaying complex numbers in X+Yi format")))
78 (defun calc-j-notation ()
79 (interactive)
80 (calc-wrapper
81 (calc-change-mode 'calc-complex-format 'j t)
82 (message "Displaying complex numbers in X+Yj format")))
85 (defun calc-polar-mode (n)
86 (interactive "P")
87 (calc-wrapper
88 (if (if n
89 (> (prefix-numeric-value n) 0)
90 (eq calc-complex-mode 'cplx))
91 (progn
92 (calc-change-mode 'calc-complex-mode 'polar)
93 (message "Preferred complex form is polar"))
94 (calc-change-mode 'calc-complex-mode 'cplx)
95 (message "Preferred complex form is rectangular"))))
98 ;;;; Complex numbers.
100 (defun math-normalize-polar (a)
101 (let ((r (math-normalize (nth 1 a)))
102 (th (math-normalize (nth 2 a))))
103 (cond ((math-zerop r)
104 '(polar 0 0))
105 ((or (math-zerop th))
107 ((and (not (eq calc-angle-mode 'rad))
108 (or (equal th '(float 18 1))
109 (equal th 180)))
110 (math-neg r))
111 ((math-negp r)
112 (math-neg (list 'polar (math-neg r) th)))
114 (list 'polar r th)))))
117 ;;; Coerce A to be complex (rectangular form). [c N]
118 (defun math-complex (a)
119 (cond ((eq (car-safe a) 'cplx) a)
120 ((eq (car-safe a) 'polar)
121 (if (math-zerop (nth 1 a))
122 (nth 1 a)
123 (let ((sc (calcFunc-sincos (nth 2 a))))
124 (list 'cplx
125 (math-mul (nth 1 a) (nth 1 sc))
126 (math-mul (nth 1 a) (nth 2 sc))))))
127 (t (list 'cplx a 0))))
129 ;;; Coerce A to be complex (polar form). [c N]
130 (defun math-polar (a)
131 (cond ((eq (car-safe a) 'polar) a)
132 ((math-zerop a) '(polar 0 0))
134 (list 'polar
135 (math-abs a)
136 (calcFunc-arg a)))))
138 ;;; Multiply A by the imaginary constant i. [N N] [Public]
139 (defun math-imaginary (a)
140 (if (and (or (Math-objvecp a) (math-infinitep a))
141 (not calc-symbolic-mode))
142 (math-mul a
143 (if (or (eq (car-safe a) 'polar)
144 (and (not (eq (car-safe a) 'cplx))
145 (eq calc-complex-mode 'polar)))
146 (list 'polar 1 (math-quarter-circle nil))
147 '(cplx 0 1)))
148 (math-mul a '(var i var-i))))
153 (defun math-want-polar (a b)
154 (cond ((eq (car-safe a) 'polar)
155 (if (eq (car-safe b) 'cplx)
156 (eq calc-complex-mode 'polar)
158 ((eq (car-safe a) 'cplx)
159 (if (eq (car-safe b) 'polar)
160 (eq calc-complex-mode 'polar)
161 nil))
162 ((eq (car-safe b) 'polar)
164 ((eq (car-safe b) 'cplx)
165 nil)
166 (t (eq calc-complex-mode 'polar))))
168 ;;; Force A to be in the (-pi,pi] or (-180,180] range.
169 (defun math-fix-circular (a &optional dir) ; [R R]
170 (cond ((eq (car-safe a) 'hms)
171 (cond ((and (Math-lessp 180 (nth 1 a)) (not (eq dir 1)))
172 (math-fix-circular (math-add a '(float -36 1)) -1))
173 ((or (Math-lessp -180 (nth 1 a)) (eq dir -1))
176 (math-fix-circular (math-add a '(float 36 1)) 1))))
177 ((eq calc-angle-mode 'rad)
178 (cond ((and (Math-lessp (math-pi) a) (not (eq dir 1)))
179 (math-fix-circular (math-sub a (math-two-pi)) -1))
180 ((or (Math-lessp (math-neg (math-pi)) a) (eq dir -1))
183 (math-fix-circular (math-add a (math-two-pi)) 1))))
185 (cond ((and (Math-lessp '(float 18 1) a) (not (eq dir 1)))
186 (math-fix-circular (math-add a '(float -36 1)) -1))
187 ((or (Math-lessp '(float -18 1) a) (eq dir -1))
190 (math-fix-circular (math-add a '(float 36 1)) 1))))))
193 ;;;; Complex numbers.
195 (defun calcFunc-polar (a) ; [C N] [Public]
196 (cond ((Math-vectorp a)
197 (math-map-vec 'calcFunc-polar a))
198 ((Math-realp a) a)
199 ((Math-numberp a)
200 (math-normalize (math-polar a)))
201 (t (list 'calcFunc-polar a))))
203 (defun calcFunc-rect (a) ; [N N] [Public]
204 (cond ((Math-vectorp a)
205 (math-map-vec 'calcFunc-rect a))
206 ((Math-realp a) a)
207 ((Math-numberp a)
208 (math-normalize (math-complex a)))
209 (t (list 'calcFunc-rect a))))
211 ;;; Compute the complex conjugate of A. [O O] [Public]
212 (defun calcFunc-conj (a)
213 (let (aa bb)
214 (cond ((Math-realp a)
216 ((eq (car a) 'cplx)
217 (list 'cplx (nth 1 a) (math-neg (nth 2 a))))
218 ((eq (car a) 'polar)
219 (list 'polar (nth 1 a) (math-neg (nth 2 a))))
220 ((eq (car a) 'vec)
221 (math-map-vec 'calcFunc-conj a))
222 ((eq (car a) 'calcFunc-conj)
223 (nth 1 a))
224 ((math-known-realp a)
226 ((and (equal a '(var i var-i))
227 (math-imaginary-i))
228 (math-neg a))
229 ((and (memq (car a) '(+ - * /))
230 (progn
231 (setq aa (calcFunc-conj (nth 1 a))
232 bb (calcFunc-conj (nth 2 a)))
233 (or (not (eq (car-safe aa) 'calcFunc-conj))
234 (not (eq (car-safe bb) 'calcFunc-conj)))))
235 (if (eq (car a) '+)
236 (math-add aa bb)
237 (if (eq (car a) '-)
238 (math-sub aa bb)
239 (if (eq (car a) '*)
240 (math-mul aa bb)
241 (math-div aa bb)))))
242 ((eq (car a) 'neg)
243 (math-neg (calcFunc-conj (nth 1 a))))
244 ((let ((inf (math-infinitep a)))
245 (and inf
246 (math-mul (calcFunc-conj (math-infinite-dir a inf)) inf))))
247 (t (calc-record-why 'numberp a)
248 (list 'calcFunc-conj a)))))
251 ;;; Compute the complex argument of A. [F N] [Public]
252 (defun calcFunc-arg (a)
253 (cond ((Math-anglep a)
254 (if (math-negp a) (math-half-circle nil) 0))
255 ((eq (car-safe a) 'cplx)
256 (calcFunc-arctan2 (nth 2 a) (nth 1 a)))
257 ((eq (car-safe a) 'polar)
258 (nth 2 a))
259 ((eq (car a) 'vec)
260 (math-map-vec 'calcFunc-arg a))
261 ((and (equal a '(var i var-i))
262 (math-imaginary-i))
263 (math-quarter-circle t))
264 ((and (equal a '(neg (var i var-i)))
265 (math-imaginary-i))
266 (math-neg (math-quarter-circle t)))
267 ((let ((signs (math-possible-signs a)))
268 (or (and (memq signs '(2 4 6)) 0)
269 (and (eq signs 1) (math-half-circle nil)))))
270 ((math-infinitep a)
271 (if (or (equal a '(var uinf var-uinf))
272 (equal a '(var nan var-nan)))
273 '(var nan var-nan)
274 (calcFunc-arg (math-infinite-dir a))))
275 (t (calc-record-why 'numvecp a)
276 (list 'calcFunc-arg a))))
278 (defun math-imaginary-i ()
279 (let ((val (calc-var-value 'var-i)))
280 (or (eq (car-safe val) 'special-const)
281 (equal val '(cplx 0 1))
282 (and (eq (car-safe val) 'polar)
283 (eq (nth 1 val) 0)
284 (Math-equal (nth 1 val) (math-quarter-circle nil))))))
286 ;;; Extract the real or complex part of a complex number. [R N] [Public]
287 ;;; Also extracts the real part of a modulo form.
288 (defun calcFunc-re (a)
289 (let (aa bb)
290 (cond ((Math-realp a) a)
291 ((memq (car a) '(mod cplx))
292 (nth 1 a))
293 ((eq (car a) 'polar)
294 (math-mul (nth 1 a) (calcFunc-cos (nth 2 a))))
295 ((eq (car a) 'vec)
296 (math-map-vec 'calcFunc-re a))
297 ((math-known-realp a) a)
298 ((eq (car a) 'calcFunc-conj)
299 (calcFunc-re (nth 1 a)))
300 ((and (equal a '(var i var-i))
301 (math-imaginary-i))
303 ((and (memq (car a) '(+ - *))
304 (progn
305 (setq aa (calcFunc-re (nth 1 a))
306 bb (calcFunc-re (nth 2 a)))
307 (or (not (eq (car-safe aa) 'calcFunc-re))
308 (not (eq (car-safe bb) 'calcFunc-re)))))
309 (if (eq (car a) '+)
310 (math-add aa bb)
311 (if (eq (car a) '-)
312 (math-sub aa bb)
313 (math-sub (math-mul aa bb)
314 (math-mul (calcFunc-im (nth 1 a))
315 (calcFunc-im (nth 2 a)))))))
316 ((and (eq (car a) '/)
317 (math-known-realp (nth 2 a)))
318 (math-div (calcFunc-re (nth 1 a)) (nth 2 a)))
319 ((eq (car a) 'neg)
320 (math-neg (calcFunc-re (nth 1 a))))
321 (t (calc-record-why 'numberp a)
322 (list 'calcFunc-re a)))))
324 (defun calcFunc-im (a)
325 (let (aa bb)
326 (cond ((Math-realp a)
327 (if (math-floatp a) '(float 0 0) 0))
328 ((eq (car a) 'cplx)
329 (nth 2 a))
330 ((eq (car a) 'polar)
331 (math-mul (nth 1 a) (calcFunc-sin (nth 2 a))))
332 ((eq (car a) 'vec)
333 (math-map-vec 'calcFunc-im a))
334 ((math-known-realp a)
336 ((eq (car a) 'calcFunc-conj)
337 (math-neg (calcFunc-im (nth 1 a))))
338 ((and (equal a '(var i var-i))
339 (math-imaginary-i))
341 ((and (memq (car a) '(+ - *))
342 (progn
343 (setq aa (calcFunc-im (nth 1 a))
344 bb (calcFunc-im (nth 2 a)))
345 (or (not (eq (car-safe aa) 'calcFunc-im))
346 (not (eq (car-safe bb) 'calcFunc-im)))))
347 (if (eq (car a) '+)
348 (math-add aa bb)
349 (if (eq (car a) '-)
350 (math-sub aa bb)
351 (math-add (math-mul (calcFunc-re (nth 1 a)) bb)
352 (math-mul aa (calcFunc-re (nth 2 a)))))))
353 ((and (eq (car a) '/)
354 (math-known-realp (nth 2 a)))
355 (math-div (calcFunc-im (nth 1 a)) (nth 2 a)))
356 ((eq (car a) 'neg)
357 (math-neg (calcFunc-im (nth 1 a))))
358 (t (calc-record-why 'numberp a)
359 (list 'calcFunc-im a)))))
361 ;;; calc-cplx.el ends here