6

Please run the following code

    from sympy.solvers import solve
    from sympy import Symbol
    x = Symbol('x')
    R2 = solve(-109*x**5/3870720+4157*x**4/1935360-3607*x**3/69120+23069*x**2/60480+5491*x/2520+38-67,x)
    print R2

The output of the code is

[2*CRootOf(109*x**5 - 4157*x**4 + 50498*x**3 - 184552*x**2 - 527136*x + 3507840, 0), 2*CRootOf(109*x**5 - 4157*x**4 + 50498*x**3 - 184552*x**2 - 527136*x + 3507840, 1), 2*CRootOf(109*x**5 - 4157*x**4 + 50498*x**3 - 184552*x**2 - 527136*x + 3507840, 2), 2*CRootOf(109*x**5 - 4157*x**4 + 50498*x**3 - 184552*x**2 - 527136*x + 3507840, 3), 2*CRootOf(109*x**5 - 4157*x**4 + 50498*x**3 - 184552*x**2 - 527136*x + 3507840, 4)]

Can someone explain what the answer represent and how to get the output in conventional form i.e. say if the answer is 0.1,0.2,0.3,0.1,0.4 sympy usually outputs the answer as [0.1,0.2,0.3,0.1,0.4]

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
vikas_hada
  • 89
  • 2
  • 9

2 Answers2

3

To get numerical approximations in an answer, you can use N(). Since you have multiple solutions, you can loop through the list. I have used an easier equation since yours takes a while ...

Try this:

from sympy.solvers import solve

from sympy import Symbol, N
x = Symbol('x')
#R2 = solve(-109*x**5/3870720+4157*x**4/1935360-3607*x**3/69120+23069*x**2/60480+5491*x/2520+38-67,x)
R2 = solve(x**2+2*x-4,x)
print R2
print [N(solution) for solution in R2]

[EDIT]: As mentioned in the comments below, the fifth order equation can only be solved after upgrading sympy (to 1.0 in my case).

tfv
  • 6,016
  • 4
  • 36
  • 67
  • Thanks but if you could explain what the answers meant for e.g. 2*CRootOf(109*x*\*5 - 4157*x*\*4 + 50498*x*\*3 - 184552*x*\*2 - 527136*x + 3507840, 0) – vikas_hada Apr 23 '16 at 14:22
  • How long have you waited for a result? Did not get any within 20 sec. – tfv Apr 23 '16 at 14:28
  • There seems to be a problem, on my laptop it isn't taking much but on my friend's laptop it is taking 31.48 seconds – vikas_hada Apr 23 '16 at 15:03
  • I don't get anyything within 2 minutes. I only get results after I delete your fifth and fourth order term. My PC isn't that bad. Strange, sorry, I am out of the loop for your second question. – tfv Apr 23 '16 at 17:12
  • To improve the speed try updating the sympy version, my friend also updated sympy to version 1.0 and got results in seconds(already mentioned that he got it in 31.48 sec earlier). – vikas_hada Apr 24 '16 at 19:30
  • Thanks for the hint to upgrade sympy, works for my now. So what is the problem? After the upgrade, I do get the results listed above. CRootOf means "cubic root of", so this is before duing the numeric approcimation.. So you could write CRootOf(8) instead of 2. – tfv Apr 25 '16 at 05:41
  • The C in CRootOf stands for "complex". – asmeurer Apr 25 '16 at 17:37
2

SymPy's solve only gives symbolic solutions. CRootOf is a way of symbolically representing roots of polynomials whose roots can't be represented by radicals. If you are only interested in numeric solutions, you can use N on each of the terms as suggested by @tfv, or use nsolve, which solves the equation numerically. In general symbolic solve may be overkill if you only care about numeric solutions.

asmeurer
  • 86,894
  • 26
  • 169
  • 240