-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.el
292 lines (232 loc) · 7.08 KB
/
complex.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
(require 'math)
(defconst complex/numerical-zero 1e-5
"Fix the value of number that are skipped,
because they are considered as numerical noise.")
(defun complex (real imag)
"Return the dotted paired list of 2 elements.
The first element is REAL.
The second element is IMAG.
Both are converted to float datatype.
i.e., REAL _is_ (car (complex (REAL IMAG)))
and IMAG _is_ (cdr (complex REAL IMAG))).
(see `real' and `imag')"
(cons (float real) (float imag)))
;; (defun complex/ify (real)
;; "See `complex'"
;; (complex real 0))
(defun complex? (number)
(let ((typeof (type-of number)))
(cond
((eq typeof 'cons)
t)
((or
(eq typeof 'integer)
(eq typeof 'float))
nil)
)))
(defalias 'complex-p 'complex?)
(defun complex/ify (number)
(if (complex-p number)
number
(complex number 0)))
(defun complex/ify--trigo (number)
"Return NUMBER as `complex' with imag-part only.
If NUMBER is already `complex',
then if real-part of NUMBER is smaller than 0 (`complex/numerical-zero'),
then NUMBER becomes `complex' 0 real-part,
else NUMBER becomes `complex' 0 imag-part;
else NUMBER is complexified with imag-part.
Therefore, be careful when the real-part and the imag-part of x are both greater than 0.
"
(let (real imag x y)
(if (complex-p number)
(progn
(setq real (complex/real number))
(setq imag (complex/imag number))
(if (< (math/abs real) complex/numerical-zero)
(setq x imag)
(setq x real)))
(setq x number))
;; Add modulo
(if (> x math/pi)
(setq y (- x (* 2 math/pi)))
(setq y x))
y
))
(defun complex/which-part (cplx i)
"Generic function used by `real' or `imag'.
Robust enough to even work with interger and float datatype."
;; instead to check using `type-of'
;; should be used: `consp' or `intergerp' or `floatp'
(let ((typeof (type-of cplx)))
(cond
((eq typeof 'cons)
(if (= i 0)
(car cplx)
(cdr cplx)))
((or
(eq typeof 'integer)
(eq typeof 'float))
(if (= i 0)
(float cplx)
0.0))
)))
(defun complex/real (cplx)
"Return real part. (see `complex')"
(complex/which-part cplx 0))
(defun complex/imag (cplx)
"Return imaginary part. (see `complex')"
(complex/which-part cplx 1))
(defun complex/conj (cplx)
"Return the conjugate of CPLX. (see `complex')"
(let ((a (complex/real cplx))
(b (complex/imag cplx)))
(complex a (- b))
))
(defun complex/add (a b)
"Return the complex addition: A+B.
Note that this obviously commutes.
(complex/add a b) _equivalent_ (complex/add b a)
(see `complex')"
(let ((ar (complex/real a))
(ai (complex/imag a))
(br (complex/real b))
(bi (complex/imag b)))
(complex (+ ar br) (+ ai bi))
))
(defun complex/sub (a b)
"Return the complex substraction: A-B.
(see `complex')"
(let ((br (complex/real b))
(bi (complex/imag b)))
(complex/add a (complex (- br) (- bi)))
))
(defun complex/mul (a b)
"Return the complex multiplication: A*B.
Note that this obviously commutes.
(complex/mul a b) _equivalent_ (complex/mul b a)
(see `complex')
"
(let ((ar (complex/real a))
(ai (complex/imag a))
(br (complex/real b))
(bi (complex/imag b))
)
(complex (- (* ar br) (* ai bi))
(+ (* ar bi) (* ai br)))
))
(defun complex/cabs2 (cplx)
"Return the complex squared modulus.
(+ `real'^2 `imag'^2) = (* CPLX (`conj' CPLX))
Note that real^2 and imag^2 do not make any sense in Lisp.
(see `complex' and `complex/mul' and `conj')"
(complex/mul cplx (complex/conj cplx)))
(defun complex/abs2 (cplx)
"Return the real squared modulus.
(see `complex/cabs2' and `real')"
(complex/real (complex/cabs2 cplx)))
(defun complex/abs-naive (cplx)
"Return the modulus.
Evaluate (`math/sqrt' `complex/abs2')
Note that the recursive computation of the square root is always done,
even if CPLX is purely `real' or purely `imag'-inary.
(see `complex/abs')"
(math/sqrt (complex/abs2 cplx)))
(defun complex/abs (cplx)
"Return the modulus.
If `real' or `imag' are less than `complex/numerical-zero'
Then return the well-adapted absolute value computed by `math/abs',
Else apply `complex/abs-naive'."
(let ((re (math/abs (complex/real cplx)))
(im (math/abs (complex/imag cplx))))
(cond
((< im complex/numerical-zero)
re)
((< re complex/numerical-zero)
im)
((and
(>= im complex/numerical-zero)
(>= re complex/numerical-zero))
(complex/abs-naive cplx)))
))
(defun complex/div (a b)
"Return the complex multiplication: A/B.
(see `complex')."
(let ((num (complex/mul a (complex/conj b)))
(inv-den (/ 1 (complex/abs2 b))))
(complex/mul inv-den num)
))
(defun complex/pow (cplx n &optional accumulate)
"Compute CPLX power N.
ACCUMULATE is set to 1 by default.
It corresponds to the value of the tail-recursion.
Even if Emacs Lisp does not optimize the tail-recursion.
WARNING:
Even if Emacs is not optimized --at all-- for computations of loop-recursion.
Try e.g., (complex/pow (complex 1 0) (\ max-lisp-eval-depth 3))
or decrease 3 a bit, and then depth will exceed `max-lisp-eval-depth'.
(see `complex' and `complex/mul')"
(let ((acc accumulate))
(when (eq nil accumulate)
(setq acc (complex 1 0)))
(if (= n 0)
acc
(complex/pow cplx (- n 1) (complex/mul cplx acc)))
))
(defvar complex/exp-tolerance 1e-6
"Fix the maximal aboslute smallest residual of `complex/exp' (serie convergence)")
(defun complex/exp (cplx &optional tolerance nth current)
(let ((cur current)
(n nth)
(tol tolerance)
nth-term)
(when (eq current nil)
(setq cur (complex 0 0)))
(when (eq nth nil)
(setq n 0))
(when (eq tolerance nil)
(setq tol complex/exp-tolerance))
(setq nth-term (complex/div
(complex/pow cplx n)
(math/fac n)))
(setq cur (complex/add cur nth-term))
(if (< (complex/abs nth-term) tol)
cur
(complex/exp cplx tol (+ n 1) cur))
))
(defun complex/sin (x &optional tolerance nth current)
"Return the complexified sine of X.
"
(complex/ify
(complex/imag
(complex/exp
(complex/ify--trigo x)
tolerance nth current))))
(defun complex/cos (x &optional tolerance nth current)
"Return the complexified cosine of X.
"
;; FIXME: do not work for x > 5 ??
(complex/ify
(complex/real
(complex/exp
(complex/ify--trigo x)
tolerance nth current))))
(defun one? (x)
(let* ((cosine (complex/cos x))
(sine (complex/sin x))
(cos2 (complex/mul cosine cosine))
(sin2 (complex/mul sine sine))
(one (complex/add cos2 sin2))
(zero (complex/sub one 1)))
(if (< (complex/abs zero) complex/numerical-zero)
t
nil)
))
;; Require the library s.el
;; Try install it: M-x package-install s
;; (require 's)
;; (defun pprinter (x)
;; (let ((real (complex/real x))
;; (imag (complex/imag x)))
;; (s-lex-format "${real} + ${imag}i")))
(provide 'complex)