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.Fri, 20 Dec 2019 00:56:24 +0100substitution of variable in the result of monomials()https://ask.sagemath.org/question/49089/substitution-of-variable-in-the-result-of-monomials/Given a polynomial in n variables, I'd like to extract the list of its monomials, and then manipulate that list by substituting certain variables for others.
Simple example: in $Z[x,y]$ consider the polynomial $1+x+y^2$; the list of its monomials (in some ordering) is $(1,x,y^2)$. My function should be able e.g. to take that list and substitute $y$ with $x$, namely return $[1,x,x^2]$.
At the moment, my code gives error, but I do not understand how to fix it.
N.<x1,x2> = PolynomialRing(ZZ, 2)
f = 1+x1+x2
g = f.monomials()
for i in range(3):
g[i] = g[i].substitute_expression(x2==x1)
Namely, how do I make a variable in the polynomial ring also have the substitute attribute? or is there a better way to achieve this?Thu, 19 Dec 2019 20:18:18 +0100https://ask.sagemath.org/question/49089/substitution-of-variable-in-the-result-of-monomials/Answer by tmonteil for <p>Given a polynomial in n variables, I'd like to extract the list of its monomials, and then manipulate that list by substituting certain variables for others.
Simple example: in $Z[x,y]$ consider the polynomial $1+x+y^2$; the list of its monomials (in some ordering) is $(1,x,y^2)$. My function should be able e.g. to take that list and substitute $y$ with $x$, namely return $[1,x,x^2]$.</p>
<p>At the moment, my code gives error, but I do not understand how to fix it.</p>
<pre><code>N.<x1,x2> = PolynomialRing(ZZ, 2)
f = 1+x1+x2
g = f.monomials()
for i in range(3):
g[i] = g[i].substitute_expression(x2==x1)
</code></pre>
<p>Namely, how do I make a variable in the polynomial ring also have the substitute attribute? or is there a better way to achieve this?</p>
https://ask.sagemath.org/question/49089/substitution-of-variable-in-the-result-of-monomials/?answer=49090#post-id-49090Substitutions can be explicitely defined through dictionaries. How about:
sage: R.<x,y> = ZZ[]
sage: f = 1+x+y^2
sage: [p.substitute({y:x}) for p in f.monomials()]
[x^2, x, 1]
Fri, 20 Dec 2019 00:56:24 +0100https://ask.sagemath.org/question/49089/substitution-of-variable-in-the-result-of-monomials/?answer=49090#post-id-49090