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.Sat, 05 Oct 2019 13:45:43 +0200uniform way to iterate over terms of uni-/multi-variate polynomialshttps://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/I deal with polynomials of varying number of variables, and need a uniform way to iterate over their terms. What works for multivariate polynomials does not for univariate ones, and vice versa. Is there a uniform approach here?
Is it possible to force multivariate methods work on univariate polynomials?
**UPDATE.** Please notice that I do not explicitly define polynomial rings, while polynomials come converted from symbolic expressions with apriori unknown number of variables. I've updated the example below to have polynomials of this kind.
Example:
sage: x,y,z = var('x,y,z')
sage: P = x + 2*y + x*y + 3
sage: P = (x + 2*y + x*y + 3).polynomial(QQ)
sage: Q = (z + 5).polynomial(QQ)
sage: for c,t in P: print c,t
1 x*y
1 x
2 y
3 1
sage: for c,t in Q: print c,t
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-26-3ef1c7d76c79> in <module>()
----> 1 for c,t in Q: print c,t
ValueError: need more than 1 value to unpackFri, 04 Oct 2019 02:01:44 +0200https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/Answer by rburing for <p>I deal with polynomials of varying number of variables, and need a uniform way to iterate over their terms. What works for multivariate polynomials does not for univariate ones, and vice versa. Is there a uniform approach here?</p>
<p>Is it possible to force multivariate methods work on univariate polynomials?</p>
<p><strong>UPDATE.</strong> Please notice that I do not explicitly define polynomial rings, while polynomials come converted from symbolic expressions with apriori unknown number of variables. I've updated the example below to have polynomials of this kind.</p>
<p>Example:</p>
<pre><code>sage: x,y,z = var('x,y,z')
sage: P = x + 2*y + x*y + 3
sage: P = (x + 2*y + x*y + 3).polynomial(QQ)
sage: Q = (z + 5).polynomial(QQ)
sage: for c,t in P: print c,t
1 x*y
1 x
2 y
3 1
sage: for c,t in Q: print c,t
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-26-3ef1c7d76c79> in <module>()
----> 1 for c,t in Q: print c,t
ValueError: need more than 1 value to unpack
</code></pre>
https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/?answer=48176#post-id-48176Define `R2` as a multivariate polynomial ring in 1 variable:
sage: R2.<z> = PolynomialRing(QQ, 1)
sage: Q = z + 5
sage: for c,t in Q: print c,t
1 z
5 1
This ring (and its elements) will have different methods than the ordinary univariate ring (elements).
Alternatively: keep your original ring, define `R3 = PolynomialRing(QQ, 1, name='z')` and use `R3(Q)`.
---
An answer to the new question:
def to_multivariate_poly(expr, base_ring):
vars = expr.variables()
if len(vars) == 0:
vars = ['x']
R = PolynomialRing(base_ring, len(vars), names=vars)
return R(expr)
Then you can do:
sage: x,y,z = var('x,y,z')
sage: P = to_multivariate_poly(x + 2*y + x*y + 3, QQ)
sage: for c,t in P: print c,t
1 x*y
1 x
2 y
3 1
sage: Q = to_multivariate_poly(z + 5, QQ)
sage: for c,t in Q: print c,t
1 z
5 1Fri, 04 Oct 2019 09:00:03 +0200https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/?answer=48176#post-id-48176Comment by Max Alekseyev for <p>Define <code>R2</code> as a multivariate polynomial ring in 1 variable:</p>
<pre><code>sage: R2.<z> = PolynomialRing(QQ, 1)
sage: Q = z + 5
sage: for c,t in Q: print c,t
1 z
5 1
</code></pre>
<p>This ring (and its elements) will have different methods than the ordinary univariate ring (elements).</p>
<p>Alternatively: keep your original ring, define <code>R3 = PolynomialRing(QQ, 1, name='z')</code> and use <code>R3(Q)</code>.</p>
<hr>
<p>An answer to the new question:</p>
<pre><code>def to_multivariate_poly(expr, base_ring):
vars = expr.variables()
if len(vars) == 0:
vars = ['x']
R = PolynomialRing(base_ring, len(vars), names=vars)
return R(expr)
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: x,y,z = var('x,y,z')
sage: P = to_multivariate_poly(x + 2*y + x*y + 3, QQ)
sage: for c,t in P: print c,t
1 x*y
1 x
2 y
3 1
sage: Q = to_multivariate_poly(z + 5, QQ)
sage: for c,t in Q: print c,t
1 z
5 1
</code></pre>
https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/?comment=48177#post-id-48177Thanks, but I don't explicitly define rings (in contrast to the example). Polynomials come converted from symbolic expressions. Say, if we have a polynomial $f$, how to convert it to multivariate-polynomial type? (even if it depends on a single variable or is a constant)Fri, 04 Oct 2019 10:37:35 +0200https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/?comment=48177#post-id-48177Comment by rburing for <p>Define <code>R2</code> as a multivariate polynomial ring in 1 variable:</p>
<pre><code>sage: R2.<z> = PolynomialRing(QQ, 1)
sage: Q = z + 5
sage: for c,t in Q: print c,t
1 z
5 1
</code></pre>
<p>This ring (and its elements) will have different methods than the ordinary univariate ring (elements).</p>
<p>Alternatively: keep your original ring, define <code>R3 = PolynomialRing(QQ, 1, name='z')</code> and use <code>R3(Q)</code>.</p>
<hr>
<p>An answer to the new question:</p>
<pre><code>def to_multivariate_poly(expr, base_ring):
vars = expr.variables()
if len(vars) == 0:
vars = ['x']
R = PolynomialRing(base_ring, len(vars), names=vars)
return R(expr)
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: x,y,z = var('x,y,z')
sage: P = to_multivariate_poly(x + 2*y + x*y + 3, QQ)
sage: for c,t in P: print c,t
1 x*y
1 x
2 y
3 1
sage: Q = to_multivariate_poly(z + 5, QQ)
sage: for c,t in Q: print c,t
1 z
5 1
</code></pre>
https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/?comment=48192#post-id-48192It's not very nice to change your question when it has received a correct answer, even if you suffered from the [XY problem](https://en.wikipedia.org/wiki/XY_problem). Anyway, I updated the answer. In the future, please ask the new question separately (and link to the previous question).Sat, 05 Oct 2019 10:49:37 +0200https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/?comment=48192#post-id-48192Comment by Max Alekseyev for <p>Define <code>R2</code> as a multivariate polynomial ring in 1 variable:</p>
<pre><code>sage: R2.<z> = PolynomialRing(QQ, 1)
sage: Q = z + 5
sage: for c,t in Q: print c,t
1 z
5 1
</code></pre>
<p>This ring (and its elements) will have different methods than the ordinary univariate ring (elements).</p>
<p>Alternatively: keep your original ring, define <code>R3 = PolynomialRing(QQ, 1, name='z')</code> and use <code>R3(Q)</code>.</p>
<hr>
<p>An answer to the new question:</p>
<pre><code>def to_multivariate_poly(expr, base_ring):
vars = expr.variables()
if len(vars) == 0:
vars = ['x']
R = PolynomialRing(base_ring, len(vars), names=vars)
return R(expr)
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: x,y,z = var('x,y,z')
sage: P = to_multivariate_poly(x + 2*y + x*y + 3, QQ)
sage: for c,t in P: print c,t
1 x*y
1 x
2 y
3 1
sage: Q = to_multivariate_poly(z + 5, QQ)
sage: for c,t in Q: print c,t
1 z
5 1
</code></pre>
https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/?comment=48193#post-id-48193I did not change the question - I just clarified it. In either case thanks for both answers.Sat, 05 Oct 2019 13:45:43 +0200https://ask.sagemath.org/question/48171/uniform-way-to-iterate-over-terms-of-uni-multi-variate-polynomials/?comment=48193#post-id-48193