blob: 41e074be694914914473ee8e0f1e5b076fd9d627 (
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
(.module: {#.doc "Complex arithmetic."}
[lux #*
["." math]
[control
[equivalence (#+ Equivalence)]
number
codec
["M" monad (#+ do Monad)]
["p" parser]]
[data
["." maybe]
["." number ("frac/." Number<Frac>)]
[text ("text/." Monoid<Text>)]
[collection
["." list ("list/." Functor<List>)]]]
["." macro
["." code]
["s" syntax (#+ syntax: Syntax)]]])
(type: #export Complex
{#real Frac
#imaginary Frac})
(syntax: #export (complex real {?imaginary (p.maybe s.any)})
{#.doc (doc "Complex literals."
(complex real imaginary)
"The imaginary part can be omitted if it's 0."
(complex real))}
(wrap (list (` {#..real (~ real)
#..imaginary (~ (maybe.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 (not-a-number? complex)
(or (number.not-a-number? (get@ #real complex))
(number.not-a-number? (get@ #imaginary complex))))
(def: #export (= param input)
(-> Complex Complex Bit)
(and (f/= (get@ #real param)
(get@ #real input))
(f/= (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))})]
[+ f/+]
[- f/-]
)
(structure: #export _ (Equivalence Complex)
(def: = ..=))
(def: #export negate
(-> Complex Complex)
(|>> (update@ #real frac/negate)
(update@ #imaginary frac/negate)))
(def: #export signum
(-> Complex Complex)
(|>> (update@ #real frac/signum)
(update@ #imaginary frac/signum)))
(def: #export conjugate
(-> Complex Complex)
(update@ #imaginary frac/negate))
(def: #export (*' param input)
(-> Frac Complex Complex)
{#real (f/* param
(get@ #real input))
#imaginary (f/* param
(get@ #imaginary input))})
(def: #export (* param input)
(-> Complex Complex Complex)
{#real (f/- (f/* (get@ #imaginary param)
(get@ #imaginary input))
(f/* (get@ #real param)
(get@ #real input)))
#imaginary (f/+ (f/* (get@ #real param)
(get@ #imaginary input))
(f/* (get@ #imaginary param)
(get@ #real input)))})
(def: #export (/ param input)
(-> Complex Complex Complex)
(let [(^slots [#real #imaginary]) param]
(if (f/< (frac/abs imaginary)
(frac/abs real))
(let [quot (f// imaginary real)
denom (|> real (f/* quot) (f/+ imaginary))]
{#real (|> (get@ #real input) (f/* quot) (f/+ (get@ #imaginary input)) (f// denom))
#imaginary (|> (get@ #imaginary input) (f/* quot) (f/- (get@ #real input)) (f// denom))})
(let [quot (f// real imaginary)
denom (|> imaginary (f/* quot) (f/+ real))]
{#real (|> (get@ #imaginary input) (f/* quot) (f/+ (get@ #real input)) (f// denom))
#imaginary (|> (get@ #imaginary input) (f/- (f/* quot (get@ #real input))) (f// denom))}))))
(def: #export (/' param subject)
(-> Frac Complex Complex)
(let [(^slots [#real #imaginary]) subject]
{#real (f// param real)
#imaginary (f// param imaginary)}))
(def: #export (% param input)
(-> Complex Complex Complex)
(let [scaled (/ param input)
quotient (|> scaled
(update@ #real math.floor)
(update@ #imaginary math.floor))]
(- (* quotient param)
input)))
(def: #export (cos subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject]
{#real (f/* (math.cosh imaginary)
(math.cos real))
#imaginary (frac/negate (f/* (math.sinh imaginary)
(math.sin real)))}))
(def: #export (cosh subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject]
{#real (f/* (math.cos imaginary)
(math.cosh real))
#imaginary (f/* (math.sin imaginary)
(math.sinh real))}))
(def: #export (sin subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject]
{#real (f/* (math.cosh imaginary)
(math.sin real))
#imaginary (f/* (math.sinh imaginary)
(math.cos real))}))
(def: #export (sinh subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject]
{#real (f/* (math.cos imaginary)
(math.sinh real))
#imaginary (f/* (math.sin imaginary)
(math.cosh real))}))
(def: #export (tan subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject
r2 (f/* 2.0 real)
i2 (f/* 2.0 imaginary)
d (f/+ (math.cos r2) (math.cosh i2))]
{#real (f// d (math.sin r2))
#imaginary (f// d (math.sinh i2))}))
(def: #export (tanh subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject
r2 (f/* 2.0 real)
i2 (f/* 2.0 imaginary)
d (f/+ (math.cosh r2) (math.cos i2))]
{#real (f// d (math.sinh r2))
#imaginary (f// d (math.sin i2))}))
(def: #export (abs subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject]
(complex (if (f/< (frac/abs imaginary)
(frac/abs real))
(if (f/= 0.0 imaginary)
(frac/abs real)
(let [q (f// imaginary real)]
(f/* (math.pow 0.5 (f/+ 1.0 (f/* q q)))
(frac/abs imaginary))))
(if (f/= 0.0 real)
(frac/abs imaginary)
(let [q (f// real imaginary)]
(f/* (math.pow 0.5 (f/+ 1.0 (f/* q q)))
(frac/abs real))))
))))
(structure: #export _ (Number Complex)
(def: + ..+)
(def: - ..-)
(def: * ..*)
(def: / ../)
(def: % ..%)
(def: (negate x)
(|> x
(update@ #real frac/negate)
(update@ #imaginary frac/negate)))
(def: abs ..abs)
(def: (signum x)
(|> x
(update@ #real frac/signum)
(update@ #imaginary frac/signum))))
(def: #export (exp subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject
r-exp (math.exp real)]
{#real (f/* r-exp (math.cos imaginary))
#imaginary (f/* r-exp (math.sin imaginary))}))
(def: #export (log subject)
(-> Complex Complex)
(let [(^slots [#real #imaginary]) subject]
{#real (|> subject ..abs (get@ #real) math.log)
#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 ..*]
[pow' Frac ..*']
)
(def: (copy-sign sign magnitude)
(-> Frac Frac Frac)
(f/* (frac/signum sign) magnitude))
(def: #export (root2 (^@ input (^slots [#real #imaginary])))
(-> Complex Complex)
(let [t (|> input ..abs (get@ #real) (f/+ (frac/abs real)) (f// 2.0) (math.pow 0.5))]
(if (f/>= 0.0 real)
{#real t
#imaginary (f// (f/* 2.0 t)
imaginary)}
{#real (f// (f/* 2.0 t)
(frac/abs imaginary))
#imaginary (f/* t (copy-sign imaginary 1.0))})))
(def: #export (root2-1z input)
(-> Complex Complex)
(|> (complex 1.0) (- (* input input)) root2))
(def: #export (reciprocal (^slots [#real #imaginary]))
(-> Complex Complex)
(if (f/< (frac/abs imaginary)
(frac/abs real))
(let [q (f// imaginary real)
scale (f// (|> real (f/* q) (f/+ imaginary))
1.0)]
{#real (f/* q scale)
#imaginary (frac/negate scale)})
(let [q (f// real imaginary)
scale (f// (|> imaginary (f/* q) (f/+ real))
1.0)]
{#real scale
#imaginary (|> scale frac/negate (f/* q))})))
(def: #export (acos input)
(-> Complex Complex)
(|> input
(+ (|> input root2-1z (* i)))
log
(* (negate i))))
(def: #export (asin input)
(-> Complex Complex)
(|> input
root2-1z
(+ (* i input))
log
(* (negate i))))
(def: #export (atan input)
(-> Complex Complex)
(|> input
(+ i)
(/ (- input i))
log
(* (/ (complex 2.0) i))))
(def: #export (argument (^slots [#real #imaginary]))
(-> Complex Frac)
(math.atan2 real imaginary))
(def: #export (roots nth input)
(-> Nat Complex (List Complex))
(if (n/= |0 nth)
(list)
(let [r-nth (|> nth .int int-to-frac)
nth-root-of-abs (|> input abs (get@ #real) (math.pow (f// r-nth 1.0)))
nth-phi (|> input argument (f// r-nth))
slice (|> math.pi (f/* 2.0) (f// r-nth))]
(|> (list.n/range |0 (dec nth))
(list/map (function (_ nth')
(let [inner (|> nth' .int int-to-frac
(f/* slice)
(f/+ nth-phi))
real (f/* nth-root-of-abs
(math.cos inner))
imaginary (f/* nth-root-of-abs
(math.sin inner))]
{#real real
#imaginary imaginary})))))))
|