1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-28 13:48:56 +08:00
golearn/metrics/pairwise/manhattan.go
2014-05-03 23:05:24 +08:00

41 lines
725 B
Go

package pairwise
import (
"math"
"github.com/gonum/matrix/mat64"
)
type Manhattan struct{}
func NewManhattan() *Manhattan {
return &Manhattan{}
}
// Manhattan distance, also known as L1 distance.
// Compute sum of absolute values of elements.
func (self *Manhattan) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
var length int
subVector := mat64.NewDense(0, 0, nil)
subVector.Sub(vectorX, vectorY)
r, c := subVector.Dims()
if r == 1 {
// Force transpose to column vector
subVector.TCopy(subVector)
length = c
} else if c == 1 {
length = r
} else {
panic(mat64.ErrShape)
}
result := .0
for i := 0; i < length; i++ {
result += math.Abs(subVector.At(i, 0))
}
return result
}