aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/math/number/complex.lux
blob: 2b8eada5982e653d05c1eb0d12a6d0a055e56561 (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
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]
    [\\specification
     ["$[0]" equivalence]]]
   [data
    [collection
     ["[0]" list (.use "[1]#[0]" functor)]]]
   [math
    ["[0]" random (.only Random)]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" / (.only)
   [//
    ["n" nat]
    ["f" frac]
    ["[0]" int]]]])

... This margin of error is necessary because floating-point arithmetic is not exact.
(def margin_of_error
  +0.000000001)

(def dimension
  (Random Frac)
  (do [! random.monad]
    [factor (|> random.nat (at ! each (|>> (n.% 1000) (n.max 1))))
     measure (|> random.safe_frac (random.only (f.> +0.0)))]
    (in (f.* (|> factor .int int.frac)
             measure))))

(def .public random
  (Random /.Complex)
  (do random.monad
    [real ..dimension
     imaginary ..dimension]
    (in (/.complex real imaginary))))

(def angle
  (Random /.Complex)
  (at random.monad each
      (|>> (revised /.#real (f.% +1.0))
           (revised /.#imaginary (f.% +1.0)))
      ..random))

(def construction
  Test
  (do random.monad
    [real ..dimension
     imaginary ..dimension]
    (all _.and
         (_.coverage [/.complex]
           (and (let [r+i (/.complex real imaginary)]
                  (and (f.= real (the /.#real r+i))
                       (f.= imaginary (the /.#imaginary r+i))))
                (let [r+i (/.complex real)]
                  (and (f.= real (the /.#real r+i))
                       (f.= +0.0 (the /.#imaginary r+i))))))
         (_.coverage [/.approximately?]
           (/.approximately? ..margin_of_error
                             (/.complex real imaginary)
                             (/.complex real imaginary)))
         (_.coverage [/.not_a_number?]
           (and (/.not_a_number? (/.complex f.not_a_number imaginary))
                (/.not_a_number? (/.complex real f.not_a_number))))
         )))

(def constant
  Test
  (do random.monad
    [sample ..random
     dimension ..dimension]
    (all _.and
         (_.coverage [/.zero]
           (/.= /.zero (/.* /.zero sample)))
         (_.coverage [/.+one]
           (/.= sample (/.* /.+one sample)))
         (_.coverage [/.-one]
           (and (/.= /.zero
                     (/.+ sample
                          (/.* /.-one sample)))
                (/.= sample (/.* /.-one (/.* /.-one sample)))))
         (_.coverage [/.i]
           (and (/.= (/.complex +0.0 dimension)
                     (/.* /.i (/.complex dimension)))
                (/.= (/.* /.-one sample)
                     (/.* /.i (/.* /.i sample)))))
         )))

(def absolute_value&argument
  Test
  (do random.monad
    [real ..dimension
     imaginary ..dimension]
    (all _.and
         (_.coverage [/.abs]
           (let [normal!
                 (let [r+i (/.complex real imaginary)]
                   (and (f.>= (f.abs real) (/.abs r+i))
                        (f.>= (f.abs imaginary) (/.abs r+i))))

                 not_a_number!
                 (and (f.not_a_number? (/.abs (/.complex f.not_a_number imaginary)))
                      (f.not_a_number? (/.abs (/.complex real f.not_a_number))))

                 infinity!
                 (and (f.= f.positive_infinity (/.abs (/.complex f.positive_infinity imaginary)))
                      (f.= f.positive_infinity (/.abs (/.complex real f.positive_infinity)))
                      (f.= f.positive_infinity (/.abs (/.complex f.negative_infinity imaginary)))
                      (f.= f.positive_infinity (/.abs (/.complex real f.negative_infinity))))]
             (and normal!
                  not_a_number!
                  infinity!)))
         ... https://en.wikipedia.org/wiki/Argument_(complex_analysis)#Identities
         (_.coverage [/.argument]
           (let [sample (/.complex real imaginary)]
             (or (/.= /.zero sample)
                 (/.approximately? ..margin_of_error
                                   sample
                                   (/.*' (/.abs sample)
                                         (/.exp (/.* /.i (/.complex (/.argument sample)))))))))
         )))

(def number
  Test
  (do random.monad
    [x ..random
     y ..random
     factor ..dimension]
    (all _.and
         (_.coverage [/.+]
           (let [z (/.+ y x)]
             (and (/.= z
                       (/.complex (f.+ (the /.#real y)
                                       (the /.#real x))
                                  (f.+ (the /.#imaginary y)
                                       (the /.#imaginary x)))))))
         (_.coverage [/.-]
           (let [normal!
                 (let [z (/.- y x)]
                   (and (/.= z
                             (/.complex (f.- (the /.#real y)
                                             (the /.#real x))
                                        (f.- (the /.#imaginary y)
                                             (the /.#imaginary x))))))

                 inverse!
                 (and (|> x (/.+ y) (/.- y) (/.approximately? ..margin_of_error x))
                      (|> x (/.- y) (/.+ y) (/.approximately? ..margin_of_error x)))]
             (and normal!
                  inverse!)))
         (_.coverage [/.* /./]
           (|> x (/.* y) (/./ y) (/.approximately? ..margin_of_error x)))
         (_.coverage [/.*' /./']
           (|> x (/.*' factor) (/./' factor) (/.approximately? ..margin_of_error x)))
         (_.coverage [/.%]
           (let [rem (/.% y x)
                 quotient (|> x (/.- rem) (/./ y))
                 floored (|> quotient
                             (revised /.#real f.floor)
                             (revised /.#imaginary f.floor))]
             (/.approximately? +0.000000000001
                               x
                               (|> quotient (/.* y) (/.+ rem)))))
         )))

(def conjugate&reciprocal&signum&negation
  Test
  (do random.monad
    [x ..random]
    (all _.and
         (_.coverage [/.conjugate]
           (let [cx (/.conjugate x)]
             (and (f.= (the /.#real x)
                       (the /.#real cx))
                  (f.= (f.opposite (the /.#imaginary x))
                       (the /.#imaginary cx)))))
         (_.coverage [/.reciprocal]
           (let [reciprocal!
                 (|> x (/.* (/.reciprocal x)) (/.approximately? ..margin_of_error /.+one))
                 
                 own_inverse!
                 (|> x /.reciprocal /.reciprocal (/.approximately? ..margin_of_error x))]
             (and reciprocal!
                  own_inverse!)))
         (_.coverage [/.signum]
           ... Absolute value of signum is always root_2(2), 1 or 0.
           (let [signum_abs (|> x /.signum /.abs)]
             (or (f.= +0.0 signum_abs)
                 (f.= +1.0 signum_abs)
                 (f.= (f.pow +0.5 +2.0) signum_abs))))
         (_.coverage [/.opposite]
           (let [own_inverse!
                 (let [there (/.opposite x)
                       back_again (/.opposite there)]
                   (and (not (/.= there x))
                        (/.= back_again x)))

                 absolute!
                 (f.= (/.abs x)
                      (/.abs (/.opposite x)))]
             (and own_inverse!
                  absolute!)))
         )))

(def (trigonometric_symmetry forward backward angle)
  (-> (-> /.Complex /.Complex) (-> /.Complex /.Complex) /.Complex Bit)
  (let [normal (|> angle forward backward)]
    (|> normal forward backward (/.approximately? ..margin_of_error normal))))

(def trigonometry
  Test
  (do [! random.monad]
    [angle ..angle]
    (all _.and
         (_.coverage [/.sin /.asin]
           (trigonometric_symmetry /.sin /.asin angle))
         (_.coverage [/.cos /.acos]
           (trigonometric_symmetry /.cos /.acos angle))
         (_.coverage [/.tan /.atan]
           (trigonometric_symmetry /.tan /.atan angle)))))

(def hyperbolic
  Test
  (do [! random.monad]
    [angle ..angle]
    (all _.and
         (_.coverage [/.sinh]
           (/.approximately? ..margin_of_error
                             (|> angle (/.* /.i) /.sin (/.* /.i) (/.* /.-one))
                             (/.sinh angle)))
         (_.coverage [/.cosh]
           (/.approximately? ..margin_of_error
                             (|> angle (/.* /.i) /.cos)
                             (/.cosh angle)))
         (_.coverage [/.tanh]
           (/.approximately? ..margin_of_error
                             (|> angle (/.* /.i) /.tan (/.* /.i) (/.* /.-one))
                             (/.tanh angle)))
         )))

(def exponentiation&logarithm
  Test
  (do random.monad
    [x ..random]
    (all _.and
         (_.coverage [/.pow /.root_2]
           (|> x (/.pow (/.complex +2.0)) /.root_2 (/.approximately? ..margin_of_error x)))
         (_.coverage [/.pow']
           (|> x (/.pow' +2.0) (/.pow' +0.5) (/.approximately? ..margin_of_error x)))
         (_.coverage [/.log /.exp]
           (|> x /.log /.exp (/.approximately? ..margin_of_error x)))
         )))

(def root
  Test
  (do [! random.monad]
    [sample ..random
     degree (|> random.nat (at ! each (|>> (n.max 1) (n.% 5))))]
    (_.coverage [/.roots]
      (|> sample
          (/.roots degree)
          (list#each (/.pow' (|> degree .int int.frac)))
          (list.every? (/.approximately? ..margin_of_error sample))))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Complex])
      (all _.and
           (_.for [/.= /.equivalence]
                  ($equivalence.spec /.equivalence ..random))

           ..construction
           ..constant
           ..absolute_value&argument
           ..number
           ..conjugate&reciprocal&signum&negation
           ..trigonometry
           ..hyperbolic
           ..exponentiation&logarithm
           ..root
           )))