# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.73109907,"math_prob":0.98511845,"size":1181,"snap":"2023-40-2023-50","text_gpt3_token_len":381,"char_repetition_ratio":0.17247239,"word_repetition_ratio":0.19534884,"special_character_ratio":0.30059272,"punctuation_ratio":0.16793893,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9971621,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-25T22:04:08Z\",\"WARC-Record-ID\":\"<urn:uuid:ce1eaf67-567e-4b90-a112-cbb14b03e08f>\",\"Content-Length\":\"52889\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ff17ba87-1b07-4ff4-ad76-0ac89374786d>\",\"WARC-Concurrent-To\":\"<urn:uuid:90dc650b-4c53-4405-a844-4b9902aa4b0b>\",\"WARC-IP-Address\":\"194.254.163.53\",\"WARC-Target-URI\":\"https://ask.sagemath.org/question/56440/how-to-find-the-degree-of-a-polynomial/?answer=56444\",\"WARC-Payload-Digest\":\"sha1:53MAZQSJNMKDU2OLCDYVLESIQARFAGC7\",\"WARC-Block-Digest\":\"sha1:WOOUAWOVYZ3LMZPYWOMXEA52NSNVCZZ7\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510100.47_warc_CC-MAIN-20230925215547-20230926005547-00049.warc.gz\"}"}