Given how the output looks like, as in a set of "==" assignments, is it possible to extract specific roots from the output of, http://reference.wolfram.com/language/ref/Roots.html ?

And then do a comparison?

Like if I want to extract the largest root among them?

  • $\begingroup$ Why not simply use Solve[] rather than Roots[]? Max[x /. Solve[x^2 + 15 x + 13 == 0, x]] $\endgroup$ – David G. Stork Jan 21 '15 at 17:57
  • $\begingroup$ Thanks! What is the benefit of using Solve over Roots? $\endgroup$ – user6818 Jan 21 '15 at 18:05
  • $\begingroup$ The benefit of Solve is that it gives a List of possible solutions, so you can easily find the Max, Min, Total, Length (number of roots) or whatever. Please give a specific example (code) so we can better help you. $\endgroup$ – David G. Stork Jan 21 '15 at 18:09
  • $\begingroup$ Roots only works with polynomials. Try Solve[Sin[x]==0,x] vs Roots[Sin[x]==0,x]. In earlier versions of Mathematica Solve would only have returned 0 which is only one solution out of many and you would have had to use Reduce[Sin[x]==0, x] to get all of them. $\endgroup$ – Szabolcs Jan 21 '15 at 18:34

You can use ToRules to convert the output to a list of rules.

From the documentation:

ToRules takes logical combinations of equations, in the form generated by Roots and Reduce, and converts them to lists of rules, of the form produced by Solve.


roots = Roots[x^2 + 1 == 0, x]
(* x == I || x == -I *)

(* Sequence[{x -> I}, {x -> -I}] *)

x /. {ToRules[roots]}
(* {I, -I} *)

Now you have a simple list of rules to work with.

Note that extracting part of the output using functions such as Part, Level, etc. is not reliable. What if you have a single root? You'd need to have a special case for that. ToRules always works and it's simpler.

| improve this answer | |
  • $\begingroup$ I agree. Thanks. $\endgroup$ – David G. Stork Jan 21 '15 at 20:00

If you need to compute Roots, you can then extract the numerical roots this way:

myRoots = Roots[x^2 + 15 x + 13 == 0, x];

Max@Level[myRoots, 1, Heads -> False][[All, 2]]


Sort@Level[myRoots, 1, Heads -> False][[All, 2]]
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.