# Defining a function that treats input as two unordered lists

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

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


or

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


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.

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@BoxFormMakeInfixForm[{a}, ",", form], ")"}]


The above output then looks like:

expr //Factor


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

• Thank you! This will do the trick, and the dressing is a welcome bonus. :) – SineOfPsi Dec 19 '18 at 20:13
• How did this get accepted, but not upvoted and zero? +1 from here... – ciao Dec 19 '18 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}]]


True

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])

• 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. – SineOfPsi Dec 19 '18 at 18:33
• 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. – SineOfPsi Dec 19 '18 at 18:36
• @SineOfPsi, you mean ClearAll[f1]; f1[{OrderlessPatternSequence[a_, b__]}][{OrderlessPatternSequence[c_, d__]}] := {a, b, c, d} (* or your choice of rhs expression*)`? – kglr Dec 19 '18 at 19:34
• @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. – SineOfPsi Dec 19 '18 at 19:46
• @SineOfPsi, please see the updated version. – kglr Dec 19 '18 at 19:52