ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 11 Dec 2016 10:50:18 +0100Getting the leading term from random polynomialshttps://ask.sagemath.org/question/35691/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.
Tue, 22 Nov 2016 19:45:34 +0100https://ask.sagemath.org/question/35691/getting-the-leading-term-from-random-polynomials/Answer by slelievre for <p>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:</p>
<pre><code>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)
</code></pre>
<p>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:</p>
<pre><code>po['ly1']
po['ly1'].degree(x)
po['ly1'].lt(x)
po['ly1'].lc(x)
</code></pre>
<p>Getting the degree works, but not the leading term or the leading coefficient. The errors I get are:</p>
<pre><code>AttributeError: 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' object has no attribute 'lt'
</code></pre>
<p>and </p>
<pre><code>AttributeError: 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint' object has no attribute 'lc'
</code></pre>
<p>respectively.</p>
<p>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.</p>
https://ask.sagemath.org/question/35691/getting-the-leading-term-from-random-polynomials/?answer=35692#post-id-35692The 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)`.Tue, 22 Nov 2016 20:51:18 +0100https://ask.sagemath.org/question/35691/getting-the-leading-term-from-random-polynomials/?answer=35692#post-id-35692Comment by mathochist for <p>The easiest for your question is to explore using the <tab> key.</p>
<p>Having run your code, I got</p>
<pre><code>sage: p = po['ly1']
-x + 2
</code></pre>
<p>then type this followed by <tab></p>
<pre><code>sage: p.
</code></pre>
<p>and you will see a list of available methods.</p>
<p>The method you are looking for is <code>leading_coefficient</code>.</p>
<pre><code>sage: p.leading_coefficient()
-1
</code></pre>
<p>For the leading term, I'm afraid you have to do</p>
<pre><code>sage: p.leading_coefficient() * x^p.degree()
-x
</code></pre>
<p>Note that since the polynomials are univariate, you can use <code>.degree()</code>
and you don't have to specify <code>.degree(x)</code>.</p>
https://ask.sagemath.org/question/35691/getting-the-leading-term-from-random-polynomials/?comment=35765#post-id-35765Thanks very much, for that <tab> hint especially!Mon, 28 Nov 2016 19:16:59 +0100https://ask.sagemath.org/question/35691/getting-the-leading-term-from-random-polynomials/?comment=35765#post-id-35765Comment by slelievre for <p>The easiest for your question is to explore using the <tab> key.</p>
<p>Having run your code, I got</p>
<pre><code>sage: p = po['ly1']
-x + 2
</code></pre>
<p>then type this followed by <tab></p>
<pre><code>sage: p.
</code></pre>
<p>and you will see a list of available methods.</p>
<p>The method you are looking for is <code>leading_coefficient</code>.</p>
<pre><code>sage: p.leading_coefficient()
-1
</code></pre>
<p>For the leading term, I'm afraid you have to do</p>
<pre><code>sage: p.leading_coefficient() * x^p.degree()
-x
</code></pre>
<p>Note that since the polynomials are univariate, you can use <code>.degree()</code>
and you don't have to specify <code>.degree(x)</code>.</p>
https://ask.sagemath.org/question/35691/getting-the-leading-term-from-random-polynomials/?comment=35979#post-id-35979New: [Sage trac ticket 21608](https://trac.sagemath.org/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"](https://ask.sagemath.org/question/35031/leading-coefficient-polynomial/).Sun, 11 Dec 2016 10:50:18 +0100https://ask.sagemath.org/question/35691/getting-the-leading-term-from-random-polynomials/?comment=35979#post-id-35979