The collect command groups the terms of a polynomial A by powers in a variable X.
f = x^2*y + x*z + x + 1; collect(f,x); 1+x*(1+z)+y*x^2