Skip to content

fix #488 : added rtol and atol arguments to LArray.equals #582

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

Merged
merged 1 commit into from
Feb 16, 2018

Conversation

alixdamman
Copy link
Collaborator

in order to compare float arrays within a relative and/or absolute tolerance.

@alixdamman alixdamman requested a review from gdementen February 15, 2018 15:26
>>> arr2
a a0 a1
1.0 1.0
>>> arr1.equals(arr2, atol=5e-5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should first show what happens when you do not specify any *tol argument, and I think the tolerance in the example should be chosen so that the result is True (or one example True and one False)

absolute(self - other) <= (atol + rtol * absolute(other))

The above equation is not symmetric in self and other, so that equals(self, other) might be different
from equals(other, self) in some rare cases.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write it like users are likely to encounter it: array1.equals(array2) might be different from array2.equals(array1)

"""
Compares self with another array and returns True if they have the same axes and elements, False otherwise.

Parameters
----------
other: LArray-like
Input array. aslarray() is used on a non-LArray input.
rtol : float, optional
The relative tolerance parameter (see Notes).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults to 0.

rtol : float, optional
The relative tolerance parameter (see Notes).
atol : float, optional
The absolute tolerance parameter (see Notes).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem.

@@ -5228,6 +5241,24 @@ def equals(self, other, nan_equals=False):
>>> arr1.equals(arr3)
False

Test equality of two floating arrays within a given tolerance range.
Return True if absolute(array1 - array2) <= (atol + rtol * absolute(arr2)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/arr2/array2/

1.0 1.0
>>> arr1.equals(arr2, atol=5e-5)
False
>>> arr1.equals(arr2, rtol=5e-5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fact that array2 is 1 makes rtol not very interesting.

I would go for something like:

arr1 = LArray([10, 10], "a=a0,a1")
arr2 = LArray([9.99, 10.01], "a=a0,a1")

>>> arr2 = ones("a=a0,a1")
>>> arr2
a a0 a1
1.0 1.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as in the release notes

"""
Compares self with another array and returns True if they have the same axes and elements, False otherwise.

Parameters
----------
other: LArray-like
Input array. aslarray() is used on a non-LArray input.
rtol : float, optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int or float

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int? why int?
When users work with arrays containing big integers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The unit of your arrays could be kg when you only care at the ton level, or whatever.

@@ -5211,6 +5215,15 @@ def equals(self, other, nan_equals=False):
bool
Returns True if self is equal to other.

Notes
-----
For finite values, equals uses the following equation to test whether two floating point values are equivalent.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not only valid for floating point values (and it makes sense to use atol or rtol on int arrays too).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/equivalent/equal/ Otherwise the wording might be ambiguous when we introduce .equiv (#237)

@@ -5228,6 +5241,24 @@ def equals(self, other, nan_equals=False):
>>> arr1.equals(arr3)
False

Test equality of two floating arrays within a given tolerance range.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not float only

@alixdamman alixdamman requested a review from gdementen February 16, 2018 14:01
@@ -5217,12 +5217,12 @@ def equals(self, other, rtol=0, atol=0, nan_equals=False):

Notes
-----
For finite values, equals uses the following equation to test whether two floating point values are equivalent.
For finite values, equals uses the following equation to test whether two float or int values are equal:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/float or int//

False
>>> arr1.equals(arr2, rtol=5e-5, atol=5e-5)
True
These two arguments can used to test the equality between two float or int arrays within a given relative and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/float or int//

@@ -5241,22 +5241,22 @@ def equals(self, other, rtol=0, atol=0, nan_equals=False):
>>> arr1.equals(arr3)
False

Test equality of two floating arrays within a given tolerance range.
Return True if absolute(array1 - array2) <= (atol + rtol * absolute(arr2)).
Test equality between two float or int arrays within a given tolerance range.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/float or int//

Closes :issue:`548`.

* added the relative tolerance `rtol` and the absolute tolerance `atol` arguments to the `LArray.equals` method.
These two arguments can used to test the equality between two float or int arrays within a given relative and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/can used/can be used/

…ls in order to test equality between two arrays within a relative or absolute tolerance
@alixdamman alixdamman merged commit 607c760 into larray-project:master Feb 16, 2018
@alixdamman alixdamman deleted the approx_equal_488 branch February 16, 2018 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants