-
Notifications
You must be signed in to change notification settings - Fork 4
/
arith.go
214 lines (184 loc) · 3.69 KB
/
arith.go
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
package vecf32
import "github.com/chewxy/math32"
// Pow performs elementwise
// a̅ ^ b̅
func Pow(a, b []float32) {
b = b[:len(a)]
for i, v := range a {
switch b[i] {
case 0:
a[i] = float32(1)
case 1:
a[i] = v
case 2:
a[i] = v * v
case 3:
a[i] = v * v * v
default:
a[i] = math32.Pow(v, b[i])
}
}
}
func Mod(a, b []float32) {
b = b[:len(a)]
for i, v := range a {
a[i] = math32.Mod(v, b[i])
}
}
// Scale multiplies all values in the slice by the scalar. It performs elementwise
// a̅ * s
func Scale(a []float32, s float32) {
for i, v := range a {
a[i] = v * s
}
}
// ScaleInv divides all values in the slice by the scalar. It performs elementwise
// a̅ / s
func ScaleInv(a []float32, s float32) {
Scale(a, 1/s)
}
/// ScaleInvR divides all numbers in the slice by a scalar
// s / a̅
func ScaleInvR(a []float32, s float32) {
for i, v := range a {
a[i] = s / v
}
}
// Trans adds all the values in the slice by a scalar
// a̅ + s
func Trans(a []float32, s float32) {
for i, v := range a {
a[i] = v + s
}
}
// TransInv subtracts all the values in the slice by a scalar
// a̅ - s
func TransInv(a []float32, s float32) {
Trans(a, -s)
}
// TransInvR subtracts all the numbers in a slice from a scalar
// s - a̅
func TransInvR(a []float32, s float32) {
for i, v := range a {
a[i] = s - v
}
}
// PowOf performs elementwise
// a̅ ^ s
func PowOf(a []float32, s float32) {
for i, v := range a {
a[i] = math32.Pow(v, s)
}
}
// PowOfR performs elementwise
// s ^ a̅
func PowOfR(a []float32, s float32) {
for i, v := range a {
a[i] = math32.Pow(s, v)
}
}
// Max takes two slices, a̅ + b̅, and compares them elementwise. The highest value is put into a̅.
func Max(a, b []float32) {
b = b[:len(a)]
for i, v := range a {
bv := b[i]
if bv > v {
a[i] = bv
}
}
}
// Min takes two slices, a̅ + b̅ and compares them elementwise. The lowest value is put into a̅.
func Min(a, b []float32) {
b = b[:len(a)]
for i, v := range a {
bv := b[i]
if bv < v {
a[i] = bv
}
}
}
/* REDUCTION RELATED */
// MaxOf finds the max of a []float32. it panics if the slice is empty
func MaxOf(a []float32) (retVal float32) {
if len(a) < 1 {
panic("Cannot find the max of an empty slice")
}
return Reduce(max, a[0], a[1:]...)
}
// MinOf finds the max of a []float32. it panics if the slice is empty
func MinOf(a []float32) (retVal float32) {
if len(a) < 1 {
panic("Cannot find the min of an empty slice")
}
return Reduce(min, a[0], a[1:]...)
}
// Argmax returns the index of the min in a slice
func Argmax(a []float32) int {
var f float32
var max int
var set bool
for i, v := range a {
if !set {
f = v
max = i
set = true
continue
}
// TODO: Maybe error instead of this?
if math32.IsNaN(v) || math32.IsInf(v, 1) {
max = i
f = v
break
}
if v > f {
max = i
f = v
}
}
return max
}
// Argmin returns the index of the min in a slice
func Argmin(a []float32) int {
var f float32
var min int
var set bool
for i, v := range a {
if !set {
f = v
min = i
set = true
continue
}
// TODO: Maybe error instead of this?
if math32.IsNaN(v) || math32.IsInf(v, -1) {
min = i
f = v
break
}
if v < f {
min = i
f = v
}
}
return min
}
/* FUNCTION VARIABLES */
var (
add = func(a, b float32) float32 { return a + b }
// sub = func(a, b float32) float32 { return a - b }
// mul = func(a, b float32) float32 { return a * b }
// div = func(a, b float32) float32 { return a / b }
// mod = func(a, b float32) float32 { return math32.Mod(a, b) }
min = func(a, b float32) float32 {
if a < b {
return a
}
return b
}
max = func(a, b float32) float32 {
if a > b {
return a
}
return b
}
)