2014-05-02 18:10:20 +08:00
|
|
|
package pairwise
|
|
|
|
|
|
|
|
import (
|
2014-05-02 20:09:54 +08:00
|
|
|
"errors"
|
2014-05-02 18:10:20 +08:00
|
|
|
"math"
|
|
|
|
|
|
|
|
mat "github.com/skelterjohn/go.matrix"
|
|
|
|
)
|
|
|
|
|
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-02 20:09:54 +08:00
|
|
|
func (self *Euclidean) InnerProduct(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix) (float64, error) {
|
|
|
|
if !CheckDimMatch(vectorX, vectorY) {
|
|
|
|
return 0, errors.New("Dimension mismatch")
|
2014-05-02 18:10:20 +08:00
|
|
|
}
|
|
|
|
|
2014-05-02 20:09:54 +08:00
|
|
|
result := mat.Product(mat.Transpose(vectorX), vectorY).Get(0, 0)
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We may need to create Metrics / Vector interface for this
|
|
|
|
func (self *Euclidean) Distance(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix) (float64, error) {
|
|
|
|
difference, err := vectorY.MinusDense(vectorX)
|
|
|
|
result, err := self.InnerProduct(difference, difference)
|
2014-05-02 18:10:20 +08:00
|
|
|
|
2014-05-02 20:09:54 +08:00
|
|
|
return math.Sqrt(result), err
|
2014-05-02 18:10:20 +08:00
|
|
|
}
|