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.Mon, 21 Mar 2016 14:04:02 +0100Performing substitutions on powers of a variablehttps://ask.sagemath.org/question/32843/performing-substitutions-on-powers-of-a-variable/I have some polynomials of degree $d$ and I would like to obtain the monomial where all exponents greater than $1$ are reduced to $1$. For example $x_1^2 x_3^4 x_2 + x_1^7x_3^3 x_2^8 + \cdots$ would become $2x_1 x_3 x_2 + \cdots $
Naively, I thought an approach along the following lines would work:
sage: x = PolynomialRing(QQ, 1, 'x').objgens()[1][0]
sage: s = 2*x^2
sage: s.substitute({x^2:x})
2*x^2
Unfortunately, this does not give the proper result. Hence I am wondering
> What is the proper way to perform the described substitution on the powers of a given monomial?
Edit. It seems that I can do ss = symbolic_expression(s).substitute({x^2:x}) and then convert ss to a polynomial. However, this seems to be extremely inefficient.
Sun, 20 Mar 2016 21:03:35 +0100https://ask.sagemath.org/question/32843/performing-substitutions-on-powers-of-a-variable/Answer by tmonteil for <p>I have some polynomials of degree $d$ and I would like to obtain the monomial where all exponents greater than $1$ are reduced to $1$. For example $x_1^2 x_3^4 x_2 + x_1^7x_3^3 x_2^8 + \cdots$ would become $2x_1 x_3 x_2 + \cdots $</p>
<p>Naively, I thought an approach along the following lines would work:</p>
<pre><code>sage: x = PolynomialRing(QQ, 1, 'x').objgens()[1][0]
sage: s = 2*x^2
sage: s.substitute({x^2:x})
2*x^2
</code></pre>
<p>Unfortunately, this does not give the proper result. Hence I am wondering</p>
<blockquote>
<p>What is the proper way to perform the described substitution on the powers of a given monomial?</p>
</blockquote>
<p>Edit. It seems that I can do ss = symbolic_expression(s).substitute({x^2:x}) and then convert ss to a polynomial. However, this seems to be extremely inefficient. </p>
https://ask.sagemath.org/question/32843/performing-substitutions-on-powers-of-a-variable/?answer=32845#post-id-32845You should work modulo the ideal generated by the $x_i^2-x_i$:
sage: R = PolynomialRing(QQ,3,'x') ; R
Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: R.inject_variables()
Defining x0, x1, x2
sage: I = R.ideal([m^2-m for m in R.gens()]) ; I
Ideal (x0^2 - x0, x1^2 - x1, x2^2 - x2) of Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: P = x1^2*x2^4 + 7*x0*x1^4*x2^5 - 12*x0*x2^7 ; P
7*x0*x1^4*x2^5 - 12*x0*x2^7 + x1^2*x2^4
sage: P.mod(I)
7*x0*x1*x2 - 12*x0*x2 + x1*x2
Sun, 20 Mar 2016 23:23:42 +0100https://ask.sagemath.org/question/32843/performing-substitutions-on-powers-of-a-variable/?answer=32845#post-id-32845Comment by Jernej for <p>You should work modulo the ideal generated by the $x_i^2-x_i$:</p>
<pre><code>sage: R = PolynomialRing(QQ,3,'x') ; R
Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: R.inject_variables()
Defining x0, x1, x2
sage: I = R.ideal([m^2-m for m in R.gens()]) ; I
Ideal (x0^2 - x0, x1^2 - x1, x2^2 - x2) of Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: P = x1^2*x2^4 + 7*x0*x1^4*x2^5 - 12*x0*x2^7 ; P
7*x0*x1^4*x2^5 - 12*x0*x2^7 + x1^2*x2^4
sage: P.mod(I)
7*x0*x1*x2 - 12*x0*x2 + x1*x2
</code></pre>
https://ask.sagemath.org/question/32843/performing-substitutions-on-powers-of-a-variable/?comment=32851#post-id-32851Thanks. Just out of curiosity - is there a reason in the different behavior of substitute on symbolic expressions and polynomials?Mon, 21 Mar 2016 11:13:27 +0100https://ask.sagemath.org/question/32843/performing-substitutions-on-powers-of-a-variable/?comment=32851#post-id-32851Comment by tmonteil for <p>You should work modulo the ideal generated by the $x_i^2-x_i$:</p>
<pre><code>sage: R = PolynomialRing(QQ,3,'x') ; R
Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: R.inject_variables()
Defining x0, x1, x2
sage: I = R.ideal([m^2-m for m in R.gens()]) ; I
Ideal (x0^2 - x0, x1^2 - x1, x2^2 - x2) of Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: P = x1^2*x2^4 + 7*x0*x1^4*x2^5 - 12*x0*x2^7 ; P
7*x0*x1^4*x2^5 - 12*x0*x2^7 + x1^2*x2^4
sage: P.mod(I)
7*x0*x1*x2 - 12*x0*x2 + x1*x2
</code></pre>
https://ask.sagemath.org/question/32843/performing-substitutions-on-powers-of-a-variable/?comment=32852#post-id-32852Polynomials and symbolic expressions are not the same objects at all, in particular they are not implemented the same way. Also, quotient ring does not makes much sense for general symbolic expressions.
If you have to deal with polynomials, i strongly encourage you to work with polynomial rings instead of symbolic expressions. They are more reliable and much faster.Mon, 21 Mar 2016 14:04:02 +0100https://ask.sagemath.org/question/32843/performing-substitutions-on-powers-of-a-variable/?comment=32852#post-id-32852