Group Abstract Group Abstract

Message Boards Message Boards

Order of terms after Expand[]

Posted 11 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 11 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

Fixed. Forgot about the z^0 case. Again, this is just proof of concept. The parsing can be done better and in a much more robust way. Any other bugs, please let me know.

POSTED BY: Nasser M. Abbasi
Posted 11 years ago

Let's assume the binomial expansion will never change the order of terms. Then neither will this. (A constant propagates to the first for even power)

Expand[(z + y)^5];
% /. y -> 1/z

1/z^5 + 5/z^3 + 10/z + 10 z + 5 z^3 + z^5

Not trusting that, you could write your own expand[].

POSTED BY: Douglas Kubler
Posted 11 years ago

As I explained in my previous comments, assuming a behaviour is not good enough for me. But even with this assumption, in

Expand[(y + z)^4] 

and

Expand[(y + z)^4] /. y -> 1/z

the order is changing with the replacement. The binomial expansion order is not kept. Mathematica recognizes that the result is of a special form, and does undocumented (or at least I did not find the relevant documentation) reordering for the output after the replacement. Again, this is not a problem for computational use of Mathematica, rather it is a strength that it recognizes so many forms of expressions, but it would be nice to know a way of forcing it not to do the reordering in this case after the replacement.

Yes, of course I can write my own algorithms, which is what I ended up doing in this case and also in several other cases (e.g. I am drawing circular arcs using ParametricPlot). I now use

CoefficientList[(z + 1/z)^4*z^4, z]

because it is well documented, and I can get all the information I need from the output.

POSTED BY: Ferenc Beleznay

You can (re)order in formatting. I do not recommend any other approach, as that would entail messing with attributes of Plus. Far safer to use formatting magic.

The ordering used by Plus, which is what you are seeing, is unlikely to change. Whether it does what you want or expect, in all cases, will depend heavily on the specifics (of what you want or expect). In the special case of a Laurent polynomial in one variable, and with explicit numbers as coefficients (that is, atoms that are NumberQ), the ordering will be by degree in that variable.

POSTED BY: Daniel Lichtblau
Posted 11 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
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 11 years ago
POSTED BY: Ferenc Beleznay
Posted 11 years ago
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