aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/math.lux
blob: d4650426f4fdc3b1c62194339d2cb7a45c2b7606 (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
(.module: {#.doc "Common mathematical constants and functions."}
  [lux #*])

## [Values]
(do-template [<name> <value> <doc>]
  [(def: #export <name>
     {#.doc <doc>}
     Frac
     <value>)]

  [e   +2.7182818284590452354  "The base of the natural logarithm."]
  [pi  +3.14159265358979323846 "The ratio of a circle's circumference to its diameter."]
  [tau +6.28318530717958647692 "The ratio of a circle's circumference to its radius."]
  )

(do-template [<name> <method>]
  [(def: #export (<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"]
  )

(def: #export (round input)
  (-> Frac Frac)
  (let [floored (floor input)
        diff (f/- floored input)]
    (cond (f/> +0.5 diff)
          (f/+ +1.0 floored)
          
          (f/< -0.5 diff)
          (f/+ -1.0 floored)
          
          ## else
          floored)))

(def: #export (pow param subject)
  (-> Frac Frac Frac)
  ("jvm invokestatic:java.lang.Math:pow:double,double" subject param))

(def: #export (atan2 param subject)
  (-> Frac Frac Frac)
  (cond (f/> +0.0 param)
        (atan (f// param subject))

        (f/< +0.0 param)
        (if (f/>= +0.0 subject)
          (|> subject (f// param) atan (f/+ pi))
          (|> subject (f// param) atan (f/- pi)))

        ## (f/= +0.0 param)
        (cond (f/> +0.0 subject)
              (|> pi (f// +2.0))
              
              (f/< +0.0 subject)
              (|> pi (f// -2.0))
              
              ## (f/= +0.0 subject)
              (f// +0.0 +0.0))))

(def: #export (log' base input)
  (-> Frac Frac Frac)
  (f// (log base)
       (log input)))

(def: #export (factorial n)
  (-> Nat Nat)
  (loop [acc 1
         n n]
    (if (n/<= 1 n)
      acc
      (recur (n/* n acc) (dec n)))))

(def: #export (hypotenuse catA catB)
  (-> Frac Frac Frac)
  (pow +0.5 (f/+ (pow +2.0 catA)
                 (pow +2.0 catB))))

(do-template [<type> <mod> <gcd> <lcm> <zero> <*> </> <->]
  [(def: #export (<gcd> a b)
     {#.doc "Greatest Common Divisor."}
     (-> <type> <type> <type>)
     (case b
       <zero> a
       _ (<gcd> b (<mod> b a))))

   (def: #export (<lcm> a b)
     {#.doc "Least Common Multiple."}
     (-> <type> <type> <type>)
     (case [a b]
       (^or [_ <zero>] [<zero> _])
       <zero>

       _
       (|> a (</> (<gcd> a b)) (<*> b))
       ))]

  [Nat n/mod n/gcd n/lcm 0 n/* n// n/-]
  [Int i/mod i/gcd i/lcm  +0 i/* i// i/-]
  )

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

   (def: #export (<inverse> x)
     (-> Frac Frac)
     (|> +2.0 (f// (|> (exp x) (<comp> (exp (f/* -1.0 x)))))))]

  [sinh f/- csch]
  [cosh f/+ sech]
  )

(do-template [<name> <top> <bottom>]
  [(def: #export (<name> x)
     (-> Frac Frac)
     (let [e+ (exp x)
           e- (exp (f/* -1.0 x))
           sinh' (|> e+ (f/- e-))
           cosh' (|> e+ (f/+ e-))]
       (|> <top> (f// <bottom>))))]

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

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

  [asinh f/+]
  [acosh f/-]
  )

(do-template [<name> <base> <diff>]
  [(def: #export (<name> x)
     (-> Frac Frac)
     (let [x+ (|> <base> (f/+ <diff>))
           x- (|> <base> (f/- <diff>))]
       (|> x+ (f// x-) log (f// +2.0))))]

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

(do-template [<name> <op>]
  [(def: #export (<name> x)
     (-> Frac Frac)
     (let [x^2 (|> x (pow +2.0))]
       (|> +1.0 (<op> x^2) (pow +0.5) (f/+ +1.0) (f// x) log)))]

  [asech f/-]
  [acsch f/+]
  )