mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-28 13:48:56 +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
|
// We may need to create Metrics / Vector interface for this
|
||||||
func (self *Euclidean) Distance(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix) (float64, error) {
|
func (self *Euclidean) Distance(vectorX *mat.DenseMatrix, vectorY *mat.DenseMatrix) (float64, error) {
|
||||||
difference, err := vectorY.MinusDense(vectorX)
|
difference, err := vectorY.MinusDense(vectorX)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
result := self.InnerProduct(difference, difference)
|
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"
|
mat "github.com/skelterjohn/go.matrix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEuclideanInnerProduct(t *testing.T) {
|
func TestEuclidean(t *testing.T) {
|
||||||
euclidean := NewEuclidean()
|
euclidean := NewEuclidean()
|
||||||
|
|
||||||
Convey("Given two vectors", t, func() {
|
Convey("Given two vectors", t, func() {
|
||||||
vectorX := mat.MakeDenseMatrix([]float64{1, 2, 3}, 3, 1)
|
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() {
|
Convey("When doing inner product", func() {
|
||||||
result := euclidean.InnerProduct(vectorX, vectorY)
|
result := euclidean.InnerProduct(vectorX, vectorY)
|
||||||
|
|
||||||
Convey("The result should be 26", func() {
|
Convey("The result should be 25", func() {
|
||||||
So(result, ShouldEqual, 26)
|
So(result, ShouldEqual, 25)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("When dimension not match", func() {
|
||||||
|
vectorZ := mat.MakeDenseMatrix([]float64{3, 4, 5}, 1, 3)
|
||||||
|
|
||||||
|
Convey("It should panic with Dimension mismatch", func() {
|
||||||
|
So(func() { euclidean.InnerProduct(vectorX, vectorZ) }, ShouldPanicWith, "Dimension mismatch")
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When dimension not match", func() {
|
Convey("When calculating distance", func() {
|
||||||
vectorY = mat.MakeDenseMatrix([]float64{3, 4, 5}, 1, 3)
|
result, err := euclidean.Distance(vectorX, vectorY)
|
||||||
|
|
||||||
Convey("It should panic with Dimension mismatch", func() {
|
Convey("The err should be nil", func() {
|
||||||
So(func() { euclidean.InnerProduct(vectorX, vectorY) }, ShouldPanicWith, "Dimension mismatch")
|
So(err, ShouldBeNil)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("The result should be 3", func() {
|
||||||
|
So(result, ShouldEqual, 3)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user