-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.clj
89 lines (70 loc) · 2.35 KB
/
expression.clj
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
(defn multi-func [f]
(fn [& items]
(fn [array-map]
(def result (mapv eval (mapv (fn [item] (item array-map)) items)))
(try (apply f result)
(catch ArithmeticException _
(/ (first result) 0.0)))
)))
(def add (multi-func +))
(def subtract (multi-func -))
(def multiply (multi-func *))
(def divide (multi-func /))
(defn variable [x]
(fn [array-map] (array-map x)))
(defn constant [x]
(fn [array-map] x))
(def negate (multi-func (partial - 0)))
(def sin (multi-func (fn [x] (Math/sin x))))
(def cos (multi-func (fn [x] (Math/cos x))))
(def sinh (multi-func (fn [x] (Math/sinh x))))
(def cosh (multi-func (fn [x] (Math/cosh x))))
(def sum add)
(def avg (multi-func (fn [& items] (/ (apply + items) (count items))) ))
(defn my-mean [& items]
(double (apply + (mapv (partial * (/ 1 (count items))) items))))
(defn my-varn [& items]
(-
(apply + (mapv (partial * (/ 1 (count items)))
(mapv * items items)))
(Math/pow (apply my-mean items) 2)))
(def mean (multi-func my-mean))
(def varn (multi-func my-varn))
(defn my-sumexp [& items]
(apply + (mapv (partial #(Math/exp %)) items)))
(defn my-softmax [& items]
(/ (Math/exp (nth items 0)) (apply my-sumexp items)))
(def sumexp (multi-func my-sumexp))
(def softmax (multi-func my-softmax))
(defn my-log [b x] (/ (Math/log (Math/abs x)) (Math/log (Math/abs b))))
(def pow (multi-func (fn [x y] (Math/pow x y))))
(def log (multi-func my-log))
(def exp (multi-func #(Math/exp %)))
(def ln (multi-func #(Math/log %)))
(def op {'+ add
'- subtract
'* multiply
'/ divide
'negate negate
'sin sin
'cos cos
'sinh sinh
'cosh cosh
'sum sum
'avg avg
'mean mean
'varn varn
'sumexp sumexp
'softmax softmax
'pow pow
'log log
'exp exp
'ln ln
})
(defn parse [result]
(cond
(number? result) (constant result)
(symbol? result) (variable (str result))
(list? result) (apply (get op (first result)) (mapv parse (rest result)))
))
(defn parseFunction [line] (parse (read-string line)))