mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-26 13:49:14 +08:00
Add euclidean distance test.
This commit is contained in:
parent
921efaffc7
commit
4fc0ed0477
@ -23,7 +23,12 @@ func (self *Euclidean) InnerProduct(vectorX *mat.DenseMatrix, vectorY *mat.Dense
|
||||
// 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)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
result := self.InnerProduct(difference, difference)
|
||||
|
||||
return math.Sqrt(result), err
|
||||
return math.Sqrt(result), nil
|
||||
}
|
||||
|
@ -7,27 +7,40 @@ import (
|
||||
mat "github.com/skelterjohn/go.matrix"
|
||||
)
|
||||
|
||||
func TestEuclideanInnerProduct(t *testing.T) {
|
||||
func TestEuclidean(t *testing.T) {
|
||||
euclidean := NewEuclidean()
|
||||
|
||||
Convey("Given two vectors", t, func() {
|
||||
vectorX := mat.MakeDenseMatrix([]float64{1, 2, 3}, 3, 1)
|
||||
vectorY := mat.MakeDenseMatrix([]float64{3, 4, 5}, 3, 1)
|
||||
vectorY := mat.MakeDenseMatrix([]float64{2, 4, 5}, 3, 1)
|
||||
|
||||
Convey("When doing inner product", func() {
|
||||
result := euclidean.InnerProduct(vectorX, vectorY)
|
||||
|
||||
Convey("The result should be 26", func() {
|
||||
So(result, ShouldEqual, 26)
|
||||
})
|
||||
|
||||
Convey("The result should be 25", func() {
|
||||
So(result, ShouldEqual, 25)
|
||||
})
|
||||
|
||||
Convey("When dimension not match", func() {
|
||||
vectorY = mat.MakeDenseMatrix([]float64{3, 4, 5}, 1, 3)
|
||||
vectorZ := mat.MakeDenseMatrix([]float64{3, 4, 5}, 1, 3)
|
||||
|
||||
Convey("It should panic with Dimension mismatch", func() {
|
||||
So(func() { euclidean.InnerProduct(vectorX, vectorY) }, ShouldPanicWith, "Dimension mismatch")
|
||||
So(func() { euclidean.InnerProduct(vectorX, vectorZ) }, ShouldPanicWith, "Dimension mismatch")
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Convey("When calculating distance", func() {
|
||||
result, err := euclidean.Distance(vectorX, vectorY)
|
||||
|
||||
Convey("The err should be nil", func() {
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("The result should be 3", func() {
|
||||
So(result, ShouldEqual, 3)
|
||||
})
|
||||
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user