2014-05-02 18:10:20 +08:00
|
|
|
package pairwise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2014-05-03 00:55:38 +08:00
|
|
|
"github.com/gonum/matrix/mat64"
|
2014-05-02 18:10:20 +08:00
|
|
|
)
|
|
|
|
|
2014-05-02 20:09:54 +08:00
|
|
|
type Euclidean struct{}
|
2014-05-02 18:10:20 +08:00
|
|
|
|
2014-05-02 20:09:54 +08:00
|
|
|
func NewEuclidean() *Euclidean {
|
|
|
|
return &Euclidean{}
|
|
|
|
}
|
2014-05-02 18:10:20 +08:00
|
|
|
|
2014-05-03 00:55:38 +08:00
|
|
|
func (self *Euclidean) InnerProduct(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
|
|
|
result := vectorX.Dot(vectorY)
|
2014-05-02 20:09:54 +08:00
|
|
|
|
2014-05-02 23:30:12 +08:00
|
|
|
return result
|
2014-05-02 20:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We may need to create Metrics / Vector interface for this
|
2014-05-03 00:55:38 +08:00
|
|
|
func (self *Euclidean) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
|
|
|
vectorX.Sub(vectorX, vectorY)
|
2014-05-02 23:49:06 +08:00
|
|
|
|
2014-05-03 00:55:38 +08:00
|
|
|
result := self.InnerProduct(vectorX, vectorX)
|
2014-05-02 18:10:20 +08:00
|
|
|
|
2014-05-03 00:55:38 +08:00
|
|
|
return math.Sqrt(result)
|
2014-05-02 18:10:20 +08:00
|
|
|
}
|