# Symmetrize a polynomial forgetting the commutativity property of multiplication

I need a script to authomatically symmetrize a given polynomial. For example, if the input is

xy


the output should be

(xy+yx)/2


The same principle should work also for higher order polynomial. For example, if the input is

 xyz


the output should be

(xyz+xzy+yxz+yzx+zxy+zyx)/6


The input is, in general, a polynomial. If the input is

xy+wz


the output should be

(xy+yx+wz+zw)/2

It may happen that some terms come with some powers. If the input is

x^2 z


the output should be either

(xxz + xxz +xzx+ xzx + zxx + zxx)/6


or

(2x^2z+2xzx+2zx^2)/6


Both these ouputs are good. Thanks in advance for the help.

• Are you always dealing with monomials, or should a solution expect things like x y + w z? – J. M.'s torpor Oct 14 '18 at 16:03
• The input could be a polynomial as well. For example, if the input is $xy+wz$, the output should be $(xy+yx+wz+zw)/2$, but I know a priori what are the variables that come into play. Moreover, of course, it may appen to have things like $x^2y$ as input. In this case, the output should be either $(xxz+xzx+xxz+xzx+zxx+zxx)/6$ or $(2x^2z + 2xzx+ 2zx^2)/6$. Both outputs are ok for me. – AndreaPaco Oct 14 '18 at 16:08
• @J.M.iscomputer-less, I've edited the question in order to meke my question more precise. – AndreaPaco Oct 14 '18 at 16:19

f[NonCommutativeMultiply[x__]] := Mean[Permutations[NonCommutativeMultiply[x]]]

f[x ** y ** z]


1/6 (x ** y ** z + x ** z ** y + y ** x ** z + y ** z ** x + z ** x ** y + z ** y ** x)

f[x_Times] := f /@ x
f[x_Plus] := f /@ x
f[x_?NumericQ] := x


allows us to treat also polynomials:

f[3 x ** x ** y + y ** y ** y + 54 x ** z ** y]


x ** x ** y + x ** y ** x + y ** x ** x + y ** y ** y + 9 (x ** y ** z + x ** z ** y + y ** x ** z + y ** z ** x + z ** x ** y + z ** y ** x)

• Thanks for your help. Can it handle a generic polynomial $P(x,y,z)$ such as $P(x,y,z)=3x^2y+y^3+54xzy$? – AndreaPaco Oct 14 '18 at 16:22
• Thanks a lot for the update. You gave me a huge help. Just a curiosity more. In the input the non-commutative multiplication $**$ is already made explicit. Is there a way to authomatically turn a traditional multiplication (such as $x*y$) into a non-commutative one, (such as $x**y$)? – AndreaPaco Oct 14 '18 at 16:32
• Yes, you can always tuse replacement rules, e.g. x x y //. {Power[a_, k_Integer?Positive] :> NonCommutativeMultiply @@ ConstantArray[a, k], Times -> NonCommutativeMultiply} (however this won't work out with the coefficients). – Henrik Schumacher Oct 14 '18 at 16:45