-
Notifications
You must be signed in to change notification settings - Fork 13
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 2 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. | ||
|
Uh oh!
There was an error while loading. Please reload this page.