Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #26 from mathetake/fix/zero-vector-error
Browse files Browse the repository at this point in the history
fix zero-vector handling
  • Loading branch information
mathetake authored May 26, 2018
2 parents e5cb9f1 + b7794d4 commit 4dd7264
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
27 changes: 22 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion index/index_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package index

import (
"testing"

"github.com/bmizerany/assert"
"github.com/mathetake/gann/item"
"testing"
)

func TestInitializeWithNormalize(t *testing.T) {
Expand Down
24 changes: 10 additions & 14 deletions item/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"
"math/rand"
"time"
"github.com/pkg/errors"
)

const (
Expand All @@ -14,14 +15,15 @@ const (
centroidCalcRatio = float32(0.0001)
)

func Normalize(v1 Vector) {
n := norm(v1)
func Normalize(v Vector) error {
n := norm(v)
if n == 0 {
panic("zero vector given.")
return errors.Errorf("zero vector is given.")
}
for i := 0; i < len(v1); i++ {
v1[i] = v1[i] / n
for i := 0; i < len(v); i++ {
v[i] = v[i] / n
}
return nil
}

func DotProduct(v1, v2 Vector) (ret float32) {
Expand Down Expand Up @@ -102,23 +104,17 @@ func GetNormalVectorOfSplittingHyperPlane(vs []Vector, dim int) Vector {
}
}

isZero := true
for d := 0; d < dim; d++ {
v := c0[d] - c1[d]
ret[d] += v
if v != 0 {
isZero = false
}
}

if isZero {
// normalize
err := Normalize(ret)
if err != nil {
d := rand.Intn(dim)
ret[d] = 1
return ret
}

// normalize
Normalize(ret)
return ret
}

Expand Down
6 changes: 4 additions & 2 deletions item/arithmetic_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package item

import (
"github.com/bmizerany/assert"
"math/rand"
"testing"
"time"

"github.com/bmizerany/assert"
)

func TestNormalize(t *testing.T) {
v1 := []float32{2, 0}
Normalize(v1)
err := Normalize(v1)
assert.Equal(t, nil, err)
assert.Equal(t, []float32{1, 0}, v1)
}

Expand Down
2 changes: 1 addition & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"sync"

"github.com/bmizerany/assert"
"github.com/mathetake/gann/item"
"github.com/stretchr/testify/assert"
)

func TestIsLeaf(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion node/quque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"math"

"gopkg.in/go-playground/assert.v1"
"github.com/bmizerany/assert"
)

func TestPriorityQueue(t *testing.T) {
Expand Down

0 comments on commit 4dd7264

Please sign in to comment.