Group Abstract Group Abstract

Message Boards Message Boards

Order of terms after Expand[]

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

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