Skip to content

Commit

Permalink
refactoring arg max check to better handle zero values
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeDittmar committed Jul 9, 2015
1 parent b22af46 commit 42341fb
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ class SparseVector(
if (size == 0) {
-1
} else {

var maxIdx = indices(0)
var maxValue = values(0)

Expand All @@ -735,33 +734,12 @@ class SparseVector(
}
}

// look for inactive values in case all active node values are negative
if (size != values.size && maxValue <= 0) {
val firstInactiveIdx = calcFirstInactiveIdx(0)
if (!(maxValue == 0 && firstInactiveIdx >= maxIdx)) {
maxIdx = firstInactiveIdx
}
maxValue = 0
var k = 0
while (k < indices.length && indices(k) == k && values(k) != 0.0) {
k += 1
}
maxIdx
}
}

/**
* Calculates the first instance of an inactive node in a sparse vector and returns the Idx
* of the element.
* @param idx starting index of computation
* @return index of first inactive node
*/
private[SparseVector] def calcFirstInactiveIdx(idx: Int): Int = {
if (idx < size) {
if (!indices.contains(idx)) {
idx
} else {
calcFirstInactiveIdx(idx + 1)
}
} else {
-1
if (maxValue <= 0.0 || k >= maxIdx) k else maxIdx
}
}
}
Expand Down

0 comments on commit 42341fb

Please sign in to comment.