5
$\begingroup$

I am new to Mathematica and I have what I am sure is a basic question, which I unfortunately have not been able to figure out. I am trying to keep terms in a polynomial that are of the same degree only. For example, if I have a polynomial in the variables x and y like the following

poly = ax^2*y - bx*y - cx*y^2 + dx + ey

what could I do to extract, for example, only the terms of third order, i.e the terms

ax^2*y-cx*y^2
$\endgroup$
  • 1
    $\begingroup$ Note that ax is a distinct entity from a x; if you want to multiply different variables, separate them with a space or explicitly use an asterisk. $\endgroup$ – J. M. will be back soon Dec 13 '16 at 14:29
  • $\begingroup$ You can extract the coefficients with CoefficientList and then take out the elements you want, e.g. by using IntegerPartitions but I'm sure there are better methods. $\endgroup$ – AndreasP Dec 13 '16 at 14:33
  • $\begingroup$ Related: (126553); (41918). $\endgroup$ – corey979 Dec 13 '16 at 14:41
7
$\begingroup$
poly = a x^2*y - b x*y - c x*y^2 + d x + e y;
var = {x, y};

FromCoefficientRules[Select[CoefficientRules[poly, var], Total@#[[1]] == 3 &], var]

a x^2 y - c x y^2

$\endgroup$
  • $\begingroup$ Great, thank you so much! $\endgroup$ – Nick Murphy Dec 13 '16 at 14:54
4
$\begingroup$
Tr[Select[MonomialList[poly],Tr[Exponent[#,{x,y}]]==3&]]

a x^2 y-c x y^2

$\endgroup$
3
$\begingroup$

Another route:

d = 3;
poly = a x^2*y - b x*y - c x*y^2 + d x + e y;
var = {x, y};

Fold[Dot, CoefficientArrays[poly, var][[d + 1]], ConstantArray[var, d]]
   x (a x y - c y^2)
$\endgroup$
2
$\begingroup$

Easiest is to use a new variable to collect powers in the ones of interest, then set it to 1.

poly = a*x^2*y - b*x*y - c*x*y^2 + d*x + e*y;
vars = {x, y};
Coefficient[poly /. Thread[vars -> t*vars], t^3] /. t -> 1

(* Out[1086]= a x^2 y - c x y^2 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.