forked from sjwhitworth/golearn
/
utilities.go
47 lines (38 loc) · 878 Bytes
/
utilities.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
// Package utilities implements a host of helpful miscellaneous functions to the library.
package utilities
import (
"sort"
"github.com/gonum/matrix/mat64"
)
type sortedIntMap struct {
m map[int]float64
s []int
}
func (sm *sortedIntMap) Len() int {
return len(sm.m)
}
func (sm *sortedIntMap) Less(i, j int) bool {
return sm.m[sm.s[i]] < sm.m[sm.s[j]]
}
func (sm *sortedIntMap) Swap(i, j int) {
sm.s[i], sm.s[j] = sm.s[j], sm.s[i]
}
func SortIntMap(m map[int]float64) []int {
sm := new(sortedIntMap)
sm.m = m
sm.s = make([]int, len(m))
i := 0
for key := range m {
sm.s[i] = key
i++
}
sort.Sort(sm)
return sm.s
}
func FloatsToMatrix(floats []float64) *mat64.Dense {
return mat64.NewDense(1, len(floats), floats)
}
func VectorToMatrix(vector *mat64.Vector) *mat64.Dense {
vec := vector.RawVector()
return mat64.NewDense(1, len(vec.Data), vec.Data)
}