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

    https://github.com/apache/spark/pull/3643#discussion_r21863319
  
    --- 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) =>
    --- End diff --
    
    I'd recommend putting a comment here about this case handling dense and 
approximately dense vectors.


---
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