TransWikia.com

Testing equivalence of analytical expressions like $x^2 -x == x(x-1)$

Mathematica Asked on June 22, 2021

On occasion I have two expressions that I may be equal but simplified differently, and I want to check if they are actually equivalent.

Using the equation in the title, $x^2 -x == x(x-1)$, sometimes I want to check if the RHS is indeed equal to the LHS. $==$ does not seem to work, though; it just returns my input, and if I use triple equals (sameQ) $===$ It will give false for $x^2 -x == x(x-1)$.

Is there a way to do this/Am I doing something wrong? Also, should I simply not use mathematica for this?

As a note, it is easy to plug in values and get a feel for it numerically when the number of variables/parameters is small, but when there are a lot that is not always feasible.

Thanks.

2 Answers

See

Equations

and

Manipulating Equations and Inequalities

eq1 = x^2 - x

-x + x^2

eq2 = x (x - 1)

(-1 + x) x

eq1 == eq2

-x + x^2 == (-1 + x) x

Expand[eq1 == eq2]

True

Even when you do tests on symbolic expressions, there are some cases where you can get definite results. An important one is when you test the equality of two expressions that are identical. Whatever the numerical values of the variables in these expressions may be, the Wolfram Language knows that the expressions must always be equal.

The two expressions are identical, so the result is True, whatever the value of x may be.

2 x + x^2 == 2 x + x^2

True

The Wolfram Language does not try to tell whether these expressions are equal. In this case, using Expand would make them have the same form.

2 x + x^2 == x (2 + x)

2 x + x^2 == x (2 + x)

Expand[%]

True

Correct answer by user9660 on June 22, 2021

Answers from the comments:

Look up Simplify[] and FullSimplify[]. Or Expand[]. – J. M.'s discontentment♦ May 17 '16 at 0:53

To illustrate the suggestion by @J.M., Simplify[x^2 - x == x (x - 1)] yields True. – bbgodfrey May 17 '16 at 4:16

I might add that for more complicated tests of equivalence, one of these may be better:

Reduce[lhs == rhs]
FullSimplify[lhs == rhs]
Simplify[lhs - rhs] == 0
FullSimplify[lhs - rhs] == 0
FullSimplify[lhs - rhs] === 0 (* SameQ for always T/F *)
PossibleZeroQ[lhs - rhs]      (* always T/F *)

Note that in the last two, FullSimplify can fail to simplify to 0 two equivalent expressions, and PossibleZeroQ can return True even when it is unsure. I don't think there is a command that will give a correct True or False in all cases. See the "Background & Context" of the docs for SameQ and related functions.

Answered by Michael E2 on June 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP