Message Boards Message Boards

Order of terms after Expand[]

Posted 10 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
POSTED BY: Nasser M. Abbasi
Posted 10 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 10 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 10 years ago
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 10 years ago

I am trying to write CDF notes with demonstrations. If something is "unlikely to change" or (as in the documentation of Sort[]) "usually ..." is not good enough. This is not a problem with usual use of Mathematica, because one can react if something is not as we would expect. But CDF is a different thing. I am producing something which I have to be sure behaves as I intend it to behave 2-3 years from now. I can play with formatting, but the question remains, will future versions format it the same way? In this case, this is a Laurent polynomial in one variable with numbers as coefficients, but the order is not according to the degrees in that variable. The constant term is first, but it should be in the middle. I think, it is because what is written in the Sort[] documentation, that least complex terms are first, but still there is this "usually" in the documentation, which I don't understand. In any case, since my post I could sort out what I want with the use of CoefficientList[], where the documentation is quite explicit about the order.

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 10 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 10 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

Group Abstract Group Abstract