-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinalg.go
53 lines (41 loc) · 1.08 KB
/
linalg.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
package linalg
import (
"math"
"github.com/twgophers/collections"
)
//Add subtracts two collections.Vectors element wise
func Add(x, y collections.Vector) collections.Vector {
pairs := collections.Zip(x, y)
result := make(collections.Vector, len(pairs))
for i, value := range pairs {
result[i] = value.A + value.B
}
return result
}
//Subtract subtracts two collections.collections.Vectors elementwise
func Subtract(x, y collections.Vector) collections.Vector {
pairs := collections.Zip(x, y)
result := make(collections.Vector, len(pairs))
for i, value := range pairs {
result[i] = value.A - value.B
}
return result
}
func Dot(x, y collections.Vector) float64 {
pairs := collections.Zip(x, y)
var sum float64
for _, tuple := range pairs {
sum += tuple.A * tuple.B
}
return sum
}
func SumOfSquares(sample collections.Vector) float64 {
return Dot(sample, sample)
}
func SquaredDistance(x, y collections.Vector) float64 {
result := Subtract(x, y)
return SumOfSquares(result)
}
func Distance(x, y collections.Vector) float64 {
return math.Sqrt(SquaredDistance(x, y))
}