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 [Maple Math] has occurred previously, even as a sub-expression (e.g. in [Maple Math] ), and you enter c*b+a , Maple will rewrite it as [Maple Math] .

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]);

[Maple Math]

This is in total-degree order, so [Maple Math] , which is of order 3 in [Maple Math] and [Maple Math] , comes before [Maple Math] which is of order 2 in those variables.

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

[Maple Math]

In pure lexicographic order in [b,c] , [Maple Math] comes before [Maple Math] 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 [Maple Math] rather than [Maple Math] .

However, suppose you have the expression
[Maple Math] and wish to change it to [Maple Math] . 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;

[Maple Math]

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

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

[Maple Math]

> c:= newc;

[Maple Math]

> p;

[Maple Math]

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);

[Maple Math]

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

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

[Maple Math]

> subs(c=newc,");

[Maple Math]

> convert(",`+`);

[Maple Math]

> c:= newc;

[Maple Math]

> p;

[Maple Math]

See also: sort , `tools/gensym`

Maple Advisor Database R. Israel, 1997