Sympy returning weird polynomial roots

30 views
Skip to first unread message

Cesar Gomes

unread,
Oct 3, 2018, 2:03:22 AM10/3/18
to sympy
Hello everyone!

While trying to find the zeros of the following polynomial: 


q = (-2/15)*x**3 + (23/10)*x**2 - (47/30)*x - 21


Sympy is returning 3 complex roots, which makes no sense since there are 3 real roots. Using nsolve the results are as expected.


What am I missing?


I'm using Python 3.6.4 and IPython 6.2.1 on OS X Mojave.

sympy_poly.png

Kalevi Suominen

unread,
Oct 3, 2018, 2:16:28 AM10/3/18
to sympy
It is a classical issue, known as Casus irreducibilis, that the algebraic expressions of the roots of an irreducible cubic polynomial involve complex expressions even in case the roots are real. (See also this for more information.)

Kalevi Suominen

Aaron Meurer

unread,
Oct 3, 2018, 2:39:45 AM10/3/18
to sy...@googlegroups.com
More specifically, because of the imaginary components in the analytic solutions, the numeric evaluation results in a small imaginary part due to round off errors inherent in floating point. SymPy automatically evaluates the solution to floats because the input had float coefficients, but they are still found using the analytic solution to the cubic when you use solve(). Only nsolve() uses purely numeric algorithms like Newton's method, which avoid nonreal values for real cubics. 

You can get rid of them by doing [i.evalf(chop=True) for i in sols]. 

Aaron Meurer 

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/8ccf7f66-980b-486f-aa04-d50bd8d44560%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages