The very small imaginary part is due to floating point issues. You can
ignore it, or remove it with solution.evalf(chop=True).

The reason this shows up is because SymPy uses the cubic equation,
which requires imaginary numbers to express, even when the full root
is real (https://en.wikipedia.org/wiki/Casus_irreducibilis). This
means that the value is mathematically real, but due to floating point
inaccuracies, the floating point value ends up with a small imaginary
part.

Setting real=True causes SymPy to filter out non-real solutions. It
isn't smart enough to know that 6.10000000000001e-8 - 0.e-30*I, should
be real because it is only doing a simple filtering on the roots
without any consideration of the original equation.

Aaron Meurer

On Thu, May 28, 2020 at 3:13 PM Rainer Dorsch <[email protected]> wrote:
>
> Hello,
>
> when solving a cubic equation
>
> fk:  95.9271531456121*x - 1.7e-18/(x - 6.0e-7)**2
>
> I get a strange result:
>
> [6.10000000000001e-8 - 0.e-30*I, 3.85627081928849e-7 + 0.e-27*I,
> 7.53372918071151e-7 - 0.e-28*I]
>
> What does the 0.e-30*I mean? I do not think that there should be an imaginary
> part...
>
> Even worse, if I decleare x as real, I do get no solution at all.
>
>
> Here is some reference code
>
> import sympy as sp
> sp.init_printing()
>
>
> k,x=sp.symbols('k,x')
> #k,x=sp.symbols('k,x',real=True)
>
> f=k*x - 1.7e-18/(x - 6e-7)**2
>
> x_test=6.1e-8
> print("x_test set: ",x_test)
> fx=f.subs(x,x_test)
> print("fx: ",fx)
> k0=sp.solvers.solve(fx,k)[0]
> print("k: ",k0)
>
> print("x_test:")
> fk=sp.simplify(f.subs(k,k0))
> print("fk: ",fk)
>
> p1=sp.plotting.plot(fk,(x,0,1e-7))
>
> x_test=sp.solvers.solve(fk,x)
> print(x_test)
>
> and the output
>
> *** example end ***
>
> (sympy) rd@h370-wlan:~$ python3 online2.py
> x_test set:  6.1e-08
> fx:  6.1e-8*k - 5.85155634188234e-6
> /home/rd/virtualenv/sympy/lib/python3.7/site-packages/sympy/__init__.py:676:
> SymPyDeprecationWarning:
>
> importing sympy.solvers.solvers with 'from sympy import *' has been
> deprecated since SymPy 1.6. Use import sympy.solvers.solvers instead.
> See https://github.com/sympy/sympy/issues/18245 for more info.
>
>  deprecated_since_version="1.6").warn()
> k:  95.9271531456121
> x_test:
> fk:  95.9271531456121*x - 1.7e-18/(x - 6.0e-7)**2
> 2.8e-06 |                                                     ..
> | ...
> | ...
> | ...
> | ..
> | ...
> | ...
> | ...
> | ..
> | ...
> -9.5e-0 |-------------------------...---------------------------
> | ..
> | ...
> | ...
> | ..
> | ...
> | ..
> | ...
> | ...
> | ..
> -4.7e-0 |_______________________________________________________
>         0                          5e-08                      1e-07
> [6.10000000000001e-8 - 0.e-30*I, 3.85627081928849e-7 + 0.e-27*I,
> 7.53372918071151e-7 - 0.e-28*I]
> (sympy) rd@h370-wlan:~$
>
> ***** Output end *****
>
>
> Any hint is welcome
> Thanks
> Rainer
>
> --
> 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 view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/CAHXQ_BJj-dyPUiNZRi0bVSFPK1Cmi-VVnC5sDYc35GjwZg-q2g%40mail.gmail.com.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6J4juhFbyULWMhMoiVKn4O1HWCQ5pFsGnScpMND_xmpcQ%40mail.gmail.com.

Reply via email to