You can collect the coefficient of x^i*y^j by ratcoeff(r, x^i*y^j);
This works as long as i,j > 0. if you want the coefficient of (say)
x^0*y^0, I suggest a special case where you just substitute x=0,y=0.
You don't say how large the polynomial is, or how large n is, or whether n
is fixed, etc. These may all affect the program design.
Here's another decomposition of the problem.
Example list_of_vars: [x1,x2,x3]
To pick out all terms of total degree exactly k in list_of_vars: Recursively
speaking, these would include:
All terms of degree 0 in x1, and from these, total degree k in x2, x3 ...
All terms of degree 1 in x1, and from these, total degree k-1 in x2, x3
...
...
All terms of degree n in x1, and total degree 0 in x2, x3, ...
If there is only one variable, say x, then ...
To pick out the terms of r which are of degree 0 in x, do subst(0,x,r).
To pick out the terms of r which are of degree d>0 in x, do ratcoeff(r, x,
d).
This