Create a list of all possible multivariate monomials of a certain order
FrobeniusSolve[]
is a very convenient function for this task:
monomialList[vars_List, m_Integer?NonNegative] :=
Flatten[Map[Apply[Times, vars^#] &,
Table[FrobeniusSolve[ConstantArray[1, Length[vars]], k],
{k, 0, m}], {2}]]
A demonstration:
monomialList[{x, y, z, w}, 3]
{1, w, z, y, x, w^2, w z, z^2, w y, y z, y^2, w x, x z, x y, x^2, w^3, w^2 z,
w z^2, z^3, w^2 y, w y z, y z^2, w y^2, y^2 z, y^3, w^2 x, w x z, x z^2, w x y,
x y z, x y^2, w x^2, x^2 z, x^2 y, x^3}
A variation using Inner[]
:
monomialList[vars_List, m_Integer?NonNegative] :=
Flatten[Inner[#2^#1 &, #, vars, Times] & /@
Table[FrobeniusSolve[ConstantArray[1, Length[vars]], k], {k, 0, m}]]
This is pretty fast.
allMonoms[n_, deg_, x_] :=
List @@ Expand[(1 + Total[Array[x, n]])^deg] /.
j_Integer*monom : _ :> monom
Quick example:
allMonoms[3, 4, x]
(* Out[89]= {1, x[1], x[1]^2, x[1]^3, x[1]^4, x[2], x[1] x[2],
x[1]^2 x[2], x[1]^3 x[2], x[2]^2, x[1] x[2]^2, x[1]^2 x[2]^2, x[2]^3,
x[1] x[2]^3, x[2]^4, x[3], x[1] x[3], x[1]^2 x[3], x[1]^3 x[3],
x[2] x[3], x[1] x[2] x[3], x[1]^2 x[2] x[3], x[2]^2 x[3],
x[1] x[2]^2 x[3], x[2]^3 x[3], x[3]^2, x[1] x[3]^2, x[1]^2 x[3]^2,
x[2] x[3]^2, x[1] x[2] x[3]^2, x[2]^2 x[3]^2, x[3]^3, x[1] x[3]^3,
x[2] x[3]^3, x[3]^4} *)
Here is a timing test.
Timing[mtab = Table[allMonoms[n, d, x], {n, 7}, {d, 14}];]
(* Out[88]= {1.974403, Null} *)
To be constructive, here is my own attempt at creating this function:
monList[vars_, order_] := Module[{tmp, tmpsub},
tmp = MonomialList[Sum[(vars /. List -> Plus)^i, {i, 0, order}] // Expand, vars];
tmpsub = Table[vars[[i]] -> 1, {i, 1, Length[vars]}];
tmp/(tmp /. tmpsub)
]
However, I suspect that the above will become very slow for large orders and when many variables are involved.