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 On Wed, Oct 3, 2018 at 12:16 AM Kalevi Suominen <[email protected]> wrote: > It is a classical issue, known as Casus irreducibilis > <https://en.wikipedia.org/wiki/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 > <https://github.com/sympy/sympy/issues/14690> for more information.) > > Kalevi Suominen > > On Wednesday, October 3, 2018 at 9:03:22 AM UTC+3, Cesar Gomes wrote: >> >> 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. >> > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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 > <https://groups.google.com/d/msgid/sympy/8ccf7f66-980b-486f-aa04-d50bd8d44560%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAKgW%3D6%2Bm4mLmWYr1_bGDY-Qupsfrtr-y1cwfNt4v0AbN9nO4cQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
