Skip to content
Snippets Groups Projects
Commit 2275acce authored by Xiangrui Meng's avatar Xiangrui Meng
Browse files

[SPARK-6651][MLLIB] delegate dense vector arithmetics to the underlying numpy array

Users should be able to use numpy operators directly on dense vectors. davies atalwalkar

Author: Xiangrui Meng <meng@databricks.com>

Closes #5312 from mengxr/SPARK-6651 and squashes the following commits:

e665c5c [Xiangrui Meng] wrap the result in a dense vector
23dfca3 [Xiangrui Meng] delegate dense vector arithmetics to the underlying numpy array
parent ee11be25
No related branches found
No related tags found
No related merge requests found
......@@ -173,7 +173,24 @@ class Vector(object):
class DenseVector(Vector):
"""
A dense vector represented by a value array.
A dense vector represented by a value array. We use numpy array for
storage and arithmetics will be delegated to the underlying numpy
array.
>>> v = Vectors.dense([1.0, 2.0])
>>> u = Vectors.dense([3.0, 4.0])
>>> v + u
DenseVector([4.0, 6.0])
>>> 2 - v
DenseVector([1.0, 0.0])
>>> v / 2
DenseVector([0.5, 1.0])
>>> v * u
DenseVector([3.0, 8.0])
>>> u / v
DenseVector([3.0, 2.0])
>>> u % 2
DenseVector([1.0, 0.0])
"""
def __init__(self, ar):
if isinstance(ar, basestring):
......@@ -292,6 +309,25 @@ class DenseVector(Vector):
def __getattr__(self, item):
return getattr(self.array, item)
def _delegate(op):
def func(self, other):
if isinstance(other, DenseVector):
other = other.array
return DenseVector(getattr(self.array, op)(other))
return func
__neg__ = _delegate("__neg__")
__add__ = _delegate("__add__")
__sub__ = _delegate("__sub__")
__mul__ = _delegate("__mul__")
__div__ = _delegate("__div__")
__mod__ = _delegate("__mod__")
__radd__ = _delegate("__radd__")
__rsub__ = _delegate("__rsub__")
__rmul__ = _delegate("__rmul__")
__rdiv__ = _delegate("__rdiv__")
__rmod__ = _delegate("__rmod__")
class SparseVector(Vector):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment