# How to find the degree of a polynomial Anonymous

Is there a function that takes in a polynomial and returns its degree? For example: suppose we have the ring R = PolynomialRing(ZZ, 'x'), and the polynomial p = (x-1)*(x-2). The degree function I'm looking for should return 2 when applied to p. Does such a function exist??

edit retag close merge delete

Sort by » oldest newest most voted

Polynomials (and symbolic expressions) have a degree method.

Beware that even after defining R as in the question, x is still a "symbolic variable" in the "symbolic ring".

In a fresh Sage session:

sage: R = PolynomialRing(ZZ, 'x')  # defines R but not x

sage: x.parent()
Symbolic Ring

sage: q = (x - 1) * (x - 2)
sage: q
(x - 1)*(x - 2)

sage: q.parent()
Symbolic Ring


Symbolic expressions have a degree method that gives the degree with respect to any chosen variable.

sage: q.degree(x)
2


Declare x as the indeterminate in R.

sage: x = R.gen()


Then:

sage: x.parent()
Univariate Polynomial Ring in x over Integer Ring

sage: p = (x - 1) * (x - 2)
sage: p
x^2 - 3*x + 2

sage: p.parent()
Univariate Polynomial Ring in x over Integer Ring


Or:

sage: p = R(q)
sage: p
x^2 - 3*x + 2

sage: p.parent()
Univariate Polynomial Ring in x over Integer Ring


To get the degree, use the degree method (no need to specify that it is with respect to x now, since p is a univariate polynomial):

sage: p.degree()
2


Sage slightly extends Python's syntax to enable defining R and x at once. For example:

sage: R.<x> = PolynomialRing(ZZ)


is equivalent to:

sage: R = PolynomialRing(ZZ, 'x')
sage: x = R.gen()

more