Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/3643#discussion_r21863333
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/util/MLUtils.scala ---
    @@ -264,6 +263,92 @@ object MLUtils {
         }
         Vectors.fromBreeze(vector1)
       }
    + 
    +  /**
    +   * Returns the squared distance between two Vectors.
    +   */
    +  private[util] def vectorSquaredDistance(v1: Vector, v2: Vector): Double 
= {
    +    var squaredDistance = 0.0
    +    (v1, v2) match { 
    +      case (v1: SparseVector, v2: SparseVector) =>
    +        val v1Values = v1.values
    +        val v1Indices = v1.indices
    +        val v2Values = v2.values
    +        val v2Indices = v2.indices
    +        val nnzv1 = v1Indices.size
    +        val nnzv2 = v2Indices.size
    +        
    +        var kv1 = 0
    +        var kv2 = 0
    +        var score = 0.0
    +        while (kv1 < nnzv1) {
    +          val iv1 = v1Indices(kv1)
    + 
    +          if (kv2 >= nnzv2 || iv1 < v2Indices(kv2)) {
    +            score = v1Values(kv1)
    +            squaredDistance += score * score
    +          }
    +          while (kv2 < nnzv2 && v2Indices(kv2) < iv1) {
    +            score = v2Values(kv2)
    +            squaredDistance += score * score
    +            kv2 += 1
    +          }
    +          if (kv2 < nnzv2 && v2Indices(kv2) == iv1) {
    +            score = v1Values(kv1) - v2Values(kv2)
    +            squaredDistance += score * score
    +            kv2 += 1
    +          }
    +          kv1 += 1
    +        }
    +
    +      case (v1: SparseVector, v2: DenseVector) if v1.indices.length / 
v1.size < 0.5 =>
    +        squaredDistance = vectorSquaredDistance(v1, v2)
    +
    +      case (v1: DenseVector, v2: SparseVector) if v2.indices.length / 
v2.size < 0.5 =>
    +        squaredDistance = vectorSquaredDistance(v2, v1)
    +
    +      case (v1, v2) =>
    +        squaredDistance = 
v1.toArray.zip(v2.toArray).foldLeft(0.0)((distance, elems) => {
    +          val score = elems._1 - elems._2
    +          distance + score * score
    +        })
    +    }
    +    squaredDistance
    +  }
    +
    +  /**
    +   * Returns the squared distance between DenseVector and SparseVector.
    +   */
    +  private[util] def vectorSquaredDistance(v1: SparseVector, v2: 
DenseVector): Double = {
    +    var squaredDistance = 0.0
    +    var count = 0
    +    var indices = v1.indices
    +    var score = 0.0
    --- End diff --
    
    Keep "score" a val local to the loop bodies.  No need to have it outside 
the loops


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to