Ask Your Question
2

How to substitute the product of two variables in polynom?

asked 2024-02-23 14:48:54 +0100

Konstantin Bats gravatar image

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.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2024-02-23 23:27:39 +0100

Emmanuel Charpentier gravatar image

Lazy alternative

sage: P._sympy_().subs(sympy.sympify({ x*y: w }))._sage_()
7*w^2 + 5*w*x + 6*w*y + 4*w + x + 2*y + 3*z

HTH,

edit flag offensive delete link more
0

answered 2024-02-23 15:22:01 +0100

Max Alekseyev gravatar image

updated 2024-02-23 16:02:48 +0100

In symbolic ring, you can perform such substitution by explicitly going over the coefficients of powers of x and y:

sum( cy*x^(dx-min(dx,dy))*y^(dy-min(dx,dy))*w^min(dx,dy) for cx,dx in P.coefficients(x) for cy,dy in cx.coefficients(y) )
edit flag offensive delete link more
0

answered 2024-02-23 15:47:30 +0100

rburing gravatar image

updated 2024-02-23 15:48:33 +0100

If the monomial ordering is suitable (see the documentation of PolynomialRing for the options), then you can use:

sage: P.reduce([x*y - w])
5*x*w + 6*y*w + 7*w^2 + x + 2*y + 3*z + 4*w

This assumes x*y > w in the monomial ordering.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2024-02-23 14:48:54 +0100

Seen: 630 times

Last updated: Feb 23