# Why does simplification in Mathematica depend on variable names

Mathematica fails to put the following into an obvious simplest form:

   numerator = C2p D2 (C1y N1x - C1x N1y - C1y N2x + N1y N2x + C1x N2y - N1x N2y) +
C1p D1 (-(C2y N1x) + C2x N1y + C2y N2x - N1y N2x - C2x N2y + N1x N2y)

numerator = Simplify[numerator]


produces

C2p D2 (-(C1x N1y) + C1y (N1x - N2x) + N1y N2x + C1x N2y - N1x N2y) +
C1p D1 (C2x N1y - N1y N2x + C2y (-N1x + N2x) - C2x N2y + N1x N2y)


which has obvious simplification failures

If I change the name of a variable it succeeds.

numerator = ReplaceAll[numerator,{N2y -> aN2y}]
numerator = Simplify[numerator]

C2p D2 (aN2y (C1x - N1x) + C1y (N1x - N2x) + N1y (-C1x + N2x)) +
C1p D1 (aN2y (-C2x + N1x) + N1y (C2x - N2x) + C2y (-N1x + N2x))

numerator = ReplaceAll[numerator,{aN2y -> N2y}]

C2p D2 (C1y (N1x - N2x) + N1y (-C1x + N2x) + (C1x - N1x) N2y) +
C1p D1 (N1y (C2x - N2x) + C2y (-N1x + N2x) + (-C2x + N1x) N2y)


Once one knows that success depends on the variable names, possibly an alphabetic sort order in the sum somewhere, one knows what to do in order to simplify, but I am concerned about much bigger expressions where you don't know that you need to trick it. Is there any way to get Mathematica to get past this variable name dependence? FullSimplify does not help here.

• If you do SetOptions[Simplify, ComplexityFunction -> LeafCount] the results are indepent or variable name change. So it has to do with the "Automatic" setting of ComplexityFunction. However, I don't know what Automatic means here. And no, the SimplifyCount function mentioned in ref/ComplexityFunction (advertised as "The automatic complexity function") is obviously not the Automatic setting of of Simplify, since doing SetOptions[Simplify, SimplifyCount] behaves differently from the default setting. This is one example where the documentation could be improved. Unexplained Automatic's are bad – Rolf Mertig Mar 25 '13 at 20:42
• I attempted an explanation of this phenomenon in this related post – Jens Mar 25 '13 at 21:07
• – Mr.Wizard Mar 29 '13 at 1:10