1 ;;; calc-cplx.el --- Complex number functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <belanger@truman.edu>
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.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-argument (arg)
38 (calc-unary-op "arg" 'calcFunc-arg arg
)))
43 (calc-unary-op "re" 'calcFunc-re arg
)))
48 (calc-unary-op "im" 'calcFunc-im arg
)))
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
))
58 (calc-enter-result 1 "r-p" (list 'calcFunc-polar arg
))))))
63 (defun calc-complex-notation ()
66 (calc-change-mode 'calc-complex-format nil t
)
67 (message "Displaying complex numbers in (X,Y) format")))
69 (defun calc-i-notation ()
72 (calc-change-mode 'calc-complex-format
'i t
)
73 (message "Displaying complex numbers in X+Yi format")))
75 (defun calc-j-notation ()
78 (calc-change-mode 'calc-complex-format
'j t
)
79 (message "Displaying complex numbers in X+Yj format")))
82 (defun calc-polar-mode (n)
86 (> (prefix-numeric-value n
) 0)
87 (eq calc-complex-mode
'cplx
))
89 (calc-change-mode 'calc-complex-mode
'polar
)
90 (message "Preferred complex form is polar"))
91 (calc-change-mode 'calc-complex-mode
'cplx
)
92 (message "Preferred complex form is rectangular"))))
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
)
102 ((or (math-zerop th
))
104 ((and (not (eq calc-angle-mode
'rad
))
105 (or (equal th
'(float 18 1))
109 (math-neg (list 'polar
(math-neg r
) th
)))
111 (list 'polar r th
)))))
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
))
120 (let ((sc (calcFunc-sincos (nth 2 a
))))
122 (math-mul (nth 1 a
) (nth 1 sc
))
123 (math-mul (nth 1 a
) (nth 2 sc
))))))
124 (t (list 'cplx a
0))))
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))
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
))
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
))
145 (math-mul a
'(var i var-i
))))
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
)
155 ((eq (car-safe a
) 'cplx
)
156 (if (eq (car-safe b
) 'polar
)
157 (eq calc-complex-mode
'polar
)
159 ((eq (car-safe b
) 'polar
)
161 ((eq (car-safe b
) 'cplx
)
163 (t (eq calc-complex-mode
'polar
))))
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))
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))
180 (math-fix-circular (math-add a
(math-two-pi)) 1))))
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))
187 (math-fix-circular (math-add a
'(float 36 1)) 1))))))
190 ;;;; Complex numbers.
192 (defun calcFunc-polar (a) ; [C N] [Public]
193 (cond ((Math-vectorp a
)
194 (math-map-vec 'calcFunc-polar a
))
197 (math-normalize (math-polar a
)))
198 (t (list 'calcFunc-polar a
))))
200 (defun calcFunc-rect (a) ; [N N] [Public]
201 (cond ((Math-vectorp a
)
202 (math-map-vec 'calcFunc-rect a
))
205 (math-normalize (math-complex a
)))
206 (t (list 'calcFunc-rect a
))))
208 ;;; Compute the complex conjugate of A. [O O] [Public]
209 (defun calcFunc-conj (a)
211 (cond ((Math-realp a
)
214 (list 'cplx
(nth 1 a
) (math-neg (nth 2 a
))))
216 (list 'polar
(nth 1 a
) (math-neg (nth 2 a
))))
218 (math-map-vec 'calcFunc-conj a
))
219 ((eq (car a
) 'calcFunc-conj
)
221 ((math-known-realp a
)
223 ((and (equal a
'(var i var-i
))
226 ((and (memq (car a
) '(+ -
* /))
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
)))))
240 (math-neg (calcFunc-conj (nth 1 a
))))
241 ((let ((inf (math-infinitep a
)))
243 (math-mul (calcFunc-conj (math-infinite-dir a inf
)) inf
))))
244 (t (calc-record-why 'numberp a
)
245 (list 'calcFunc-conj a
)))))
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
)
257 (math-map-vec 'calcFunc-arg a
))
258 ((and (equal a
'(var i var-i
))
260 (math-quarter-circle t
))
261 ((and (equal a
'(neg (var i var-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
)))))
268 (if (or (equal a
'(var uinf var-uinf
))
269 (equal a
'(var nan var-nan
)))
271 (calcFunc-arg (math-infinite-dir a
))))
272 (t (calc-record-why 'numvecp a
)
273 (list 'calcFunc-arg a
))))
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
)
281 (Math-equal (nth 1 val
) (math-quarter-circle nil
))))))
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)
287 (cond ((Math-realp a
) a
)
288 ((memq (car a
) '(mod cplx
))
291 (math-mul (nth 1 a
) (calcFunc-cos (nth 2 a
))))
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
))
300 ((and (memq (car a
) '(+ -
*))
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
)))))
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
)))
317 (math-neg (calcFunc-re (nth 1 a
))))
318 (t (calc-record-why 'numberp a
)
319 (list 'calcFunc-re a
)))))
321 (defun calcFunc-im (a)
323 (cond ((Math-realp a
)
324 (if (math-floatp a
) '(float 0 0) 0))
328 (math-mul (nth 1 a
) (calcFunc-sin (nth 2 a
))))
330 (math-map-vec 'calcFunc-im a
))
331 ((math-known-realp a
)
333 ((eq (car a
) 'calcFunc-conj
)
334 (math-neg (calcFunc-im (nth 1 a
))))
335 ((and (equal a
'(var i var-i
))
338 ((and (memq (car a
) '(+ -
*))
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
)))))
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
)))
354 (math-neg (calcFunc-im (nth 1 a
))))
355 (t (calc-record-why 'numberp a
)
356 (list 'calcFunc-im a
)))))
360 ;;; arch-tag: de73a331-941c-4507-ae76-46c76adc70dd
361 ;;; calc-cplx.el ends here