linalg

package
v0.0.0-...-9825f04 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2017 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IFVector

type IFVector map[uint64]float64

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

func (vec IFVector) Clone() IFVector
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

func (vec IFVector) CosineSimilarity(other IFVector) float64
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

func (vec IFVector) DivF(divisor float64)

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

func (vec IFVector) Dot(multiplier IFVector) float64

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

func (vec IFVector) L1Norm() float64
Example
a := NewIfVector()
a[1] = -4
a[2] = 4

fmt.Println(a.L1Norm())
Output:

8

func (IFVector) L2Norm

func (vec IFVector) L2Norm() float64
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

func (vec IFVector) Max() float64
Example
vec := NewIfVector()
vec[1] = 1
vec[2] = 200
vec[4] = -4

max := vec.Max()

fmt.Println(max)
Output:

200

func (IFVector) MultF

func (vec IFVector) MultF(multiplier float64)
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

func (vec IFVector) Prod(multiplier IFVector)

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

func (IFVector) Reduce

func (vec IFVector) Reduce(init float64, reducer IFVectorReduce) float64
Example
vec := NewIfVector()
vec[1] = 2
vec[2] = 4
vec[3] = 8

sum := vec.Reduce(100, func(key uint64, curr, last float64) float64 {
	return last + curr
})

fmt.Println(sum)
Output:

114

func (IFVector) Sum

func (vec IFVector) Sum() float64
Example
vec := NewIfVector()
vec[1] = 1
vec[2] = 2
vec[4] = 4

sum := vec.Sum()

fmt.Println(sum)
Output:

7

type IFVectorEach

type IFVectorEach func(key uint64, value float64)

type IFVectorFilter

type IFVectorFilter func(key uint64, value float64) bool

type IFVectorMap

type IFVectorMap func(key uint64, value float64) float64

type IFVectorReduce

type IFVectorReduce func(key uint64, value, lastValue float64) float64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL