Ask Your Question

Revision history [back]

How to substitute the product of two variables in polynom?

I want to replace all occurrences of the product of two or more variables in a polynomial with some expression.

If I call subs for expression, nothing happens:

x, y, z, w = var("x y z w")
P = 7*x^2*y^2 + 5*x^2*y + 6*x*y^2 + 4*x*y + x + 2*y + 3*z
P.subs({ x*y: w })

returns 7*x^2*y^2 + 5*x^2*y + 6*x*y^2 + 4*x*y + x + 2*y + 3*z.

If I call subs for polynoms, it returns incorrect result.

R.<x, y, z, w> = QQ[]
P = 7*x^2*y^2 + 5*x^2*y + 6*x*y^2 + 4*x*y + x + 2*y + 3*z
P.subs({ x*y: w })

returns 7*y^2*w^2 + 6*y^2*w + 5*y*w^2 + 4*y*w + 2*y + 3*z + w.

But in bots cases I expected 7*w^2 + 5*x*w + 6*w*y + 4*w + x + 2*y + 3*z.