Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Re: Changing the variable order... & Rearranging the Terms of a...
Replies: 0

 Alexei Boulbitch Posts: 483 Registered: 2/28/08
Re: Changing the variable order... & Rearranging the Terms of a...
Posted: Jul 2, 2013 12:31 AM

Hi, I have another formatting question.

I have a polynomial as the output of an expression, and I would like to rearrange the terms of the polynomial in the output. The current output I am getting is x1*x3-\[Alpha]2*x2-x3. However, I wish the output to read x1*x3- x3 - \[Alpha]2*x2, that is, rearrange the second and third terms. Does anyone have an idea of how I might be able to do this for this polynomial or any polynomial in general? Thanks.

Hi,

I'm trying to change the order of arguments in the output of commutative
operators such as
Plus and Times.

For example the input
y+x+z
always gives the output
x+y+z

Is there a way to specify a user defined order on these variables
to get the output
y+x+z

Hi, Dave, hi, Kevin.

Your questions address a difficult point of Mathematica that was being discussed here since long time with no satisfactory result.

I would like to add few things to a very nice answer of David Bailey here: http://forums.wolfram.com/mathgroup/archive/2013/Jun/msg00327.html.

Rather recently David Park offered a solution to this question and published it here. Unfortunately I did not save the reference to this post, but since it has been published on this site, I think I it is eligible to repeat it here.
So here is the solution of David Park:

HoldOrderForm::usage =
"HoldOrderForm[permutation][expr] will reorder the terms or factors \
\ of a Plus or Times expression according to the permutation and put \
\ the result in a HoldForm.";

SyntaxInformation[HoldOrderForm] = {"ArgumentsPattern" -> {_}};

HoldOrderForm[permutation_?PermutationListQ][(f : Plus | Times)[
args__]] /;
Length[{args}] ==
Length[permutation] := (HoldForm @@ {(List @@ f[args])[[
permutation]]}) /. List -> f

Attention 1: it NOT the permutation of the expression as it is initially written down, but the one returned by Mathematica
Attention 2: the final expression is held, and will not correctly take part in operations. In order to operate, the ReleaseHold should be first applied to the result.

With this function one has:

expr1 = x + y + z;
HoldOrderForm[{2, 1, 3}][expr1]

\!\(
TagBox[
RowBox[{"y", "+", "x", "+", "z"}],
HoldForm]\)

expr2 = x1*x3 - \[Alpha]2*x2 - x3;

HoldOrderForm[{2, 3, 1}][expr2]

Try it in Mathematica. The complex from of the output that you see here (after the first expression) is just due to the use of the HoldForm inside the function. Im Mathematica, however, the expression will look just like one expects.

In general, as it has been commented by David Bailey, the use of Hold or HoldForm is inevitable. This means that one may rearrange the terms in a desired order, but typically cannot preserve this order in further analytic calculations, if any. In such a case one should use ReleaseHold[ ] function, and order will be gone. It is just since the desired order is only kept until Hold or HoldForm is there.

Nevertheless, sometimes it is very important to have terms rearranged, even against the price (penalty?) of having HoldForm in your expression.

If so, one may sometimes do it in a very simple way by using rule-based rearrangements. Try this

HoldForm[x + y + z] /. {x -> y, y -> x}

And this:

HoldForm[x1*x3 - \[Alpha]2*x2 - x3] /. {\[Alpha]2 -> A, x2 -> B} /.
x1 -> \[Alpha]2 /. -x3 -> x1*x2 /. A*B -> x3

Have fun,
Alexei

Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.boulbitch@iee.lu