[SPARK-26638][PYSPARK][ML] Pyspark vector classes always return error 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>
This commit is contained in:
Sean Owen 2019-01-17 14:24:21 -06:00
parent ede35c88e0
commit 0b3abef195
2 changed files with 10 additions and 2 deletions

View file

@ -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):
@ -436,6 +438,9 @@ class DenseVector(Vector):
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):
@ -443,7 +448,6 @@ class DenseVector(Vector):
return DenseVector(getattr(self.array, op)(other))
return func
__neg__ = _delegate("__neg__")
__add__ = _delegate("__add__")
__sub__ = _delegate("__sub__")
__mul__ = _delegate("__mul__")

View file

@ -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):
@ -480,6 +482,9 @@ class DenseVector(Vector):
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):
@ -487,7 +492,6 @@ class DenseVector(Vector):
return DenseVector(getattr(self.array, op)(other))
return func
__neg__ = _delegate("__neg__")
__add__ = _delegate("__add__")
__sub__ = _delegate("__sub__")
__mul__ = _delegate("__mul__")