2014-05-04 09:57:57 +01:00
|
|
|
// Package utilities implements a host of helpful miscellaneous functions to the library.
|
2014-01-04 19:16:05 +00:00
|
|
|
package utilities
|
|
|
|
|
|
|
|
import (
|
2014-04-30 08:57:13 +01:00
|
|
|
"sort"
|
2014-05-03 23:08:43 +01:00
|
|
|
|
2018-03-23 23:39:55 +00:00
|
|
|
"gonum.org/v1/gonum/mat"
|
2014-04-30 08:57:13 +01:00
|
|
|
)
|
2014-01-04 19:16:05 +00:00
|
|
|
|
|
|
|
type sortedIntMap struct {
|
|
|
|
m map[int]float64
|
|
|
|
s []int
|
|
|
|
}
|
2014-04-30 08:57:13 +01:00
|
|
|
|
2014-01-04 19:16:05 +00:00
|
|
|
func (sm *sortedIntMap) Len() int {
|
|
|
|
return len(sm.m)
|
|
|
|
}
|
2014-04-30 08:57:13 +01:00
|
|
|
|
2014-01-04 19:16:05 +00:00
|
|
|
func (sm *sortedIntMap) Less(i, j int) bool {
|
|
|
|
return sm.m[sm.s[i]] < sm.m[sm.s[j]]
|
|
|
|
}
|
2014-04-30 08:57:13 +01:00
|
|
|
|
2014-01-04 19:16:05 +00:00
|
|
|
func (sm *sortedIntMap) Swap(i, j int) {
|
|
|
|
sm.s[i], sm.s[j] = sm.s[j], sm.s[i]
|
|
|
|
}
|
2014-04-30 08:57:13 +01:00
|
|
|
|
2014-01-04 19:16:05 +00:00
|
|
|
func SortIntMap(m map[int]float64) []int {
|
|
|
|
sm := new(sortedIntMap)
|
|
|
|
sm.m = m
|
|
|
|
sm.s = make([]int, len(m))
|
|
|
|
i := 0
|
2014-07-18 13:17:19 +03:00
|
|
|
for key := range m {
|
2014-01-04 19:16:05 +00:00
|
|
|
sm.s[i] = key
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
sort.Sort(sm)
|
|
|
|
return sm.s
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:39:55 +00:00
|
|
|
func FloatsToMatrix(floats []float64) *mat.Dense {
|
|
|
|
return mat.NewDense(1, len(floats), floats)
|
2014-05-03 23:08:43 +01:00
|
|
|
}
|
2015-01-27 12:32:19 +00:00
|
|
|
|
2018-03-23 23:39:55 +00:00
|
|
|
func VectorToMatrix(vector mat.Vector) *mat.Dense {
|
|
|
|
denseCopy := mat.VecDenseCopyOf(vector)
|
|
|
|
vec := denseCopy.RawVector()
|
|
|
|
return mat.NewDense(1, len(vec.Data), vec.Data)
|
2015-01-27 12:32:19 +00:00
|
|
|
}
|