In Mathematica, how can I create a polynomial function in given variables of a given degree with unknown coefficents?
That is, I am looking for a function Poly[vars, degree]
that generates, for example, if I evaluate
Poly[{x, y, z}, 3]
I should get the polynomial
a + a1*x + a2*y + a3*z + a12*xy + a13*xz + a23*yz + a11*x^2 + a22*y^2 + a33*z^2
+ a112*x^2*y + a113*x^2*z + a122*x*y^2 + a133*x*z^2 + a223*y^2*z + a233*y*z^2
+ a111*x^3 + a222*y^3 + a333*z^3
where the variables with a are unknown coefficients? Is there a simple way to do this?
Answer
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] *)
Comments
Post a Comment