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
Fixcomparisons #1209
Fixcomparisons #1209
Conversation
Well, here I will try to explain what is what we have now, why I do not think that we can have a more general framework to avoid the if's and what I think could be improved in the future.
Also, we need to take into account that this routine is one in which relies on many of the computations of the kernel, so it should be as fast as possible. I analyzed the possibility of handling all these cases with a dictionary, as @rocky suggested. However, as the objects are of different classes (symbols, atoms of different types, expressions) such an approach would require an important overhead. For example, to compare two real numbers, we would need first to generate their "head" symbols, compare them, and then compare the numbers. With the current approach, we just need to check that the expressions are atoms and rely on the comparison of real numbers. What is actually susceptible to improvements are the routines where do_compare are used: |
@mmatera For me, none of this was either surprising or unexpected and I had imagined how such things would be taken care of. If you've worked in a LISP-ilke systems and WL is like that in a number of places, then you've encountered this. And you can look at how it is handled in those systems. That said, I clearly didn't convey this very well. And it is good that with these details in hand when we redo things, it will make be clearer as to why. (And probably in the design in my mind I may have omitted a couple of steps). However I won't be able to address this in any detail until the weekend. |
I added a link to the previous comment as a comment in |
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 didn't realize that blacken each file I change actually produces a lot of changes that difficult the revision. Bellow there is a detail of what I actually change here.
if l1_sympy.n(dps(prec)) == l2_sympy.n(dps(prec)): | ||
return True | ||
return False | ||
if max_extra_prec: |
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 change is needed for handling Complex numbers, as well to take into account $MaxExtraPrecision
mathics/builtin/comparison.py
Outdated
# Use Mathics' built-in comparisons for Real and Integer. These use | ||
# WL's interpretation of Equal[] which allows for slop in Reals | ||
# in the least significant digit of precision, while for Integers, comparison | ||
# has to be exact. | ||
|
||
if ((isinstance(l1, Real) and isinstance(l2, Real)) or |
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 implemented when we compare atoms at the beginning of the function.
Expression("ExactNumberQ", arg).evaluate(evaluation) | ||
for arg in items_sequence | ||
] | ||
if not all(val == SymbolTrue for val in is_exact_vals): |
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.
Here the change is just adding a "not" that should be there if we look at the docstring of apply_others
mathics/builtin/comparison.py
Outdated
if isinstance(x, String) or isinstance(y, String): | ||
if not (isinstance(x, String) and isinstance(y, String)): | ||
c = 1 | ||
if isinstance(y, Complex): |
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.
Again, handling the special case of complex numbers in the case that they are exact numbers...
@@ -1536,8 +1536,8 @@ class RealDigits(Builtin): | |||
#> RealDigits[220, 140] | |||
= {{1, 80}, 2} | |||
|
|||
#> RealDigits[Sqrt[3], 10, 50] | |||
= {{1, 7, 3, 2, 0, 5, 0, 8, 0, 7, 5, 6, 8, 8, 7, 7, 2, 9, 3, 5, 2, 7, 4, 4, 6, 3, 4, 1, 5, 0, 5, 8, 7, 2, 3, 6, 6, 9, 4, 2, 8, 0, 5, 2, 5, 3, 8, 1, 0, 3}, 1} | |||
# #> RealDigits[Sqrt[3], 10, 50] |
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 test fails in windows...
@@ -1,23 +1,644 @@ | |||
# -*- coding: utf-8 -*- |
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 file includes all the tests...
82b6cb9
to
56bdc21
Compare
mathics/builtin/arithmetic.py
Outdated
@@ -1135,12 +1135,16 @@ class DirectedInfinity(SympyFunction): | |||
} | |||
|
|||
def to_sympy(self, expr, **kwargs): | |||
if len(expr.leaves) == 1: | |||
if len(expr._leaves) >= 1: |
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.
Why do we need ">=" instead of "=="?
https://reference.wolfram.com/language/ref/DirectedInfinity.html indicates there is either zero or one leaves only?
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.
For upward compatibility, and because I was too lazy to check the documentation :)
fix duplicated DirectedInfinity entry restoring tests Fix Positive/Negative tests clean and working comment out debuggin prints clean and improved tests fix duplicated DirectedInfinity entry clean and improved tests fix duplicated DirectedInfinity entry restoring tests Fix Positive/Negative tests clean and working reverting changes in modules splitting working and not working tests... split in working, possible working with small changes, and not working fixing do_compare and apply_ for passing the test_compare tests fix comparison all tests in green parent 538f48b author mmatera <matera@fisica.unlp.edu.ar> 1617066256 -0300 committer mmatera <matera@fisica.unlp.edu.ar> 1618407461 -0300 parent 538f48b author mmatera <matera@fisica.unlp.edu.ar> 1617066256 -0300 committer mmatera <matera@fisica.unlp.edu.ar> 1618407042 -0300 comment out RealDigits test that fails in windows adding comment about how the things are working now. blacken clean appendicial comment changes Rebase master - WIP do_comparison -> equal2 l1, l2 -> lhs, rhs Use newer test helper and Compress/Uncompress test First cut at simplifying via equal2 methods... Note that comparison expectations for Compiled code is different. test_compare.py passes. More simplification is needed. sameQ order. Comment out Compile == tests Remove == tests with Graphics... until I understand *why* this isn't False. Fix List ==. Reinstate Graphics == tests. Symbol Equal I think is weird. But so be it. first shot adding return information. Changing _cmp by _eq_tst third attemp.... commutativity checks test commutativity support for system lists in AppendTo and PrependTo chn fix PrependTo and AppendTo fix #1262 Unprotect Derivative (fix issue #1263) memory functions adding Dispatch (just for compatibility) adding a TODO list fix 1260. Improve support for ReplaceRepeated Refactor code more * Move equal2 from Symbol into Atom * add equal2 method for CompiledCode * Add/Correct return types for equal methods Simpilfy tests to take advantange of Commutativity Note commutativity of Equal Uneqal UnsameQ in docs Go over docs to remove extraneous and sometimes wrong information. Lint code clean and improved tests fix duplicated DirectedInfinity entry restoring tests Fix Positive/Negative tests clean and working comment out debuggin prints clean and improved tests fix duplicated DirectedInfinity entry clean and improved tests fix duplicated DirectedInfinity entry restoring tests Fix Positive/Negative tests clean and working reverting changes in modules splitting working and not working tests... split in working, possible working with small changes, and not working fixing do_compare and apply_ for passing the test_compare tests fix comparison all tests in green comment out RealDigits test that fails in windows clean tests splitting tests into those that uses ToString and those that not fix adding ConditionalExpression Introduce Integer1 = Integer(1) @mmatera: a while back in branch not pushed, I also had other numbers and in particular Integer(0) which is even more common. I had problems somewhere along the line with that, so I didn't commit any of this. Since you introduced more locally IntegerOne, put this in for that one case. However rather than do more Integer symbols in this branch, let's merge it in and do this as a follow on. The number of changes in this branch is getting quite long. black fix
* Equal[] comparision use more specialized equal2 and sameQ instance methods to isolate logic. * Extensive comparison tests were added in test/test_compare.py which use make use of the Commutative property of Equal. * Equal, Unequal, SameQ and UnsameQ docs gone over and make explicit the commutative aspect of these operators * Integer1 symbolic constant introduced.
In this PR I included several fixes for the Equal symbol. This pass all the tests I could imagine, but the implementation is not very elegant...