6

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
corey979
  • 23,947
  • 7
  • 58
  • 101
Nick Murphy
  • 189
  • 4

4 Answers4

10
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

corey979
  • 23,947
  • 7
  • 58
  • 101
6
Tr[Select[MonomialList[poly],Tr[Exponent[#,{x,y}]]==3&]]

a x^2 y-c x y^2

yode
  • 26,686
  • 4
  • 62
  • 167
4

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)
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
3

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 *)
Daniel Lichtblau
  • 58,970
  • 2
  • 101
  • 199