Create a polynomial of a given degree
polynomial[vars_List, n_Integer, coeff_] :=
#.Array[coeff, Length@#] &@ DeleteDuplicates[Times @@@ Tuples[Prepend[vars, 1], n]]
Clear[a]
polynomial[{x, y, z}, 3, a]
(* a[1] + x a[2] + y a[3] + z a[4] + x^2 a[5] + x y a[6] + x z a[7]
+ y^2 a[8] + y z a[9] + z^2 a[10] + x^3 a[11] + x^2 y a[12]
+ x^2 z a[13] + x y^2 a[14] + x y z a[15] + x z^2 a[16]
+ y^3 a[17] + y^2 z a[18] + y z^2 a[19] + z^3 a[20] *)
for a one liner
poly[{x_,y_,z_},n_,a_]:= Sum[a[i, j, k] x^i y^j z^k,
{i, 0, n}, {j, 0, n - i}, {k, 0, n - i - j}]
another way
poly[vars_List, a_, order_] := Module[{n = Length@vars, idx, z},
idx = Cases[Tuples[Range[0, order], n], x_ /; Plus @@ x <= order];
z = Times @@@ (vars^# & /@ idx);
z.((Subscript[a, Row[#]]) & /@ idx)
]
poly[{x, y, z}, a, 3] (*a is used for coefficient*)
poly[{x, y, z}, a, 2]
poly[{x, y}, a, 2]
poly[{x}, a, 4]
poly[{x, y, z, w}, a, 5]
To make M display the coeffs first, use ParameterVariables :> {a}
with TraditionalForm
(this is for display only)
TraditionalForm[poly[{x, y, z}, a, 3], ParameterVariables :> {a}]
ps. If you do not like to use subscripts, you can use this instead:
poly[vars_List, order_] := Module[{n = Length@vars, idx, z},
idx = Cases[Tuples[Range[0, order], n], x_ /; Plus @@ x <= order];
z = Times @@@ (vars^# & /@ idx);
z.((ToExpression["a" <> ToString[Row[#]]]) & /@ idx)
]
and now poly[{x, y, z}, 3]
gives