# Improving Performance – Finding Polynomial Roots [duplicate]

Does NRoots own an abstract counterpart? If not, can we write one?

I’m fairly new to Mathematica. In the past, my usage has mostly been limited to solving the occasional equation, making some plots, and working with small scaled statistics. None of these have been requiring regarding performance so I typically did not give much thought to optimizing my code.

Right now, I am working on a project that requires a large number of polynomial roots. I have written the following code to find these roots for polynomials with coefficients (1) and (-1).

fileloc = FileNameJoin[{NotebookDirectory[], “data”}];
out = OpenWrite [fileloc];

maxDeg = 12;
f[c_, x_, m_] = c*x^m;
coefficients = {-1, 1};

(* Making List of Functions *)
polyList = coefficients;
n = 1;
While[n <= maxDeg, Do[Do[polyList = Append[polyList, j + f[i, x, n]], {i, coefficients}], {j, polyList}]; n++]; (* Solving and Printing *) Do[Write[out, N[Solve[i == 0, x]]], {i, polyList}]; Close[out]; I have run this script up to a maximum degree of 12. Doing that took roughly 7 hours to complete but it did not create enough roots for my needs. Could someone give me some suggestions as to where I should start to improve my code? Moreover, are there any good resources that could tell me some of the differences between similar built-in-functions (ie Solve, NSolve, FindRoot)? As an aside, I have noticed in Parallel Kernel Status, the four cores assigned to my Local kernel are all idle while my script is running. I am not sure how to interpret this but it seemed somewhat odd to me. ================= ================= 1 Answer 1 ================= This function prints out the solutions to all polynomials of degree d with coefficients of unit magnitude. printAllSolutions[d_] := Do[ With[{p = FromDigits[c~Prepend~1, x]}, Print[Expand[p] -> (x /. NSolve[p == 0, x])]
],
{c, Tuples[{-1, +1}, d]}
]

I made one optimization, which is that the leading coefficient is always 1. If it is allowed to be -1, then for each positive polynomial we will have a corresponding negative polynomial with the same roots. This cuts our work in half.

Instead of generating the list iteratively like you do, I generate the lists of coefficcents directly with Tuple, then use FromDigits to convert to a polynomial. I use NSolve instead of N@Solve, which affords a significant speedup.