# How to exract the roots from “Roots”? [duplicate]

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?

## marked as duplicate by Artes, Kuba♦, m_goldberg, Dr. belisarius, xzczdJan 22 '15 at 4:58

• Why not simply use Solve[] rather than Roots[]? Max[x /. Solve[x^2 + 15 x + 13 == 0, x]] – David G. Stork Jan 21 '15 at 17:57
• Thanks! What is the benefit of using Solve over Roots? – user6818 Jan 21 '15 at 18:05
• 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. – David G. Stork Jan 21 '15 at 18:09
• 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. – 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.

Example:

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

ToRules[roots]
(* 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.

• I agree. Thanks. – 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]]


or

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