Skip to content

Commit 390b85e

Browse files
committed
fix #871 : implemented the allclose() method
1 parent b868298 commit 390b85e

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ Testing/Searching
414414
:toctree: _generated/
415415

416416
Array.equals
417+
Array.allclose
417418
Array.eq
418419
Array.isin
419420
Array.nonzero

doc/source/changes/version_0_33.rst.inc

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Miscellaneous improvements
6161

6262
* implemented :py:obj:`isscalar()` function (closes :issue:`872`).
6363

64+
* implemented the :py:obj:`Array.allclose()` method (closes :issue:`871`).
65+
6466
Fixes
6567
^^^^^
6668

larray/core/array.py

+77-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import functools
3535
import warnings
3636

37+
from typing import Any
38+
3739
import numpy as np
3840
import pandas as pd
3941

@@ -5673,7 +5675,7 @@ def equals(self, other, rtol=0, atol=0, nans_equal=False, check_axes=False):
56735675
56745676
See Also
56755677
--------
5676-
Array.eq
5678+
Array.eq, Array.allclose
56775679
56785680
Notes
56795681
-----
@@ -5775,6 +5777,79 @@ def equals(self, other, rtol=0, atol=0, nans_equal=False, check_axes=False):
57755777
except ValueError:
57765778
return False
57775779

5780+
def allclose(self, other: Any, rtol: float = 1e-05, atol: float = 1e-08, nans_equal: bool = True,
5781+
check_axes: bool = False) -> bool:
5782+
"""
5783+
Compares self with another array and returns True if they are element-wise equal within a tolerance.
5784+
5785+
The tolerance values are positive, typically very small numbers.
5786+
The relative difference (rtol * abs(other)) and the absolute difference atol are added together to compare
5787+
against the absolute difference between self and other.
5788+
5789+
NaN values are treated as equal if they are in the same place and if `nans_equal=True`.
5790+
5791+
Parameters
5792+
----------
5793+
other : Array-like
5794+
Input array. asarray() is used on a non-Array input.
5795+
rtol : float or int, optional
5796+
The relative tolerance parameter (see Notes). Defaults to 1e-05.
5797+
atol : float or int, optional
5798+
The absolute tolerance parameter (see Notes). Defaults to 1e-08.
5799+
nans_equal : boolean, optional
5800+
Whether or not to consider NaN values at the same positions in the two arrays as equal.
5801+
By default, an array containing NaN values is never equal to another array, even if that other array
5802+
also contains NaN values at the same positions. The reason is that a NaN value is different from
5803+
*anything*, including itself. Defaults to True.
5804+
check_axes : boolean, optional
5805+
Whether or not to check that the set of axes and their order is the same on both sides. Defaults to False.
5806+
If False, two arrays with compatible axes (and the same data) will compare equal, even if some axis is
5807+
missing on either side or if the axes are in a different order.
5808+
5809+
Returns
5810+
-------
5811+
bool
5812+
Returns True if the two arrays are equal within the given tolerance; False otherwise.
5813+
5814+
See Also
5815+
--------
5816+
Array.equals
5817+
5818+
Notes
5819+
-----
5820+
If the following equation is element-wise True, then `allclose` returns True.
5821+
5822+
absolute(array1 - array2) <= (atol + rtol * absolute(array2))
5823+
5824+
The above equation is not symmetric in array1 and array2, so that array1.allclose(array2) might be different
5825+
from array2.allclose(array1) in some rare cases.
5826+
5827+
Examples
5828+
--------
5829+
>>> arr1 = Array([1e10, 1e-7], "a=a0,a1")
5830+
>>> arr2 = Array([1.00001e10, 1e-8], "a=a0,a1")
5831+
>>> arr1.allclose(arr2)
5832+
False
5833+
5834+
>>> arr1 = Array([1e10, 1e-8], "a=a0,a1")
5835+
>>> arr2 = Array([1.00001e10, 1e-9], "a=a0,a1")
5836+
>>> arr1.allclose(arr2)
5837+
True
5838+
5839+
>>> arr1 = Array([1e10, 1e-8], "a=a0,a1")
5840+
>>> arr2 = Array([1.0001e10, 1e-9], "a=a0,a1")
5841+
>>> arr1.allclose(arr2)
5842+
False
5843+
5844+
>>> arr1 = Array([1.0, nan], "a=a0,a1")
5845+
>>> arr2 = Array([1.0, nan], "a=a0,a1")
5846+
>>> arr1.allclose(arr2)
5847+
True
5848+
>>> arr1.allclose(arr2, nans_equal=False)
5849+
False
5850+
"""
5851+
return self.equals(other=other, rtol=rtol, atol=atol, nans_equal=nans_equal, check_axes=check_axes)
5852+
57785853
@deprecate_kwarg('nan_equals', 'nans_equal')
57795854
def eq(self, other, rtol=0, atol=0, nans_equal=False):
57805855
"""
@@ -5803,7 +5878,7 @@ def eq(self, other, rtol=0, atol=0, nans_equal=False):
58035878
58045879
See Also
58055880
--------
5806-
Array.equals
5881+
Array.equals, Array.isclose
58075882
58085883
Notes
58095884
-----

0 commit comments

Comments
 (0)