Skip to content

Commit

Permalink
Modified for comment and fix bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Jan 6, 2015
1 parent 90a08f3 commit fb743da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,17 @@ object Vectors {
var kv2 = 0
val indices = v1.indices
var squaredDistance = 0.0
var iv1 = indices(kv1)
val nnzv1 = indices.size
val nnzv2 = v2.size
var iv1 = if (nnzv1 > 0) indices(kv1) else -1

while (kv2 < nnzv2) {
var score = 0.0
if (kv2 != iv1) {
score = v2(kv2)
} else {
score = v1.values(kv1) - v2(kv2)
if (kv1 < indices.length - 1) {
if (kv1 < nnzv1 - 1) {
kv1 += 1
iv1 = indices(kv1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ class VectorsSuite extends FunSuite {
}

test("sqdist") {
val random = new Random(System.nanoTime())
for (m <- 1 until 1000 by 10) {
val nnz = random.nextInt(m) + 1
val random = new Random()
for (m <- 1 until 1000 by 100) {
val nnz = random.nextInt(m)

val indices1 = random.shuffle(0 to m - 1).toArray.slice(0, nnz).sorted
val values1 = indices1.map(i => random.nextInt(m + 1) * random.nextDouble())
val indices1 = random.shuffle(0 to m - 1).slice(0, nnz).sorted.toArray
val values1 = Array.fill(nnz)(random.nextDouble)
val sparseVector1 = Vectors.sparse(m, indices1, values1)

val indices2 = random.shuffle(0 to m - 1).toArray.slice(0, nnz).sorted
val values2 = indices2.map(i => random.nextInt(m + 1) * random.nextDouble())
val indices2 = random.shuffle(0 to m - 1).slice(0, nnz).sorted.toArray
val values2 = Array.fill(nnz)(random.nextDouble)
val sparseVector2 = Vectors.sparse(m, indices2, values2)

val denseVector1 = Vectors.dense(sparseVector1.toArray)
Expand All @@ -196,11 +196,11 @@ class VectorsSuite extends FunSuite {
val squaredDist = breezeSquaredDistance(sparseVector1.toBreeze, sparseVector2.toBreeze)

// SparseVector vs. SparseVector
assert(Vectors.sqdist(sparseVector1, sparseVector2) === squaredDist)
assert(Vectors.sqdist(sparseVector1, sparseVector2) ~== squaredDist relTol 1E-8)
// DenseVector vs. SparseVector
assert(Vectors.sqdist(denseVector1, sparseVector2) === squaredDist)
assert(Vectors.sqdist(denseVector1, sparseVector2) ~== squaredDist relTol 1E-8)
// DenseVector vs. DenseVector
assert(Vectors.sqdist(denseVector1, denseVector2) === squaredDist)
assert(Vectors.sqdist(denseVector1, denseVector2) ~== squaredDist relTol 1E-8)
}
}

Expand Down

0 comments on commit fb743da

Please sign in to comment.