Documentation ¶
Index ¶
- type IFVector
- func (vec IFVector) Clone() IFVector
- func (vec IFVector) CosineSimilarity(other IFVector) float64
- func (vec IFVector) DivF(divisor float64)
- func (vec IFVector) Dot(multiplier IFVector) float64
- func (vec IFVector) Filter(filter IFVectorFilter)
- func (vec IFVector) FilterMap(filter IFVectorFilter, mapper IFVectorMap)
- func (vec IFVector) ForEach(apply IFVectorEach)
- func (vec IFVector) L1Norm() float64
- func (vec IFVector) L2Norm() float64
- func (vec IFVector) Map(mapper IFVectorMap) IFVector
- func (vec IFVector) Max() float64
- func (vec IFVector) MultF(multiplier float64)
- func (vec IFVector) Prod(multiplier IFVector)
- func (vec IFVector) Reduce(init float64, reducer IFVectorReduce) float64
- func (vec IFVector) Sum() float64
- type IFVectorEach
- type IFVectorFilter
- type IFVectorMap
- type IFVectorReduce
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IFVector ¶
a sparse vector of uint64->float64s
func NewIfVector ¶
func NewIfVector() IFVector
Example ¶
// create a sparse vector vec := NewIfVector() fmt.Println(len(vec)) // add a value vec[100] = 1.06 fmt.Println(len(vec)) // try getting a non-existant value _, ok := vec[0] fmt.Println(ok) val := vec[100] fmt.Println(val)
Output: 0 1 false 1.06
func (IFVector) Clone ¶
Example ¶
a := NewIfVector() a[1] = 1 a[2] = 2 b := a.Clone() fmt.Println(b[1]) fmt.Println(b[2]) a[1] = 100 fmt.Println(b[1])
Output: 1 2 1
func (IFVector) CosineSimilarity ¶
Example ¶
a := NewIfVector() a[1] = 0.702753576 a[2] = 0.702753576 b := NewIfVector() b[1] = 0.140550715 b[2] = 0.140550715 // do it manually dotProduct := a.Dot(b) l2A := a.L2Norm() l2B := b.L2Norm() manualResult := dotProduct / (l2A * l2B) fmt.Printf("%.3f\n", dotProduct) fmt.Printf("%.3f\n", l2A) fmt.Printf("%.3f\n", l2B) fmt.Printf("%.3f\n", manualResult) fmt.Printf("%.3f\n", a.CosineSimilarity(b))
Output: 0.198 0.994 0.199 1.000 1.000
func (IFVector) DivF ¶
DivF divides each element in the vector by the given value.
Example ¶
a := NewIfVector() a[1] = 1 a[2] = 2 a.DivF(2) fmt.Println(a[1]) fmt.Println(a[2])
Output: 0.5 1
func (IFVector) Dot ¶
Dot computes the dot product of the two vectors
Example ¶
a := NewIfVector() a[1] = -6 a[2] = 8 a[40] = 40 // no overlap b := NewIfVector() b[1] = 5 b[2] = 12 b[100] = 100 fmt.Println(a.Dot(b))
Output: 66
func (IFVector) Filter ¶
func (vec IFVector) Filter(filter IFVectorFilter)
Example ¶
vec := NewIfVector() vec[1] = 1 vec[2] = 2 vec[3] = 4 vec[4] = 8 vec.Filter(func(key uint64, val float64) bool { return key%2 == 0 }) // Kept because they were even. fmt.Println(vec[2]) fmt.Println(vec[4]) // Odd rows were removed _, ok := vec[1] fmt.Println(ok) _, ok = vec[3] fmt.Println(ok)
Output: 2 8 false false
func (IFVector) FilterMap ¶
func (vec IFVector) FilterMap(filter IFVectorFilter, mapper IFVectorMap)
Example ¶
vec := NewIfVector() vec[1] = 4 vec[2] = 4 filterOdd := func(key uint64, val float64) bool { return key%2 == 0 } squareVal := func(key uint64, val float64) float64 { return val * val } vec.FilterMap(filterOdd, squareVal) fmt.Println(vec[1]) fmt.Println(vec[2])
Output: 0 16
func (IFVector) ForEach ¶
func (vec IFVector) ForEach(apply IFVectorEach)
Example ¶
vec := NewIfVector() vec[1] = 2 vec[2] = 4 vec[3] = 8 vec[4] = 16 var keysum uint64 var valsum float64 vec.ForEach(func(key uint64, val float64) { keysum += key valsum += val }) fmt.Println(keysum) fmt.Println(valsum)
Output: 10 30
func (IFVector) L1Norm ¶
Example ¶
a := NewIfVector() a[1] = -4 a[2] = 4 fmt.Println(a.L1Norm())
Output: 8
func (IFVector) L2Norm ¶
Example ¶
a := NewIfVector() a[1] = -3 a[2] = -2 a[3] = 1 fmt.Printf("%.3f\n", a.L2Norm())
Output: 3.742
func (IFVector) Map ¶
func (vec IFVector) Map(mapper IFVectorMap) IFVector
Example ¶
vec := NewIfVector() vec[10] = 0 vec[20] = 0 vec[30] = 0 vec.Map(func(key uint64, val float64) float64 { return float64(key) }) fmt.Println(vec[10]) fmt.Println(vec[20]) fmt.Println(vec[30])
Output: 10 20 30
func (IFVector) Max ¶
Example ¶
vec := NewIfVector() vec[1] = 1 vec[2] = 200 vec[4] = -4 max := vec.Max() fmt.Println(max)
Output: 200
func (IFVector) MultF ¶
Example ¶
a := NewIfVector() a[1] = 1 a[2] = 2 a.MultF(10) fmt.Println(a[1]) fmt.Println(a[2])
Output: 10 20
func (IFVector) Prod ¶
Prod is an element-wise product between vectors. a.Prod(b) = [a[1] * b[1], a[2] * b[2], ...]
Example ¶
a := NewIfVector() a[1] = 1 a[2] = 2 b := NewIfVector() b[1] = 3 b[2] = 4 a.Prod(b) fmt.Println(a[1]) fmt.Println(a[2])
Output: 3 8
type IFVectorEach ¶
type IFVectorFilter ¶
type IFVectorMap ¶
type IFVectorReduce ¶
Click to show internal directories.
Click to hide internal directories.