-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.go
67 lines (55 loc) · 1.22 KB
/
utility.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
package naivebayes
import (
"math"
"reflect"
)
// https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.logsumexp.html
func logsumexp(array []float64) float64 {
_, aMax := argmax(array)
if math.IsInf(aMax, 0) {
aMax = 0
}
tmp := make([]float64, len(array))
sum := .0
for i, value := range array {
tmp[i] = math.Exp(value - aMax)
sum += tmp[i]
}
return math.Log(sum) + aMax
}
func in_array(val interface{}, array interface{}) (exists bool, index int) {
exists = false
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
index = i
exists = true
return
}
}
}
return
}
func all_in_array(val interface{}, array interface{}) (exists bool, indexes []int) {
exists = false
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
indexes = append(indexes, i)
exists = true
}
}
}
return
}
func int_as_float(val []int) (out []float64) {
for _, v := range val {
out = append(out, float64(v))
}
return
}