-
Notifications
You must be signed in to change notification settings - Fork 399
Fix #764: wrong SymbolicOperator.isclose() result #1114
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
Open
mhucka
wants to merge
2
commits into
master
Choose a base branch
from
mh-fix-764
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -605,7 +605,7 @@ def __pow__(self, exponent): | |
|
||
def __eq__(self, other): | ||
"""Approximate numerical equality (not true equality).""" | ||
return self.isclose(other) | ||
return self.isclose(other, rel_tol=EQ_TOLERANCE, abs_tol=EQ_TOLERANCE) | ||
|
||
def __ne__(self, other): | ||
return not (self == other) | ||
|
@@ -618,15 +618,17 @@ def __next__(self): | |
term, coefficient = next(self._iter) | ||
return self.__class__(term=term, coefficient=coefficient) | ||
|
||
def isclose(self, other, tol=EQ_TOLERANCE): | ||
def isclose(self, other, rel_tol=EQ_TOLERANCE, abs_tol=EQ_TOLERANCE): | ||
"""Check if other (SymbolicOperator) is close to self. | ||
|
||
Comparison is done for each term individually. Return True | ||
if the difference between each term in self and other is | ||
less than EQ_TOLERANCE | ||
less than the specified tolerance. | ||
|
||
Args: | ||
other(SymbolicOperator): SymbolicOperator to compare against. | ||
rel_tol(float): Relative tolerance. | ||
abs_tol(float): Absolute tolerance. | ||
Comment on lines
+630
to
+631
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. consider rtol and atol for consistency with numpy/scipy |
||
""" | ||
if not isinstance(self, type(other)): | ||
return NotImplemented | ||
|
@@ -635,17 +637,26 @@ def isclose(self, other, tol=EQ_TOLERANCE): | |
for term in set(self.terms).intersection(set(other.terms)): | ||
a = self.terms[term] | ||
b = other.terms[term] | ||
if not (isinstance(a, sympy.Expr) or isinstance(b, sympy.Expr)): | ||
tol *= max(1, abs(a), abs(b)) | ||
if self._issmall(a - b, tol) is False: | ||
if isinstance(a, sympy.Expr) or isinstance(b, sympy.Expr): | ||
if self._issmall(a - b, abs_tol) is False: | ||
return False | ||
elif not abs(a - b) <= abs_tol + rel_tol * max(abs(a), abs(b)): | ||
return False | ||
# terms only in one (compare to 0.0 so only abs_tol) | ||
for term in set(self.terms).symmetric_difference(set(other.terms)): | ||
if term in self.terms: | ||
if self._issmall(self.terms[term], tol) is False: | ||
coeff = self.terms[term] | ||
if isinstance(coeff, sympy.Expr): | ||
if self._issmall(coeff, abs_tol) is False: | ||
return False | ||
elif not abs(coeff) <= abs_tol: | ||
return False | ||
else: | ||
if self._issmall(other.terms[term], tol) is False: | ||
coeff = other.terms[term] | ||
if isinstance(coeff, sympy.Expr): | ||
if self._issmall(coeff, abs_tol) is False: | ||
return False | ||
elif not abs(coeff) <= abs_tol: | ||
return False | ||
return True | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this is technically a breaking API change. are we ok with that? weigh confusion vs. code compatibility to keeping one of them named
tol
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.
Yeah, maybe we could keep 'tol' as a deprecated parameter and set atol=tol if it exists.