If we sort f then g is also sorted (that's unexpected, maybe it's because the same expression in the memory?):
restart;
f:=b*x^3*y + x*y^2 + a*x^3*y;
sort(f,[x,y,a,b],plex);
g:=b*x^3*y + x*y^2 + a*x^3*y;
If we sort a different polynomial then g is still sorted (probably because it has the same term a*x^3*y
), but now g is sorted differently than before:
restart;
f:=b*x^3*y + x*y^2 + a*x^3*y;
sort(x*y^3 + a*x^3*y,[x,y,a,b],plex);
g:=b*x^3*y + x*y^2 + a*x^3*y;
Below g is not sorted even when the sorted polynomial has the same term x*y^2
:
restart;
f:=b*x^3*y + x*y^2 + a*x^3*y;
sort(x*y^2 + a*x,[x,y,a,b],plex);
g:=b*x^3*y + x*y^2 + a*x^3*y;
What is the reason for this behaviour?
Yes, it's the same expression in memory. Maple tries not to store multiple copies of a polynomial. Sorting a polynomial changes it in place. So in your first example, sorting f changes the way it is stored. When Maple then creates g, it recognizes it as the same polynomial and g points to the same memory location as the (now sorted) f. You can see this by noting that
addressof(f);
addressof(g);
return the same address. If you add
dismantle(f)
before and after sorting f, you see that Maple changed from an efficient default (poly) method for storing polynomials, to a sum-of-products form to accommodate the required sorted order.
In the second example, f and g are again stored in the same location in poly form, since neither were sorted. The sorting did seem to influence the display of g, as you noted, presumably because of the a*x^3*y term.
The third example is the same as the second, but since the term x*y^2 was not changed in the sort, the display of g was not changed.