Message Boards Message Boards


A doubt about SubsetCases

Posted 1 month ago
8 Replies
5 Total Likes

Hello all. Version 12.1 has several very useful functions. SubsetCases seems to be one of them. However, I have a doubt regarding its use. It comes from an example from the documentation:

SubsetCases[{1, 2, 3, a, a, b, c}, {a, _Integer}]

This returns {{a,1},{a,2}}. Why not {a,3}? Indeed it has an overlap with the previous two lists, but the chosen output also has the same overlap (that is, the symbol a}

If indeed overlaps are turned to true {a,3} and much more is included. But if the overlaps are omitted, why does the code return {a,2} and not {a,3}? Any clarification is welcomed. Francisco

8 Replies
Posted 1 month ago

I think your interpretation of the documentation is reasonable. In fact, your understanding matched mine until I started experimenting.

Assuming that the SubsetCases[] works as designed, "overlap" is based on the elements of the first argument.

To illustrate, adding an additional a gives you the results we both expected:

SubsetCases[{1, 2, 3, a, a, a, b, c}, {a, _Integer}]


{{a, 1}, {a, 2}, {a, 3}}

So, "overlap" seems to be controlling whether elements of the first list can be reused.

Unfortunately, allowing overlaps probably does not give what you want. For example:

SubsetCases[{1, 2, 3, a, a, b, c}, {a, _Integer}, Overlaps -> True]


{{a, 1}, {a, 2}, {a, 3}, {a, 1}, {a, 2}, {a, 3}}

Fortunately, I think there is a fix that might work for you:

Union[SubsetCases[{1, 2, 3, a, a, b, c}, {a, _Integer}, 
  Overlaps -> True]]

returns what we both originally expected to see:

{{a, 1}, {a, 2}, {a, 3}}

Knowing how it actually works, I think that the function might be working as designed. But, I think they could have done a better job explaining it in the documentation. And of course, some additional examples that highlights how overlap works would help.

Good luck.

Many thanks for this, Mike, now I fully understood. All the best, Fg

Posted 1 month ago


You are welcome. Happy to help.

Posted 1 month ago

Hello. I'm the developer of SubsetCases and related functions.

SubsetCases considers elements which appear multiple times as distinct, just like Subsets does:

In[1]:= Subsets[{a, a, b}, {2}]

Out[1]= {{a, a}, {a, b}, {a, b}}

To construct the solution of the given example, one can first generate all the subsets, then filter by an orderless version of the pattern:

In[2]:= Cases[Subsets[{1, 2, 3, a, a, b, c}, {2}], {OrderlessPatternSequence[a, _Integer]}]

Out[2]= {{1, a}, {1, a}, {2, a}, {2, a}, {3, a}, {3, a}}

Finally, to satisfy the Overlaps -> False option value (the default for SubsetCases), one has to go through the list and drop each subset which contains any element already present in a previous subset.

One can see this more easily when looking at the result of SubsetPosition:

In[3]:= SubsetPosition[{1, 2, 3, a, a, b, c}, {a, _Integer}, Overlaps -> False]

Out[3]= {{4, 1}, {5, 2}}

Here one can see that no position element (integer) appears twice.

Posted 1 month ago


Thanks for the clarification.

Is there any way to get this explanation and a few examples that illustrate how overlaps works into the documentation?

Posted 1 month ago

Thanks for the question, we'll improve the documentation.

Many thanks Toni. I would suggest putting something of this unto the documentation, maybe with a couple of additional examples to prevent possible ambiguities.

I was having the same problem. This thread has been very helpful.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract