6

How can I determine in sage whether two expressions are equivalent? For example:

sage: var('x')
x
sage: e1 = 1 + x
sage: e2 = x + 1
sage: e3 = x + 2
sage: is_equivalent(e1, e2)
True
sage: is_equivalent(e1, e3)
False
sage: var('y')
y
sage: e4 = x * (1 + y)
sage: e5 = x + (x * y)
sage: is_equivalent(e4, e5)
True
sage: is_equivalent(e4, e1)
False
sage: assume(x, 'real')
sage: e6 = sqrt(x**2) + 1
sage: is_equivalent(e6, e1)
True

What has already been suggested/ tried: (sage 6.4.1 on Ubuntu Linux)

sage: e1 == e2
x + 1 == x + 1
sage: e1 is e2
False
sage: e1.match(e2) is not None
True
sage: e4.match(e5) is not None
False
Oleg
  • 1,659
  • 13
  • 23

1 Answers1

6

The usual way to do this is to make an equation out of them and check whether it is True or False.

sage: e4 == e5
x*(y + 1) == x*y + x
sage: bool(_)
True

However, keep in mind that Sage will return False if it cannot prove it is True, which is not the same thing as being false. Checking equivalence of two arbitrary expressions might be arbitrarily long to do, and might require a crazy sequence of expansions/'simplifications' the computer cannot predict.

This is answering a different question:

sage: e1 is e2
False

This is Python, and is a very strong condition that two things are the same "object", which in this case they aren't.

sage: a = 1
sage: b = 1
sage: a is b
False
sage: a = 1
sage: b = a
sage: a is b
True
kcrisman
  • 4,374
  • 20
  • 41
  • There is no better way, such as one that specifies the vars are rational numbers? For example, why doesn't this work (seems simple enough): `bool((x^2)^(1/2) == x)` – Oleg Jan 16 '15 at 02:44
  • I am familiar with python but tried "is" as a last resort. I searched the docs but got way too many results. By any chance, do you happen to know where in the sage docs I should have looked to find this? – Oleg Jan 16 '15 at 02:59
  • Well, I have to say that it is there but not organized as well as possible. The symbolic expression documentation is probably where it's best laid out. – kcrisman Jan 16 '15 at 03:03