Advice: Reordering an expression

Normally, once an expression has been created in a Maple session, it is always written in the same order. Thus if a+b*c has occurred previously, even as a sub-expression (e.g. in (a+b*c)*d ), and you enter c*b+a , Maple will rewrite it as a+b*c .

You can use
sort to change the order of an expression. The first argument to sort is the expression to sort, and the second is a list of variables or functions. A third argument plex causes the sorting to be in pure lexicographic order in the members of the list, rather than decreasing total degree in the members of the list.

After being sorted, the expression stays in the sorted order.

> sort(a+b*c+c^3*d, [b,c]);

d*c^3+b*c+a

This is in total-degree order, so d*c^3 , which is of order 3 in b and c , comes before b*c which is of order 2 in those variables.

> sort(a+b*c+c^3*d, [b,c],plex);

b*c+d*c^3+a

In pure lexicographic order in [b,c] , b*c comes before d*c^3 because b has first priority. In a sum, terms containing no members of the list come last. In a product, however, factors containing no members of the list come first. Thus we obtained d*c^3 rather than c^3*d .

However, suppose you have the expression
c^3+c^2+c+1 and wish to change it to 1+c+c^2+c^3 . There is no way to use sort to do this. You would have to create the expression initially as 1 + c + c^2 + c^3 . But there is a trick you can use: obtain a new variable that looks like c but is not the same for Maple, and create the expression using that new variable. Then assign the new variable as a value to c .

> p:= c^3 + c^2 + c + 1;

p := c^3+c^2+c+1

> newc:= `tools/gensym`(c):

> 1 + newc + newc^2 + newc^3;

1+c+c^2+c^3

> c:= newc;

c := c

> p;

1+c+c^2+c^3

In a more complicated case, to create the new version of the expression you could first convert the expression to a list, permute the elements as desired, substitute newc for c , and then convert back to a sum.

> plist:= convert(p,list);

plist := [1, c, c^2, c^3]

> newc:= `tools/gensym`(c):

> [op(plist[2..4]), plist[1]];

[c, c^2, c^3, 1]

> subs(c=newc,%);

[c, c^2, c^3, 1]

> convert(%,`+`);

c+c^2+c^3+1

> c:= newc;

c := c

> p;

c+c^2+c^3+1

See also: sort , `tools/gensym`

Maple Advisor Database R. Israel, 1997