Ask Your Question
2

Getting the leading term from random polynomials

asked 2016-11-22 19:45:34 +0100

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:

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

1 Answer

Sort by ยป oldest newest most voted
3

answered 2016-11-22 20:51:18 +0100

slelievre gravatar image

updated 2016-11-22 20:53:19 +0100

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

edit flag offensive delete link more

Comments

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

mathochist gravatar imagemathochist ( 2016-11-28 19:16:59 +0100 )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 10:50:18 +0100 )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

Stats

Asked: 2016-11-22 19:45:34 +0100

Seen: 1,030 times

Last updated: Nov 22 '16