mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-26 13:49:14 +08:00
32 lines
676 B
Go
32 lines
676 B
Go
package pairwise
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/gonum/matrix/mat64"
|
|
)
|
|
|
|
type Euclidean struct{}
|
|
|
|
func NewEuclidean() *Euclidean {
|
|
return &Euclidean{}
|
|
}
|
|
|
|
// Compute usual inner product in the sense of euclidean.
|
|
func (self *Euclidean) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
|
result := vectorX.Dot(vectorY)
|
|
|
|
return result
|
|
}
|
|
|
|
// Compute usual distance in the sense of euclidean.
|
|
// Also known as L2 distance.
|
|
func (self *Euclidean) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
|
subVector := mat64.NewDense(0, 0, nil)
|
|
subVector.Sub(vectorX, vectorY)
|
|
|
|
result := self.InnerProduct(subVector, subVector)
|
|
|
|
return math.Sqrt(result)
|
|
}
|