1 ;;; calc-cplx.el --- Complex number functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 (defun calc-argument (arg)
35 (calc-unary-op "arg" 'calcFunc-arg arg
)))
40 (calc-unary-op "re" 'calcFunc-re arg
)))
45 (calc-unary-op "im" 'calcFunc-im arg
)))
51 (let ((arg (calc-top-n 1)))
52 (if (or (calc-is-inverse)
53 (eq (car-safe arg
) 'polar
))
54 (calc-enter-result 1 "p-r" (list 'calcFunc-rect arg
))
55 (calc-enter-result 1 "r-p" (list 'calcFunc-polar arg
))))))
60 (defun calc-complex-notation ()
63 (calc-change-mode 'calc-complex-format nil t
)
64 (message "Displaying complex numbers in (X,Y) format")))
66 (defun calc-i-notation ()
69 (calc-change-mode 'calc-complex-format
'i t
)
70 (message "Displaying complex numbers in X+Yi format")))
72 (defun calc-j-notation ()
75 (calc-change-mode 'calc-complex-format
'j t
)
76 (message "Displaying complex numbers in X+Yj format")))
79 (defun calc-polar-mode (n)
83 (> (prefix-numeric-value n
) 0)
84 (eq calc-complex-mode
'cplx
))
86 (calc-change-mode 'calc-complex-mode
'polar
)
87 (message "Preferred complex form is polar"))
88 (calc-change-mode 'calc-complex-mode
'cplx
)
89 (message "Preferred complex form is rectangular"))))
94 (defun math-normalize-polar (a)
95 (let ((r (math-normalize (nth 1 a
)))
96 (th (math-normalize (nth 2 a
))))
101 ((and (not (eq calc-angle-mode
'rad
))
102 (or (equal th
'(float 18 1))
106 (math-neg (list 'polar
(math-neg r
) th
)))
108 (list 'polar r th
)))))
111 ;;; Coerce A to be complex (rectangular form). [c N]
112 (defun math-complex (a)
113 (cond ((eq (car-safe a
) 'cplx
) a
)
114 ((eq (car-safe a
) 'polar
)
115 (if (math-zerop (nth 1 a
))
117 (let ((sc (calcFunc-sincos (nth 2 a
))))
119 (math-mul (nth 1 a
) (nth 1 sc
))
120 (math-mul (nth 1 a
) (nth 2 sc
))))))
121 (t (list 'cplx a
0))))
123 ;;; Coerce A to be complex (polar form). [c N]
124 (defun math-polar (a)
125 (cond ((eq (car-safe a
) 'polar
) a
)
126 ((math-zerop a
) '(polar 0 0))
132 ;;; Multiply A by the imaginary constant i. [N N] [Public]
133 (defun math-imaginary (a)
134 (if (and (or (Math-objvecp a
) (math-infinitep a
))
135 (not calc-symbolic-mode
))
137 (if (or (eq (car-safe a
) 'polar
)
138 (and (not (eq (car-safe a
) 'cplx
))
139 (eq calc-complex-mode
'polar
)))
140 (list 'polar
1 (math-quarter-circle nil
))
142 (math-mul a
'(var i var-i
))))
147 (defun math-want-polar (a b
)
148 (cond ((eq (car-safe a
) 'polar
)
149 (if (eq (car-safe b
) 'cplx
)
150 (eq calc-complex-mode
'polar
)
152 ((eq (car-safe a
) 'cplx
)
153 (if (eq (car-safe b
) 'polar
)
154 (eq calc-complex-mode
'polar
)
156 ((eq (car-safe b
) 'polar
)
158 ((eq (car-safe b
) 'cplx
)
160 (t (eq calc-complex-mode
'polar
))))
162 ;;; Force A to be in the (-pi,pi] or (-180,180] range.
163 (defun math-fix-circular (a &optional dir
) ; [R R]
164 (cond ((eq (car-safe a
) 'hms
)
165 (cond ((and (Math-lessp 180 (nth 1 a
)) (not (eq dir
1)))
166 (math-fix-circular (math-add a
'(float -
36 1)) -
1))
167 ((or (Math-lessp -
180 (nth 1 a
)) (eq dir -
1))
170 (math-fix-circular (math-add a
'(float 36 1)) 1))))
171 ((eq calc-angle-mode
'rad
)
172 (cond ((and (Math-lessp (math-pi) a
) (not (eq dir
1)))
173 (math-fix-circular (math-sub a
(math-two-pi)) -
1))
174 ((or (Math-lessp (math-neg (math-pi)) a
) (eq dir -
1))
177 (math-fix-circular (math-add a
(math-two-pi)) 1))))
179 (cond ((and (Math-lessp '(float 18 1) a
) (not (eq dir
1)))
180 (math-fix-circular (math-add a
'(float -
36 1)) -
1))
181 ((or (Math-lessp '(float -
18 1) a
) (eq dir -
1))
184 (math-fix-circular (math-add a
'(float 36 1)) 1))))))
187 ;;;; Complex numbers.
189 (defun calcFunc-polar (a) ; [C N] [Public]
190 (cond ((Math-vectorp a
)
191 (math-map-vec 'calcFunc-polar a
))
194 (math-normalize (math-polar a
)))
195 (t (list 'calcFunc-polar a
))))
197 (defun calcFunc-rect (a) ; [N N] [Public]
198 (cond ((Math-vectorp a
)
199 (math-map-vec 'calcFunc-rect a
))
202 (math-normalize (math-complex a
)))
203 (t (list 'calcFunc-rect a
))))
205 ;;; Compute the complex conjugate of A. [O O] [Public]
206 (defun calcFunc-conj (a)
208 (cond ((Math-realp a
)
211 (list 'cplx
(nth 1 a
) (math-neg (nth 2 a
))))
213 (list 'polar
(nth 1 a
) (math-neg (nth 2 a
))))
215 (math-map-vec 'calcFunc-conj a
))
216 ((eq (car a
) 'calcFunc-conj
)
218 ((math-known-realp a
)
220 ((and (equal a
'(var i var-i
))
223 ((and (memq (car a
) '(+ -
* /))
225 (setq aa
(calcFunc-conj (nth 1 a
))
226 bb
(calcFunc-conj (nth 2 a
)))
227 (or (not (eq (car-safe aa
) 'calcFunc-conj
))
228 (not (eq (car-safe bb
) 'calcFunc-conj
)))))
237 (math-neg (calcFunc-conj (nth 1 a
))))
238 ((let ((inf (math-infinitep a
)))
240 (math-mul (calcFunc-conj (math-infinite-dir a inf
)) inf
))))
241 (t (calc-record-why 'numberp a
)
242 (list 'calcFunc-conj a
)))))
245 ;;; Compute the complex argument of A. [F N] [Public]
246 (defun calcFunc-arg (a)
247 (cond ((Math-anglep a
)
248 (if (math-negp a
) (math-half-circle nil
) 0))
249 ((eq (car-safe a
) 'cplx
)
250 (calcFunc-arctan2 (nth 2 a
) (nth 1 a
)))
251 ((eq (car-safe a
) 'polar
)
254 (math-map-vec 'calcFunc-arg a
))
255 ((and (equal a
'(var i var-i
))
257 (math-quarter-circle t
))
258 ((and (equal a
'(neg (var i var-i
)))
260 (math-neg (math-quarter-circle t
)))
261 ((let ((signs (math-possible-signs a
)))
262 (or (and (memq signs
'(2 4 6)) 0)
263 (and (eq signs
1) (math-half-circle nil
)))))
265 (if (or (equal a
'(var uinf var-uinf
))
266 (equal a
'(var nan var-nan
)))
268 (calcFunc-arg (math-infinite-dir a
))))
269 (t (calc-record-why 'numvecp a
)
270 (list 'calcFunc-arg a
))))
272 (defun math-imaginary-i ()
273 (let ((val (calc-var-value 'var-i
)))
274 (or (eq (car-safe val
) 'special-const
)
275 (equal val
'(cplx 0 1))
276 (and (eq (car-safe val
) 'polar
)
278 (Math-equal (nth 1 val
) (math-quarter-circle nil
))))))
280 ;;; Extract the real or complex part of a complex number. [R N] [Public]
281 ;;; Also extracts the real part of a modulo form.
282 (defun calcFunc-re (a)
284 (cond ((Math-realp a
) a
)
285 ((memq (car a
) '(mod cplx
))
288 (math-mul (nth 1 a
) (calcFunc-cos (nth 2 a
))))
290 (math-map-vec 'calcFunc-re a
))
291 ((math-known-realp a
) a
)
292 ((eq (car a
) 'calcFunc-conj
)
293 (calcFunc-re (nth 1 a
)))
294 ((and (equal a
'(var i var-i
))
297 ((and (memq (car a
) '(+ -
*))
299 (setq aa
(calcFunc-re (nth 1 a
))
300 bb
(calcFunc-re (nth 2 a
)))
301 (or (not (eq (car-safe aa
) 'calcFunc-re
))
302 (not (eq (car-safe bb
) 'calcFunc-re
)))))
307 (math-sub (math-mul aa bb
)
308 (math-mul (calcFunc-im (nth 1 a
))
309 (calcFunc-im (nth 2 a
)))))))
310 ((and (eq (car a
) '/)
311 (math-known-realp (nth 2 a
)))
312 (math-div (calcFunc-re (nth 1 a
)) (nth 2 a
)))
314 (math-neg (calcFunc-re (nth 1 a
))))
315 (t (calc-record-why 'numberp a
)
316 (list 'calcFunc-re a
)))))
318 (defun calcFunc-im (a)
320 (cond ((Math-realp a
)
321 (if (math-floatp a
) '(float 0 0) 0))
325 (math-mul (nth 1 a
) (calcFunc-sin (nth 2 a
))))
327 (math-map-vec 'calcFunc-im a
))
328 ((math-known-realp a
)
330 ((eq (car a
) 'calcFunc-conj
)
331 (math-neg (calcFunc-im (nth 1 a
))))
332 ((and (equal a
'(var i var-i
))
335 ((and (memq (car a
) '(+ -
*))
337 (setq aa
(calcFunc-im (nth 1 a
))
338 bb
(calcFunc-im (nth 2 a
)))
339 (or (not (eq (car-safe aa
) 'calcFunc-im
))
340 (not (eq (car-safe bb
) 'calcFunc-im
)))))
345 (math-add (math-mul (calcFunc-re (nth 1 a
)) bb
)
346 (math-mul aa
(calcFunc-re (nth 2 a
)))))))
347 ((and (eq (car a
) '/)
348 (math-known-realp (nth 2 a
)))
349 (math-div (calcFunc-im (nth 1 a
)) (nth 2 a
)))
351 (math-neg (calcFunc-im (nth 1 a
))))
352 (t (calc-record-why 'numberp a
)
353 (list 'calcFunc-im a
)))))
357 ;;; calc-cplx.el ends here