Ask Your Question
1

Obtaining the polynomial given its list of coefficients while using SageMath as an imported library in python

asked 2021-04-17 21:00:39 +0100

Kanaad gravatar image

Hi, I have imported the SageMath library in my python code.

If I have a list [1,5,4,5,22,0,0,1] And I need to generate a 7th degree polynomial from this, how should I proceed?

I tried using the methods given online, but it either crashes or gives me a clearly wrong answer ( I'm trying to find all the real roots by using real_roots() or roots() )

Any help would be appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2021-04-18 12:44:18 +0100

tmonteil gravatar image

updated 2021-04-18 12:48:14 +0100

Let us start with defining your list and give it a Python name:

sage: L = [1,5,4,5,22,0,0,1]

You first have to construct the polynomial ring in which your polynomial will leave (its parent):

sage: R = PolynomialRing(QQ,'x')
sage: R                                                                                                                                                                                                      
Univariate Polynomial Ring in x over Rational Field

Then you can construct the polynomial from the list:

sage: P = R(L)                                                                                                                                                                                               
sage: P                                                                                                                                                                                                      
x^7 + 22*x^4 + 5*x^3 + 4*x^2 + 5*x + 1

You can look for its roots:

sage: P.roots()                                                                                                                                                                                              
[]

As you can see, there is no roots, this is because the polynomial is defined over the rational field QQ.

If you want the real roots as numerical numbers, you can do:

sage: P.roots(RDF)                                                                                                                                                                                           
[(-2.7359871581727626, 1),
 (-0.44920797825715253, 1),
 (-0.25305397624275894, 1)]

If you want the real roots as algebraic numbers, you can do:

sage: P.roots(AA)                                                                                                                                                                                            
[(-2.735987158172763?, 1),
 (-0.4492079782571527?, 1),
 (-0.2530539762427587?, 1)]

If you do not care about the multiplicities, you can do :

sage: P.real_roots()                                                                                                                                                                                         
[-2.73598715817276, -0.449207978257153, -0.253053976242759]

Side remark: the previous code should work within a Python script (if you do the correct import statements) since i disabled the Sage preparser when preparing the answer:

sage: preparser(False)

If you get a complain that he name PolynomialRing is not defined, you have to import it at the beginning of your script:

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing

To be able to guess such import statement, you can do, within a Sage session:

sage: import_statements(PolynomialRing)                                                                                                                                                                      
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
edit flag offensive delete link more

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: 2021-04-17 21:00:39 +0100

Seen: 277 times

Last updated: Apr 18 '21