3
$\begingroup$

I am new to Mathematica. So I have read the documentation for comparing expressions in Mathematica and I still don't get the SameQ (===) function. Let me give you a short example:

2+2 === 4
True

Log[x*x] === 2*Log[x]
False

I hope I'm not seeing something here. Help is much appreciated!

$\endgroup$
0

1 Answer 1

12
$\begingroup$

Every expression in Mathematica is characterized by it's FullForm.

So in your case particularly

FullForm[Log[x*x]]===Log[Power[x,2]]

and

FullForm[2*Log[x]]===Times[2,Log[x]]

These two, although equivalent from a Mathematical point of view (but only if x>0) they have different FullForm representations.

So SameQ(===) checks if the expression trees are the same.

In case more conditions are induced (like x>0) and simplifications take place then you will certainly end up to same expressions.

Assuming[x > 0, Simplify[Log[x*x]]]===2*Log[x]

returns True

$\endgroup$
4
  • 1
    $\begingroup$ +1 Note that for approximate numbers, there can be difference between numbers that are considered the same. Compare 1. + $MachineEpsilon === 1. and FullForm /@ {1. + $MachineEpsilon, 1.}. The tolerance is controlled by Internal`$SameQTolerance. Also, as you almost point out, for x == -1, the identity fails: {Log[(-1)^2], 2 Log[-1]} equals {0, 2 I Pi}. $\endgroup$
    – Michael E2
    Jan 26, 2015 at 13:46
  • $\begingroup$ True about the approximate numbers! I think it is better to avoid those when checking with SameQ. True also about the complex solutions - We could also say that x is an object different than the usual where also the identity holds but this would be going to far :-) $\endgroup$
    – tchronis
    Jan 26, 2015 at 14:22
  • $\begingroup$ But what is the problem in this case: 1/(a)^(3/4) === (1/a)^(3/4) returns False $\endgroup$
    – drabus
    Jun 15, 2017 at 10:22
  • $\begingroup$ @drabus 1/(a)^(3/4) === Simplify[(1/a)^(3/4), a > 0] $\endgroup$
    – tchronis
    Jul 19, 2017 at 19:36

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.