1
$\begingroup$

Consider the following groupings code:

Groupings[IntegerPartitions[3], {A -> 2, B -> 2}]

This generates:

 {A[2, 1], B[2, 1], A[A[1, 1], 1], A[1, A[1, 1]], A[B[1, 1], 1], 
  A[1, B[1, 1]], B[A[1, 1], 1], B[1, A[1, 1]], B[B[1, 1], 1], 
  B[1, B[1, 1]]}

In other words, the expressions with heads A and heads B over the arguments given by integer partition are generated.

These expressions correspond to expression trees of which Orderless versions can be obtained (``Orderless" in the sense of Mathematica, for trees, i.e. essentially trees identified up to permutations of children of a node).

Is there an Orderless version for Groupings?

I tried something like:

    Groupings[IntegerPartitions[3], {A -> 2, B -> 2}, Orderless]

Or:

    Groupings[IntegerPartitions[3], {A -> 2, B -> 2, Orderless}]

both of which produce errors.

I would like to obtain an efficient way to only keep ``Orderless versions of the expressions'' (as determined by the orderless versions of their expression trees).

Note that a distinction of heads matters, i.e. A and B are not interchangeable).

$\endgroup$
4
  • $\begingroup$ what is the desired output for your example? $\endgroup$
    – kglr
    Jun 19, 2019 at 20:26
  • $\begingroup$ does SetAttributes[{A,B}, Orderless] work? $\endgroup$
    – kglr
    Jun 19, 2019 at 20:28
  • $\begingroup$ ... or Groupings[IntegerPartitions[3], {foo -> 2, bar -> 2}, Sort]? $\endgroup$
    – kglr
    Jun 19, 2019 at 20:36
  • $\begingroup$ Sort won't do it as it keeps copies of identical trees (generated by Sort). I found the answer though, copied below $\endgroup$
    – Michel
    Jun 19, 2019 at 20:49

1 Answer 1

2
$\begingroup$

Ok, I found the answer:

Groupings[IntegerPartitions[3], {A -> {2, Orderless}, B -> {2, Orderless}}]

this returns:

 {A[2, 1], B[2, 1], A[A[1, 1], 1], A[B[1, 1], 1], B[A[1, 1], 1], 
  B[B[1, 1], 1]}
$\endgroup$

Your Answer

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

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