srowen closed pull request #23048: transform DenseVector x DenseVector sqdist from imperativ to function… URL: https://github.com/apache/spark/pull/23048
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/mllib-local/src/main/scala/org/apache/spark/ml/linalg/Vectors.scala b/mllib-local/src/main/scala/org/apache/spark/ml/linalg/Vectors.scala index 6e950f968a65d..42364fe132dd5 100644 --- a/mllib-local/src/main/scala/org/apache/spark/ml/linalg/Vectors.scala +++ b/mllib-local/src/main/scala/org/apache/spark/ml/linalg/Vectors.scala @@ -370,14 +370,19 @@ object Vectors { case (v1: DenseVector, v2: SparseVector) => squaredDistance = sqdist(v2, v1) - case (DenseVector(vv1), DenseVector(vv2)) => - var kv = 0 + case (DenseVector(vv1), DenseVector(vv2)) => { val sz = vv1.length - while (kv < sz) { - val score = vv1(kv) - vv2(kv) - squaredDistance += score * score - kv += 1 + @annotation.tailrec + def go(d: Double, kv: Int): Double = { + if (kv < sz) { + val score = vv1(kv) - vv2(kv) + go(d + score * score, kv + 1) + } + else d } + go(0D, 0) + } + case _ => throw new IllegalArgumentException("Do not support vector type " + v1.getClass + " and " + v2.getClass) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org