2
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ Can you elaborate on what you are trying to achieve? 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$
    – Szabolcs
    Nov 25 at 11:08
  • $\begingroup$ Not an answer because I don't know if this is what you really want. f[a1_, a2_, b1_, b2_] /; Not[OrderedQ[{a1, a2}] && OrderedQ[{b1, b2}]] := f @@ Join[Sort[{a1, a2}], Sort[{b1, b2}]]. Now try f[4, 3, 2, 1]. $\endgroup$
    – Szabolcs
    Nov 25 at 11:10
  • $\begingroup$ Sure! I am trying to define rules to be applied on patterns of type a[i_] F[t[i_],t[j_],s[i_],s[j_]]. F is a symmetric function in the first two variables and last two separately and I want the same rule to be applied if the pattern is a[j_] F[t[i_],t[j_],s[i_],s[j_]]. I tried that if I consider only two variables a[i_] F[t[i_],t[j_]] everything works by setting F Orderless. I wanted something similar with 4 variables as too many rules would be necessary otherwise. Thanks! $\endgroup$
    – McSenegal
    Nov 25 at 16:58
3
$\begingroup$
ClearAll[f]
f[OrderlessPatternSequence[i1_, i2_], OrderlessPatternSequence[i3_, i4_]] := 
  FOO[i1, i2, i3, i4]


f[i4, i3, i2, i1]
 FOO[i3, i4, i1, i2]
f[i1, i2, i3, i4] == f[i2, i1, i3, i4]
True
f[i1, i2, i3, i4] == f[i1, i2, i4, i3]
True
f[4, 3, 2, 1] == f[4, 3, 1, 2] == f[3, 4, 2, 1] == f[3, 4, 1, 2]
True
$\endgroup$
1
$\begingroup$

Assume that "f2" is the function you want. Then we first define a function of 2 arguments that is orderless:

ClearAll[fol,f1,f2]
SetAttributes[fol, Orderless]

Now we define the final function "f1" using "fol";

f1[x1_, x2_, x3_, x4] = f2[fol[x1, x2], fol[x3, x4]]

To test if this works:

d= f1 @@ # & /@ Permutations[{1, 2, 3, 4}];
AllTrue[d, d[[1]] == # &]
(*True*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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