SameQ

lhs===rhs

yields True if the expression lhs is identical to rhs, and yields False otherwise.

Details

  • SameQ requires exact correspondence between expressions, except that it still considers Real numbers equal if they differ in their last binary digit.
  • 2===2. gives False.
  • e1===e2===e3 gives True if all the are identical.

Background & Context

  • SameQ[expr1,expr2] returns True if expr1 is identical to expr2 and otherwise returns False. Here, "identical" means there is exact correspondence between the underlying FullForm representations of expressions expr1 and expr2, with the exception that real numbers are considered SameQ if they differ only in their last binary digit. SameQ[expr1,expr2] may be input using triple equal signs as expr1===expr2. The multiple-argument form SameQ[expr1,expr2,], which may also be input as expr1===expr2===, returns True if and only if all expressions expri are identical.
  • Different input forms of expressions may be SameQ if their underlying representations are identical, for example n! ===Factorial[n] returns True. On the other hand, SameQ differentiates between representations of numbers that are numerically equal but that do not have identical representations. For example, SameQ[1,1.] and SameQ[1.,1.+0.I] both return False. This behavior is expressly different from that exhibited by Equal, which performs equality testing and remains unevaluated in cases that cannot be resolved.
  • SameQ considers only literal correspondence, not isomorphism. In the case of graphs, IsomorphicGraphQ should be used to check sameness up to isomorphism. (Alternately, SameQ may be used after first converting graphs to canonical form using CanonicalGraph.)
  • SameQ is related to a number of other symbols. Set[expr1,expr2] (which may be input using the "single equals" syntax expr1=expr2) evaluates expr2 and assigns the result to be the value of expr1, while Equal[expr1,expr2] (which may be input using the "double equals" syntax expr1==expr2) returns True if expr1 and expr2 are numerically equal. UnsameQ (which may be input as =!=) is the converse of SameQ.
  • PossibleZeroQ can be used to indicate if a given expression has value in some cases where SameQ returns False. For example, SameQ[Erf[Log[4]+2Log[Sin[Pi/8]]]-Erf[Log[2-Sqrt[2]]],0] returns False, whereas calling PossibleZeroQ on its first argument returns True (together with an informative message indicating that a zero value could not be rigorously established). Symbolic simplifiers like Simplify, FullSimplify and RootReduce can sometimes also be used to rigorously establish equality (including in the example just given) when SameQ and Equal cannot.

Examples

open all close all

Basic Examples  (2)

Test whether two expressions are identically the same:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

== remains symbolic unless literal values are given:

In[3]:=
Click for copyable input
Out[3]=

Full form:

In[1]:=
Click for copyable input
Out[1]=

Scope  (4)

Applications  (1)

Introduced in 1988
(1.0)