flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tillrohrmann <...@git.apache.org>
Subject [GitHub] flink pull request: [FLINK-1933] Add distance measure interface an...
Date Wed, 29 Apr 2015 09:12:08 GMT
Github user tillrohrmann commented on a diff in the pull request:

    https://github.com/apache/flink/pull/629#discussion_r29320274
  
    --- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/math/SparseVector.scala
---
    @@ -53,6 +53,22 @@ class SparseVector(
         new SparseVector(size, indices.clone, data.clone)
       }
     
    +  /** Returns the dot product of the recipient and the argument
    +    *
    +    * @param other a Vector
    +    * @return a scalar double of dot product
    +    */
    +  override def dot(other: Vector): Double = {
    +    require(size == other.size, "The size of vector must be equal.")
    +    indices.map(i => this(i) * other(i)).sum
    --- End diff --
    
    This will be very inefficient because each apply method triggers a binary search in the
index array. Better would be a case distinction:
    
    ```other match {
        case DenseVector(data) => indices.zipWithIndex.map((sparseIndex, index) => data(sparseIndex)
* data(index)).sum
        case SparseVector(_, otherIndices, otherData) =>
          var left = 0
          var right = 0
          var result = 0.0
    
          while(left < indices.length && right < otherIndices.length ) {
            if(indices(left) < otherIndices(right)) {
              left += 1
            } else if(otherIndices(right) < indices(left)) {
              right += 1
            } else {
              result += data(left) * otherData(right)
              left += 1
              right += 1
            }
          }
    
          result
      }
    ```
      


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message