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/>.
28 ;; This file is autoloaded from calc-ext.el.
33 (defun calc-argument (arg)
36 (calc-unary-op "arg" 'calcFunc-arg arg
)))
41 (calc-unary-op "re" 'calcFunc-re arg
)))
46 (calc-unary-op "im" 'calcFunc-im arg
)))
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 ()
64 (calc-change-mode 'calc-complex-format nil t
)
65 (message "Displaying complex numbers in (X,Y) format")))
67 (defun calc-i-notation ()
70 (calc-change-mode 'calc-complex-format
'i t
)
71 (message "Displaying complex numbers in X+Yi format")))
73 (defun calc-j-notation ()
76 (calc-change-mode 'calc-complex-format
'j t
)
77 (message "Displaying complex numbers in X+Yj format")))
80 (defun calc-polar-mode (n)
84 (> (prefix-numeric-value n
) 0)
85 (eq calc-complex-mode
'cplx
))
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"))))
95 (defun math-normalize-polar (a)
96 (let ((r (math-normalize (nth 1 a
)))
97 (th (math-normalize (nth 2 a
))))
100 ((or (math-zerop th
))
102 ((and (not (eq calc-angle-mode
'rad
))
103 (or (equal th
'(float 18 1))
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
))
118 (let ((sc (calcFunc-sincos (nth 2 a
))))
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))
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
))
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
))
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
)
157 ((eq (car-safe b
) 'polar
)
159 ((eq (car-safe b
) 'cplx
)
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
))
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
))
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)
209 (cond ((Math-realp a
)
212 (list 'cplx
(nth 1 a
) (math-neg (nth 2 a
))))
214 (list 'polar
(nth 1 a
) (math-neg (nth 2 a
))))
216 (math-map-vec 'calcFunc-conj a
))
217 ((eq (car a
) 'calcFunc-conj
)
219 ((math-known-realp a
)
221 ((and (equal a
'(var i var-i
))
224 ((and (memq (car a
) '(+ -
* /))
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
)))))
238 (math-neg (calcFunc-conj (nth 1 a
))))
239 ((let ((inf (math-infinitep a
)))
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
)
255 (math-map-vec 'calcFunc-arg a
))
256 ((and (equal a
'(var i var-i
))
258 (math-quarter-circle t
))
259 ((and (equal a
'(neg (var i var-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
)))))
266 (if (or (equal a
'(var uinf var-uinf
))
267 (equal a
'(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
)
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)
285 (cond ((Math-realp a
) a
)
286 ((memq (car a
) '(mod cplx
))
289 (math-mul (nth 1 a
) (calcFunc-cos (nth 2 a
))))
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
))
298 ((and (memq (car a
) '(+ -
*))
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
)))))
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
)))
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)
321 (cond ((Math-realp a
)
322 (if (math-floatp a
) '(float 0 0) 0))
326 (math-mul (nth 1 a
) (calcFunc-sin (nth 2 a
))))
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
))
336 ((and (memq (car a
) '(+ -
*))
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
)))))
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
)))
352 (math-neg (calcFunc-im (nth 1 a
))))
353 (t (calc-record-why 'numberp a
)
354 (list 'calcFunc-im a
)))))
358 ;; arch-tag: de73a331-941c-4507-ae76-46c76adc70dd
359 ;;; calc-cplx.el ends here