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.Sat, 02 May 2015 16:29:21 +0200Finding complex rootshttps://ask.sagemath.org/question/26702/finding-complex-roots/ So, what is the best way to find complex roots of polynomials?
I only found that [one](http://www.sagemath.org/doc/reference/polynomial_rings/sage/rings/polynomial/complex_roots.html):
sage: from sage.rings.polynomial.complex_roots import complex_roots
sage: x=polygen(ZZ)
sage: complex_roots(1.3*x^3-x^2-5*x-1)
[(-1.479272103982880?, 1), (-0.2113935621844148?, 1),
(2.459896435398064?, 1)]
Should I use that? Isn't there an inbuilt function? Even PARI/GP has one, namely `polroots`...
And why is the `x` casted as an element of $\mathbb{Z}[X]$, why not $\mathbb{C}[X]$? Can somebody explain that to me?
Thank you!Sat, 02 May 2015 16:05:56 +0200https://ask.sagemath.org/question/26702/finding-complex-roots/Answer by tmonteil for <p>So, what is the best way to find complex roots of polynomials?</p>
<p>I only found that <a href="http://www.sagemath.org/doc/reference/polynomial_rings/sage/rings/polynomial/complex_roots.html">one</a>:</p>
<pre><code>sage: from sage.rings.polynomial.complex_roots import complex_roots
sage: x=polygen(ZZ)
sage: complex_roots(1.3*x^3-x^2-5*x-1)
[(-1.479272103982880?, 1), (-0.2113935621844148?, 1),
(2.459896435398064?, 1)]
</code></pre>
<p>Should I use that? Isn't there an inbuilt function? Even PARI/GP has one, namely <code>polroots</code>...</p>
<p>And why is the <code>x</code> casted as an element of $\mathbb{Z}[X]$, why not $\mathbb{C}[X]$? Can somebody explain that to me?</p>
<p>Thank you!</p>
https://ask.sagemath.org/question/26702/finding-complex-roots/?answer=26703#post-id-26703When you write
sage: x = polygen(ZZ)
you define the variable `x` as an element of the polynomial ring in one variable over the integers:
sage: x.parent()
Univariate Polynomial Ring in x over Integer Ring
Hence you can define any polynomial in $\mathbb{Z}[x]$ and find its roots as follows:
sage: P = 2*x - 3
sage: P
2*x - 3
sage: P.roots()
[]
As you can see, there is no root in $\mathbb{Z}$, if you want to have the roots in $\mathbb{Q}$:
sage: P.roots(QQ)
[(3/2, 1)]
You can also change the parent of the polynomial to be the rationals:
sage: Q = P.change_ring(QQ)
sage: Q
2*x - 3
sage: Q.parent()
Univariate Polynomial Ring in x over Rational Field
sage: Q.roots()
[(3/2, 1)]
Of course you can define `x` as being a monomial over the retionals:
sage: x = polygen(QQ)
sage: x.parent()
Univariate Polynomial Ring in x over Rational Field
Concerning complex roots, let me just add that the rings `CC` and `CDF` (faster) are floating -point approximation (fast, but inexact), there is also the field of algebraic numbers to work in:
sage: x = polygen(ZZ)
sage: P = x^2-2
sage: P.roots()
[]
sage: P.roots(QQ)
[]
sage: P.roots(CC)
[(-1.41421356237310, 1), (1.41421356237310, 1)]
sage: P.roots(CC)[0][0]^2
2.00000000000000
sage: P.roots(CC)[0][0]^2 == 2
False
sage: P.roots(CC)[0][0]^2 - 2
4.44089209850063e-16
sage: P.roots(QQbar)
[(-1.414213562373095?, 1), (1.414213562373095?, 1)]
sage: P.roots(QQbar)[0][0]^2 == 2
True
Sat, 02 May 2015 16:29:21 +0200https://ask.sagemath.org/question/26702/finding-complex-roots/?answer=26703#post-id-26703