blob: eb7796bb2c5e8fa1cf33daf643ebf6d0ef2dad80 (
plain)
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
|
## Copyright (c) Eduardo Julian. All rights reserved.
## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
## If a copy of the MPL was not distributed with this file,
## You can obtain one at http://mozilla.org/MPL/2.0/.
(;module:
lux
(lux [math]
(control eq
[ord]
number
codec
monad)
(data [number "r:" Number<Real> Codec<Text,Real>]
[text "Text/" Monoid<Text>]
error
maybe
(struct [list "List/" Monad<List>]))
[compiler]
(macro [ast]
["s" syntax #+ syntax: Syntax])))
## Based on org.apache.commons.math4.complex.Complex
(type: #export Complex
{#real Real
#imaginary Real})
(syntax: #export (complex real {?imaginary (s;opt s;any)})
(wrap (list (` {#;;real (~ real)
#;;imaginary (~ (default (` 0.0)
?imaginary))}))))
(def: #export i Complex (complex 0.0 1.0))
(def: #export one Complex (complex 1.0 0.0))
(def: #export zero Complex (complex 0.0 0.0))
(def: #export (c= param input)
(-> Complex Complex Bool)
(and (=. (get@ #real param)
(get@ #real input))
(=. (get@ #imaginary param)
(get@ #imaginary input))))
(do-template [<name> <op>]
[(def: #export (<name> param input)
(-> Complex Complex Complex)
{#real (<op> (get@ #real param)
(get@ #real input))
#imaginary (<op> (get@ #imaginary param)
(get@ #imaginary input))})]
[c+ +.]
[c- -.]
)
(struct: #export _ (Eq Complex)
(def: = c=))
(def: #export negate
(-> Complex Complex)
(|>. (update@ #real r:negate)
(update@ #imaginary r:negate)))
(def: #export signum
(-> Complex Complex)
(|>. (update@ #real r:signum)
(update@ #imaginary r:signum)))
(def: #export conjugate
(-> Complex Complex)
(update@ #imaginary r:negate))
(def: #export (c*' param input)
(-> Real Complex Complex)
{#real (*. param
(get@ #real input))
#imaginary (*. param
(get@ #imaginary input))})
(def: #export (c* param input)
(-> Complex Complex Complex)
{#real (-. (*. (get@ #imaginary param)
(get@ #imaginary input))
(*. (get@ #real param)
(get@ #real input)))
#imaginary (+. (*. (get@ #real param)
(get@ #imaginary input))
(*. (get@ #imaginary param)
(get@ #real input)))})
(def: #export (c/ (^slots [#real #imaginary]) input)
(-> Complex Complex Complex)
(if (<. (r:abs imaginary)
(r:abs real))
(let [quot (/. imaginary real)
denom (|> real (*. quot) (+. imaginary))]
{#real (|> (get@ #real input) (*. quot) (+. (get@ #imaginary input)) (/. denom))
#imaginary (|> (get@ #imaginary input) (*. quot) (-. (get@ #real input)) (/. denom))})
(let [quot (/. real imaginary)
denom (|> imaginary (*. quot) (+. real))]
{#real (|> (get@ #imaginary input) (*. quot) (+. (get@ #real input)) (/. denom))
#imaginary (|> (get@ #imaginary input) (-. (*. quot (get@ #real input))) (/. denom))})))
(def: #export (c/' param (^slots [#real #imaginary]))
(-> Real Complex Complex)
{#real (/. param real)
#imaginary (/. param imaginary)})
(def: #export (cos (^slots [#real #imaginary]))
(-> Complex Complex)
{#real (*. (math;cosh imaginary)
(math;cos real))
#imaginary (*. (math;sinh imaginary)
(r:negate (math;sin real)))})
(def: #export (cosh (^slots [#real #imaginary]))
(-> Complex Complex)
{#real (*. (math;cos imaginary)
(math;cosh real))
#imaginary (*. (math;sin imaginary)
(math;sinh real))})
(def: #export (sin (^slots [#real #imaginary]))
(-> Complex Complex)
{#real (*. (math;cosh imaginary)
(math;sin real))
#imaginary (*. (math;sinh imaginary)
(math;cos real))})
(def: #export (sinh (^slots [#real #imaginary]))
(-> Complex Complex)
{#real (*. (math;cos imaginary)
(math;sinh real))
#imaginary (*. (math;sin imaginary)
(math;cosh real))})
(def: #export (tan (^slots [#real #imaginary]))
(-> Complex Complex)
(let [r2 (*. 2.0 real)
i2 (*. 2.0 imaginary)
d (+. (math;cos r2) (math;cosh i2))]
{#real (/. d (math;sin r2))
#imaginary (/. d (math;sinh i2))}))
(def: #export (tanh (^slots [#real #imaginary]))
(-> Complex Complex)
(let [r2 (*. 2.0 real)
i2 (*. 2.0 imaginary)
d (+. (math;cosh r2) (math;cos i2))]
{#real (/. d (math;sinh r2))
#imaginary (/. d (math;sin i2))}))
(def: #export (abs (^slots [#real #imaginary]))
(-> Complex Real)
(if (<. (r:abs imaginary)
(r:abs real))
(if (=. 0.0 imaginary)
(r:abs real)
(let [q (/. imaginary real)]
(*. (math;sqrt (+. 1.0 (*. q q)))
(r:abs imaginary))))
(if (=. 0.0 real)
(r:abs imaginary)
(let [q (/. real imaginary)]
(*. (math;sqrt (+. 1.0 (*. q q)))
(r:abs real))))
))
(def: #export (exp (^slots [#real #imaginary]))
(-> Complex Complex)
(let [r-exp (math;exp real)]
{#real (*. r-exp (math;cos imaginary))
#imaginary (*. r-exp (math;sin imaginary))}))
(def: #export (log (^@ input (^slots [#real #imaginary])))
(-> Complex Complex)
{#real (math;log (abs input))
#imaginary (math;atan2 real imaginary)})
(do-template [<name> <type> <op>]
[(def: #export (<name> param input)
(-> <type> Complex Complex)
(|> input log (<op> param) exp))]
[pow Complex c*]
[pow' Real c*']
)
(def: (copy-sign sign magnitude)
(-> Real Real Real)
(*. (r:signum sign) magnitude))
(def: #export (sqrt (^@ input (^slots [#real #imaginary])))
(-> Complex Complex)
(let [t (|> input abs (+. (r:abs real)) (/. 2.0) math;sqrt)]
(if (>=. 0.0 real)
{#real t
#imaginary (/. (*. 2.0 t)
imaginary)}
{#real (/. (*. 2.0 t)
(r:abs imaginary))
#imaginary (*. t (copy-sign imaginary 1.0))})))
(def: #export (sqrt-1z input)
(-> Complex Complex)
(|> (complex 1.0) (c- (c* input input)) sqrt))
(def: #export (reciprocal (^slots [#real #imaginary]))
(-> Complex Complex)
(if (<. (r:abs imaginary)
(r:abs real))
(let [q (/. imaginary real)
scale (/. (|> real (*. q) (+. imaginary))
1.0)]
{#real (*. q scale)
#imaginary (r:negate scale)})
(let [q (/. real imaginary)
scale (/. (|> imaginary (*. q) (+. real))
1.0)]
{#real scale
#imaginary (|> scale r:negate (*. q))})))
(def: #export (acos input)
(-> Complex Complex)
(|> input
(c+ (|> input sqrt-1z (c* i)))
log
(c* (negate i))))
(def: #export (asin input)
(-> Complex Complex)
(|> input
sqrt-1z
(c+ (c* i input))
log
(c* (negate i))))
(def: #export (atan input)
(-> Complex Complex)
(|> input
(c+ i)
(c/ (c- input i))
log
(c* (c/ (complex 2.0) i))))
(def: #export (argument (^slots [#real #imaginary]))
(-> Complex Real)
(math;atan2 real imaginary))
(def: #export (nth-root nth input)
(-> Nat Complex (List Complex))
(if (=+ +0 nth)
(list)
(let [r-nth (|> nth nat-to-int int-to-real)
nth-root-of-abs (math;pow (/. r-nth 1.0)
(abs input))
nth-phi (|> input argument (/. r-nth))
slice (|> math;pi (*. 2.0) (/. r-nth))]
(|> (list;range+ +0 (dec+ nth))
(List/map (lambda [nth']
(let [inner (|> nth' nat-to-int int-to-real
(*. slice)
(+. nth-phi))
real (*. nth-root-of-abs
(math;cos inner))
imaginary (*. nth-root-of-abs
(math;sin inner))]
{#real real
#imaginary imaginary})))))))
(struct: #export _ (Codec Text Complex)
(def: (encode (^slots [#real #imaginary]))
($_ Text/append "(" (r:encode real) ", " (r:encode imaginary) ")"))
(def: (decode input)
(case (do Monad<Maybe>
[input' (text;sub +1 (-+ +1 (text;size input)) input)]
(text;split-with "," input'))
#;None
(#;Left (Text/append "Wrong syntax for complex numbers: " input))
(#;Some [r' i'])
(do Monad<Error>
[r (r:decode (text;trim r'))
i (r:decode (text;trim i'))]
(wrap {#real r
#imaginary i}))
)))
|