Skip to content

Commit d5b5423

Browse files
committed
Fixing code style and updating if logic on when to check for zero values
1 parent ee1a85a commit d5b5423

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,11 +736,9 @@ class SparseVector(
736736
}
737737

738738
// look for inactive values in case all active node values are negative
739-
if(size != values.size && maxValue <= 0){
739+
if (size != values.size && maxValue <= 0){
740740
val firstInactiveIdx = calcFirstInactiveIdx(0)
741-
if(maxValue == 0){
742-
if(firstInactiveIdx >= maxIdx) maxIdx else maxIdx = firstInactiveIdx
743-
}else{
741+
if (!(maxValue == 0 && firstInactiveIdx >= maxIdx)){
744742
maxIdx = firstInactiveIdx
745743
}
746744
maxValue = 0

mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,26 @@ class VectorsSuite extends FunSuite {
9191
val max = vec2.argmax
9292
assert(max === 3)
9393

94-
val vec3 = Vectors.sparse(5,Array(2, 4),Array(1.0,-.7))
94+
val vec3 = Vectors.sparse(5,Array(2, 3, 4),Array(1.0, 0.0, -.7))
9595
val max2 = vec3.argmax
9696
assert(max2 === 2)
9797

9898
// check for case that sparse vector is created with only negative values {0.0, 0.0,-1.0, -0.7, 0.0}
99-
val vec4 = Vectors.sparse(5,Array(2, 3),Array(-1.0,-.7))
99+
val vec4 = Vectors.sparse(5,Array(2, 3),Array(-1.0, -.7))
100100
val max3 = vec4.argmax
101101
assert(max3 === 0)
102102

103-
val vec5 = Vectors.sparse(11,Array(0, 3, 10),Array(-1.0,-.7,0.0))
103+
val vec5 = Vectors.sparse(11,Array(0, 3, 10),Array(-1.0, -.7, 0.0))
104104
val max4 = vec5.argmax
105105
assert(max4 === 1)
106106

107-
val vec6 = Vectors.sparse(5,Array(0, 1, 3),Array(-1.0, 0.0, -.7))
107+
val vec6 = Vectors.sparse(11,Array(0, 1, 2),Array(-1.0, -.7, 0.0))
108108
val max5 = vec6.argmax
109-
assert(max5 === 1)
109+
assert(max5 === 2)
110+
111+
val vec7 = Vectors.sparse(5,Array(0, 1, 3),Array(-1.0, 0.0, -.7))
112+
val max6 = vec7.argmax
113+
assert(max6 === 1)
110114

111115
var vec8 = Vectors.sparse(5,Array(1, 2),Array(0.0, -1.0))
112116
val max7 = vec8.argmax

0 commit comments

Comments
 (0)