# 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 help

edit retag close merge delete

Sort by ยป oldest newest most voted

I did some code investigation and found the following explanation. The changes were introduced in trac ticket 23338, and you can see in the changes made on the polynomial_ring_constructor.py function, 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)

more

Thanks for this very clear explanation !

( 2018-03-06 15:52:28 -0600 )edit

What 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)

more

It works with k=binomial(n,2). My sage version is 7.5.1. Thanks !

( 2018-03-05 03:41:26 -0600 )edit

The 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.

( 2018-03-05 05:55:45 -0600 )edit