Is there an elegant way to "partially" match monomials in a polynomial expression?
Let me explain what I mean by "partially" with an example. Suppose I have
expr = x^2*y + x*y^2 + x*y^3
and I would like to use pattern matching to write a rule that replaces x*y^2 -> z
. Of course, taken literally, this pattern only matches the second term of expr
, but what I would like to achieve is that the result of the replacement is x^2*y + z + y*z
, i.e. if the monomial expression on the LHS of the rule appears (with at least the powers given on the LHS) this monomial expression should be replaced by the RHS.
I know about optional patterns and conditions, i.e. something like
expr /. x^a_.*y^b_. /; a >= 1 && b >= 2 :> z*x^(a-1)*y^(b-2)
does what I want, but it feels rather clumsy and fragile.
Is there a more elegant way to do this?
In[1192]:= PolynomialReduce[x^2*y + x*y^2 + x*y^3, x*y^2 - z, {x, y, z}][[2]] Out[1192]= x^2 y + z + y z
$\endgroup$PolynomialReduce
, I think this is exactly what I was looking for. $\endgroup$