diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index a7465865b707..cf1bd52c2228 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -50,7 +50,8 @@ 'true_divide', 'nonzero', 'quantile', 'percentile', 'shares_memory', 'may_share_memory', 'interp', 'diff', 'ediff1d', 'resize', 'polyval', 'nan_to_num', 'isnan', 'isinf', 'isposinf', 'isneginf', 'isfinite', 'atleast_1d', 'atleast_2d', 'atleast_3d', 'fill_diagonal', 'squeeze', - 'where', 'bincount', 'rollaxis', 'diagflat', 'repeat', 'prod', 'pad', 'cumsum', 'sum', 'diag', 'diagonal'] + 'where', 'bincount', 'rollaxis', 'diagflat', 'repeat', 'prod', 'pad', 'cumsum', 'sum', 'diag', 'diagonal', + 'positive'] @set_module('mxnet.ndarray.numpy') @@ -3561,6 +3562,40 @@ def negative(x, out=None, **kwargs): return _pure_unary_func_helper(x, _api_internal.negative, _np.negative, out=out) +@set_module('mxnet.ndarray.numpy') +@wrap_np_unary_func +def positive(x, out=None, **kwargs): + r""" + Computes the numerical positive of each element `x_i` (i.e.,`y_i = +x_i`) + of the input array x . + + Parameters + ---------- + x : ndarray or scalar + Input array. + + Returns + ------- + y : ndarray or scalar + Returned array or scalar: y = +x. This is a scalar if x is a scalar. + + Notes + ----- + Equivalent to `x.copy()`, but only defined for types that support arithmetic. + + Examples + -------- + >>> x1 = np.array(([1., -1.])) + >>> np.positive(x1) + array([ 1., -1.]) + >>> +x1 + array([ 1., -1.]) + """ + if out is x: + return x + return _pure_unary_func_helper(x, _api_internal.copy, _np.positive, out=out) + + @set_module('mxnet.ndarray.numpy') @wrap_np_unary_func def fix(x, out=None, **kwargs): diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index fadac105d69b..a58f1faf5587 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -80,7 +80,7 @@ 'quantile', 'percentile', 'shares_memory', 'may_share_memory', 'diff', 'ediff1d', 'resize', 'matmul', 'nan_to_num', 'isnan', 'isinf', 'isposinf', 'isneginf', 'isfinite', 'polyval', 'where', 'bincount', 'atleast_1d', 'atleast_2d', 'atleast_3d', 'fill_diagonal', 'squeeze', - 'diagflat', 'repeat', 'prod', 'pad', 'cumsum', 'sum', 'rollaxis', 'diag', 'diagonal'] + 'diagflat', 'repeat', 'prod', 'pad', 'cumsum', 'sum', 'rollaxis', 'diag', 'diagonal', 'positive'] __all__ += fallback.__all__ @@ -1114,8 +1114,13 @@ def __mul__(self, other): return multiply(self, other) def __neg__(self): + """x.__neg__() <=> -x""" return negative(self) + def __pos__(self): + """x.__pos__() <=> +x""" + return positive(self) + @wrap_mxnp_np_ufunc def __imul__(self, other): r"""x.__imul__(y) <=> x \*= y""" @@ -5056,6 +5061,38 @@ def negative(x, out=None, **kwargs): return _mx_nd_np.negative(x, out=out) +@set_module('mxnet.numpy') +@wrap_np_unary_func +def positive(x, out=None, **kwargs): + r""" + Computes the numerical positive of each element `x_i` (i.e.,`y_i = +x_i`) + of the input array x . + + Parameters + ---------- + x : ndarray or scalar + Input array. + + Returns + ------- + y : ndarray or scalar + Returned array or scalar: y = +x. This is a scalar if x is a scalar. + + Notes + ----- + Equivalent to `x.copy()`, but only defined for types that support arithmetic. + + Examples + -------- + >>> x1 = np.array(([1., -1.])) + >>> np.positive(x1) + array([ 1., -1.]) + >>> +x1 + array([ 1., -1.]) + """ + return _mx_nd_np.positive(x, out=out) + + @set_module('mxnet.numpy') @wrap_np_unary_func def fix(x, out=None, **kwargs): diff --git a/tests/python/unittest/test_numpy_op.py b/tests/python/unittest/test_numpy_op.py index 880e617522fd..1010475c605d 100644 --- a/tests/python/unittest/test_numpy_op.py +++ b/tests/python/unittest/test_numpy_op.py @@ -2818,6 +2818,7 @@ def forward(self, a, *args, **kwargs): 'absolute' : (lambda x: -1. * (x < 0) + (x > 0), -1.0, 1.0), 'logical_not' : (None, -1.0, 1.0), 'negative' : (lambda x: -1. * onp.ones(x.shape), -1.0, 1.0), + 'positive' : (lambda x: onp.ones(x.shape), -1.0, 1.0), 'reciprocal' : (lambda x: -1. / (x ** 2), 0.01, 1.0), 'sign' : (None, -1.0, 1.0), 'square' : (lambda x: 2.0 * x, -1.0, 1.0),