Close bug#5755.
[emacs.git] / lisp / calc / calc-cplx.el
blob6b0075e338b9165cee0ad089f85e563f34359bd1
1 ;;; calc-cplx.el --- Complex number functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 ;; This file is autoloaded from calc-ext.el.
30 (require 'calc-ext)
31 (require 'calc-macs)
33 (defun calc-argument (arg)
34 (interactive "P")
35 (calc-slow-wrapper
36 (calc-unary-op "arg" 'calcFunc-arg arg)))
38 (defun calc-re (arg)
39 (interactive "P")
40 (calc-slow-wrapper
41 (calc-unary-op "re" 'calcFunc-re arg)))
43 (defun calc-im (arg)
44 (interactive "P")
45 (calc-slow-wrapper
46 (calc-unary-op "im" 'calcFunc-im arg)))
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))
56 (calc-enter-result 1 "r-p" (list 'calcFunc-polar arg))))))
61 (defun calc-complex-notation ()
62 (interactive)
63 (calc-wrapper
64 (calc-change-mode 'calc-complex-format nil t)
65 (message "Displaying complex numbers in (X,Y) format")))
67 (defun calc-i-notation ()
68 (interactive)
69 (calc-wrapper
70 (calc-change-mode 'calc-complex-format 'i t)
71 (message "Displaying complex numbers in X+Yi format")))
73 (defun calc-j-notation ()
74 (interactive)
75 (calc-wrapper
76 (calc-change-mode 'calc-complex-format 'j t)
77 (message "Displaying complex numbers in X+Yj format")))
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)
88 (message "Preferred complex form is polar"))
89 (calc-change-mode 'calc-complex-mode 'cplx)
90 (message "Preferred complex form is rectangular"))))
93 ;;;; Complex numbers.
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))
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)))
109 (list 'polar r th)))))
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))))))
122 (t (list 'cplx a 0))))
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))
129 (list 'polar
130 (math-abs a)
131 (calcFunc-arg a)))))
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)))
143 (math-mul a '(var i var-i))))
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)
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)
159 ((eq (car-safe b) 'cplx)
160 nil)
161 (t (eq calc-complex-mode 'polar))))
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))
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))
178 (math-fix-circular (math-add a (math-two-pi)) 1))))
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))
185 (math-fix-circular (math-add a '(float 36 1)) 1))))))
188 ;;;; Complex numbers.
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)))
196 (t (list 'calcFunc-polar a))))
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)))
204 (t (list 'calcFunc-rect a))))
206 ;;; Compute the complex conjugate of A. [O O] [Public]
207 (defun calcFunc-conj (a)
208 (let (aa bb)
209 (cond ((Math-realp 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)
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)
243 (list 'calcFunc-conj a)))))
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)
271 (list 'calcFunc-arg a))))
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)
279 (Math-equal (nth 1 val) (math-quarter-circle nil))))))
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))
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)
317 (list 'calcFunc-re a)))))
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)
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))
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)
354 (list 'calcFunc-im a)))))
356 (provide 'calc-cplx)
358 ;; arch-tag: de73a331-941c-4507-ae76-46c76adc70dd
359 ;;; calc-cplx.el ends here