ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 03 Mar 2015 18:26:45 +0100substitute x*y by uhttps://ask.sagemath.org/question/25972/substitute-xy-by-u/ Hi there!
I have a polynomial $f(x,y) = x*y + x^2*y^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?Sat, 28 Feb 2015 19:00:54 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/Comment by vdelecroix for <p>Hi there!</p>
<p>I have a polynomial $f(x,y) = x<em>y + x^2</em>y^2 + x*y^2$</p>
<p>I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)</p>
<p>This is how I proceeded:</p>
<pre><code># 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)
</code></pre>
<p>Of course this code doesn't work... Any idea what I could do?</p>
https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=25978#post-id-25978What exactly do you want to replace? Do you want x^2 y^3 -> u^2 y and x^3 y^2 -> x u^2?Sun, 01 Mar 2015 00:13:31 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=25978#post-id-25978Comment by SL for <p>Hi there!</p>
<p>I have a polynomial $f(x,y) = x<em>y + x^2</em>y^2 + x*y^2$</p>
<p>I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)</p>
<p>This is how I proceeded:</p>
<pre><code># 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)
</code></pre>
<p>Of course this code doesn't work... Any idea what I could do?</p>
https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=25975#post-id-25975It doesn't look like there's support for substituting products. Instead you can do
ff = f.subs(x = u/y)Sat, 28 Feb 2015 21:44:34 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=25975#post-id-25975Comment by mimoo for <p>Hi there!</p>
<p>I have a polynomial $f(x,y) = x<em>y + x^2</em>y^2 + x*y^2$</p>
<p>I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)</p>
<p>This is how I proceeded:</p>
<pre><code># 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)
</code></pre>
<p>Of course this code doesn't work... Any idea what I could do?</p>
https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=25990#post-id-25990yes @vdelecroixSun, 01 Mar 2015 02:16:06 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=25990#post-id-25990Comment by kcrisman for <p>Hi there!</p>
<p>I have a polynomial $f(x,y) = x<em>y + x^2</em>y^2 + x*y^2$</p>
<p>I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)</p>
<p>This is how I proceeded:</p>
<pre><code># 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)
</code></pre>
<p>Of course this code doesn't work... Any idea what I could do?</p>
https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=25991#post-id-25991Maybe with wildcards?Sun, 01 Mar 2015 03:02:20 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=25991#post-id-25991Answer by rws for <p>Hi there!</p>
<p>I have a polynomial $f(x,y) = x<em>y + x^2</em>y^2 + x*y^2$</p>
<p>I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)</p>
<p>This is how I proceeded:</p>
<pre><code># 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)
</code></pre>
<p>Of course this code doesn't work... Any idea what I could do?</p>
https://ask.sagemath.org/question/25972/substitute-xy-by-u/?answer=25994#post-id-25994It'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*y` as result and Sage cannot do that at the moment. I've opened a ticket for it: http://trac.sagemath.org/ticket/17879
Sun, 01 Mar 2015 09:03:23 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/?answer=25994#post-id-25994Comment by JesterEE for <p>It's better to use the equality sign with subs: <code>f.subs(x*y==u)</code> or a dictionary:</p>
<pre><code>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
</code></pre>
<p>Note this different from</p>
<pre><code>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
</code></pre>
<p>because of the ring. But certainly, we would want <code>u+u^2+u*y</code>as result and Sage cannot do that at the moment. I've opened a ticket for it: <a href="http://trac.sagemath.org/ticket/17879">http://trac.sagemath.org/ticket/17879</a></p>
https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=26002#post-id-26002Also 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*yMon, 02 Mar 2015 22:21:57 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=26002#post-id-26002Answer by tmonteil for <p>Hi there!</p>
<p>I have a polynomial $f(x,y) = x<em>y + x^2</em>y^2 + x*y^2$</p>
<p>I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)</p>
<p>This is how I proceeded:</p>
<pre><code># 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)
</code></pre>
<p>Of course this code doesn't work... Any idea what I could do?</p>
https://ask.sagemath.org/question/25972/substitute-xy-by-u/?answer=25996#post-id-25996Perhaps 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
Sun, 01 Mar 2015 14:42:05 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/?answer=25996#post-id-25996Comment by mimoo for <p>Perhaps what you are looking for is not a substitution but a quotient ring where <code>u</code> and <code>x*y</code> are identified:</p>
<pre><code>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)
</code></pre>
<p>Now, if you want your polynomial back in <code>P</code>, you can do:</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=26023#post-id-26023it works! magicTue, 03 Mar 2015 18:26:45 +0100https://ask.sagemath.org/question/25972/substitute-xy-by-u/?comment=26023#post-id-26023