Group Abstract Group Abstract

Message Boards Message Boards

Order of terms after Expand[]

Posted 12 years ago

I am trying to collect specific terms in the expansion of (z+1/z)^n for different exponents (in a Manipulate environment). I noticed, that applying

Expand[(z + 1/z)^5]

the output is in increasing order of z. Can I rely on this behaviour? I did not find it anywhere documented that Expand[] orders the output in any way.

Is there a way of sorting the output? Unfortunately, both

Expand[(z + 1/z)^4]

and

Sort[Expand[(z + 1/z)^4]]

puts the constant term in front and only after that the negative and then positive powers of z.

POSTED BY: Ferenc Beleznay
13 Replies

I think also the safest way is to format it at the end for displaying. Here is a quick function hacked to do this. If you find any bugs, please let me know. This is just for final display.

   formatIt[Expand[(z + 1/z)^5], z]
   formatIt[Normal@Series[Sin[x], {x, 0, 10}], x]
   formatIt[Expand[(z + 1/z)^4], z]
   formatIt[1, z]
   formatIt[-1 + z^3, z]
   formatIt[Expand[(z + 1/z)^6, z], z]

Which gives

enter image description here

ps. I am not too good in parsing and patterns with Mathematica, and I am sure this can be done much better, but just a proof of concept.

  formatIt[r_, z_] := Module[{x, e, c, n, o},
     (*nma, version 8/23/14, 2 PM*)
     fixMiddle[c_] := If[Sign[c] < 0, If[Abs[c] == 1, Row[{"-", Spacer[1]}], c], Row[{"+", Spacer[1], If[c != 1, c]}]];
     fixFirst[c_] := If[Sign[c] < 0, If[Abs[c] == 1, Row[{"-", Spacer[1]}], c], If[c == 1, Sequence @@ {}, c]];
     o = If[Head[r] === Plus, r, List@r]; (*to handle single terms*)
     o = Cases[lis, Alternatives[x_. Power[z, e_.], Times[x_, Power[z, e_.]], Dot[x_, e_: 0]] :> {x, e}];
     o = Sort[o, #1[[2]] > #2[[2]] &];
     o = {#[[1]], If[#[[2]] < 0, Superscript[z, #[[2]]], If[#[[2]] > 0, z^#[[2]], z^"0"]]} & /@ o;
     o = MapIndexed[{n = First[#2]; c = #1[[1]]; e = #1[[2]]; 
         Row[{If[n == 1, fixFirst[c], fixMiddle[c]], Spacer[2], e, Spacer[1]}]} &, o];
     Row[Flatten[o]]
     ];
POSTED BY: Nasser M. Abbasi
Posted 12 years ago

Thanks for this. I tried to understand the code, but unfortunately I am new to Mathematica, I need more time. But I noticed, that when I run it on

formatIt[Expand[(z + 1/z)^4], z]

the constant term is missing from the output.

POSTED BY: Ferenc Beleznay
POSTED BY: Nasser M. Abbasi
Posted 12 years ago
POSTED BY: Douglas Kubler
Posted 12 years ago
POSTED BY: Ferenc Beleznay
POSTED BY: Daniel Lichtblau
Posted 12 years ago
POSTED BY: Ferenc Beleznay

"Orderless" corresponds to commutative nature of an (appropriate) operation like "Plus", therefore (as a Mathematica 7 user) I'm thinking it will stay orderless.

POSTED BY: Isaac Abraham

Polynomials are always output in the increasing order of powers. If you wish to see the highest power to the left (as we might do when handwriting), one might try the following.

ClearAttributes[Plus, Orderless](*Remove the "Orderless" attribute of Plus*);
(*Apply the orderless*)Plus @@(List @@ (*Change head from Plus to List*)Expand[(z + 1/z)^5])

By removing the builtin "Orderless", we tell the Plus sign that it must keep the user provided order.

POSTED BY: Isaac Abraham

I would not recommend changing build-in function attribute as that can cause some subtle problems somewhere else. If one wants just the order changed for display purposes, a known simple solution is to use TraditionalForm as mentioned in the linked post above

       Expand[(z + 1/z)^5] // TraditionalForm

enter image description here

POSTED BY: Nasser M. Abbasi
Posted 12 years ago

I don't want the order changed for display purposes. I want to be sure, that what I get as an output now, it will be the same in future versions of Mathematica. Apparently Mathematica uses some algorithms to order expressions, but these are hidden from the user.

POSTED BY: Ferenc Beleznay
Posted 12 years ago

Thanks, but this link did not answer my question. The "OrderedForm" operation there did not put the terms of (z+1/z)^4 in increasing powers of z. However, my main question is: Can I rely on the behaviour I see now to stay like this in future versions of Mathematica? In the documentation I do not see any reference as to how Expand[] orders the output, and in this case it is not the normal order of the binomial expansion. I am writing demonstrations to use by others, so I cannot rely on experimenting with the output. I need a guarantee that it will always be the same. Mathematica is very powerful, but because of this, calculations happen behind the scene, and although the output is nice, I don't see how I can control it.

POSTED BY: Ferenc Beleznay
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard