# substitute x*y by u

Hi there!

I have a polynomial $f(x,y) = xy + x^2y^2 + x*y^2$

I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)

This is how I proceeded:

# this is the default code
P.<x, y> = PolynomialRing(Zmod(5))
f = x*y + x^2*y^2 + x*y^2
# now I want to introduce the substitution so I have a f(x,y,u)
new_ring.<x,y,u> = PolynomialRing(ZZ)
ff = f.sub(x*y = u)


Of course this code doesn't work... Any idea what I could do?

edit retag close merge delete

It doesn't look like there's support for substituting products. Instead you can do

ff = f.subs(x = u/y)


What exactly do you want to replace? Do you want x^2 y^3 -> u^2 y and x^3 y^2 -> x u^2?

Sort by » oldest newest most voted

Perhaps what you are looking for is not a substitution but a quotient ring where u and x*y are identified:

sage: P.<x, y, u> = PolynomialRing(Zmod(5)) ; P
Multivariate Polynomial Ring in x, y, u over Ring of integers modulo 5
sage: f = x*y + x^2*y^2 + x*y^2
sage: f.parent()
Multivariate Polynomial Ring in x, y, u over Ring of integers modulo 5
sage: Q = P.quotient(x*y-u) ; Q
Quotient of Multivariate Polynomial Ring in x, y, u over Ring of integers modulo 5 by the ideal (x*y - u)
sage: ff = Q(f) ; ff
ybar*ubar + ubar^2 + ubar
sage: ff.parent()
Quotient of Multivariate Polynomial Ring in x, y, u over Ring of integers modulo 5 by the ideal (x*y - u)


Now, if you want your polynomial back in P, you can do:

sage: fff = ff.lift() ; fff
y*u + u^2 + u
sage: fff.parent()
Multivariate Polynomial Ring in x, y, u over Ring of integers modulo 5

more

It's better to use the equality sign with subs: f.subs(x*y==u) or a dictionary:

sage: P.<x, y> = PolynomialRing(Zmod(5))
sage: f = x*y + x^2*y^2 + x*y^2
sage: f.subs({x*y:u})
u^2*y^2 + u*y^2 + u*y


Note this different from

sage: x,y = var('x,y')
sage: f = x*y + x^2*y^2 + x*y^2
sage: f.subs({x*y:u})
x^2*y^2 + x*y^2 + u


because of the ring. But certainly, we would want u+u^2+u*yas result and Sage cannot do that at the moment. I've opened a ticket for it: http://trac.sagemath.org/ticket/17879

more

Also interesting, using the above Symbolic ring example, if one factors first to expose two instances of x*y, the subs() function will only replace one.

sage: _ = var('x,y,u')
sage: f = x*y + x^2*y^2 + x*y^2
sage: f.factor()
sage: f.factor().subs({x*y:u})
(x*y + y + 1)*x*y
(u + y + 1)*x*y