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, 02 May 2016 15:31:53 +0200GCD of multivariable polynomials and conversion of Laurent polynomials to ordinary polynomialshttps://ask.sagemath.org/question/33260/gcd-of-multivariable-polynomials-and-conversion-of-laurent-polynomials-to-ordinary-polynomials/Let's assume that I am working with some set of Laurent polynomials in $\mathbb{C}[t_1^{\pm1}, \ldots, t_n^{\pm 1}]$.
R = LaurentPolynomialRing(CC, 't', n)
My first question: is there any method which would multiply elements of $R$ by a big enough monomial $t_1^{k_1}\cdot \ldots \cdot t_n^{k_n}$ to get rid of the negative powers and after that changed them to ordinary polynomials? I will consider resulting polynomials up to $t_1^{k_1}\cdot \ldots \cdot t_n^{k_n}$, so $k_i$'s don't matter that much as long as multiplication will result in a Laurent polynomial with non-negative powers (however I would like to avoid shifting exponents by some huge constant). At the moment I am using following workaround which I hope is redundant to some sage method.
# Since I am working over C I would like to get rid of summands coming from numeric errors
def clean_poly(laurent_poly, precision):
decomp = list(laurent_poly)
ret = 0
for (coeff, monom) in decomp:
if(coeff * coeff.conjugate() >= precision):
ret += coeff * monom
return ret
# Here I am finding lowest exponents of laurent_poly corresponding to particular t_i's
def find_shift(laurent_poly, n):
shift = [0 for i in range(0, n)]
for exp in laurent_poly.exponents():
for i in range(0, n):
shift[i] = max(shift[i], -exp[i])
return shift
def change_laurent_poly_to_ordinary(laurent_poly, R_ordinary, n):
laurent_poly = clean_poly(laurent_poly, 0.00000000001)
decomp = list(laurent_poly)
shift = find_shift(laurent_poly, n)
ret = 0
for (coeff, monom) in decomp:
exp = monom.exponents()
exp = exp[0]
summand = 1
for i in range(0, n):
summand *= (R_ordinary.gen(i)) ^ (exp[i] + shift[i])
ret += coeff * summand
return ret
Where $R_{\text{ordinary}} = \mathbb{C}[t_1, \ldots, t_n]$ is passed as argument in the last method.
R_ordinary = PolynomialRing(CC, 's', n)
My second and the most important question is how to fix the last method to return a polynomial which posses a gcd() method just like in this snippet which I've found in the documentation (I can't post a link due to insufficient karma):
sage: R, (x, y) = PolynomialRing(RationalField(), 2, 'xy').objgens()
sage: f = (x^3 + 2*y^2*x)^2
sage: g = x^2*y^2
sage: f.gcd(g)
x^2
Unfortunately at the moment method change_laurent_poly_to_ordinary returns class of type MPolynomial_polydict which doesn't have gcd() method, what results in the following error after attempting to use them:
Traceback (most recent call last):
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 28, in <module>
File "sage/structure/element.pyx", line 420, in sage.structure.element.Element.__getattr__ (/projects/sage/sage-6.10/src/build/cythonized/sage/structure/element.c:4675)
return getattr_from_other_class(self, P._abstract_element_class, name)
File "sage/structure/misc.pyx", line 259, in sage.structure.misc.getattr_from_other_class (/projects/sage/sage-6.10/src/build/cythonized/sage/structure/misc.c:1771)
raise dummy_attribute_error
AttributeError: 'MPolynomial_polydict' object has no attribute 'gcd'
I am working with sage version offered by cloud sagemath.Sat, 30 Apr 2016 00:21:39 +0200https://ask.sagemath.org/question/33260/gcd-of-multivariable-polynomials-and-conversion-of-laurent-polynomials-to-ordinary-polynomials/Comment by B r u n o for <div class="snippet"><p>Let's assume that I am working with some set of Laurent polynomials in $\mathbb{C}[t_1^{\pm1}, \ldots, t_n^{\pm 1}]$.</p>
<pre><code>R = LaurentPolynomialRing(CC, 't', n)
</code></pre>
<p>My first question: is there any method which would multiply elements of $R$ by a big enough monomial $t_1^{k_1}\cdot \ldots \cdot t_n^{k_n}$ to get rid of the negative powers and after that changed them to ordinary polynomials? I will consider resulting polynomials up to $t_1^{k_1}\cdot \ldots \cdot t_n^{k_n}$, so $k_i$'s don't matter that much as long as multiplication will result in a Laurent polynomial with non-negative powers (however I would like to avoid shifting exponents by some huge constant). At the moment I am using following workaround which I hope is redundant to some sage method.</p>
<pre><code># Since I am working over C I would like to get rid of summands coming from numeric errors
def clean_poly(laurent_poly, precision):
decomp = list(laurent_poly)
ret = 0
for (coeff, monom) in decomp:
if(coeff * coeff.conjugate() >= precision):
ret += coeff * monom
return ret
# Here I am finding lowest exponents of laurent_poly corresponding to particular t_i's
def find_shift(laurent_poly, n):
shift = [0 for i in range(0, n)]
for exp in laurent_poly.exponents():
for i in range(0, n):
shift[i] = max(shift[i], -exp[i])
return shift
def change_laurent_poly_to_ordinary(laurent_poly, R_ordinary, n):
laurent_poly = clean_poly(laurent_poly, 0.00000000001)
decomp = list(laurent_poly)
shift = find_shift(laurent_poly, n)
ret = 0
for (coeff, monom) in decomp:
exp = monom.exponents()
exp = exp[0]
summand = 1
for i in range(0, n):
summand *= (R_ordinary.gen(i)) ^ (exp[i] + shift[i])
ret += coeff * summand
return ret
</code></pre>
<p>Where $R_{\text{ordinary}} = \mathbb{C}[t_1, \ldots, t_n]$ is passed as argument in the last method.</p>
<pre><code>R_ordinary = PolynomialRing(CC, 's', n)
</code></pre>
<p>My second and the most important question is how to fix the last method to return a polynomial which posses a gcd() method just like in this snippet which I've found in the documentation (I can't post a link due to insufficient karma):</p>
<pre><code>sage: R, (x, y) = PolynomialRing(RationalField(), 2, 'xy').objgens()
sage: f = (x^3 + 2*y^2*x)^2
sage: g = x^2*y^2
sage: f.gcd(g)
x^2
</code></pre>
<p>Unfortunately at the moment method change_laurent_poly_to_ordinary returns class of type MPolynomial_polydict which doesn't have gcd() method, what results in the following error after attempting to use them:</p>
<pre><code>Traceback (most recent call last):
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 28, in <module>
File "sage/structure/element.pyx", line 420, in sage.structure.element.Element.__getattr__ (/projects/sage/sage-6.10/src/build/cythonized/sage/structure/element.c:4675)
return getattr_from_other_class(self, P._abstract_element_class, name)
File "sage/structure/misc.pyx", line 259, in sage.structure.misc.getattr_from_other_class (/projects/sage/sage-6.10/src/build/cythonized/sage/structure/misc.c:1771)
raise dummy_attribute_error
AttributeError: 'MPolynomial_polydict' object has no attribute 'gcd'
</code></pre>
<p>I am working with sage version offered ...<span class="expander"> <a>(more)</a></span></p></div>https://ask.sagemath.org/question/33260/gcd-of-multivariable-polynomials-and-conversion-of-laurent-polynomials-to-ordinary-polynomials/?comment=33269#post-id-33269I've not read all your code (I'll do it later) but as a quick answer to your second question: The problem is that gcd of multivariate polynomial over `CC` appears not to be implemented. **[EDIT]** Introduced in ticket [#20220](http://trac.sagemath.org/ticket/20220), available from version 7.2beta1. **[/EDIT]** Three solutions: 1. You work over another field than `CC` (probably not applicable...); 2. You convert your polynomials to "recursive univariate" polynomials (ring `CC['t1']['t2']...['tn']`) and use the gcd provided for these univariate polynomial rings; 3. <s>(Better!) You propose a ticket on trac.sagemath.org to add a gcd implementation for multivariate polynomials over `CC` (or a generic implementation).</s> Use Sage version at least 7.2beta1.Sat, 30 Apr 2016 23:48:39 +0200https://ask.sagemath.org/question/33260/gcd-of-multivariable-polynomials-and-conversion-of-laurent-polynomials-to-ordinary-polynomials/?comment=33269#post-id-33269Answer by B r u n o for <div class="snippet"><p>Let's assume that I am working with some set of Laurent polynomials in $\mathbb{C}[t_1^{\pm1}, \ldots, t_n^{\pm 1}]$.</p>
<pre><code>R = LaurentPolynomialRing(CC, 't', n)
</code></pre>
<p>My first question: is there any method which would multiply elements of $R$ by a big enough monomial $t_1^{k_1}\cdot \ldots \cdot t_n^{k_n}$ to get rid of the negative powers and after that changed them to ordinary polynomials? I will consider resulting polynomials up to $t_1^{k_1}\cdot \ldots \cdot t_n^{k_n}$, so $k_i$'s don't matter that much as long as multiplication will result in a Laurent polynomial with non-negative powers (however I would like to avoid shifting exponents by some huge constant). At the moment I am using following workaround which I hope is redundant to some sage method.</p>
<pre><code># Since I am working over C I would like to get rid of summands coming from numeric errors
def clean_poly(laurent_poly, precision):
decomp = list(laurent_poly)
ret = 0
for (coeff, monom) in decomp:
if(coeff * coeff.conjugate() >= precision):
ret += coeff * monom
return ret
# Here I am finding lowest exponents of laurent_poly corresponding to particular t_i's
def find_shift(laurent_poly, n):
shift = [0 for i in range(0, n)]
for exp in laurent_poly.exponents():
for i in range(0, n):
shift[i] = max(shift[i], -exp[i])
return shift
def change_laurent_poly_to_ordinary(laurent_poly, R_ordinary, n):
laurent_poly = clean_poly(laurent_poly, 0.00000000001)
decomp = list(laurent_poly)
shift = find_shift(laurent_poly, n)
ret = 0
for (coeff, monom) in decomp:
exp = monom.exponents()
exp = exp[0]
summand = 1
for i in range(0, n):
summand *= (R_ordinary.gen(i)) ^ (exp[i] + shift[i])
ret += coeff * summand
return ret
</code></pre>
<p>Where $R_{\text{ordinary}} = \mathbb{C}[t_1, \ldots, t_n]$ is passed as argument in the last method.</p>
<pre><code>R_ordinary = PolynomialRing(CC, 's', n)
</code></pre>
<p>My second and the most important question is how to fix the last method to return a polynomial which posses a gcd() method just like in this snippet which I've found in the documentation (I can't post a link due to insufficient karma):</p>
<pre><code>sage: R, (x, y) = PolynomialRing(RationalField(), 2, 'xy').objgens()
sage: f = (x^3 + 2*y^2*x)^2
sage: g = x^2*y^2
sage: f.gcd(g)
x^2
</code></pre>
<p>Unfortunately at the moment method change_laurent_poly_to_ordinary returns class of type MPolynomial_polydict which doesn't have gcd() method, what results in the following error after attempting to use them:</p>
<pre><code>Traceback (most recent call last):
File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 28, in <module>
File "sage/structure/element.pyx", line 420, in sage.structure.element.Element.__getattr__ (/projects/sage/sage-6.10/src/build/cythonized/sage/structure/element.c:4675)
return getattr_from_other_class(self, P._abstract_element_class, name)
File "sage/structure/misc.pyx", line 259, in sage.structure.misc.getattr_from_other_class (/projects/sage/sage-6.10/src/build/cythonized/sage/structure/misc.c:1771)
raise dummy_attribute_error
AttributeError: 'MPolynomial_polydict' object has no attribute 'gcd'
</code></pre>
<p>I am working with sage version offered ...<span class="expander"> <a>(more)</a></span></p></div> https://ask.sagemath.org/question/33260/gcd-of-multivariable-polynomials-and-conversion-of-laurent-polynomials-to-ordinary-polynomials/?answer=33292#post-id-33292A more complete answer:
1. First question: The code you are writing does not appear to be redundant with any of Sage's methods. As a comment, note that given a Laurent polynomial `p`, you can access its parent (the Laurent polynomial ring) using `p.parent()`, and the corresponding polynomial ring using `p.parent().polynomial_ring()` so that you do not need to pass the polynomial ring as argument of your last method. In the same way, the number `n` of variables is accessible through `p.parent().ngens()`.
2. As I wrote in the comment, multivariate polynomials over `CC` have no method `gcd` yet. Actually, I forgot that I added such a method in the ticket [#20220](http://trac.sagemath.org/ticket/20220), that is merged in sage version 7.2beta1.¹ In the cloud.sagemath.com, the current version seems to be version 6.10 (type `sage -v` in a terminal to get the information), but you can access a more recent version, though potentially buggy, using `sage-develop` (which is 7.2beta6 apparently, so contains in particular the code I introduced in #20220).
¹ That is, it will be available in the future version 7.2 of Sage.Mon, 02 May 2016 15:31:53 +0200https://ask.sagemath.org/question/33260/gcd-of-multivariable-polynomials-and-conversion-of-laurent-polynomials-to-ordinary-polynomials/?answer=33292#post-id-33292