aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/math.lux
blob: db314f400f77f8285f704e8a0c947a3c1fa3755c (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [abstract
     [monad (#+ do)]]
    [macro
     ["." template]]
    [math
     ["." random (#+ Random)]
     [number
      ["n" nat]
      ["i" int]
      ["f" frac]]]]]
  [\\library
   ["." /]]
  ["." / #_
   ["#." infix]
   ["#." modulus]
   ["#." modular]
   ["#." number]
   ["#." logic #_
    ["#/." continuous]
    ["#/." fuzzy]]])

(def: margin_of_error
  +0.0000001)

(def: (trigonometric_symmetry forward backward angle)
  (-> (-> Frac Frac) (-> Frac Frac) Frac Bit)
  (let [normal (|> angle forward backward)]
    (|> normal forward backward (f.approximately? ..margin_of_error normal))))

(def: #export test
  Test
  (<| (_.covering /._)
      ($_ _.and
          (do {! random.monad}
            [#let [~= (f.approximately? ..margin_of_error)]
             angle (|> random.safe_frac (\ ! map (f.* /.tau)))]
            ($_ _.and
                (_.cover [/.sin /.asin]
                         (trigonometric_symmetry /.sin /.asin angle))
                (_.cover [/.cos /.acos]
                         (trigonometric_symmetry /.cos /.acos angle))
                (_.cover [/.tan /.atan]
                         (trigonometric_symmetry /.tan /.atan angle))
                (_.cover [/.tau]
                         (and (and (~= +0.0 (/.sin /.tau))
                                   (~= +1.0 (/.cos /.tau)))
                              (and (~= +0.0 (/.sin (f./ +2.0 /.tau)))
                                   (~= -1.0 (/.cos (f./ +2.0 /.tau))))
                              (and (~= +1.0 (/.sin (f./ +4.0 /.tau)))
                                   (~= +0.0 (/.cos (f./ +4.0 /.tau))))
                              (and (~= -1.0 (/.sin (f.* +3.0 (f./ +4.0 /.tau))))
                                   (~= +0.0 (/.cos (f.* +3.0 (f./ +4.0 /.tau)))))
                              (let [x2+y2 (f.+ (/.pow +2.0 (/.sin angle))
                                               (/.pow +2.0 (/.cos angle)))]
                                (~= +1.0 x2+y2))))
                (_.cover [/.pi]
                         (~= (f./ +2.0 /.tau) /.pi))
                ))
          (do {! random.monad}
            [sample (|> random.safe_frac (\ ! map (f.* +1000.0)))]
            ($_ _.and
                (_.cover [/.ceil]
                         (let [ceil'd (/.ceil sample)]
                           (and (|> ceil'd f.int i.frac (f.= ceil'd))
                                (f.>= sample ceil'd)
                                (f.<= +1.0 (f.- sample ceil'd)))))
                (_.cover [/.floor]
                         (let [floor'd (/.floor sample)]
                           (and (|> floor'd f.int i.frac (f.= floor'd))
                                (f.<= sample floor'd)
                                (f.<= +1.0 (f.- floor'd sample)))))
                (_.cover [/.round]
                         (let [round'd (/.round sample)]
                           (and (|> round'd f.int i.frac (f.= round'd))
                                (f.<= +1.0 (f.abs (f.- sample round'd))))))
                (_.cover [/.root/2]
                         (let [sample (f.abs sample)]
                           (|> sample
                               /.root/2
                               (/.pow +2.0)
                               (f.approximately? ..margin_of_error sample))))
                (_.cover [/.root/3]
                         (|> sample
                             /.root/3
                             (/.pow +3.0)
                             (f.approximately? ..margin_of_error sample)))
                ))
          (do {! random.monad}
            [#let [~= (f.approximately? ..margin_of_error)]
             sample (\ ! map (f.* +10.0) random.safe_frac)
             power (\ ! map (|>> (n.% 10) inc n.frac) random.nat)]
            ($_ _.and
                (_.cover [/.exp /.log]
                         (|> sample /.exp /.log (f.approximately? +0.000000000000001 sample)))
                (_.cover [/.e]
                         (~= +1.0 (/.log /.e)))
                (_.cover [/.pow /.log']
                         (let [sample (f.abs sample)]
                           (|> sample
                               (/.pow power)
                               (/.log' sample)
                               (~= power))))
                ))
          (do {! random.monad}
            [#let [~= (f.approximately? ..margin_of_error)]
             angle (\ ! map (f.* /.tau) random.safe_frac)
             sample (\ ! map f.abs random.safe_frac)
             big (\ ! map (f.* +1,000,000,000.00) random.safe_frac)]
            (template.let [(odd! <function>)
                           [(_.cover [<function>]
                                     (~= (f.negate (<function> angle))
                                         (<function> (f.negate angle))))]

                           (even! <function>)
                           [(_.cover [<function>]
                                     (~= (<function> angle)
                                         (<function> (f.negate angle))))]

                           (inverse! <left> <right> <input>)
                           [(_.cover [<left> <right>]
                                     (~= (<right> <input>)
                                         (<left> (f./ <input> +1.0))))]]
              ($_ _.and
                  (odd! /.sinh)
                  (even! /.cosh)
                  (odd! /.tanh)
                  (odd! /.coth)
                  (even! /.sech)
                  (odd! /.csch)

                  (inverse! /.acosh /.asech sample)
                  (inverse! /.asinh /.acsch sample)
                  (inverse! /.atanh /.acoth big)
                  )))
          (do {! random.monad}
            [x (\ ! map (|>> (f.* +10.0) f.abs) random.safe_frac)
             y (\ ! map (|>> (f.* +10.0) f.abs) random.safe_frac)]
            (_.cover [/.hypotenuse]
                     (let [h (/.hypotenuse x y)]
                       (and (f.>= x h)
                            (f.>= y h)))))
          (do {! random.monad}
            [#let [~= (f.approximately? ..margin_of_error)
                   tau/4 (f./ +4.0 /.tau)]
             x (\ ! map (f.* tau/4) random.safe_frac)
             y (\ ! map (f.* tau/4) random.safe_frac)]
            (_.cover [/.atan/2]
                     (let [expected (/.atan/2 x y)
                           actual (if (f.> +0.0 x)
                                    (/.atan (f./ x y))
                                    (if (f.< +0.0 y)
                                      (f.- /.pi (/.atan (f./ x y)))
                                      (f.+ /.pi (/.atan (f./ x y)))))]
                       (and (~= expected actual)
                            (~= tau/4 (/.atan/2 +0.0 (f.abs y)))
                            (~= (f.negate tau/4) (/.atan/2 +0.0 (f.negate (f.abs y))))
                            (f.not_a_number? (/.atan/2 +0.0 +0.0))))))
          (do {! random.monad}
            [of (\ ! map (|>> (n.% 10) inc) random.nat)]
            (_.cover [/.factorial]
                     (and (n.= 1 (/.factorial 0))
                          (|> (/.factorial of) (n.% of) (n.= 0)))))

          /infix.test
          /modulus.test
          /modular.test
          /number.test
          /logic/continuous.test
          /logic/fuzzy.test
          )))