# Choice of variables name affects the behavior of expression evaluation

I tried to expand this polynomials. With x, it is increasing with the right order, however with b it is in a random order. The only difference is that I change b to x and it works.

Expand[FullSimplify[a00*1 + a01*0 + a02/12 + a10*0 + a11*(1/(12 k) - b/(2 k) + b^2/(2 k)) + a12*(b/(6 k) - b^2/(2 k) + b^3/(3 k)) + a20/12 +    a21*(-(b/(6 k^2)) + b^2/(2 k^2) - b^3/(3 k^2)) + a22 (1/144 + 1/(180 k^2) - b^2/(6 k^2) + b^3/(3 k^2) - b^4/(6 k^2))], b]

1/12 (12 a00 + a02 + a20) + a22/144 + a22/(180 k^2) - (a21 b)/( 6 k^2) + (a21 b^2)/(2 k^2) - (a22 b^2)/(6 k^2) - (a21 b^3)/( 3 k^2) + (a22 b^3)/(3 k^2) - (a22 b^4)/(6 k^2) + a11/(12 k) - ( a11 b)/(2 k) + (a12 b)/(6 k) + (a11 b^2)/(2 k) - (a12 b^2)/(2 k) + ( a12 b^3)/(3 k)


Change b to x and it works.

Expand[FullSimplify[a00*1 + a01*0 + a02/12 + a10*0 + a11*(1/(12 k) - x/(2 k) + x^2/(2 k)) + a12*(x/(6 k) - x^2/(2 k) + x^3/(3 k)) + a20/12 +    a21*(-(x/(6 k^2)) + x^2/(2 k^2) - x^3/(3 k^2)) + a22 (1/144 + 1/(180 k^2) - x^2/(6 k^2) + x^3/(3 k^2) - x^4/(6 k^2))], x]

a22/144 + a22/(180 k^2) + (a11 + (12 a00 + a02 + a20) k)/(12 k) - ( a21 x)/(6 k^2) - (a11 x)/(2 k) + (a12 x)/(6 k) + (a21 x^2)/( 2 k^2) - (a22 x^2)/(6 k^2) + (a11 x^2)/(2 k) - (a12 x^2)/(2 k) - ( a21 x^3)/(3 k^2) + (a22 x^3)/(3 k^2) + (a12 x^3)/(3 k) - (a22 x^4)/( 6 k^2)


What is wrong?

• (1) (Full)Simplify uses heuristics to try to come up with something reasonable. There are no guarantees about the result. (2) Parts of expressions are auto-sorted by Mathematica, e.g. b+a is transformed to a+b. This makes it easy to detect that a+b and b+a are the same thing. But it also means that the choice of symbol names may affect the final structure, and therefore the simplification of expression. – Szabolcs Aug 21 '18 at 7:36
• (1) But the final result is always some kinds of polynomials and expand should sort the polynomials correctly. (2) The command expand is done at the last after the (Full)Simplify is finished. Thus, expand should do what the manual said to sort the polynomials with respect to b regardless what the command (Full)Simplify is or the expression is. – I don't care Aug 21 '18 at 15:59
• I think the correct result mathematica should return is regardless of the variables, a correct ascending order with respect to whatever variable is given, no matter x or b. The (Full)Simplify is a red herring here. The code should work no matter what expression is given because expand is done at the last. – I don't care Aug 21 '18 at 16:01
• Even I don't use (Full)Simplify, Expand still gives me a random order if I uses the variable name called b. – I don't care Aug 21 '18 at 16:19
• What FullSimplify does is not mathematically well defined. There is no such thing as a "correct result". – Szabolcs Aug 22 '18 at 6:03

They are the same expression, just simplified differently. You can verify by changing the b->x in the first:

(Expand[FullSimplify[
a00*1 + a01*0 + a02/12 + a10*0 +
a11*(1/(12 k) - b/(2 k) + b^2/(2 k)) +
a12*(b/(6 k) - b^2/(2 k) + b^3/(3 k)) + a20/12 +
a21*(-(b/(6 k^2)) + b^2/(2 k^2) - b^3/(3 k^2)) +
a22 (1/144 + 1/(180 k^2) - b^2/(6 k^2) + b^3/(3 k^2) -
b^4/(6 k^2))], b] //. b -> x //
FullSimplify) ==
(Expand[
FullSimplify[
a00*1 + a01*0 + a02/12 + a10*0 +
a11*(1/(12 k) - x/(2 k) + x^2/(2 k)) +
a12*(x/(6 k) - x^2/(2 k) + x^3/(3 k)) + a20/12 +
a21*(-(x/(6 k^2)) + x^2/(2 k^2) - x^3/(3 k^2)) +
a22 (1/144 + 1/(180 k^2) - x^2/(6 k^2) + x^3/(3 k^2) -
x^4/(6 k^2))], x] // FullSimplify)


which returns True

• I understand they equal. However, my question is why expand with repsect to x gives me a correct ascending order while just a change of variable from x to b make the command expand fail to sort in a correct ascending order? – I don't care Aug 21 '18 at 15:56
• What do you mean by "correct order"? There are many ways to order the elements of an equation, the process of "simplification" is a pretty complicated thing. Ordering terms is a kind of sorting... and sometimes the names used can make a difference in the output. – bill s Aug 21 '18 at 18:24