# How to get all (numerical) solutions of an equation? Mathematica's NSolve can produce all roots of a polynomial equation, like this:

sage: mathematica('NSolve[9*x^6 + 4*x^4 + 3*x^3 + x - 17 == 0,  x]')
{{x -> -1.1030150726298147},
{x -> -0.49110203599909275 - 0.9883314953720708*I},
{x -> -0.49110203599909275 + 0.9883314953720708*I},
{x -> 0.5426095723140001 - 1.0543115206871092*I},
{x -> 0.5426095723140001 + 1.0543115206871092*I}, {x -> 1.}}


OTOH, Sage's solve gives just one real solution:

sage: solve(9*x^6 + 4*x^4 + 3*x^3 + x - 17 == 0,  x)
[x == 1, 0 == 9*x^5 + 9*x^4 + 13*x^3 + 16*x^2 + 16*x + 17]


Is there a simple way to get all solutions?

edit retag close merge delete

Sort by » oldest newest most voted You can use the roots method and specify that you want the results as complex numbers (say as elements of CC in Sage):

sage: eq = 9.0*x^6 + 4*x^4 + 3*x^3 + x - 17 == 0
sage: eq.roots(x, ring=CC, multiplicities=False)
[-1.10301507262981,
1.00000000000000,
-0.491102035999093 - 0.988331495372071*I,
-0.491102035999093 + 0.988331495372071*I,
0.542609572314000 - 1.05431152068711*I,
0.542609572314000 + 1.05431152068711*I]

more

Yet another ring raising its ugly head ;) I wish I had time to make this easier to figure out.

1

You may also select only the real portion of the answer: by doing something like : [ (real(cp)) for cp in (eq).roots(x, ring=CC, multiplicities=False)] . This will remove the imaginary portion of the answer.

import numpy coeff=[9,0,4,3,0,1,-17] p=numpy.poly1d(coeff) r=numpy.roots(p) print str(r)

more It's strange, but I have tried this:

sage: eq = 9*x^6 + 4*x^4 + 3*x^3 + x - 17 == 0
sage: s = eq.solve(x, to_poly_solve = True)
sage: s
[0 == 9*x^5 + 9*x^4 + 13*x^3 + 16*x^2 + 16*x + 17, x == 1]
sage: s.solve(x, to_poly_solve = True)
[x == -1.10301507538, x == (-0.491102035999 - 0.988331495372*I), x == (0.542609572314 + 1.05431152069*I), x == (10757/10884*I - 3008/6125), x == (-2213/2099*I + 1127/2077)]


I expected the to_poly_solve option to work in the first case, but it didn't.

more