Ask Your Question

Getting the leading term from random polynomials

asked 2016-11-22 12:45:34 -0500

mathochist gravatar image

Hi there. I'm writing self generating algebra tests for a class I teach. I'm using this code to generate random polynomials of various degrees up to 8 with single digit integer coefficients:

R.<x> = PolynomialRing(ZZ)
po = {}
for index in range(1, 10):          # Picks random polynomials for use.
    deg = ZZ.random_element(0, 8)
    po["ly{0}".format(index)] = R.random_element(deg, -9, 10)

To make the answer key, I'm attempting to show the polynomial, its degree, its leading term, and its leading coefficient of each polynomial. My code for that looks like:


Getting the degree works, but not the leading term or the leading coefficient. The errors I get are:

AttributeError: 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' object has no attribute 'lt'


AttributeError: 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' object has no attribute 'lc'


I have been unable to ascertain from the documentation what the correct commands should actually look like. I have tried both ".lt()" and ".lt(x)" and similar for .lc. Please let me know, if you can, what my error is. Thank you.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-11-22 13:51:18 -0500

updated 2016-11-22 13:53:19 -0500

The easiest for your question is to explore using the <tab> key.

Having run your code, I got

sage: p = po['ly1']
-x + 2

then type this followed by <tab>

sage: p.

and you will see a list of available methods.

The method you are looking for is leading_coefficient.

sage: p.leading_coefficient()

For the leading term, I'm afraid you have to do

sage: p.leading_coefficient() * x^

Note that since the polynomials are univariate, you can use .degree() and you don't have to specify .degree(x).

edit flag offensive delete link more


Thanks very much, for that <tab> hint especially!

mathochist gravatar imagemathochist ( 2016-11-28 12:16:59 -0500 )edit

New: Sage trac ticket 21608 provides methods for "leading term", "leading coefficient", "leading monomial", and is available in Sage 7.5.beta6.

See also Ask Sage question 35031 "leading coefficient polynomial".

slelievre gravatar imageslelievre ( 2016-12-11 03:50:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-11-22 12:45:34 -0500

Seen: 39 times

Last updated: Nov 22 '16