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.
maxDeg=9it took 28 seconds on my machine. FormaxDeg=10it took around 5 minutes. I expect roughly a factor of 9 for each increment (factor of 3 growth, squared due to theAppendTocomplexity). – Daniel Lichtblau Apr 19 '15 at 21:06p[x]because case of -1 will show up as the negative version of same (that is, -p[x]`). (This was also noted in a response by @2012rcampion.) – Daniel Lichtblau Apr 19 '15 at 21:08Timing[maxDeg = 12; polys = 1 + Flatten[Outer[List, Apply[Sequence, Table[{1, 0, -1}, {maxDeg}]]], maxDeg - 1].x^Range[maxDeg];]– Daniel Lichtblau Apr 19 '15 at 21:09