-
Notifications
You must be signed in to change notification settings - Fork 11
ENH: new function isclose
#113
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
Changes from 3 commits
55208b0
5f54c92
c3861a7
33b37df
542da61
42da174
38e88b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
cov | ||
create_diagonal | ||
expand_dims | ||
isclose | ||
kron | ||
nunique | ||
pad | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -305,6 +305,41 @@ def expand_dims( | |
return a | ||
|
||
|
||
def isclose( | ||
a: Array, | ||
b: Array, | ||
*, | ||
rtol: float = 1e-05, | ||
atol: float = 1e-08, | ||
equal_nan: bool = False, | ||
xp: ModuleType | None = None, | ||
) -> Array: # numpydoc ignore=PR01,RT01 | ||
"""See docstring in array_api_extra._delegation.""" | ||
xp = array_namespace(a, b) if xp is None else xp | ||
crusaderky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
a_inexact = xp.isdtype(a.dtype, ("real floating", "complex floating")) | ||
b_inexact = xp.isdtype(b.dtype, ("real floating", "complex floating")) | ||
if a_inexact or b_inexact: | ||
# FIXME: use scipy's lazywhere to suppress warnings on inf | ||
out = xp.abs(a - b) <= (atol + rtol * xp.abs(b)) | ||
out = xp.where(xp.isinf(a) & xp.isinf(b), xp.sign(a) == xp.sign(b), out) | ||
if equal_nan: | ||
out = xp.where(xp.isnan(a) & xp.isnan(b), xp.asarray(True), out) | ||
return out | ||
|
||
if xp.isdtype(a.dtype, "bool") or xp.isdtype(b.dtype, "bool"): | ||
if atol >= 1 or rtol >= 1: | ||
return xp.ones_like(a == b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On eager backends, this is less performant than return xp.ones(xp.broadcast_arrays(a, b)[0], dtype=bool, device=a.device) but it supports backends with NaN shapes like Dask. |
||
return a == b | ||
|
||
# integer types | ||
atol = int(atol) | ||
if rtol == 0: | ||
return xp.abs(a - b) <= atol | ||
nrtol = int(1.0 / rtol) | ||
return xp.abs(a - b) <= (atol + xp.abs(b) // nrtol) | ||
|
||
|
||
def kron(a: Array, b: Array, /, *, xp: ModuleType | None = None) -> Array: | ||
""" | ||
Kronecker product of two arrays. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,13 @@ | |
Note that this is private API; don't expect it to be stable. | ||
""" | ||
|
||
import math | ||
from types import ModuleType | ||
|
||
from ._utils._compat import ( | ||
array_namespace, | ||
is_cupy_namespace, | ||
is_dask_namespace, | ||
is_pydata_sparse_namespace, | ||
is_torch_namespace, | ||
) | ||
|
@@ -40,8 +42,16 @@ def _check_ns_shape_dtype( | |
msg = f"namespaces do not match: {actual_xp} != f{desired_xp}" | ||
assert actual_xp == desired_xp, msg | ||
|
||
msg = f"shapes do not match: {actual.shape} != f{desired.shape}" | ||
assert actual.shape == desired.shape, msg | ||
actual_shape = actual.shape | ||
desired_shape = desired.shape | ||
if is_dask_namespace(desired_xp): | ||
if any(math.isnan(i) for i in actual_shape): | ||
actual_shape = actual.compute().shape | ||
if any(math.isnan(i) for i in desired_shape): | ||
desired_shape = desired.compute().shape | ||
|
||
msg = f"shapes do not match: {actual_shape} != f{desired_shape}" | ||
assert actual_shape == desired_shape, msg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will have to be replicated on the scipy PR for dask support |
||
|
||
msg = f"dtypes do not match: {actual.dtype} != {desired.dtype}" | ||
assert actual.dtype == desired.dtype, msg | ||
|
@@ -61,6 +71,11 @@ def xp_assert_equal(actual: Array, desired: Array, err_msg: str = "") -> None: | |
The expected array (typically hardcoded). | ||
err_msg : str, optional | ||
Error message to display on failure. | ||
|
||
See Also | ||
-------- | ||
xp_assert_close | ||
numpy.testing.assert_array_equal | ||
lucascolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
xp = _check_ns_shape_dtype(actual, desired) | ||
|
||
|
@@ -112,6 +127,16 @@ def xp_assert_close( | |
Absolute tolerance. Default: 0. | ||
err_msg : str, optional | ||
Error message to display on failure. | ||
|
||
See Also | ||
-------- | ||
xp_assert_equal | ||
isclose | ||
numpy.testing.assert_allclose | ||
lucascolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Notes | ||
----- | ||
The default `atol` and `rtol` differ from `xp.all(xpx.allclose(a, b))`. | ||
crusaderky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
xp = _check_ns_shape_dtype(actual, desired) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.