mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-28 13:48:56 +08:00

Previously, Manhattan distance measure was only able to calculate vector distances. Now, it's possible to calculate distances on matrices, too, without unnecessary overhead of copying vectors, doing an in-place lookups.
35 lines
595 B
Go
35 lines
595 B
Go
package pairwise
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/gonum/matrix/mat64"
|
|
)
|
|
|
|
type Chebyshev struct{}
|
|
|
|
func NewChebyshev() *Chebyshev {
|
|
return &Chebyshev{}
|
|
}
|
|
|
|
func (self *Chebyshev) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
|
r1, c1 := vectorX.Dims()
|
|
r2, c2 := vectorY.Dims()
|
|
if r1 != r2 || c1 != c2 {
|
|
panic(mat64.ErrShape)
|
|
}
|
|
|
|
subVector := mat64.NewDense(0, 0, nil)
|
|
subVector.Sub(vectorX, vectorY)
|
|
|
|
max := float64(0)
|
|
|
|
for i := 0; i < r1; i++ {
|
|
for j := 0; j < c1; j++ {
|
|
max = math.Max(max, math.Abs(vectorX.At(i, j) - vectorY.At(i, j)))
|
|
}
|
|
}
|
|
|
|
return max
|
|
}
|