Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
[API] Add positive (#20667)
Browse files Browse the repository at this point in the history
* [API] Add positive

* support in-place
  • Loading branch information
barry-jin authored Oct 20, 2021
1 parent 6dff660 commit 481eba7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
37 changes: 36 additions & 1 deletion python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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):
Expand Down
39 changes: 38 additions & 1 deletion python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/python/unittest/test_numpy_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 481eba7

Please sign in to comment.