3

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!

drabus
  • 427
  • 3
  • 9

1 Answers1

12

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

tchronis
  • 2,445
  • 1
  • 14
  • 26
  • 1
    +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}. – Michael E2 Jan 26 '15 at 13:46
  • 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 :-) – tchronis Jan 26 '15 at 14:22
  • But what is the problem in this case: 1/(a)^(3/4) === (1/a)^(3/4) returns False – drabus Jun 15 '17 at 10:22
  • @drabus 1/(a)^(3/4) === Simplify[(1/a)^(3/4), a > 0] – tchronis Jul 19 '17 at 19:36