Opened 2 years ago

Closed 2 years ago

#24236 closed enhancement (fixed)

Structural comparison of expressions

Reported by: rws Owned by:
Priority: major Milestone: sage-8.2
Component: symbolics Keywords:
Cc: Merged in:
Authors: Ralf Stephan Reviewers: Eric Gourgoulhon
Report Upstream: N/A Work issues:
Branch: 5955d56 (Commits) 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 (ex1-ex2).is_trivial_zero(). This ticket implements a well visible member function for this task.

Change History (10)

comment:1 Changed 2 years ago by rws

  • Branch set to u/rws/structural_comparison_of_expressions

comment:2 Changed 2 years ago by rws

  • Authors set to Ralf Stephan
  • Commit set to 92f95cef93000042532acbf8d84e4bede8e699ea
  • Status changed from new to needs_review

comment:3 Changed 2 years ago by git

  • Commit changed from 92f95cef93000042532acbf8d84e4bede8e699ea to e53fd2bd572f171604ac26a4952c3841f890012d

Branch pushed to git repo; I updated commit sha1. New commits:

e53fd2b24236: Structural comparison of expressions

comment:4 Changed 2 years ago by egourgoulhon

Thanks for implementing this!

A quick question: is this strictly equivalent to (ex1-ex2).is_trivial_zero()? I mean, can we have expressions ex1 and ex2 for which (ex1-ex2).is_trivial_zero() and ex1.is_trivially_equal(ex2) do not give the same result?

comment:5 Changed 2 years ago by rws

One case is:

sage: inf = SR(oo)
sage: inf-inf
...
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: (half-1/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 2 years ago by git

  • Commit changed from e53fd2bd572f171604ac26a4952c3841f890012d to 5955d56eeb3e0e9799a8aab0330db19a4d83d8e2

Branch pushed to git repo; I updated commit sha1. New commits:

5955d5624236: fix

comment:7 Changed 2 years ago by rws

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 follow-up: Changed 2 years ago by egourgoulhon

  • 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 (ex1-ex2).is_trivial_zero(), isn't it?

comment:9 in reply to: ↑ 8 Changed 2 years ago by rws

Replying to egourgoulhon:

One last question: ex1.is_trivially_equal(ex2) is always faster than (ex1-ex2).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 2 years ago by vbraun

  • Branch changed from u/rws/structural_comparison_of_expressions to 5955d56eeb3e0e9799a8aab0330db19a4d83d8e2
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.