4
$\begingroup$

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?

$\endgroup$
4
  • $\begingroup$ It's the ordering,of course-. Try Sort[t] == Sort[s] $\endgroup$ Commented Sep 25, 2015 at 3:00
  • $\begingroup$ @belisarius Sort::normal: Nonatomic expression expected at position 1 in Sort[t] $\endgroup$
    – bzm3r
    Commented Sep 25, 2015 at 3:18
  • $\begingroup$ Well, it works here. Please restart your Mathematica and try again. Just in case, I'm also using v9 $\endgroup$ Commented Sep 25, 2015 at 3:35
  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Sep 25, 2015 at 7:21

1 Answer 1

4
$\begingroup$

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

$\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.