mirror of
https://github.com/sjwhitworth/golearn.git
synced 2025-04-26 13:49:14 +08:00
Run goftm on all files ti fix indentation
This commit is contained in:
parent
ad77c0dc7e
commit
13327e9250
@ -1,15 +1,15 @@
|
|||||||
package pairwise
|
package pairwise
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/gonum/matrix/mat64"
|
"github.com/gonum/matrix/mat64"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Chebyshev struct{}
|
type Chebyshev struct{}
|
||||||
|
|
||||||
func NewChebyshev() *Chebyshev {
|
func NewChebyshev() *Chebyshev {
|
||||||
return &Chebyshev{}
|
return &Chebyshev{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Chebyshev) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
func (self *Chebyshev) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||||
@ -19,16 +19,16 @@ func (self *Chebyshev) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) floa
|
|||||||
panic(mat64.ErrShape)
|
panic(mat64.ErrShape)
|
||||||
}
|
}
|
||||||
|
|
||||||
subVector := mat64.NewDense(0, 0, nil)
|
subVector := mat64.NewDense(0, 0, nil)
|
||||||
subVector.Sub(vectorX, vectorY)
|
subVector.Sub(vectorX, vectorY)
|
||||||
|
|
||||||
max := float64(0)
|
max := float64(0)
|
||||||
|
|
||||||
for i := 0; i < r1; i++ {
|
for i := 0; i < r1; i++ {
|
||||||
for j := 0; j < c1; j++ {
|
for j := 0; j < c1; j++ {
|
||||||
max = math.Max(max, math.Abs(vectorX.At(i, j) - vectorY.At(i, j)))
|
max = math.Max(max, math.Abs(vectorX.At(i, j)-vectorY.At(i, j)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return max
|
return max
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,8 @@ func TestChebyshev(t *testing.T) {
|
|||||||
chebyshev := NewChebyshev()
|
chebyshev := NewChebyshev()
|
||||||
|
|
||||||
Convey("Given two vectors", t, func() {
|
Convey("Given two vectors", t, func() {
|
||||||
vectorX = mat64.NewDense(4, 1, []float64{ 1, 2, 3, 4 })
|
vectorX = mat64.NewDense(4, 1, []float64{1, 2, 3, 4})
|
||||||
vectorY = mat64.NewDense(4, 1, []float64{ -5, -6, 7, 8 })
|
vectorY = mat64.NewDense(4, 1, []float64{-5, -6, 7, 8})
|
||||||
|
|
||||||
Convey("When calculating distance with two vectors", func() {
|
Convey("When calculating distance with two vectors", func() {
|
||||||
result := chebyshev.Distance(vectorX, vectorY)
|
result := chebyshev.Distance(vectorX, vectorY)
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
package pairwise
|
package pairwise
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/gonum/matrix/mat64"
|
"github.com/gonum/matrix/mat64"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cranberra struct{}
|
type Cranberra struct{}
|
||||||
|
|
||||||
func NewCranberra() *Cranberra {
|
func NewCranberra() *Cranberra {
|
||||||
return &Cranberra{}
|
return &Cranberra{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cranberraDistanceStep(num float64, denom float64) float64 {
|
func cranberraDistanceStep(num float64, denom float64) float64 {
|
||||||
if num == .0 && denom == .0 {
|
if num == .0 && denom == .0 {
|
||||||
return .0
|
return .0
|
||||||
} else {
|
} else {
|
||||||
return num/denom
|
return num / denom
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Cranberra) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
func (self *Cranberra) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) float64 {
|
||||||
@ -27,10 +27,10 @@ func (self *Cranberra) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) floa
|
|||||||
panic(mat64.ErrShape)
|
panic(mat64.ErrShape)
|
||||||
}
|
}
|
||||||
|
|
||||||
subVector := mat64.NewDense(0, 0, nil)
|
subVector := mat64.NewDense(0, 0, nil)
|
||||||
subVector.Sub(vectorX, vectorY)
|
subVector.Sub(vectorX, vectorY)
|
||||||
|
|
||||||
sum := .0
|
sum := .0
|
||||||
|
|
||||||
for i := 0; i < r1; i++ {
|
for i := 0; i < r1; i++ {
|
||||||
for j := 0; j < c1; j++ {
|
for j := 0; j < c1; j++ {
|
||||||
@ -44,5 +44,5 @@ func (self *Cranberra) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) floa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ func TestCranberrra(t *testing.T) {
|
|||||||
cranberra := NewCranberra()
|
cranberra := NewCranberra()
|
||||||
|
|
||||||
Convey("Given two vectors that are same", t, func() {
|
Convey("Given two vectors that are same", t, func() {
|
||||||
vec := mat64.NewDense(7, 1, []float64 { 0, 1, -2, 3.4, 5, -6.7, 89 })
|
vec := mat64.NewDense(7, 1, []float64{0, 1, -2, 3.4, 5, -6.7, 89})
|
||||||
distance := cranberra.Distance(vec, vec)
|
distance := cranberra.Distance(vec, vec)
|
||||||
|
|
||||||
Convey("The result should be 0", func() {
|
Convey("The result should be 0", func() {
|
||||||
@ -20,10 +20,9 @@ func TestCranberrra(t *testing.T) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
Convey("Given two vectors", t, func() {
|
Convey("Given two vectors", t, func() {
|
||||||
vectorX = mat64.NewDense(5, 1, []float64{ 1, 2, 3, 4, 9 })
|
vectorX = mat64.NewDense(5, 1, []float64{1, 2, 3, 4, 9})
|
||||||
vectorY = mat64.NewDense(5, 1, []float64{ -5, -6, 7, 4, 3 })
|
vectorY = mat64.NewDense(5, 1, []float64{-5, -6, 7, 4, 3})
|
||||||
|
|
||||||
Convey("When calculating distance with two vectors", func() {
|
Convey("When calculating distance with two vectors", func() {
|
||||||
result := cranberra.Distance(vectorX, vectorY)
|
result := cranberra.Distance(vectorX, vectorY)
|
||||||
|
@ -28,5 +28,5 @@ func (self *Manhattan) Distance(vectorX *mat64.Dense, vectorY *mat64.Dense) floa
|
|||||||
result += math.Abs(vectorX.At(i, j) - vectorY.At(i, j))
|
result += math.Abs(vectorX.At(i, j) - vectorY.At(i, j))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -1,51 +1,51 @@
|
|||||||
package pairwise
|
package pairwise
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gonum/matrix/mat64"
|
"github.com/gonum/matrix/mat64"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestManhattan(t *testing.T) {
|
func TestManhattan(t *testing.T) {
|
||||||
var vectorX, vectorY *mat64.Dense
|
var vectorX, vectorY *mat64.Dense
|
||||||
manhattan := NewManhattan()
|
manhattan := NewManhattan()
|
||||||
|
|
||||||
Convey("Given two vectors that are same", t, func() {
|
Convey("Given two vectors that are same", t, func() {
|
||||||
vec := mat64.NewDense(7, 1, []float64 { 0, 1, -2, 3.4, 5, -6.7, 89 })
|
vec := mat64.NewDense(7, 1, []float64{0, 1, -2, 3.4, 5, -6.7, 89})
|
||||||
distance := manhattan.Distance(vec, vec)
|
distance := manhattan.Distance(vec, vec)
|
||||||
|
|
||||||
Convey("The result should be 0", func() {
|
Convey("The result should be 0", func() {
|
||||||
So(distance, ShouldEqual, 0)
|
So(distance, ShouldEqual, 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Given two vectors", t, func() {
|
Convey("Given two vectors", t, func() {
|
||||||
vectorX = mat64.NewDense(3, 1, []float64{2, 2, 3})
|
vectorX = mat64.NewDense(3, 1, []float64{2, 2, 3})
|
||||||
vectorY = mat64.NewDense(3, 1, []float64{1, 4, 5})
|
vectorY = mat64.NewDense(3, 1, []float64{1, 4, 5})
|
||||||
|
|
||||||
Convey("When calculating distance with column vectors", func() {
|
Convey("When calculating distance with column vectors", func() {
|
||||||
result := manhattan.Distance(vectorX, vectorY)
|
result := manhattan.Distance(vectorX, vectorY)
|
||||||
|
|
||||||
Convey("The result should be 5", func() {
|
Convey("The result should be 5", func() {
|
||||||
So(result, ShouldEqual, 5)
|
So(result, ShouldEqual, 5)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When calculating distance with row vectors", func() {
|
Convey("When calculating distance with row vectors", func() {
|
||||||
vectorX.TCopy(vectorX)
|
vectorX.TCopy(vectorX)
|
||||||
vectorY.TCopy(vectorY)
|
vectorY.TCopy(vectorY)
|
||||||
result := manhattan.Distance(vectorX, vectorY)
|
result := manhattan.Distance(vectorX, vectorY)
|
||||||
|
|
||||||
Convey("The result should be 5", func() {
|
Convey("The result should be 5", func() {
|
||||||
So(result, ShouldEqual, 5)
|
So(result, ShouldEqual, 5)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When calculating distance with different dimention matrices", func() {
|
Convey("When calculating distance with different dimention matrices", func() {
|
||||||
vectorX.TCopy(vectorX)
|
vectorX.TCopy(vectorX)
|
||||||
So(func() { manhattan.Distance(vectorX, vectorY) }, ShouldPanicWith, mat64.ErrShape)
|
So(func() { manhattan.Distance(vectorX, vectorY) }, ShouldPanicWith, mat64.ErrShape)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user