Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Given a function, g, which is symmetric in its argument, g(a,b)=g(b,a), consider the product g(a,b)*g(c,d)*g(e,f). I would like to get all the permutations of this product with respect to the arguments, that is, terms such as g(a,c)*g(b,d)*g(e,f), which respect the symmetry so that g(c,a)*g(b,d)*g(e,f) would not be among the generated terms. Is there a way to get Mathematica to do this?

Many thanks for any help.

share|improve this question
g(a,b)^3 counts? –  Kuba Aug 15 '13 at 20:57
That's a good question by @Kuba, my answer supposes we cannot reuse elements. –  Pickett Aug 15 '13 at 20:59
Since I don't know what exactly we are looking for, I'm just going to leave here this comment :) Times @@@ Apply[g, Tuples[Subsets[l, {2}], 3], {2}], where l = {a, b, c, d, e, f} –  Kuba Aug 15 '13 at 21:00

1 Answer 1

I'm working with four variables instead of six but the same would work for six. The strategy is to find a list of suitable permutations of possible arguments and then apply that to a helper function that encapsulates the product you're looking for.

h[a1_, a2_, a3_, a4_] := g[a1, a2] g[a3, a4];
permutations = 
  Permutations[{a1, a2, a3, a4}], (#[[1 ;; 2]] == #2[[1 ;; 2]] || #[[1 ;; 2]] == 
       Reverse[#2[[1 ;; 2]]]) && (#[[3 ;; 4]] == #2[[3 ;; 
          4]] || #[[3 ;; 4]] == Reverse[#2[[3 ;; 4]]]) &];
DeleteDuplicates[h @@ # & /@ permutations]

{g[a1, a2] g[a3, a4], g[a1, a3] g[a2, a4], g[a1, a4] g[a2, a3]}

After thinking for a while I also came up with this method, which is a bit prettier:

Clear[h,g]; SetAttributes[g, Orderless]
h[{a1_, a2_, a3_, a4_}] := g[a1, a2] g[a3, a4]
DeleteDuplicates[h /@ Permutations[{a1, a2, a3, a4}]]

{g[a1, a2] g[a3, a4], g[a1, a3] g[a2, a4], g[a1, a4] g[a2, a3]}

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.