I have a function f2 I'd like to code into Mathematica with the property that it takes in two lists, treats those lists as unordered, and maps to a symbolic value.

If I were instead using a function f1 that takes in a single unordered list, then I would use...

SetAttributes[f1, Orderless];

and Mathematica would correctly recognize (for example) f1[a,b] = f1[b,a] and correctly simplify expressions as such. What I'd like is some means by which I can ensure Mathematica recognizes (for example) f2[a,b][c,d] = f2[b,a][c,d] = f2[a,b][d,c] = f2[b,a][d,c]. The resulting symbolic expressions will eventually have a,b,c,d and so-on set to non-negative integers. My specific application requires variable list lengths.

Q: Is there a way to symmetrize both input channels of the function? Or perhaps by instead writing




is there a way to tell Mathematica that it should treat the inputs as unordered lists?

Thank you for your time. Best wishes.


Edit (for further clarity): As a concrete example, I want a means by which I can write out something like...

f2[{a,b},{c,d}] * g1[x] + f2[{a,b},{d,c}] * g2[x] + f2[{b,a},{c,d}] * g3[x]

that Mathematica correctly reduces to,

f2[{a,b},{c,d}] * ( g1[x] + g2[x] + g3[x] )

Much in the style of how I might manipulate the f1[...] described above.


2 Answers 2


Why not use an auxiliary head to handle the orderless attribute? For example:

SetAttributes[s, Orderless];

expr = f2[{a,b},{c,d}] g1[x]+f2[{a,b},{d,c}] g2[x]+f2[{b,a},{c,d}] g3[x] /. List -> s;
expr //Factor

f2[s[a, b], s[c, d]] (g1[x] + g2[x] + g3[x])

You could dress up the head s with a format:

MakeBoxes[s[a__], form_] ^:= RowBox[{"(", RowBox@BoxForm`MakeInfixForm[{a}, ",", form], ")"}]

The above output then looks like:

expr //Factor

f2[(a, b), (c, d)] (g1[x] + g2[x] + g3[x])

  • $\begingroup$ Thank you! This will do the trick, and the dressing is a welcome bonus. :) $\endgroup$
    – SineOfPsi
    Dec 19, 2018 at 20:13
  • $\begingroup$ How did this get accepted, but not upvoted and zero? +1 from here... $\endgroup$
    – ciao
    Dec 19, 2018 at 20:15

You can use OrderlessPatternSequence:

ClearAll[f2, f]
f2[{OrderlessPatternSequence[a_, b_]}, {OrderlessPatternSequence[c_, d_]}] :=
   f[{a, b}, {c, d}]

Equal[f2[{a, b}, {c, d}], f2[{b, a}, {c, d}], f2[{a, b}, {d, c}], f2[{b, a}, {d, c}]]


Simplify[f2[{a, b}, {c, d}]*g1[x] + f2[{a, b}, {d, c}]*g2[x] + f2[{b, a}, {c, d}]*g3[x]]

f[{a, b}, {c, d}] (g1[x] + g2[x] + g3[x])

  • $\begingroup$ Thanks for the speedy reply! If I understand your answer correctly, this would be a method of mapping a symbol to a new symbol, and its that mapping which would possess unordered arguments. Is there a way to implement this behavior while maintaining the same variable name (like f1 in my example)? The symbolic calculation I'm doing will proceed in several stages wherein multiple instances of the function f2 will be combined and where each instance has a different combination of arguments. As such, it'd be convenient if the same variable could retain these properties throughout the calculation. $\endgroup$
    – SineOfPsi
    Dec 19, 2018 at 18:33
  • $\begingroup$ In other words, I want to treat the function as a symbol in its own right, and only map it to values at the end of the computation. $\endgroup$
    – SineOfPsi
    Dec 19, 2018 at 18:36
  • $\begingroup$ @SineOfPsi, you mean ClearAll[f1]; f1[{OrderlessPatternSequence[a_, b__]}][{OrderlessPatternSequence[c_, d__]}] := {a, b, c, d} (* or your choice of rhs expression*)? $\endgroup$
    – kglr
    Dec 19, 2018 at 19:34
  • $\begingroup$ @klgr I've added additional details to my original post to clarify what I'm looking for. Perhaps I'm misunderstanding your answer, but it's not clear to me how your proposed solution could be used in the way I desire. $\endgroup$
    – SineOfPsi
    Dec 19, 2018 at 19:46
  • $\begingroup$ @SineOfPsi, please see the updated version. $\endgroup$
    – kglr
    Dec 19, 2018 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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