4

SameQ checks to see if symbolic expressions are the same. There are some caveats, as pointed out by the answers to:

Consider the following two expressions:

s = ((ax-bx)^2+(ay-by)^2) Abs[ay bx-ax by-ay px+by px+ax py-bx py] > 0 ||
    ((ax-bx)^2+(ay-by)^2) ((ax-bx) (bx-px)+(ay-by) (by-py)) > 0 ||
    ((ax-bx)^2+(ay-by)^2) (ax^2+ay^2+bx px-ax (bx+px)+by py-ay (by+py)) < 0

t = ((ax-bx)^2+(ay-by)^2) Abs[ay bx-ax by-ay px+by px+ax py-bx py] > 0 ||
    ((ax-bx)^2+(ay-by)^2) (ax^2+ay^2+bx px-ax (bx+px)+by py-ay (by+py)) < 0 ||
    ((ax-bx)^2+(ay-by)^2) ((ax-bx) (bx-px)+(ay-by) (by-py)) > 0

On Mathematica 9:

In[47]:= s == t
Out[47]= (((ax-bx)^2+(ay-by)^2) Abs[ay bx-ax by-ay px+by px+ax py-bx py] > 0 ||
((ax-bx)^2+(ay-by)^2) ((ax-bx) (bx-px)+(ay-by) (by-py)) > 0 || 
((ax-bx)^2+(ay-by)^2) (ax^2+ay^2+bx px-ax (bx+px)+by py-ay (by+py)) < 0) == 
(((ax-bx)^2+(ay-by)^2) Abs[ay bx-ax by-ay px+by px+ax py-bx py] > 0 || 
((ax-bx)^2+(ay-by)^2) (ax^2+ay^2+bx px-ax (bx+px)+by py-ay (by+py)) < 0 ||
((ax-bx)^2+(ay-by)^2) ((ax-bx) (bx-px)+(ay-by) (by-py)) > 0)

In[48]:= s === t
Out[48]= False

In[49]:= Assuming[{ax, bx, px, ay, by, py} \[Element] Reals, s === t]
Out[49]= False

In[50]:= Assuming[{ax, bx, px, ay, by, py} \[Element] Reals, FullSimplify[s === t]]
Out[50]= False

Can I get Mathematica to identify where it thinks the statements are different?

bzm3r
  • 175
  • 7
  • It's the ordering,of course-. Try Sort[t] == Sort[s] – Dr. belisarius Sep 25 '15 at 03:00
  • @belisarius Sort::normal: Nonatomic expression expected at position 1 in Sort[t] – bzm3r Sep 25 '15 at 03:18
  • Well, it works here. Please restart your Mathematica and try again. Just in case, I'm also using v9 – Dr. belisarius Sep 25 '15 at 03:35
  • Are you sure you want SameQ and not Equal? SameQ tests for structural equality of two data structures, and has nothing to do with mathematics. Assuming, Simplify, etc. have absolutely no effect on it because those functions are for symbolic mathematics. SameQ is purely for programming. If two things don't look exactly the same according to FullForm, then they are different for SameQ. – Szabolcs Sep 25 '15 at 07:21

1 Answers1

4

Sorry it's kind of a mess (I'm a little bit sleep-deprived at the moment), but here's something that might work for you:

Module[{cf = ColorData[97], $color = 1},
style["", c_] := Style[Overscript[Overscript["", \[OverBracket]], c], {Darker@cf[c], Bold}];
style[str_, c_] := Style[Overscript[Overscript[str, \[OverBracket]], c], {Darker@cf[c], Bold}];
join[{Row[{}], Row[{}]}, next_String] := ($color = 1;{Row[{next}], Row[{next}]});
    join[{Row[{}], Row[{}]}, {next1_String, next2_String}] := ($color = 1;{Row[{next1}], Row[{next2}]});
join[{s1_Row, s2_Row}, next_String] := {Row[{s1, next}], Row[{s2, next}]};
join[{s1_Row, s2_Row}, {next1_String, next2_String}] := With[{c = $color++}, {Row[{s1, style[next1, c]}], Row[{s2, style[next2, c]}]}];
    highlightDifference[s_, t_] := Column[Fold[join, {Row[{}], Row[{}]}, SequenceAlignment[ToString[s, InputForm], ToString[t, InputForm]]]];
]

enter image description here

rhennigan
  • 1,783
  • 10
  • 19