Skip to content

Commit 1fba230

Browse files
committed
merge while loop together
1 parent 69e1f37 commit 1fba230

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

mllib/src/main/scala/org/apache/spark/mllib/rdd/VectorRDDFunctions.scala

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ private class VectorRDDStatisticsAggregator(
5050
var totalCnt: Double,
5151
val nnz: BDV[Double],
5252
val currMax: BDV[Double],
53-
val currMin: BDV[Double]) extends VectorRDDStatisticalSummary with Serializable {
53+
val currMin: BDV[Double])
54+
extends VectorRDDStatisticalSummary with Serializable {
5455

5556
// lazy val is used for computing only once time. Same below.
5657
override def mean = {
@@ -130,31 +131,22 @@ private class VectorRDDStatisticsAggregator(
130131

131132
var i = 0
132133
while (i < other.currMean.length) {
133-
if (other.currMean(i) != 0.0)
134+
// merge mean together
135+
if (other.currMean(i) != 0.0) {
134136
currMean(i) = (currMean(i) * nnz(i) + other.currMean(i) * other.nnz(i)) /
135137
(nnz(i) + other.nnz(i))
136-
i += 1
137-
}
138+
}
138139

139-
i = 0
140-
while (i < currM2n.size) {
141-
(nnz(i), other.nnz(i)) match {
142-
case (0.0, 0.0) =>
143-
case _ => currM2n(i) +=
144-
other.currM2n(i) + deltaMean(i) * deltaMean(i) * nnz(i) * other.nnz(i) / (nnz(i)+other.nnz(i))
140+
// merge m2n together
141+
if (nnz(i) + other.nnz(i) != 0.0) {
142+
currM2n(i) += other.currM2n(i) + deltaMean(i) * deltaMean(i) * nnz(i) * other.nnz(i) /
143+
(nnz(i)+other.nnz(i))
145144
}
146-
i += 1
147-
}
148145

149-
i = 0
150-
while (i < other.currMax.length) {
151146
if (currMax(i) < other.currMax(i)) currMax(i) = other.currMax(i)
152-
i += 1
153-
}
154147

155-
i = 0
156-
while (i < other.currMin.length) {
157148
if (currMin(i) > other.currMin(i)) currMin(i) = other.currMin(i)
149+
158150
i += 1
159151
}
160152

mllib/src/test/scala/org/apache/spark/mllib/rdd/VectorRDDFunctionsSuite.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class VectorRDDFunctionsSuite extends FunSuite with LocalSparkContext {
8989
}
9090

9191
object VectorRDDFunctionsSuite {
92-
9392
def equivVector(lhs: Vector, rhs: Vector): Boolean = {
9493
(lhs.toBreeze - rhs.toBreeze).norm(2) < 1e-9
9594
}

0 commit comments

Comments
 (0)