Skip to content

Commit 42341fb

Browse files
committed
refactoring arg max check to better handle zero values
1 parent b22af46 commit 42341fb

File tree

1 file changed

+4
-26
lines changed

1 file changed

+4
-26
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,6 @@ class SparseVector(
724724
if (size == 0) {
725725
-1
726726
} else {
727-
728727
var maxIdx = indices(0)
729728
var maxValue = values(0)
730729

@@ -735,33 +734,12 @@ class SparseVector(
735734
}
736735
}
737736

738-
// look for inactive values in case all active node values are negative
739-
if (size != values.size && maxValue <= 0) {
740-
val firstInactiveIdx = calcFirstInactiveIdx(0)
741-
if (!(maxValue == 0 && firstInactiveIdx >= maxIdx)) {
742-
maxIdx = firstInactiveIdx
743-
}
744-
maxValue = 0
737+
var k = 0
738+
while (k < indices.length && indices(k) == k && values(k) != 0.0) {
739+
k += 1
745740
}
746-
maxIdx
747-
}
748-
}
749741

750-
/**
751-
* Calculates the first instance of an inactive node in a sparse vector and returns the Idx
752-
* of the element.
753-
* @param idx starting index of computation
754-
* @return index of first inactive node
755-
*/
756-
private[SparseVector] def calcFirstInactiveIdx(idx: Int): Int = {
757-
if (idx < size) {
758-
if (!indices.contains(idx)) {
759-
idx
760-
} else {
761-
calcFirstInactiveIdx(idx + 1)
762-
}
763-
} else {
764-
-1
742+
if (maxValue <= 0.0 || k >= maxIdx) k else maxIdx
765743
}
766744
}
767745
}

0 commit comments

Comments
 (0)