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.Wed, 17 Oct 2012 06:33:03 +0200roots() returns no real solutions for cubic functionhttps://ask.sagemath.org/question/9437/roots-returns-no-real-solutions-for-cubic-function/Hi there
I have a pretty complicated third-order polynomial which I use Sage to solve. However, somehow roots() only returns imaginary solutions. This should not be possible as a cubic function always has at least one root, so naturally I'm puzzled by this.
I could't seem to include all my code (formatting gets messed up).
First I checked that the equation really is a polynomial in m:
print dldm.degree(m)
Outputs 3, so that's OK. Next, I obtain the roots, which I have printed below:
-0.000119434160296805 + 2.27373675443232e-12*I
-5395.14738658974 - 7.95807864051312e-13*I
2560.33896686341 - 1.36424205265939e-12*I
As you can see, they're all imaginary, which should not be possible. The imaginary part is very small on all the solutions. My guess is that Sage uses some numerical approximations to obtain the solutions which is why it seemingly violates mathematics. But what do you think? Thanks!Wed, 17 Oct 2012 02:21:52 +0200https://ask.sagemath.org/question/9437/roots-returns-no-real-solutions-for-cubic-function/Comment by achrzesz for <p>Hi there</p>
<p>I have a pretty complicated third-order polynomial which I use Sage to solve. However, somehow roots() only returns imaginary solutions. This should not be possible as a cubic function always has at least one root, so naturally I'm puzzled by this. </p>
<p>I could't seem to include all my code (formatting gets messed up).
First I checked that the equation really is a polynomial in m:</p>
<pre><code>print dldm.degree(m)
</code></pre>
<p>Outputs 3, so that's OK. Next, I obtain the roots, which I have printed below:</p>
<pre><code>-0.000119434160296805 + 2.27373675443232e-12*I
-5395.14738658974 - 7.95807864051312e-13*I
2560.33896686341 - 1.36424205265939e-12*I
</code></pre>
<p>As you can see, they're all imaginary, which should not be possible. The imaginary part is very small on all the solutions. My guess is that Sage uses some numerical approximations to obtain the solutions which is why it seemingly violates mathematics. But what do you think? Thanks!</p>
https://ask.sagemath.org/question/9437/roots-returns-no-real-solutions-for-cubic-function/?comment=18854#post-id-18854Perhaps using more precision can help
p.roots(ring=RealField(300))
p.roots(ring=ComplexField(300))Wed, 17 Oct 2012 06:21:31 +0200https://ask.sagemath.org/question/9437/roots-returns-no-real-solutions-for-cubic-function/?comment=18854#post-id-18854Answer by Volker Braun for <p>Hi there</p>
<p>I have a pretty complicated third-order polynomial which I use Sage to solve. However, somehow roots() only returns imaginary solutions. This should not be possible as a cubic function always has at least one root, so naturally I'm puzzled by this. </p>
<p>I could't seem to include all my code (formatting gets messed up).
First I checked that the equation really is a polynomial in m:</p>
<pre><code>print dldm.degree(m)
</code></pre>
<p>Outputs 3, so that's OK. Next, I obtain the roots, which I have printed below:</p>
<pre><code>-0.000119434160296805 + 2.27373675443232e-12*I
-5395.14738658974 - 7.95807864051312e-13*I
2560.33896686341 - 1.36424205265939e-12*I
</code></pre>
<p>As you can see, they're all imaginary, which should not be possible. The imaginary part is very small on all the solutions. My guess is that Sage uses some numerical approximations to obtain the solutions which is why it seemingly violates mathematics. But what do you think? Thanks!</p>
https://ask.sagemath.org/question/9437/roots-returns-no-real-solutions-for-cubic-function/?answer=14158#post-id-14158You should tell us how you construct your polynomial and what its parent is if you want meaningful advice. Basically, if the polynomial is over an exact ring like the rationals then the roots will be exact (if they are rational). If the polynomial is over real floating-point ring the real roots will be numerically computed. If the polynomial is over a complex floating-point ring the roots will be numerically computed and complex.
sage: R.<x> = QQ[]
sage: (x^3-8).roots()
[(2, 1)]
sage: (x^3-2).roots() # one real root but not rational
[]
sage: (x^3-2).roots(ring=CDF)
[(1.25992104989, 1), (-0.629960524947 - 1.09112363597*I, 1), (-0.629960524947 + 1.09112363597*I, 1)]
sage: R.<x> = CDF[]
sage: (x^3-8).roots()
[(2.0, 1), (-1.0 - 1.73205080757*I, 1), (-1.0 + 1.73205080757*I, 1)]
sage: (x^3-2).roots()
[(1.25992104989, 1), (-0.629960524947 - 1.09112363597*I, 1), (-0.629960524947 + 1.09112363597*I, 1)]
Its unavoidable that numerical root solvers sometimes return a tiny imaginary part. Wed, 17 Oct 2012 06:33:03 +0200https://ask.sagemath.org/question/9437/roots-returns-no-real-solutions-for-cubic-function/?answer=14158#post-id-14158