{{ message }}
Improved behavior of comparison between quantities#249
Open
wagenadl wants to merge 3 commits intopython-quantities:masterfrom
Open
Improved behavior of comparison between quantities#249wagenadl wants to merge 3 commits intopython-quantities:masterfrom
wagenadl wants to merge 3 commits intopython-quantities:masterfrom
Conversation
For quantities A and B of unequal dimensionality, the `==` operator
now returns an array of the same shape that numpy would return
if A and B were different and filled with False.
In particular, if both A and B are scalar arrays, the result is
a scalar False. Previously, a scalar array (`np.array(False)`)
would be returned under these circumstances.
If A and B do not have compatible sizes, an exception is generated.
The `!=` operator behaves the same except that it fills the result
with True values.
The other comparison operators now raise exceptions if a quantity
that is not dimensionless is compared to a plain number. For
effectively dimensionless quantities the comparison is now
consistent. Previously, if
n = pq.s * pq.kHz,
then n < 2 would return True but n.simplified < 2 would return
False. Now, both return False.
Comparison between incompatible quantities raises exceptions as
before.
Typo caused __gt__ to behave like __ge__. No more.
Contributor
Author
Contributor
|
Thank you for this, the new behaviour as you describe it does seem to be an improvement. There is, however, a risk that it breaks downstream code that relies on the current behaviour, so I would like to test it first with some of the downstream projects, such as Neo. I note that this would require #248 to be merged first. @wagenadl perhaps you could modify the implementation, at least temporarily, to not use the In addition, please could you add some unit tests, for the "standard" usage, and for any potential corner cases you can foresee? |
The "test_comparison.py" test file now verifies that the behavior of the comparison operators matches the new specification. This involved changing various assertions and adding some new ones. Also, this patch now also works without the new “dimensionless_magnitude” property.
Contributor
Author
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

For quantities A and B of unequal dimensionality, the
==operator now returns an array of the same shape that numpy would return if A and B were different and filled with False.In particular, if both A and B are scalar arrays, the result is a scalar False. Previously, a scalar array (
np.array(False)) would be returned under these circumstances.If A and B do not have compatible sizes, an exception is generated.
The
!=operator behaves the same except that it fills the result with True values.The other comparison operators now raise exceptions if a quantity that is not dimensionless is compared to a plain number. For effectively dimensionless quantities the comparison is now consistent. Previously, if
then
n < 2would return True butn.simplified < 2would return False. Now, both return False.Comparison between incompatible quantities raises exceptions as before.
Closes #245