ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 11 Jun 2012 07:48:06 -0500How do I iterate through factors of a monomial?https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/I am (very naively) trying to implement a certain non-linear map of polynomial rings. I can get the monomials in a polynomial by simply iterating through a polynomial:
>>> R.<x,y,z>=QQ[]
>>> list(x*y^3 + 2*x*y)
[(1, x*y^3), (2, x*y)]
How do I now iterate through the factors of each monomial, i.e. convert x*y^3 into something like [(x, 1), (y, 3)] or even just [x, y, y, y]?
Also, what is the correct way to do the opposite conversion, i.e. [x, y, y, y] to x*y^3?
I hope you won't be offended by such newbie questions. I couldn't find this in any of the tutorials easily (did I miss the right one?) Thank you! Sun, 10 Jun 2012 04:46:35 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/Answer by Eviatar Bach for <p>I am (very naively) trying to implement a certain non-linear map of polynomial rings. I can get the monomials in a polynomial by simply iterating through a polynomial:</p>
<pre><code>>>> R.<x,y,z>=QQ[]
>>> list(x*y^3 + 2*x*y)
[(1, x*y^3), (2, x*y)]
</code></pre>
<p>How do I now iterate through the factors of each monomial, i.e. convert x*y^3 into something like [(x, 1), (y, 3)] or even just [x, y, y, y]? </p>
<p>Also, what is the correct way to do the opposite conversion, i.e. [x, y, y, y] to x*y^3?</p>
<p>I hope you won't be offended by such newbie questions. I couldn't find this in any of the tutorials easily (did I miss the right one?) Thank you! </p>
https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?answer=13683#post-id-13683Not offended at all! I didn't know about this myself until now.
This works:
sage: (x*y^3).factor_list()
[(x, 1), (y, 3)]
sage: mul([x, y, y, y])
x*y^3
And, if you want to go from the factor list to the expression:
sage: a = (x*y^3).factor_list()
sage: mul(t^m for t, m in a)
x*y^3
Hope this helps!Sun, 10 Jun 2012 07:44:42 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?answer=13683#post-id-13683Comment by Eviatar Bach for <p>Not offended at all! I didn't know about this myself until now.</p>
<p>This works:</p>
<pre><code>sage: (x*y^3).factor_list()
[(x, 1), (y, 3)]
sage: mul([x, y, y, y])
x*y^3
</code></pre>
<p>And, if you want to go from the factor list to the expression:</p>
<pre><code>sage: a = (x*y^3).factor_list()
sage: mul(t^m for t, m in a)
x*y^3
</code></pre>
<p>Hope this helps!</p>
https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19624#post-id-19624I see it has to do with `R.<x,y,z>=QQ[]`. Looks like a bug.Mon, 11 Jun 2012 07:48:06 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19624#post-id-19624Comment by DSM for <p>Not offended at all! I didn't know about this myself until now.</p>
<p>This works:</p>
<pre><code>sage: (x*y^3).factor_list()
[(x, 1), (y, 3)]
sage: mul([x, y, y, y])
x*y^3
</code></pre>
<p>And, if you want to go from the factor list to the expression:</p>
<pre><code>sage: a = (x*y^3).factor_list()
sage: mul(t^m for t, m in a)
x*y^3
</code></pre>
<p>Hope this helps!</p>
https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19631#post-id-19631I think something like `mul(t^m for t,m in a)` is simpler.Sun, 10 Jun 2012 08:16:53 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19631#post-id-19631Comment by Eviatar Bach for <p>Not offended at all! I didn't know about this myself until now.</p>
<p>This works:</p>
<pre><code>sage: (x*y^3).factor_list()
[(x, 1), (y, 3)]
sage: mul([x, y, y, y])
x*y^3
</code></pre>
<p>And, if you want to go from the factor list to the expression:</p>
<pre><code>sage: a = (x*y^3).factor_list()
sage: mul(t^m for t, m in a)
x*y^3
</code></pre>
<p>Hope this helps!</p>
https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19630#post-id-19630Yes, you're right. I changed it.Sun, 10 Jun 2012 08:35:50 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19630#post-id-19630Comment by Ilya for <p>Not offended at all! I didn't know about this myself until now.</p>
<p>This works:</p>
<pre><code>sage: (x*y^3).factor_list()
[(x, 1), (y, 3)]
sage: mul([x, y, y, y])
x*y^3
</code></pre>
<p>And, if you want to go from the factor list to the expression:</p>
<pre><code>sage: a = (x*y^3).factor_list()
sage: mul(t^m for t, m in a)
x*y^3
</code></pre>
<p>Hope this helps!</p>
https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19626#post-id-19626Thank you very much, don't know how long I'd be looking for it without your help. However, I have to do `list((x*y^3).factor())`. For some reason, `(x*y^3).factor_list()` gives an error ('sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingu\
lar' object has no attribute 'factor_list'). Mon, 11 Jun 2012 00:49:41 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19626#post-id-19626Answer by achrzesz for <p>I am (very naively) trying to implement a certain non-linear map of polynomial rings. I can get the monomials in a polynomial by simply iterating through a polynomial:</p>
<pre><code>>>> R.<x,y,z>=QQ[]
>>> list(x*y^3 + 2*x*y)
[(1, x*y^3), (2, x*y)]
</code></pre>
<p>How do I now iterate through the factors of each monomial, i.e. convert x*y^3 into something like [(x, 1), (y, 3)] or even just [x, y, y, y]? </p>
<p>Also, what is the correct way to do the opposite conversion, i.e. [x, y, y, y] to x*y^3?</p>
<p>I hope you won't be offended by such newbie questions. I couldn't find this in any of the tutorials easily (did I miss the right one?) Thank you! </p>
https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?answer=13682#post-id-13682 R.<x,y,z>=QQ[]
[u for u in factor(x*y^3)]
#[(x, 1), (y, 3)]Sun, 10 Jun 2012 07:42:53 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?answer=13682#post-id-13682Comment by Eviatar Bach for <pre><code>R.<x,y,z>=QQ[]
[u for u in factor(x*y^3)]
#[(x, 1), (y, 3)]
</code></pre>
https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19627#post-id-19627`list(factor(x*y^3))` also works.Sun, 10 Jun 2012 19:43:42 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19627#post-id-19627Comment by Ilya for <pre><code>R.<x,y,z>=QQ[]
[u for u in factor(x*y^3)]
#[(x, 1), (y, 3)]
</code></pre>
https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19625#post-id-19625This works, thank you!Mon, 11 Jun 2012 00:51:19 -0500https://ask.sagemath.org/question/9055/how-do-i-iterate-through-factors-of-a-monomial/?comment=19625#post-id-19625