calc-cplx.el 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. ;;; calc-cplx.el --- Complex number functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. ;; This file is autoloaded from calc-ext.el.
  18. (require 'calc-ext)
  19. (require 'calc-macs)
  20. (defun calc-argument (arg)
  21. (interactive "P")
  22. (calc-slow-wrapper
  23. (calc-unary-op "arg" 'calcFunc-arg arg)))
  24. (defun calc-re (arg)
  25. (interactive "P")
  26. (calc-slow-wrapper
  27. (calc-unary-op "re" 'calcFunc-re arg)))
  28. (defun calc-im (arg)
  29. (interactive "P")
  30. (calc-slow-wrapper
  31. (calc-unary-op "im" 'calcFunc-im arg)))
  32. (defun calc-polar ()
  33. (interactive)
  34. (calc-slow-wrapper
  35. (let ((arg (calc-top-n 1)))
  36. (if (or (calc-is-inverse)
  37. (eq (car-safe arg) 'polar))
  38. (calc-enter-result 1 "p-r" (list 'calcFunc-rect arg))
  39. (calc-enter-result 1 "r-p" (list 'calcFunc-polar arg))))))
  40. (defun calc-complex-notation ()
  41. (interactive)
  42. (calc-wrapper
  43. (calc-change-mode 'calc-complex-format nil t)
  44. (message "Displaying complex numbers in (X,Y) format")))
  45. (defun calc-i-notation ()
  46. (interactive)
  47. (calc-wrapper
  48. (calc-change-mode 'calc-complex-format 'i t)
  49. (message "Displaying complex numbers in X+Yi format")))
  50. (defun calc-j-notation ()
  51. (interactive)
  52. (calc-wrapper
  53. (calc-change-mode 'calc-complex-format 'j t)
  54. (message "Displaying complex numbers in X+Yj format")))
  55. (defun calc-polar-mode (n)
  56. (interactive "P")
  57. (calc-wrapper
  58. (if (if n
  59. (> (prefix-numeric-value n) 0)
  60. (eq calc-complex-mode 'cplx))
  61. (progn
  62. (calc-change-mode 'calc-complex-mode 'polar)
  63. (message "Preferred complex form is polar"))
  64. (calc-change-mode 'calc-complex-mode 'cplx)
  65. (message "Preferred complex form is rectangular"))))
  66. ;;;; Complex numbers.
  67. (defun math-normalize-polar (a)
  68. (let ((r (math-normalize (nth 1 a)))
  69. (th (math-normalize (nth 2 a))))
  70. (cond ((math-zerop r)
  71. '(polar 0 0))
  72. ((or (math-zerop th))
  73. r)
  74. ((and (not (eq calc-angle-mode 'rad))
  75. (or (equal th '(float 18 1))
  76. (equal th 180)))
  77. (math-neg r))
  78. ((math-negp r)
  79. (math-neg (list 'polar (math-neg r) th)))
  80. (t
  81. (list 'polar r th)))))
  82. ;;; Coerce A to be complex (rectangular form). [c N]
  83. (defun math-complex (a)
  84. (cond ((eq (car-safe a) 'cplx) a)
  85. ((eq (car-safe a) 'polar)
  86. (if (math-zerop (nth 1 a))
  87. (nth 1 a)
  88. (let ((sc (calcFunc-sincos (nth 2 a))))
  89. (list 'cplx
  90. (math-mul (nth 1 a) (nth 1 sc))
  91. (math-mul (nth 1 a) (nth 2 sc))))))
  92. (t (list 'cplx a 0))))
  93. ;;; Coerce A to be complex (polar form). [c N]
  94. (defun math-polar (a)
  95. (cond ((eq (car-safe a) 'polar) a)
  96. ((math-zerop a) '(polar 0 0))
  97. (t
  98. (list 'polar
  99. (math-abs a)
  100. (calcFunc-arg a)))))
  101. ;;; Multiply A by the imaginary constant i. [N N] [Public]
  102. (defun math-imaginary (a)
  103. (if (and (or (Math-objvecp a) (math-infinitep a))
  104. (not calc-symbolic-mode))
  105. (math-mul a
  106. (if (or (eq (car-safe a) 'polar)
  107. (and (not (eq (car-safe a) 'cplx))
  108. (eq calc-complex-mode 'polar)))
  109. (list 'polar 1 (math-quarter-circle nil))
  110. '(cplx 0 1)))
  111. (math-mul a '(var i var-i))))
  112. (defun math-want-polar (a b)
  113. (cond ((eq (car-safe a) 'polar)
  114. (if (eq (car-safe b) 'cplx)
  115. (eq calc-complex-mode 'polar)
  116. t))
  117. ((eq (car-safe a) 'cplx)
  118. (if (eq (car-safe b) 'polar)
  119. (eq calc-complex-mode 'polar)
  120. nil))
  121. ((eq (car-safe b) 'polar)
  122. t)
  123. ((eq (car-safe b) 'cplx)
  124. nil)
  125. (t (eq calc-complex-mode 'polar))))
  126. ;;; Force A to be in the (-pi,pi] or (-180,180] range.
  127. (defun math-fix-circular (a &optional dir) ; [R R]
  128. (cond ((eq (car-safe a) 'hms)
  129. (cond ((and (Math-lessp 180 (nth 1 a)) (not (eq dir 1)))
  130. (math-fix-circular (math-add a '(float -36 1)) -1))
  131. ((or (Math-lessp -180 (nth 1 a)) (eq dir -1))
  132. a)
  133. (t
  134. (math-fix-circular (math-add a '(float 36 1)) 1))))
  135. ((eq calc-angle-mode 'rad)
  136. (cond ((and (Math-lessp (math-pi) a) (not (eq dir 1)))
  137. (math-fix-circular (math-sub a (math-two-pi)) -1))
  138. ((or (Math-lessp (math-neg (math-pi)) a) (eq dir -1))
  139. a)
  140. (t
  141. (math-fix-circular (math-add a (math-two-pi)) 1))))
  142. (t
  143. (cond ((and (Math-lessp '(float 18 1) a) (not (eq dir 1)))
  144. (math-fix-circular (math-add a '(float -36 1)) -1))
  145. ((or (Math-lessp '(float -18 1) a) (eq dir -1))
  146. a)
  147. (t
  148. (math-fix-circular (math-add a '(float 36 1)) 1))))))
  149. ;;;; Complex numbers.
  150. (defun calcFunc-polar (a) ; [C N] [Public]
  151. (cond ((Math-vectorp a)
  152. (math-map-vec 'calcFunc-polar a))
  153. ((Math-realp a) a)
  154. ((Math-numberp a)
  155. (math-normalize (math-polar a)))
  156. (t (list 'calcFunc-polar a))))
  157. (defun calcFunc-rect (a) ; [N N] [Public]
  158. (cond ((Math-vectorp a)
  159. (math-map-vec 'calcFunc-rect a))
  160. ((Math-realp a) a)
  161. ((Math-numberp a)
  162. (math-normalize (math-complex a)))
  163. (t (list 'calcFunc-rect a))))
  164. ;;; Compute the complex conjugate of A. [O O] [Public]
  165. (defun calcFunc-conj (a)
  166. (let (aa bb)
  167. (cond ((Math-realp a)
  168. a)
  169. ((eq (car a) 'cplx)
  170. (list 'cplx (nth 1 a) (math-neg (nth 2 a))))
  171. ((eq (car a) 'polar)
  172. (list 'polar (nth 1 a) (math-neg (nth 2 a))))
  173. ((eq (car a) 'vec)
  174. (math-map-vec 'calcFunc-conj a))
  175. ((eq (car a) 'calcFunc-conj)
  176. (nth 1 a))
  177. ((math-known-realp a)
  178. a)
  179. ((and (equal a '(var i var-i))
  180. (math-imaginary-i))
  181. (math-neg a))
  182. ((and (memq (car a) '(+ - * /))
  183. (progn
  184. (setq aa (calcFunc-conj (nth 1 a))
  185. bb (calcFunc-conj (nth 2 a)))
  186. (or (not (eq (car-safe aa) 'calcFunc-conj))
  187. (not (eq (car-safe bb) 'calcFunc-conj)))))
  188. (if (eq (car a) '+)
  189. (math-add aa bb)
  190. (if (eq (car a) '-)
  191. (math-sub aa bb)
  192. (if (eq (car a) '*)
  193. (math-mul aa bb)
  194. (math-div aa bb)))))
  195. ((eq (car a) 'neg)
  196. (math-neg (calcFunc-conj (nth 1 a))))
  197. ((let ((inf (math-infinitep a)))
  198. (and inf
  199. (math-mul (calcFunc-conj (math-infinite-dir a inf)) inf))))
  200. (t (calc-record-why 'numberp a)
  201. (list 'calcFunc-conj a)))))
  202. ;;; Compute the complex argument of A. [F N] [Public]
  203. (defun calcFunc-arg (a)
  204. (cond ((Math-anglep a)
  205. (if (math-negp a) (math-half-circle nil) 0))
  206. ((eq (car-safe a) 'cplx)
  207. (calcFunc-arctan2 (nth 2 a) (nth 1 a)))
  208. ((eq (car-safe a) 'polar)
  209. (nth 2 a))
  210. ((eq (car a) 'vec)
  211. (math-map-vec 'calcFunc-arg a))
  212. ((and (equal a '(var i var-i))
  213. (math-imaginary-i))
  214. (math-quarter-circle t))
  215. ((and (equal a '(neg (var i var-i)))
  216. (math-imaginary-i))
  217. (math-neg (math-quarter-circle t)))
  218. ((let ((signs (math-possible-signs a)))
  219. (or (and (memq signs '(2 4 6)) 0)
  220. (and (eq signs 1) (math-half-circle nil)))))
  221. ((math-infinitep a)
  222. (if (or (equal a '(var uinf var-uinf))
  223. (equal a '(var nan var-nan)))
  224. '(var nan var-nan)
  225. (calcFunc-arg (math-infinite-dir a))))
  226. (t (calc-record-why 'numvecp a)
  227. (list 'calcFunc-arg a))))
  228. (defun math-imaginary-i ()
  229. (let ((val (calc-var-value 'var-i)))
  230. (or (eq (car-safe val) 'special-const)
  231. (equal val '(cplx 0 1))
  232. (and (eq (car-safe val) 'polar)
  233. (eq (nth 1 val) 0)
  234. (Math-equal (nth 1 val) (math-quarter-circle nil))))))
  235. ;;; Extract the real or complex part of a complex number. [R N] [Public]
  236. ;;; Also extracts the real part of a modulo form.
  237. (defun calcFunc-re (a)
  238. (let (aa bb)
  239. (cond ((Math-realp a) a)
  240. ((memq (car a) '(mod cplx))
  241. (nth 1 a))
  242. ((eq (car a) 'polar)
  243. (math-mul (nth 1 a) (calcFunc-cos (nth 2 a))))
  244. ((eq (car a) 'vec)
  245. (math-map-vec 'calcFunc-re a))
  246. ((math-known-realp a) a)
  247. ((eq (car a) 'calcFunc-conj)
  248. (calcFunc-re (nth 1 a)))
  249. ((and (equal a '(var i var-i))
  250. (math-imaginary-i))
  251. 0)
  252. ((and (memq (car a) '(+ - *))
  253. (progn
  254. (setq aa (calcFunc-re (nth 1 a))
  255. bb (calcFunc-re (nth 2 a)))
  256. (or (not (eq (car-safe aa) 'calcFunc-re))
  257. (not (eq (car-safe bb) 'calcFunc-re)))))
  258. (if (eq (car a) '+)
  259. (math-add aa bb)
  260. (if (eq (car a) '-)
  261. (math-sub aa bb)
  262. (math-sub (math-mul aa bb)
  263. (math-mul (calcFunc-im (nth 1 a))
  264. (calcFunc-im (nth 2 a)))))))
  265. ((and (eq (car a) '/)
  266. (math-known-realp (nth 2 a)))
  267. (math-div (calcFunc-re (nth 1 a)) (nth 2 a)))
  268. ((eq (car a) 'neg)
  269. (math-neg (calcFunc-re (nth 1 a))))
  270. (t (calc-record-why 'numberp a)
  271. (list 'calcFunc-re a)))))
  272. (defun calcFunc-im (a)
  273. (let (aa bb)
  274. (cond ((Math-realp a)
  275. (if (math-floatp a) '(float 0 0) 0))
  276. ((eq (car a) 'cplx)
  277. (nth 2 a))
  278. ((eq (car a) 'polar)
  279. (math-mul (nth 1 a) (calcFunc-sin (nth 2 a))))
  280. ((eq (car a) 'vec)
  281. (math-map-vec 'calcFunc-im a))
  282. ((math-known-realp a)
  283. 0)
  284. ((eq (car a) 'calcFunc-conj)
  285. (math-neg (calcFunc-im (nth 1 a))))
  286. ((and (equal a '(var i var-i))
  287. (math-imaginary-i))
  288. 1)
  289. ((and (memq (car a) '(+ - *))
  290. (progn
  291. (setq aa (calcFunc-im (nth 1 a))
  292. bb (calcFunc-im (nth 2 a)))
  293. (or (not (eq (car-safe aa) 'calcFunc-im))
  294. (not (eq (car-safe bb) 'calcFunc-im)))))
  295. (if (eq (car a) '+)
  296. (math-add aa bb)
  297. (if (eq (car a) '-)
  298. (math-sub aa bb)
  299. (math-add (math-mul (calcFunc-re (nth 1 a)) bb)
  300. (math-mul aa (calcFunc-re (nth 2 a)))))))
  301. ((and (eq (car a) '/)
  302. (math-known-realp (nth 2 a)))
  303. (math-div (calcFunc-im (nth 1 a)) (nth 2 a)))
  304. ((eq (car a) 'neg)
  305. (math-neg (calcFunc-im (nth 1 a))))
  306. (t (calc-record-why 'numberp a)
  307. (list 'calcFunc-im a)))))
  308. (provide 'calc-cplx)
  309. ;;; calc-cplx.el ends here