aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/math.lux
blob: 700bc9919815c01bb13c4bcc2741de847f2d729a (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
  (lux (control monad
                ["p" parser "p/" Functor<Parser>])
       (data (coll [list "L/" Fold<List>])
             [product])
       [macro]
       (macro ["s" syntax #+ syntax: Syntax]
              [code])))

## [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   "lux math cos"]
  [sin   "lux math sin"]
  [tan   "lux math tan"]

  [acos  "lux math acos"]
  [asin  "lux math asin"]
  [atan  "lux math atan"]
  
  [cosh  "lux math cosh"]
  [sinh  "lux math sinh"]
  [tanh  "lux math tanh"]

  [exp   "lux math exp"]
  [log   "lux math log"]
  
  [root2  "lux math root2"]
  [root3  "lux math root3"]

  [ceil  "lux math ceil"]
  [floor "lux math floor"]
  [round "lux math round"]
  )

(do-template [<name> <method>]
  [(def: #export (<name> param subject)
     (-> Frac Frac Frac)
     (<method> subject param))]

  [atan2 "lux math atan2"]
  [pow   "lux math pow"]
  )

(def: #export (log' base input)
  (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) (n.dec n)))))

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

(def: #export (gcd a b)
  {#;doc "Greatest Common Divisor."}
  (-> Nat Nat Nat)
  (case b
    +0 a
    _ (gcd b (n.% b a))))

(def: #export (lcm x y)
  {#;doc "Least Common Multiple."}
  (-> Nat Nat Nat)
  (case [x y]
    (^or [_ +0] [+0 _])
    +0

    _
    (|> x (n./ (gcd x y)) (n.* y))
    ))

## [Syntax]
(type: #rec Infix
  (#Const Code)
  (#Call (List Code))
  (#Unary Code Infix)
  (#Binary Infix Code Infix))

(def: infix^
  (Syntax Infix)
  (<| p;rec (function [infix^])
      ($_ p;alt
          ($_ p;either
              (p/map code;bool s;bool)
              (p/map code;nat s;nat)
              (p/map code;int s;int)
              (p/map code;deg s;deg)
              (p/map code;frac s;frac)
              (p/map code;text s;text)
              (p/map code;symbol s;symbol)
              (p/map code;tag s;tag))
          (s;form (p;many s;any))
          (s;tuple (p;seq s;any infix^))
          (s;tuple ($_ p;either
                       (do p;Monad<Parser>
                         [_ (s;this (' #and))
                          init-subject infix^
                          init-op s;any
                          init-param infix^
                          steps (p;some (p;seq s;any infix^))]
                         (wrap (product;right (L/fold (function [[op param] [subject [_subject _op _param]]]
                                                        [param [(#Binary _subject _op _param)
                                                                (` and)
                                                                (#Binary subject op param)]])
                                                      [init-param [init-subject init-op init-param]]
                                                      steps))))
                       (do p;Monad<Parser>
                         [init-subject infix^
                          init-op s;any
                          init-param infix^
                          steps (p;some (p;seq s;any infix^))]
                         (wrap (L/fold (function [[op param] [_subject _op _param]]
                                         [(#Binary _subject _op _param) op param])
                                       [init-subject init-op init-param]
                                       steps)))
                       ))
          )))

(def: (infix-to-prefix infix)
  (-> Infix Code)
  (case infix
    (#Const value)
    value
    
    (#Call parts)
    (code;form parts)

    (#Unary op subject)
    (` ((~ op) (~ (infix-to-prefix subject))))
    
    (#Binary left op right)
    (` ((~ op) (~ (infix-to-prefix right)) (~ (infix-to-prefix left))))
    ))

(syntax: #export (infix [expr infix^])
  {#;doc (doc "Infix math syntax."
              (infix [x i.* 10])
              (infix [[x i.+ y] i.* [x i.- y]])
              (infix [sin [x i.+ y]])
              (infix [[x n.< y] and [y n.< z]])
              (infix [#and x n.< y n.< z])
              (infix [(n.* +3 +9) gcd +450])

              "The rules for infix syntax are simple."
              "If you want your binary function to work well with it."
              "Then take the argument to the right (y) as your first argument,"
              "and take the argument to the left (x) as your second argument.")}
  (wrap (list (infix-to-prefix expr))))