1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-25 13:48:49 +08:00
golearn/metrics/pairwise/manhattan.go
Richard Townsend ff52c013eb Update gonum to latest version
Should fix #200 and #205
2018-03-24 00:19:35 +00:00

34 lines
636 B
Go

package pairwise
import (
"math"
"github.com/gonum/matrix"
"gonum.org/v1/gonum/mat"
)
type Manhattan struct{}
func NewManhattan() *Manhattan {
return &Manhattan{}
}
// Distance computes the Manhattan distance, also known as L1 distance.
// == the sum of the absolute values of elements.
func (m *Manhattan) Distance(vectorX *mat.Dense, vectorY *mat.Dense) float64 {
r1, c1 := vectorX.Dims()
r2, c2 := vectorY.Dims()
if r1 != r2 || c1 != c2 {
panic(matrix.ErrShape)
}
result := .0
for i := 0; i < r1; i++ {
for j := 0; j < c1; j++ {
result += math.Abs(vectorX.At(i, j) - vectorY.At(i, j))
}
}
return result
}