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

    https://github.com/apache/spark/pull/7222#discussion_r34008168
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -649,27 +645,20 @@ def squared_distance(self, other):
                 return result
     
             elif isinstance(other, SparseVector):
    -            result = 0.0
    -            i, j = 0, 0
    -            while i < len(self.indices) and j < len(other.indices):
    -                if self.indices[i] == other.indices[j]:
    -                    diff = self.values[i] - other.values[j]
    -                    result += diff * diff
    -                    i += 1
    -                    j += 1
    -                elif self.indices[i] < other.indices[j]:
    -                    result += self.values[i] * self.values[i]
    -                    i += 1
    -                else:
    -                    result += other.values[j] * other.values[j]
    -                    j += 1
    -            while i < len(self.indices):
    -                result += self.values[i] * self.values[i]
    -                i += 1
    -            while j < len(other.indices):
    -                result += other.values[j] * other.values[j]
    -                j += 1
    -            return result
    +            self_cmind = np.in1d(self.indices, other.indices)
    --- End diff --
    
    Could we simply do the following?
    
    ~~~python
    return math.sqrt(np.dot(self.values, self.values) + np.dot(other.values, 
other.values) - 2.0 * self.dot(other))
    ~~~
    
    It might generate some numeric errors when both vectors are long and very 
close to each other. It should be sufficient for normal use cases.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to