-
Notifications
You must be signed in to change notification settings - Fork 1
/
euclidean.go
47 lines (39 loc) · 1.11 KB
/
euclidean.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 disfun
import (
"github.com/gonum/matrix/mat64"
"math"
)
// Euclidean finds the "ordinary" distance between vectors [u,v] given by the Pythagorean formula (sum of squares of all points):
//
// Example:
// dist((u, v) = dist(v, u)) = √(v_1 - u_1)² + (v_2 - u_2)² + ... + (v_n - u_n)²
//
// References:
// http://reference.wolfram.com/language/ref/EuclideanDistance.html
type Euclidean struct{}
// NewEuclidean initializes the Euclidean struct.
func NewEuclidean() *Euclidean {
return &Euclidean{}
}
// InnerProduct computes a Eucledian inner product.
func (e *Euclidean) InnerProduct(u, v *mat64.Dense) (result float64) {
result = mat64.Dot(u, v)
return
}
// Distance finds the Euclidean distance.
func (e *Euclidean) Distance(u, v *mat64.Dense) float64 {
subVec := mat64.NewDense(0, 0, nil)
subVec.Sub(u, v)
result := e.InnerProduct(subVec, subVec)
return math.Sqrt(result)
}
/*
Not sure why this doesn't work!!
func EuclideanDistance(u, v *mat64.Dense) float64 {
subVec := mat64.NewDense(0, 0, nil)
subVec.Sub(u, v)
result := u.Dot(v)
//fmt.Printf("%v\n", result)
return math.Sqrt(result)
}
*/