Skip to content
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

Structural comparison of expressions #24236

Closed
rwst opened this issue Nov 18, 2017 · 14 comments
Closed

Structural comparison of expressions #24236

rwst opened this issue Nov 18, 2017 · 14 comments

Comments

@rwst
Copy link

rwst commented Nov 18, 2017

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.

Component: symbolics

Author: Ralf Stephan

Branch/Commit: 5955d56

Reviewer: Eric Gourgoulhon

Issue created by migration from https://trac.sagemath.org/ticket/24236

@rwst rwst added this to the sage-8.2 milestone Nov 18, 2017
@rwst
Copy link
Author

rwst commented Nov 18, 2017

@rwst
Copy link
Author

rwst commented Nov 18, 2017

Author: Ralf Stephan

@rwst
Copy link
Author

rwst commented Nov 18, 2017

Commit: 92f95ce

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 18, 2017

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

e53fd2b24236: Structural comparison of expressions

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 18, 2017

Changed commit from 92f95ce to e53fd2b

@egourgoulhon
Copy link
Member

comment:4

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?

@rwst
Copy link
Author

rwst commented Nov 19, 2017

comment:5

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.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 19, 2017

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

5955d5624236: fix

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 19, 2017

Changed commit from e53fd2b to 5955d56

@rwst
Copy link
Author

rwst commented Nov 19, 2017

comment:7

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.

@egourgoulhon
Copy link
Member

comment:8

LGTM. Thanks!

One last question: ex1.is_trivially_equal(ex2) is always faster than (ex1-ex2).is_trivial_zero(), isn't it?

@egourgoulhon
Copy link
Member

Reviewer: Eric Gourgoulhon

@rwst
Copy link
Author

rwst commented Nov 20, 2017

comment:9

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.

@vbraun
Copy link
Member

vbraun commented Dec 11, 2017

Changed branch from u/rws/structural_comparison_of_expressions to 5955d56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants