Skip to content

Commit

Permalink
[SPARK-26638][PYSPARK][ML] Pyspark vector classes always return error…
Browse files Browse the repository at this point in the history
… for unary negation

## What changes were proposed in this pull request?

Fix implementation of unary negation (`__neg__`) in Pyspark DenseVectors

## How was this patch tested?

Existing tests, plus new doctest

Closes #23570 from srowen/SPARK-26638.

Authored-by: Sean Owen <sean.owen@databricks.com>
Signed-off-by: Sean Owen <sean.owen@databricks.com>
(cherry picked from commit 0b3abef)
Signed-off-by: Sean Owen <sean.owen@databricks.com>
  • Loading branch information
srowen committed Jan 17, 2019
1 parent c0fc6d0 commit 8debdbd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion python/pyspark/ml/linalg/__init__.py
Expand Up @@ -270,6 +270,8 @@ class DenseVector(Vector):
DenseVector([3.0, 2.0])
>>> u % 2
DenseVector([1.0, 0.0])
>>> -v
DenseVector([-1.0, -2.0])
"""
def __init__(self, ar):
if isinstance(ar, bytes):
Expand Down Expand Up @@ -436,14 +438,16 @@ def __hash__(self):
def __getattr__(self, item):
return getattr(self.array, item)

def __neg__(self):
return DenseVector(-self.array)

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__")
Expand Down
6 changes: 5 additions & 1 deletion python/pyspark/mllib/linalg/__init__.py
Expand Up @@ -281,6 +281,8 @@ class DenseVector(Vector):
DenseVector([3.0, 2.0])
>>> u % 2
DenseVector([1.0, 0.0])
>>> -v
DenseVector([-1.0, -2.0])
"""
def __init__(self, ar):
if isinstance(ar, bytes):
Expand Down Expand Up @@ -480,14 +482,16 @@ def __hash__(self):
def __getattr__(self, item):
return getattr(self.array, item)

def __neg__(self):
return DenseVector(-self.array)

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__")
Expand Down

0 comments on commit 8debdbd

Please sign in to comment.