2
$\begingroup$

So I'm looking for a function that takes in the degree of the polynomial and the range of coefficients from -c to c, and outputs a list of all the monic polynomials of that degree and with coefficients in that range.

I already have code to numerically compute the roots and plot in the complex plane, I just need a way to compute this list. I haven't been able to find previously posted code to do this task on stackexchange.

$\endgroup$
3
$\begingroup$

toMonicpol[lis_] := x^(Length[lis]) + Dot[lis, Table[x^r, {r, 0, Length[lis] - 1}]]

pols[deg_, c_] := Map[toMonicpol[#] &, Tuples[Range[-c, c], deg]]

pols[2, 3]

{-3 - 3 x + x^2, -3 - 2 x + x^2, -3 - x + x^2, -3 + x^2, -3 + x + x^2, -3 + 2 x + x^2, -3 + 3 x + x^2, -2 - 3 x + x^2, -2 - 2 x + x^2, -2 - x + x^2, -2 + x^2, -2 + x + x^2, -2 + 2 x + x^2, -2 + 3 x + x^2, -1 - 3 x + x^2, -1 - 2 x + x^2, -1 - x + x^2, -1 + x^2, -1 + x + x^2, -1 + 2 x + x^2, -1 + 3 x + x^2, -3 x + x^2, -2 x + x^2, -x + x^2, x^2, x + x^2, 2 x + x^2, 3 x + x^2, 1 - 3 x + x^2, 1 - 2 x + x^2, 1 - x + x^2, 1 + x^2, 1 + x + x^2, 1 + 2 x + x^2, 1 + 3 x + x^2, 2 - 3 x + x^2, 2 - 2 x + x^2, 2 - x + x^2, 2 + x^2, 2 + x + x^2, 2 + 2 x + x^2, 2 + 3 x + x^2, 3 - 3 x + x^2, 3 - 2 x + x^2, 3 - x + x^2, 3 + x^2, 3 + x + x^2, 3 + 2 x + x^2, 3 + 3 x + x^2}

$\endgroup$
1
$\begingroup$

Note that we can use FromDigits[{1, a, b, c}, x] to get a polynomial of degree Length[{a,b,c}] in x with leading coefficient 1:

 Expand @ FromDigits[{1, a, b, c}, x]

c + b x + a x^2 + x^3

Using Tuples[Range[-c,c], deg], as in ulvi's answer, to generate all tuples (i.e., all possible non-leading coefficients in a polynomial with degree deg), and Prepending each tuple with 1, we can use FromDigits on each list to get the desired list of monic polynomials:

mp[d_, c_] := Expand @ FromDigits[Prepend[#, 1], x] & /@ Tuples[Range[-c, c], d];

This gives the same list of polynomials as ulvi's pols:

Sort[mp[2, 3] ] == Sort[pols[2, 3]]

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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