mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-26 13:49:14 +08:00
31 lines
656 B
Go
31 lines
656 B
Go
package pairwise
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/gonum/matrix/mat64"
|
|
)
|
|
|
|
type Euclidean struct{}
|
|
|
|
func NewEuclidean() *Euclidean {
|
|
return &Euclidean{}
|
|
}
|
|
|
|
// InnerProduct computes a Eucledian inner product.
|
|
func (self *Euclidean) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
|
result := vectorX.Dot(vectorY)
|
|
|
|
return result
|
|
}
|
|
|
|
// Distance computes Euclidean distance (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)
|
|
}
|