ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 06 Mar 2018 15:52:28 -0600creating a polynomial ringhttp://ask.sagemath.org/question/41395/creating-a-polynomial-ring/The following code
n=4
k=n*(n-1)/2
L=PolynomialRing(ZZ,"x",k)
produces the following error
if second arguments is a string with no commas, then there must be no other non-optional arguments...
If I replace k by 6 or binomial(n,2) everything works, I cannot understand why. Thanks for any helpSun, 04 Mar 2018 18:30:01 -0600http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/Answer by tmonteil for <p>The following code</p>
<pre><code>n=4
k=n*(n-1)/2
L=PolynomialRing(ZZ,"x",k)
</code></pre>
<p>produces the following error</p>
<pre><code>if second arguments is a string with no commas, then there must be no other non-optional arguments...
</code></pre>
<p>If I replace k by 6 or binomial(n,2) everything works, I cannot understand why. Thanks for any help</p>
http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?answer=41400#post-id-41400I did some code investigation and found the following explanation. The changes were introduced in [trac ticket 23338](https://trac.sagemath.org/ticket/23338), and you can see in [the changes made on the polynomial_ring_constructor.py function](https://git.sagemath.org/sage.git/diff/src/sage/rings/polynomial/polynomial_ring_constructor.py?id=0ef62928875ef9c858c04a16ce7dff33a55825d6), that before, your `k` was tested to be an integer (see the `isinstance(arg1, integer_types + (Integer,))` tests), this is why you get an error since your `k` is a rational, not an integer:
sage: k
6
sage: k.parent()
Rational Field
sage: from six import integer_types
sage: isinstance(k, integer_types + (Integer,))
False
In the recent implementation, your `k` is transformed into an integer, see `k = Integer(arg)`, this is why your code works on recent versions of Sage.
So, though i encourage you to upgrade your Sage installation, you can also transform your `k` into an integer:
sage: k = ZZ(n*(n-1)/2)
or
sage: k = Integer(n*(n-1)/2)Mon, 05 Mar 2018 04:13:46 -0600http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?answer=41400#post-id-41400Comment by vincent.beck for <p>I did some code investigation and found the following explanation. The changes were introduced in <a href="https://trac.sagemath.org/ticket/23338">trac ticket 23338</a>, and you can see in <a href="https://git.sagemath.org/sage.git/diff/src/sage/rings/polynomial/polynomial_ring_constructor.py?id=0ef62928875ef9c858c04a16ce7dff33a55825d6">the changes made on the polynomial_ring_constructor.py function</a>, that before, your <code>k</code> was tested to be an integer (see the <code>isinstance(arg1, integer_types + (Integer,))</code> tests), this is why you get an error since your <code>k</code> is a rational, not an integer:</p>
<pre><code>sage: k
6
sage: k.parent()
Rational Field
sage: from six import integer_types
sage: isinstance(k, integer_types + (Integer,))
False
</code></pre>
<p>In the recent implementation, your <code>k</code> is transformed into an integer, see <code>k = Integer(arg)</code>, this is why your code works on recent versions of Sage.</p>
<p>So, though i encourage you to upgrade your Sage installation, you can also transform your <code>k</code> into an integer:</p>
<pre><code>sage: k = ZZ(n*(n-1)/2)
</code></pre>
<p>or </p>
<pre><code>sage: k = Integer(n*(n-1)/2)
</code></pre>
http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?comment=41416#post-id-41416Thanks for this very clear explanation !Tue, 06 Mar 2018 15:52:28 -0600http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?comment=41416#post-id-41416Answer by slelievre for <p>The following code</p>
<pre><code>n=4
k=n*(n-1)/2
L=PolynomialRing(ZZ,"x",k)
</code></pre>
<p>produces the following error</p>
<pre><code>if second arguments is a string with no commas, then there must be no other non-optional arguments...
</code></pre>
<p>If I replace k by 6 or binomial(n,2) everything works, I cannot understand why. Thanks for any help</p>
http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?answer=41397#post-id-41397What version of Sage are you using?
It works for me in Sage 8.2.beta4.
sage: version()
'SageMath version 8.2.beta4, Release Date: 2018-01-27'
sage: n = 4
sage: k = n*(n-1)/2
sage: L = PolynomialRing(ZZ, k, "x")
sage: L
Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5 over Integer Ring
What happens if you define `k` as a integer instead of a rational, as follows?
k = n*(n-1)//2
or
sage: k = binomial(n, 2)
Mon, 05 Mar 2018 00:56:27 -0600http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?answer=41397#post-id-41397Comment by slelievre for <p>What version of Sage are you using?</p>
<p>It works for me in Sage 8.2.beta4.</p>
<pre><code>sage: version()
'SageMath version 8.2.beta4, Release Date: 2018-01-27'
sage: n = 4
sage: k = n*(n-1)/2
sage: L = PolynomialRing(ZZ, k, "x")
sage: L
Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5 over Integer Ring
</code></pre>
<p>What happens if you define <code>k</code> as a integer instead of a rational, as follows?</p>
<pre><code>k = n*(n-1)//2
</code></pre>
<p>or</p>
<pre><code>sage: k = binomial(n, 2)
</code></pre>
http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?comment=41403#post-id-41403The problem is that `n*(n-1)/2` returns a rational. Using `binomial(n, 2)` or `n * (n-1) // 2`, you get an integer.
See @tmonteil's more in-depth explanation.Mon, 05 Mar 2018 05:55:45 -0600http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?comment=41403#post-id-41403Comment by vincent.beck for <p>What version of Sage are you using?</p>
<p>It works for me in Sage 8.2.beta4.</p>
<pre><code>sage: version()
'SageMath version 8.2.beta4, Release Date: 2018-01-27'
sage: n = 4
sage: k = n*(n-1)/2
sage: L = PolynomialRing(ZZ, k, "x")
sage: L
Multivariate Polynomial Ring in x0, x1, x2, x3, x4, x5 over Integer Ring
</code></pre>
<p>What happens if you define <code>k</code> as a integer instead of a rational, as follows?</p>
<pre><code>k = n*(n-1)//2
</code></pre>
<p>or</p>
<pre><code>sage: k = binomial(n, 2)
</code></pre>
http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?comment=41399#post-id-41399It works with k=binomial(n,2). My sage version is 7.5.1. Thanks !Mon, 05 Mar 2018 03:41:26 -0600http://ask.sagemath.org/question/41395/creating-a-polynomial-ring/?comment=41399#post-id-41399