Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dpnp.isneginf and dpnp.isposinf #1888

Merged
merged 17 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions dpnp/dpnp_iface_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"isfinite",
"isinf",
"isnan",
"isneginf",
"isposinf",
"less",
"less_equal",
"logical_and",
Expand Down Expand Up @@ -710,6 +712,134 @@ def isclose(x1, x2, rtol=1e-05, atol=1e-08, equal_nan=False):
)


def isneginf(x, out=None):
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
"""
Test element-wise for negative infinity, return result as bool array.

For full documentation refer to :obj:`numpy.isneginf`.

Parameters
----------
x : {dpnp.ndarray, usm_ndarray}
Input array.
out : {None, dpnp.ndarray, usm_ndarray}, optional
A location into which the result is stored. If provided, it must have a
shape that the input broadcasts to and a boolean data type.
If not provided or None, a freshly-allocated boolean array is returned
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
out : dpnp.ndarray
Boolean array of same shape as ``x``.

See Also
--------
:obj:`dpnp.isinf` : Test element-wise for positive or negative infinity.
:obj:`dpnp.isposinf` : Test element-wise for positive infinity,
return result as bool array.
:obj:`dpnp.isnan` : Test element-wise for NaN and
return result as a boolean array.
:obj:`dpnp.isfinite` : Test element-wise for finiteness.

Examples
--------
>>> import dpnp as np
>>> x = np.array(np.inf)
>>> np.isneginf(-x)
array(True)
>>> np.isneginf(x)
array(False)

>>> x = np.array([-np.inf, 0., np.inf])
>>> np.isneginf(x)
array([ True, False, False])

>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.zeros(x.shape, dtype='bool')
>>> np.isneginf(x, y)
array([ True, False, False])
>>> y
array([ True, False, False])

"""

is_inf = dpnp.isinf(x)
try:
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
signbit = dpnp.signbit(x)
except ValueError as e:
dtype = x.dtype
raise TypeError(
f"This operation is not supported for {dtype} values "
"because it would be ambiguous."
) from e

return dpnp.logical_and(is_inf, signbit, out)
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved


def isposinf(x, out=None):
"""
Test element-wise for positive infinity, return result as bool array.

For full documentation refer to :obj:`numpy.isposinf`.

Parameters
----------
x : {dpnp.ndarray, usm_ndarray}
Input array.
out : {None, dpnp.ndarray, usm_ndarray}, optional
A location into which the result is stored. If provided, it must have a
shape that the input broadcasts to and a boolean data type.
If not provided or None, a freshly-allocated boolean array is returned

Returns
-------
out : dpnp.ndarray
Boolean array of same shape as ``x``.

See Also
--------
:obj:`dpnp.isinf` : Test element-wise for positive or negative infinity.
:obj:`dpnp.isneginf` : Test element-wise for negative infinity,
return result as bool array.
:obj:`dpnp.isnan` : Test element-wise for NaN and
return result as a boolean array.
:obj:`dpnp.isfinite` : Test element-wise for finiteness.

Examples
--------
>>> import dpnp as np
>>> x = np.array(np.inf)
>>> np.isposinf(x)
array(True)
>>> np.isposinf(-x)
array(False)

>>> x = np.array([-np.inf, 0., np.inf])
>>> np.isposinf(x)
array([False, False, True])

>>> x = np.array([-np.inf, 0., np.inf])
>>> y = np.zeros(x.shape, dtype='bool')
>>> np.isposinf(x, y)
array([False, False, True])
>>> y
array([False, False, True])

"""

is_inf = dpnp.isinf(x)
try:
signbit = ~dpnp.signbit(x)
except ValueError as e:
dtype = x.dtype
raise TypeError(
f"This operation is not supported for {dtype} values "
"because it would be ambiguous."
) from e

return dpnp.logical_and(is_inf, signbit, out)


_LESS_DOCSTRING = """
Computes the less-than test results for each element `x1_i` of
the input array `x1` with the respective element `x2_i` of the input array `x2`.
Expand Down
30 changes: 29 additions & 1 deletion tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from .helper import (
get_all_dtypes,
get_float_complex_dtypes,
has_support_aspect64,
)


Expand Down Expand Up @@ -394,3 +393,32 @@ def test_finite(op, data, dtype):
dpnp_res = getattr(dpnp, op)(x, out=dp_out)
assert dp_out is dpnp_res
assert_equal(dpnp_res, np_res)
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize("func", ["isneginf", "isposinf"])
@pytest.mark.parametrize(
"data",
[
[dpnp.inf, -1, 0, 1, dpnp.nan, -dpnp.inf],
[[dpnp.inf, dpnp.nan], [dpnp.nan, 0], [1, -dpnp.inf]],
],
ids=[
"1D array",
"2D array",
],
)
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
def test_infinity_sign(func, data, dtype):
x = dpnp.asarray(data, dtype=dtype)
if dpnp.issubdtype(dtype, dpnp.complexfloating):
with pytest.raises(TypeError):
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
dpnp_res = getattr(dpnp, func)(x)
else:
np_res = getattr(numpy, func)(x.asnumpy())
dpnp_res = getattr(dpnp, func)(x)
assert_equal(dpnp_res, np_res)

dp_out = dpnp.empty(np_res.shape, dtype=dpnp.bool)
vlad-perevezentsev marked this conversation as resolved.
Show resolved Hide resolved
dpnp_res = getattr(dpnp, func)(x, out=dp_out)
assert dp_out is dpnp_res
assert_equal(dpnp_res, np_res)
13 changes: 13 additions & 0 deletions tests/third_party/cupy/logic_tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ def test_isinf(self):

def test_isnan(self):
self.check_unary_nan("isnan")


class TestUfuncLike(unittest.TestCase):
@testing.numpy_cupy_array_equal()
def check_unary(self, name, xp):
a = xp.array([-3, xp.inf, -1, -xp.inf, 0, 1, 2, xp.nan])
return getattr(xp, name)(a)

def test_isneginf(self):
self.check_unary("isneginf")

def test_isposinf(self):
self.check_unary("isposinf")
Loading