# Getting the leading term from random polynomials

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:

po['ly1']
po['ly1'].degree(x)
po['ly1'].lt(x)
po['ly1'].lc(x)


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'


and

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


respectively.

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 close merge delete

Sort by » oldest newest most voted

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()
-1


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

sage: p.leading_coefficient() * x^p.degree()
-x


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

more

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