Opened 3 years ago
Closed 3 years ago
#24236 closed enhancement (fixed)
Structural comparison of expressions
Reported by:  rws  Owned by:  

Priority:  major  Milestone:  sage8.2 
Component:  symbolics  Keywords:  
Cc:  Merged in:  
Authors:  Ralf Stephan  Reviewers:  Eric Gourgoulhon 
Report Upstream:  N/A  Work issues:  
Branch:  5955d56 (Commits, GitHub, GitLab)  Commit:  5955d56eeb3e0e9799a8aab0330db19a4d83d8e2 
Dependencies:  Stopgaps: 
Description
In many cases it is not necessary to prove equality of expressions but just check structural identity. An undocumented trick for doing this is (ex1ex2).is_trivial_zero()
. This ticket implements a well visible member function for this task.
Change History (10)
comment:1 Changed 3 years ago by
 Branch set to u/rws/structural_comparison_of_expressions
comment:2 Changed 3 years ago by
 Commit set to 92f95cef93000042532acbf8d84e4bede8e699ea
 Status changed from new to needs_review
comment:3 Changed 3 years ago by
 Commit changed from 92f95cef93000042532acbf8d84e4bede8e699ea to e53fd2bd572f171604ac26a4952c3841f890012d
comment:4 Changed 3 years ago by
Thanks for implementing this!
A quick question: is this strictly equivalent to (ex1ex2).is_trivial_zero()
? I mean, can we have expressions ex1
and ex2
for which (ex1ex2).is_trivial_zero()
and ex1.is_trivially_equal(ex2)
do not give the same result?
comment:5 Changed 3 years ago by
One case is:
sage: inf = SR(oo) sage: infinf ... RuntimeError: indeterminate expression: infinity  infinity encountered. sage: inf.is_trivially_equal(inf) True
When I tried to construct another I found a segfault:
sage: half = SR(QQbar(1/2)) sage: (half1/2).is_trivial_zero() True sage: half.is_trivially_equal(1/2) BOOM
which is fixed in the next commit. Nice question! It turns out this gives True as well.
comment:6 Changed 3 years ago by
 Commit changed from e53fd2bd572f171604ac26a4952c3841f890012d to 5955d56eeb3e0e9799a8aab0330db19a4d83d8e2
Branch pushed to git repo; I updated commit sha1. New commits:
5955d56  24236: fix

comment:7 Changed 3 years ago by
So to answer your question, no, except for objects that can't be subtracted the result is the same, and for those objects the answer is now correct.
comment:8 followup: ↓ 9 Changed 3 years ago by
 Reviewers set to Eric Gourgoulhon
 Status changed from needs_review to positive_review
LGTM. Thanks!
One last question: ex1.is_trivially_equal(ex2)
is always faster than (ex1ex2).is_trivial_zero()
, isn't it?
comment:9 in reply to: ↑ 8 Changed 3 years ago by
Replying to egourgoulhon:
One last question:
ex1.is_trivially_equal(ex2)
is always faster than(ex1ex2).is_trivial_zero()
, isn't it?
Yes, it's a tree walk in C++ that returns as soon as a difference in the tree is seen.
comment:10 Changed 3 years ago by
 Branch changed from u/rws/structural_comparison_of_expressions to 5955d56eeb3e0e9799a8aab0330db19a4d83d8e2
 Resolution set to fixed
 Status changed from positive_review to closed
Branch pushed to git repo; I updated commit sha1. New commits:
24236: Structural comparison of expressions