MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at
  • Subject: [mg131376] Re: Changing the variable order... & Rearranging the Terms of a...
  • From: Alexei Boulbitch <Alexei.Boulbitch at>
  • Date: Tue, 2 Jul 2013 00:46:02 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:

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.


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

For example the input
always gives the output

Is there a way to specify a user defined order on these variables
to get the output
instead ?

Thanks in advance,
Dave Snead

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:

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]

RowBox[{"y", "+", "x", "+", "z"}],

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 BOULBITCH, Dr., habil.
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 at

  • Prev by Date: problem with elementary transformation for matrix
  • Next by Date: where can I find CartesianMap code
  • Previous by thread: problem with elementary transformation for matrix
  • Next by thread: where can I find CartesianMap code