Situation
I use Last to extract the last element of an expression. Let poly=a+b*c+Log[a]+z.
Problem
When Using Last@poly, I expect the output is z. However, the command gave me Log[a].
Debug
With FullForm@poly, I found the output is Plus[a,Times[b,c],z,Log[a]]. So, Log[a] should be the correct answer of the command, but not my expected answer.
My Question
The strange behavior confuse me.
- I wonder whether elements of an
expressionin mathematica have definite order. - If the answer is "no", is there any alternative other than
Lastto avoid the undetermined behavior, so that I can extract particular element ?
Plus[]is commutative, so it has theOrderlessattribute, and sorts its arguments into a canonical order. Anyway:Defer[a + b c + Log[a] + z][[1, -1]]. Not recommended, though. – J. M.'s missing motivation Nov 07 '15 at 09:54poly = .... The last element is indeedLog[a]because Mathematica sorts the terms ofPlus. Why? Check here: http://reference.wolfram.com/language/ref/Orderless.html http://reference.wolfram.com/language/tutorial/FlatAndOrderlessFunctions.html – Szabolcs Nov 07 '15 at 09:55