aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math.lux
blob: e088df673f3f1ddbfa27d8c933146ee09611f203 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
(.module: {#.doc "Common mathematical constants and functions."}
  [library
   [lux #*
    ["@" target]
    [math
     [number
      ["n" nat]
      ["i" int]]]]])

(template [<name> <value> <doc>]
  [(def: .public <name>
     {#.doc <doc>}
     <value>)]

  [e   +2.7182818284590452354  "The base of the natural logarithm."]
  ... ["π is wrong!" by Bob Palais](https://www.math.utah.edu/~palais/pi.html)
  [pi  +3.14159265358979323846 "The ratio of a circle's circumference to its diameter."]
  ... [The Tau Manifesto](https://tauday.com/tau-manifesto)
  [tau +6.28318530717958647692 "The ratio of a circle's circumference to its radius."]
  )

(for {@.old
      (as_is (template [<name> <method>]
               [(def: .public (<name> input)
                  (-> Frac Frac)
                  (<method> input))]

               [cos   "jvm invokestatic:java.lang.Math:cos:double"]
               [sin   "jvm invokestatic:java.lang.Math:sin:double"]
               [tan   "jvm invokestatic:java.lang.Math:tan:double"]

               [acos  "jvm invokestatic:java.lang.Math:acos:double"]
               [asin  "jvm invokestatic:java.lang.Math:asin:double"]
               [atan  "jvm invokestatic:java.lang.Math:atan:double"]
               
               [exp   "jvm invokestatic:java.lang.Math:exp:double"]
               [log   "jvm invokestatic:java.lang.Math:log:double"]
               
               [ceil  "jvm invokestatic:java.lang.Math:ceil:double"]
               [floor "jvm invokestatic:java.lang.Math:floor:double"]

               [root/2 "jvm invokestatic:java.lang.Math:sqrt:double"]
               [root/3 "jvm invokestatic:java.lang.Math:cbrt:double"]
               )
             (def: .public (pow param subject)
               (-> Frac Frac Frac)
               ("jvm invokestatic:java.lang.Math:pow:double,double" subject param)))

      @.jvm
      (as_is (template: (!double value)
               [(|> value
                    (:as (primitive "java.lang.Double"))
                    "jvm object cast")])
             
             (template: (!frac value)
               [(|> value
                    "jvm object cast"
                    (: (primitive "java.lang.Double"))
                    (:as Frac))])
             
             (template [<name> <method>]
               [(def: .public <name>
                  (-> Frac Frac)
                  (|>> !double
                       ["D"]
                       ("jvm member invoke static" [] "java.lang.Math" <method> [])
                       !frac))]

               [cos   "cos"]
               [sin   "sin"]
               [tan   "tan"]

               [acos  "acos"]
               [asin  "asin"]
               [atan  "atan"]
               
               [exp   "exp"]
               [log   "log"]
               
               [ceil  "ceil"]
               [floor "floor"]
               
               [root/2 "sqrt"]
               [root/3 "cbrt"]
               )
             
             (def: .public (pow param subject)
               (-> Frac Frac Frac)
               (|> ("jvm member invoke static" [] "java.lang.Math" "pow" []
                    ["D" (!double subject)] ["D" (!double param)])
                   !frac)))

      @.js
      (as_is (template [<name> <method>]
               [(def: .public <name>
                  (-> Frac Frac)
                  (|>> ("js apply" ("js constant" <method>))
                       (:as Frac)))]

               [cos   "Math.cos"]
               [sin   "Math.sin"]
               [tan   "Math.tan"]

               [acos  "Math.acos"]
               [asin  "Math.asin"]
               [atan  "Math.atan"]
               
               [exp   "Math.exp"]
               [log   "Math.log"]
               
               [ceil  "Math.ceil"]
               [floor "Math.floor"]

               [root/2 "Math.sqrt"]
               [root/3 "Math.cbrt"]
               )
             
             (def: .public (pow param subject)
               (-> Frac Frac Frac)
               (:as Frac ("js apply" ("js constant" "Math.pow") subject param))))

      @.python
      (as_is (template [<name> <method>]
               [(def: .public <name>
                  (-> Frac Frac)
                  (|>> ("python object do" <method> ("python import" "math"))
                       (:as Frac)))]

               [cos   "cos"]
               [sin   "sin"]
               [tan   "tan"]

               [acos  "acos"]
               [asin  "asin"]
               [atan  "atan"]
               
               [exp   "exp"]
               [log   "log"]
               
               [ceil  "ceil"]
               [floor "floor"]

               [root/2 "sqrt"]
               )
             
             (def: .public (pow param subject)
               (-> Frac Frac Frac)
               (:as Frac ("python object do" "pow" ("python import" "math") subject param)))

             (def: .public root/3
               (-> Frac Frac)
               (..pow ("lux f64 /" +3.0 +1.0))))

      @.lua
      (as_is (template [<name> <method>]
               [(def: .public <name>
                  (-> Frac Frac)
                  (|>> ("lua apply" ("lua constant" <method>))
                       (:as Frac)))]

               [cos   "math.cos"]
               [sin   "math.sin"]
               [tan   "math.tan"]

               [acos  "math.acos"]
               [asin  "math.asin"]
               [atan  "math.atan"]
               
               [exp   "math.exp"]
               [log   "math.log"]
               
               [ceil  "math.ceil"]
               [floor "math.floor"]

               [root/2 "math.sqrt"]
               )
             
             (def: .public (pow param subject)
               (-> Frac Frac Frac)
               ("lua power" param subject))

             (def: .public root/3
               (-> Frac Frac)
               (..pow ("lux f64 /" +3.0 +1.0))))

      @.ruby
      (as_is (template [<name> <method>]
               [(def: .public <name>
                  (-> Frac Frac)
                  (|>> ("ruby apply" ("ruby constant" <method>))
                       (:as Frac)))]

               [cos   "Math.cos"]
               [sin   "Math.sin"]
               [tan   "Math.tan"]

               [acos  "Math.acos"]
               [asin  "Math.asin"]
               [atan  "Math.atan"]
               
               [exp   "Math.exp"]
               [log   "Math.log"]
               
               [root/2 "Math.sqrt"]
               [root/3 "Math.cbrt"]
               )

             (template [<name> <method>]
               [(def: .public <name>
                  (-> Frac Frac)
                  (|>> ("ruby object do" <method>)
                       (:as Int)
                       ("lux i64 f64")))]

               [ceil  "ceil"]
               [floor "floor"]
               )

             (def: .public (pow param subject)
               (-> Frac Frac Frac)
               (:as Frac ("ruby object do" "**" subject param))))

      @.php
      (as_is (template [<name> <method>]
               [(def: .public <name>
                  (-> Frac Frac)
                  (|>> ("php apply" ("php constant" <method>))
                       (:as Frac)))]

               [cos   "cos"]
               [sin   "sin"]
               [tan   "tan"]

               [acos  "acos"]
               [asin  "asin"]
               [atan  "atan"]
               
               [exp   "exp"]
               [log   "log"]

               [ceil  "ceil"]
               [floor "floor"]
               
               [root/2 "sqrt"]
               )

             (def: .public (pow param subject)
               (-> Frac Frac Frac)
               (:as Frac ("php apply" ("php constant" "pow") subject param)))

             (def: .public root/3
               (-> Frac Frac)
               (..pow ("lux f64 /" +3.0 +1.0))))

      @.scheme
      (as_is (template [<name> <method>]
               [(def: .public <name>
                  (-> Frac Frac)
                  (|>> ("scheme apply" ("scheme constant" <method>))
                       (:as Frac)))]

               [cos   "cos"]
               [sin   "sin"]
               [tan   "tan"]

               [acos  "acos"]
               [asin  "asin"]
               [atan  "atan"]
               
               [exp   "exp"]
               [log   "log"]

               [ceil  "ceiling"]
               [floor "floor"]
               
               [root/2 "sqrt"]
               )

             (def: .public (pow param subject)
               (-> Frac Frac Frac)
               (:as Frac ("scheme apply" ("scheme constant" "expt") subject param)))

             (def: .public root/3
               (-> Frac Frac)
               (..pow ("lux f64 /" +3.0 +1.0))))
      })

(def: .public (round input)
  (-> Frac Frac)
  (let [floored (floor input)
        diff ("lux f64 -" floored input)]
    (cond ("lux f64 <" diff +0.5)
          ("lux f64 +" +1.0 floored)
          
          ("lux f64 <" -0.5 diff)
          ("lux f64 +" -1.0 floored)
          
          ... else
          floored)))

(def: .public (atan/2 x y)
  (-> Frac Frac Frac)
  (cond ("lux f64 <" x +0.0)
        (..atan ("lux f64 /" x y))

        ("lux f64 <" +0.0 x)
        (if (or ("lux f64 <" y +0.0)
                ("lux f64 =" +0.0 y))
          (|> y ("lux f64 /" x) atan ("lux f64 +" pi))
          (|> y ("lux f64 /" x) atan ("lux f64 -" pi)))

        ... ("lux f64 =" +0.0 x)
        (cond ("lux f64 <" y +0.0)
              (|> pi ("lux f64 /" +2.0))
              
              ("lux f64 <" +0.0 y)
              (|> pi ("lux f64 /" -2.0))
              
              ... ("lux f64 =" +0.0 y)
              ("lux f64 /" +0.0 +0.0))))

(def: .public (log' base input)
  (-> Frac Frac Frac)
  ("lux f64 /"
   (..log base)
   (..log input)))

(def: .public (factorial n)
  (-> Nat Nat)
  (loop [acc 1
         n n]
    (if (n.<= 1 n)
      acc
      (recur (n.* n acc) (-- n)))))

(def: .public (hypotenuse catA catB)
  (-> Frac Frac Frac)
  (..pow +0.5 ("lux f64 +"
               (..pow +2.0 catA)
               (..pow +2.0 catB))))

... Hyperbolic functions
... https://en.wikipedia.org/wiki/Hyperbolic_function#Definitions
(template [<name> <comp> <inverse>]
  [(def: .public (<name> x)
     (-> Frac Frac)
     (|> (..exp x) (<comp> (..exp ("lux f64 *" -1.0 x))) ("lux f64 /" +2.0)))

   (def: .public (<inverse> x)
     (-> Frac Frac)
     (|> +2.0 ("lux f64 /" (|> (..exp x) (<comp> (..exp ("lux f64 *" -1.0 x)))))))]

  [sinh "lux f64 -" csch]
  [cosh "lux f64 +" sech]
  )

(template [<name> <top> <bottom>]
  [(def: .public (<name> x)
     (-> Frac Frac)
     (let [e+ (exp x)
           e- (exp ("lux f64 *" -1.0 x))
           sinh' (|> e+ ("lux f64 -" e-))
           cosh' (|> e+ ("lux f64 +" e-))]
       (|> <top> ("lux f64 /" <bottom>))))]

  [tanh sinh' cosh']
  [coth cosh' sinh']
  )

... https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions#Definitions_in_terms_of_logarithms
(template [<name> <comp>]
  [(def: .public (<name> x)
     (-> Frac Frac)
     (|> x (pow +2.0) (<comp> +1.0) (pow +0.5) ("lux f64 +" x) log))]

  [asinh "lux f64 +"]
  [acosh "lux f64 -"]
  )

(template [<name> <base> <diff>]
  [(def: .public (<name> x)
     (-> Frac Frac)
     (let [x+ (|> <base> ("lux f64 +" <diff>))
           x- (|> <base> ("lux f64 -" <diff>))]
       (|> x+ ("lux f64 /" x-) log ("lux f64 /" +2.0))))]

  [atanh +1.0 x]
  [acoth x +1.0]
  )

(template [<name> <op>]
  [(def: .public (<name> x)
     (-> Frac Frac)
     (let [x^2 (|> x (pow +2.0))]
       (|> +1.0 (<op> x^2) (pow +0.5) ("lux f64 +" +1.0) ("lux f64 /" x) log)))]

  [asech "lux f64 -"]
  [acsch "lux f64 +"]
  )