5
$\begingroup$

I want to check if two expressions are equivalent with Mathematica. For example, is $2 x + 4 + 2$ equivalent to $2 (x + 2 + 1)$. I tried the === operator, which I expected to return True in both examples.

In[]:= 2 x + 4 + 2 === 2 (x + 2 + 1)
Out[]:= False
In[]:= 2 x + 4 + 2 === x + 6 + x
Out[]:= True

I also tried == operator, which just returned the whole expression for one example and True for the other.

In[]:= 2 x + 4 + 2 == 2 (x + 2 + 1)
Out[]:= 6 + 2 x == 2 (3 + x)
In[]:= 2 x + 4 + 2 == x + 6 + x
Out[]:= True

So my question is there another operator/function that determines whether or not two expressions are equivalent.

$\endgroup$
9
  • 1
    $\begingroup$ Try Simplify[2 x + 4 + 2 == 2 (x + 2 + 1)]. Do not use ===; that's for structural identity. Your two expressions are identical in meaning, but not in structure, if that makes sense to you. $\endgroup$
    – MarcoB
    Commented Jun 2, 2020 at 18:59
  • $\begingroup$ Sadly Simplify doesn't always fit my needs. For example: Simplify[8/15 x Sqrt[x Sqrt[x^(3/2)]]] and Simplify[x^(15/8)/(15/8)] do not return the same result although the expressions are equivalent. $\endgroup$ Commented Jun 2, 2020 at 19:14
  • $\begingroup$ PossibleZeroQ can be useful for this. $\endgroup$ Commented Jun 2, 2020 at 19:19
  • $\begingroup$ @NejcJezersek No they are not for some complex values. You can find a counterexample: FindInstance[8/15 x Sqrt[x Sqrt[x^(3/2)]] != x^(15/8)/(15/8), x] which returns {{x -> -(28/5) + (79 I)/5}} as an example of a value for which the two expressions are different. Alternatively, you can Simplify the difference of the two and see if it reduces to $0$. $\endgroup$
    – MarcoB
    Commented Jun 2, 2020 at 19:21
  • 1
    $\begingroup$ @MarcoB thank you; FindInstance[ 8/15 x Sqrt[x Sqrt[x^(3/2)]] != x^(15/7)/(15/8), x, Reals] with domain specified does the job for me. $\endgroup$ Commented Jun 2, 2020 at 19:35

2 Answers 2

2
$\begingroup$

A predicate command returns either True or False. Always. The Equal command (==) is not a predicate in that sense. In many cases, Equal returns neither, instead Equal just returns the entire expression without change. Mathematica does not know if they are equal.

The SameQ command is a predicate. It returns True if both arguments are structurally identical (using some simple algrebra too), otherwise False. Always. But there are some expressions that are mathematically equal, but not structurally equal. And it can be proven that it is not always possible to determine this in all cases.

PossibleZeroQ is also a predicate dealing essentially with (lhs-rhs). I'm not sure, but I think it works by substituting random numbers (perhaps complex numbers) for all the symbols. Multiple times. If ~0 is obtained during all these experiments, it returns True. PossibleZeroQ is a very well-named command.

$\endgroup$
0
$\begingroup$

Tricky it is, for example, see the following somewhat inconsistent results. enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.