I am a Mathematica beginner and I'm trying to implement a function of type
f[i1_,i2_,i3_,i4_]
such that it is orderless in the first two arguments and in the last two separately. Namely, I want
f[i4,i3,i2,i1]
returning
f[i3,i4,i1,i2]
and
f[i1,i2,i3,i4]==f[i2,i1,i3,i4]
f[i1,i2,i3,i4]==f[i1,i2,i4,i3]
returning
True
True
How can I define such object?
Orderless
does more than what you show. It does not only ensure equality in cases like your example, but also affects pattern matching. Ensuring this sort of equality is easy enough. How to also handle pattern matching, I don't know (maybe not possible). $\endgroup$f[a1_, a2_, b1_, b2_] /; Not[OrderedQ[{a1, a2}] && OrderedQ[{b1, b2}]] := f @@ Join[Sort[{a1, a2}], Sort[{b1, b2}]]
. Now tryf[4, 3, 2, 1]
. $\endgroup$