aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/number/complex.lux
blob: 8708b9c7f8ec7ed4fda291ebaec37c5a97e65c83 (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
311
312
313
314
(.using
 [library
  [lux (.except)
   [abstract
    [equivalence (.only Equivalence)]]
   [control
    ["[0]" maybe]
    ["<>" parser (.only)
     ["<[0]>" code]]]
   [data
    [collection
     ["[0]" list (.open: "[1]#[0]" functor)]]]
   [macro
    [syntax (.only syntax)]]
   [math
    [number
     ["f" frac]
     ["[0]" int]]]]])

(type: .public Complex
  (Record
   [#real Frac
    #imaginary Frac]))

(def: .public complex
  (syntax (_ [real <code>.any
              ?imaginary (<>.maybe <code>.any)])
    (in (list (` [..#real (~ real)
                  ..#imaginary (~ (maybe.else (' +0.0) ?imaginary))])))))

(def: .public i
  Complex
  (..complex +0.0 +1.0))

(def: .public +one
  Complex
  (..complex +1.0 +0.0))

(def: .public -one
  Complex
  (..complex -1.0 +0.0))

(def: .public zero
  Complex
  (..complex +0.0 +0.0))

(def: .public (not_a_number? complex)
  (-> Complex Bit)
  (or (f.not_a_number? (the #real complex))
      (f.not_a_number? (the #imaginary complex))))

(def: .public (= param input)
  (-> Complex Complex Bit)
  (and (f.= (the #real param)
            (the #real input))
       (f.= (the #imaginary param)
            (the #imaginary input))))

(with_template [<name> <op>]
  [(def: .public (<name> param input)
     (-> Complex Complex Complex)
     [#real (<op> (the #real param)
                  (the #real input))
      #imaginary (<op> (the #imaginary param)
                       (the #imaginary input))])]

  [+ f.+]
  [- f.-]
  )

(def: .public equivalence
  (Equivalence Complex)
  (implementation
   (def: = ..=)))

(with_template [<name> <transform>]
  [(def: .public <name>
     (-> Complex Complex)
     (|>> (revised #real <transform>)
          (revised #imaginary <transform>)))]

  [opposite f.opposite]
  [signum f.signum]
  )

(def: .public conjugate
  (-> Complex Complex)
  (revised #imaginary f.opposite))

(def: .public (*' param input)
  (-> Frac Complex Complex)
  [#real (f.* param
              (the #real input))
   #imaginary (f.* param
                   (the #imaginary input))])

(def: .public (* param input)
  (-> Complex Complex Complex)
  [#real (f.- (f.* (the #imaginary param)
                   (the #imaginary input))
              (f.* (the #real param)
                   (the #real input)))
   #imaginary (f.+ (f.* (the #real param)
                        (the #imaginary input))
                   (f.* (the #imaginary param)
                        (the #real input)))])

(def: .public (/ param input)
  (-> Complex Complex Complex)
  (let [(open "[0]") param]
    (if (f.< (f.abs #imaginary)
             (f.abs #real))
      (let [quot (f./ #imaginary #real)
            denom (|> #real (f.* quot) (f.+ #imaginary))]
        [..#real (|> (the ..#real input) (f.* quot) (f.+ (the ..#imaginary input)) (f./ denom))
         ..#imaginary (|> (the ..#imaginary input) (f.* quot) (f.- (the ..#real input)) (f./ denom))])
      (let [quot (f./ #real #imaginary)
            denom (|> #imaginary (f.* quot) (f.+ #real))]
        [..#real (|> (the ..#imaginary input) (f.* quot) (f.+ (the ..#real input)) (f./ denom))
         ..#imaginary (|> (the ..#imaginary input) (f.- (f.* quot (the ..#real input))) (f./ denom))]))))

(def: .public (/' param subject)
  (-> Frac Complex Complex)
  (let [(open "[0]") subject]
    [..#real (f./ param #real)
     ..#imaginary (f./ param #imaginary)]))

(def: .public (% param input)
  (-> Complex Complex Complex)
  (let [scaled (/ param input)
        quotient (|> scaled
                     (revised #real f.floor)
                     (revised #imaginary f.floor))]
    (- (* quotient param)
       input)))

(def: .public (cos subject)
  (-> Complex Complex)
  (let [(open "[0]") subject]
    [..#real (f.* (f.cosh #imaginary)
                  (f.cos #real))
     ..#imaginary (f.opposite (f.* (f.sinh #imaginary)
                                   (f.sin #real)))]))

(def: .public (cosh subject)
  (-> Complex Complex)
  (let [(open "[0]") subject]
    [..#real (f.* (f.cos #imaginary)
                  (f.cosh #real))
     ..#imaginary (f.* (f.sin #imaginary)
                       (f.sinh #real))]))

(def: .public (sin subject)
  (-> Complex Complex)
  (let [(open "[0]") subject]
    [..#real (f.* (f.cosh #imaginary)
                  (f.sin #real))
     ..#imaginary (f.* (f.sinh #imaginary)
                       (f.cos #real))]))

(def: .public (sinh subject)
  (-> Complex Complex)
  (let [(open "[0]") subject]
    [..#real (f.* (f.cos #imaginary)
                  (f.sinh #real))
     ..#imaginary (f.* (f.sin #imaginary)
                       (f.cosh #real))]))

(def: .public (tan subject)
  (-> Complex Complex)
  (let [(open "[0]") subject
        r2 (f.* +2.0 #real)
        i2 (f.* +2.0 #imaginary)
        d (f.+ (f.cos r2) (f.cosh i2))]
    [..#real (f./ d (f.sin r2))
     ..#imaginary (f./ d (f.sinh i2))]))

(def: .public (tanh subject)
  (-> Complex Complex)
  (let [(open "[0]") subject
        r2 (f.* +2.0 #real)
        i2 (f.* +2.0 #imaginary)
        d (f.+ (f.cosh r2) (f.cos i2))]
    [..#real (f./ d (f.sinh r2))
     ..#imaginary (f./ d (f.sin i2))]))

(def: .public (abs subject)
  (-> Complex Frac)
  (let [(open "[0]") subject]
    (if (f.< (f.abs #imaginary)
             (f.abs #real))
      (if (f.= +0.0 #imaginary)
        (f.abs #real)
        (let [q (f./ #imaginary #real)]
          (f.* (f.pow +0.5 (f.+ +1.0 (f.* q q)))
               (f.abs #imaginary))))
      (if (f.= +0.0 #real)
        (f.abs #imaginary)
        (let [q (f./ #real #imaginary)]
          (f.* (f.pow +0.5 (f.+ +1.0 (f.* q q)))
               (f.abs #real)))))))

(def: .public (exp subject)
  (-> Complex Complex)
  (let [(open "[0]") subject
        r_exp (f.exp #real)]
    [..#real (f.* r_exp (f.cos #imaginary))
     ..#imaginary (f.* r_exp (f.sin #imaginary))]))

(def: .public (log subject)
  (-> Complex Complex)
  (let [(open "[0]") subject]
    [..#real (|> subject ..abs f.log)
     ..#imaginary (f.atan_2 #real #imaginary)]))

(with_template [<name> <type> <op>]
  [(def: .public (<name> param input)
     (-> <type> Complex Complex)
     (|> input log (<op> param) exp))]

  [pow  Complex ..*]
  [pow' Frac    ..*']
  )

(def: (with_sign sign magnitude)
  (-> Frac Frac Frac)
  (f.* (f.signum sign) magnitude))

(def: .public (root_2 input)
  (-> Complex Complex)
  (let [(open "[0]") input
        t (|> input ..abs (f.+ (f.abs #real)) (f./ +2.0) (f.pow +0.5))]
    (if (f.< +0.0 #real)
      [..#real (f./ (f.* +2.0 t)
                    (f.abs #imaginary))
       ..#imaginary (f.* t (..with_sign #imaginary +1.0))]
      [..#real t
       ..#imaginary (f./ (f.* +2.0 t)
                         #imaginary)])))

(def: (root_2-1z input)
  (-> Complex Complex)
  (|> (complex +1.0) (- (* input input)) ..root_2))

(def: .public (reciprocal (open "[0]"))
  (-> Complex Complex)
  (if (f.< (f.abs #imaginary)
           (f.abs #real))
    (let [q (f./ #imaginary #real)
          scale (f./ (|> #real (f.* q) (f.+ #imaginary))
                     +1.0)]
      [..#real (f.* q scale)
       ..#imaginary (f.opposite scale)])
    (let [q (f./ #real #imaginary)
          scale (f./ (|> #imaginary (f.* q) (f.+ #real))
                     +1.0)]
      [..#real scale
       ..#imaginary (|> scale f.opposite (f.* q))])))

(def: .public (acos input)
  (-> Complex Complex)
  (|> input
      (..+ (|> input ..root_2-1z (..* ..i)))
      ..log
      (..* (..opposite ..i))))

(def: .public (asin input)
  (-> Complex Complex)
  (|> input
      ..root_2-1z
      (..+ (..* ..i input))
      ..log
      (..* (..opposite ..i))))

(def: .public (atan input)
  (-> Complex Complex)
  (|> input
      (..+ ..i)
      (../ (..- input ..i))
      ..log
      (..* (../ (..complex +2.0) ..i))))

(def: .public (argument (open "[0]"))
  (-> Complex Frac)
  (f.atan_2 #real #imaginary))

(def: .public (roots nth input)
  (-> Nat Complex (List Complex))
  (case nth
    0 (list)
    _ (let [r_nth (|> nth .int int.frac)
            nth_root_of_abs (|> input ..abs (f.pow (f./ r_nth +1.0)))
            nth_phi (|> input ..argument (f./ r_nth))
            slice (|> f.pi (f.* +2.0) (f./ r_nth))]
        (|> (list.indices nth)
            (list#each (function (_ nth')
                         (let [inner (|> nth' .int int.frac
                                         (f.* slice)
                                         (f.+ nth_phi))
                               real (f.* nth_root_of_abs
                                         (f.cos inner))
                               imaginary (f.* nth_root_of_abs
                                              (f.sin inner))]
                           [..#real real
                            ..#imaginary imaginary])))))))

(def: .public (approximately? margin_of_error standard value)
  (-> Frac Complex Complex Bit)
  (and (f.approximately? margin_of_error
                         (the ..#real standard)
                         (the ..#real value))
       (f.approximately? margin_of_error
                         (the ..#imaginary standard)
                         (the ..#imaginary value))))