Repository navigation
Implemented np.allclose in numba/np/arraymath.py
#7940
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 14 commits
e209bc1
d16cd63
eee11d0
2dc5bb7
ca98b63
6542301
f09d8ff
50367ef
0cc9234
9ee9842
4d90833
ead6817
9c1d44f
1b24788
a93b0ef
66969ce
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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -864,6 +864,100 @@ def flat_all(a): | |||||||||||||||||||||||||||||||
| return flat_all | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @register_jitable | ||||||||||||||||||||||||||||||||
| def _allclose_scalars(a_v, b_v, rtol=1e-05, atol=1e-08, equal_nan=False): | ||||||||||||||||||||||||||||||||
| a_v_isnan = np.isnan(a_v) | ||||||||||||||||||||||||||||||||
| b_v_isnan = np.isnan(b_v) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # only one of the values is NaN and the | ||||||||||||||||||||||||||||||||
| # other is not. | ||||||||||||||||||||||||||||||||
| if ( (not a_v_isnan and b_v_isnan) or | ||||||||||||||||||||||||||||||||
| (a_v_isnan and not b_v_isnan) ): | ||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # either both of the values are NaN | ||||||||||||||||||||||||||||||||
| # or both are numbers | ||||||||||||||||||||||||||||||||
| if a_v_isnan and b_v_isnan: | ||||||||||||||||||||||||||||||||
| if not equal_nan: | ||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||
| if np.isinf(a_v) or np.isinf(b_v): | ||||||||||||||||||||||||||||||||
| return a_v == b_v | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if np.abs(a_v - b_v) > atol + rtol * np.abs(b_v * 1.0): | ||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @overload(np.allclose) | ||||||||||||||||||||||||||||||||
| @overload_method(types.Array, "allclose") | ||||||||||||||||||||||||||||||||
| def np_allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False): | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if not type_can_asarray(a): | ||||||||||||||||||||||||||||||||
| raise TypeError('The first argument "a" must be array-like') | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if not type_can_asarray(b): | ||||||||||||||||||||||||||||||||
| raise TypeError('The first argument "b" must be array-like') | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if not isinstance(rtol, types.Float): | ||||||||||||||||||||||||||||||||
| raise TypeError('The third argument "rtol" must be a ' | ||||||||||||||||||||||||||||||||
| 'floating point') | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if not isinstance(atol, types.Float): | ||||||||||||||||||||||||||||||||
| raise TypingError('The fourth argument "atol" must be a ' | ||||||||||||||||||||||||||||||||
| 'floating point') | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if not isinstance(equal_nan, types.Boolean): | ||||||||||||||||||||||||||||||||
| raise TypeError('The fifth argument "equal_nan" must be a ' | ||||||||||||||||||||||||||||||||
| 'boolean') | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| is_a_scalar = isinstance(a, types.Number) | ||||||||||||||||||||||||||||||||
| is_b_scalar = isinstance(b, types.Number) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if is_a_scalar and is_b_scalar: | ||||||||||||||||||||||||||||||||
| def np_allclose_impl_scalar_scalar(a, b, rtol=1e-05, atol=1e-08, | ||||||||||||||||||||||||||||||||
| equal_nan=False): | ||||||||||||||||||||||||||||||||
| return _allclose_scalars(a, b, rtol=rtol, atol=atol, | ||||||||||||||||||||||||||||||||
| equal_nan=equal_nan) | ||||||||||||||||||||||||||||||||
| return np_allclose_impl_scalar_scalar | ||||||||||||||||||||||||||||||||
| elif is_a_scalar and not is_b_scalar: | ||||||||||||||||||||||||||||||||
| def np_allclose_impl_scalar_array(a, b, rtol=1e-05, atol=1e-08, | ||||||||||||||||||||||||||||||||
| equal_nan=False): | ||||||||||||||||||||||||||||||||
| b = np.asarray(b) | ||||||||||||||||||||||||||||||||
| for bv in np.nditer(b): | ||||||||||||||||||||||||||||||||
| if not _allclose_scalars(a, bv.item(), rtol=rtol, atol=atol, | ||||||||||||||||||||||||||||||||
| equal_nan=equal_nan): | ||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||
| return np_allclose_impl_scalar_array | ||||||||||||||||||||||||||||||||
|
Comment on lines
+924
to
+933
Contributor
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. You can replace this block of code by:
Suggested change
Contributor
Author
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. I don't think this will give right results all the time, because the check depends on the ordering of Quoting from "Notes" section of https://numpy.org/doc/stable/reference/generated/numpy.allclose.html
Please let me know if should still apply the above code suggestion.
Contributor
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. That's fine. I wasn't aware of this info. |
||||||||||||||||||||||||||||||||
| elif not is_a_scalar and is_b_scalar: | ||||||||||||||||||||||||||||||||
| def np_allclose_impl_array_scalar(a, b, rtol=1e-05, atol=1e-08, | ||||||||||||||||||||||||||||||||
| equal_nan=False): | ||||||||||||||||||||||||||||||||
| a = np.asarray(a) | ||||||||||||||||||||||||||||||||
| for av in np.nditer(a): | ||||||||||||||||||||||||||||||||
| if not _allclose_scalars(av.item(), b, rtol=rtol, atol=atol, | ||||||||||||||||||||||||||||||||
| equal_nan=equal_nan): | ||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||
| return np_allclose_impl_array_scalar | ||||||||||||||||||||||||||||||||
| elif not is_a_scalar and not is_b_scalar: | ||||||||||||||||||||||||||||||||
| def np_allclose_impl_array_array(a, b, rtol=1e-05, atol=1e-08, | ||||||||||||||||||||||||||||||||
| equal_nan=False): | ||||||||||||||||||||||||||||||||
| a = np.asarray(a) | ||||||||||||||||||||||||||||||||
| b = np.asarray(b) | ||||||||||||||||||||||||||||||||
| a_a, b_b = np.broadcast_arrays(a, b) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for av, bv in np.nditer((a_a, b_b)): | ||||||||||||||||||||||||||||||||
| if not _allclose_scalars(av.item(), bv.item(), rtol=rtol, | ||||||||||||||||||||||||||||||||
| atol=atol, equal_nan=equal_nan): | ||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return np_allclose_impl_array_array | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @overload(np.any) | ||||||||||||||||||||||||||||||||
| @overload_method(types.Array, "any") | ||||||||||||||||||||||||||||||||
| def np_any(a): | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have addressed the review and the tests are passing. Please let me know if anything else is to be done here. Thanks.