From ccb812f069013e66d10f63e6865c022329d1a671 Mon Sep 17 00:00:00 2001 From: barry-jin Date: Wed, 13 Oct 2021 17:20:06 -0700 Subject: [PATCH 1/2] [API] Add positive --- python/mxnet/ndarray/numpy/_op.py | 35 ++++++++++++++++++++++- python/mxnet/numpy/multiarray.py | 39 +++++++++++++++++++++++++- tests/python/unittest/test_numpy_op.py | 1 + 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index a7465865b707..0f3d7afefe06 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,38 @@ 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.]) + """ + 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 c2d9db95f471..10d1543f8ea5 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__ @@ -1094,8 +1094,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""" @@ -4823,6 +4828,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) + + @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 488f1a80285d..5ff088854e54 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), From 4785ed924fd10c1ca08efdb7c2e5b44cd5843f68 Mon Sep 17 00:00:00 2001 From: barry-jin Date: Tue, 19 Oct 2021 11:47:11 -0700 Subject: [PATCH 2/2] support in-place --- python/mxnet/ndarray/numpy/_op.py | 2 ++ python/mxnet/numpy/multiarray.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 0f3d7afefe06..cf1bd52c2228 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -3591,6 +3591,8 @@ def positive(x, out=None, **kwargs): >>> +x1 array([ 1., -1.]) """ + if out is x: + return x return _pure_unary_func_helper(x, _api_internal.copy, _np.positive, out=out) diff --git a/python/mxnet/numpy/multiarray.py b/python/mxnet/numpy/multiarray.py index 10d1543f8ea5..f6123afc682a 100644 --- a/python/mxnet/numpy/multiarray.py +++ b/python/mxnet/numpy/multiarray.py @@ -4857,7 +4857,7 @@ def positive(x, out=None, **kwargs): >>> +x1 array([ 1., -1.]) """ - return _mx_nd_np.positive(x) + return _mx_nd_np.positive(x, out=out) @set_module('mxnet.numpy')