# Symmetrise a complicated expression

I am calculating the derivatives of a symmetric function $F(a_1, a_2)$ and got very complicated expressions, which I'd like to write in a symmetric form with respect to $1 \leftrightarrow 2$ (clearly such a form exists, since $\frac{\partial^2}{\partial a_1 \partial a_2} F = \frac{\partial^2}{\partial a_2 \partial a_1} F$). Does Mathematica have a function that does this, i.e. to symmetrise an expression that is known to be symmetric?

Example: Instead of something like $a(a+b) + ab + b^2$, I'd like to have $(a+b)^2$.

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. Commented Aug 18, 2015 at 17:45
• What would you do with an expression like Abs[Log[a/b]? I don't think the problem is well-defined in this form.
– Jens
Commented Aug 18, 2015 at 18:08
• I suppose Factor[a (a + b) + a b + b^2] won't always work. But I don't what kind of answer you're looking for. Commented Aug 18, 2015 at 18:08
• SymmetricReduction works on polynomials only. Is $F$ a polynomial? Commented Aug 18, 2015 at 18:11
• For non-polynomials, you can always use the method in my answer to What is the command to find function invariant?. But you seem to be looking for some kind of "visual" symmetry. This should be defined more clearly, I think.
– Jens
Commented Aug 18, 2015 at 18:11

This symmetrizes an arbitrary expression by adding it to itself with the variable names interchanged. As a result, any term in the original expression has a symmetry-related counterpart, making the expression manifestly symmetric in the only sense that can be reasonably applied to an arbitrary expression. It's a special case of my answer to What is the command to find function invariant:

symmetrize[expr_, {a_, b_}] :=
1/2 (expr + (expr /. {a -> b, b -> a}))

symmetrize[a (a + b) + a b + b^2, {a, b}]

(* ==> 1/2 (a^2 + 2 a b + b^2 + a (a + b) + b (a + b)) *)

symmetrize[Cos[Log[a/b]], {a, b}]

(* ==> 1/2 (Cos[Log[a/b]] + Cos[Log[b/a]]) *)


The last example shows why I say that this is the only way you can reasonably make an arbitrary expression "look symmetric." You can't get this into the form of a single term. So I think in general this the best you can do, although in special cases there are simpler forms, as the example in the question shows. But that example is an exception, not the rule.

f[a_, b_] := a (a + b) + a b + b^2

Simplify@(f[a, b] + f[b, a])/2


or for polynomials:

SymmetricReduction[a (a + b) + a b + b^2, {a, b}][[1]]


$(a + b)^2$

The first approach works for functions such as:

f[a_, b_] := Cos[a] + Sin[a + b]

Simplify@(f[a, b] + f[b, a])/2


$1/2 (Cos[a] + Cos[b] + 2 Sin[a + b])$

• But will Simplify always transform an expression into a manifestly symmetric form? Commented Aug 18, 2015 at 18:08