4
$\begingroup$

I have two lists of lists, like:

list1 = {{2}, {4}, {3, 1}, {2, 2}, {6}, {5, 1}, {4, 2}, {4, 2}, {8}, {7, 1}, 
{6, 2}, {6, 2}, {5, 3}, {4, 4}}
list2={{2, 2}, {4, 2}, {6, 2}, {4, 4}}

Note that, in list1, the partitions {4,2} and {6,2} occur twice. In general, there could be more than 2 occurrences of a particular element of either list, and my partitions could have more than 2 parts.

I want to "subtract" the contents of list2 from list1 (i.e. count its occurrences negatively, and merge it with list1) so the final result is

list3= {{2}, {4}, {3, 1}, {6}, {5, 1}, {4, 2}, {8}, {7, 1}, {6, 2},  {5, 3}}

i.e. if a partition $(a_1,a_2,\ldots,a_k)$ occurs $p_1(a)$ times in list1 and $q_1(a)$ times in list2, the result should be a list where $(a_1,a_2,\ldots,a_k)$ occurs $p_1(a)-q_1(a)$ times.

"Complement" doesn't quite work here as it would eliminate {4,2} and {6,2} from list3. Any help would be appreciated.

For context this come up when using modification rules for non-standard representations, which here are represented by partitions. The modification rules produce partitions with negative coefficient which ``cancels out'' an appropriate number of like standard representations in some expansion: see for instance

King RC. Modification rules and products of irreducible representations of the unitary, orthogonal, and symplectic groups. Journal of Mathematical Physics. 1971 Aug;12(8):1588-98.

$\endgroup$
4
  • $\begingroup$ list3 should not contain {2,2}, right? $\endgroup$
    – kglr
    May 18 at 19:43
  • $\begingroup$ @kglr fixed. Thanks. $\endgroup$ May 18 at 19:46
  • $\begingroup$ you also need to remove {4,4} from list3? $\endgroup$
    – kglr
    May 18 at 20:01
  • $\begingroup$ @kglr yes as you can see removing manually from a long list is not very convenient... $\endgroup$ May 18 at 20:49
7
$\begingroup$

1. Fold + DeleteCases

Fold[DeleteCases[##, 1, 1] &, list1, list2]
{{2}, {4}, {3, 1}, {6}, {5, 1}, {4, 2}, {8}, {7, 1}, {6, 2}, {5, 3}}

2. Counts + KeyUnion + KeyValueMap

Join @@ KeyValueMap[ConstantArray][Subtract @@ KeyUnion[Counts /@ {list1, list2}, 0 &]]
{{2}, {4}, {3, 1}, {6}, {5, 1}, {4, 2}, {8}, {7, 1}, {6, 2}, {5, 3}}

Alternatively,

KeyValueMap[Apply[Sequence]@*ConstantArray][
 Subtract @@ KeyUnion[Counts /@ {list1, list2}, 0 &]]
{{2}, {4}, {3, 1}, {6}, {5, 1}, {4, 2}, {8}, {7, 1}, {6, 2}, {5, 3}}
$\endgroup$
1
  • $\begingroup$ The second option I understand better and works perfectly. Thanks. $\endgroup$ May 18 at 21:04
5
$\begingroup$
Delete[list1,FirstPosition[list1, #]&/@list2]

{{2}, {4}, {3, 1}, {6}, {5, 1}, {4, 2}, {8}, {7, 1}, {6, 2}, {5, 3}}

$\endgroup$
1
  • $\begingroup$ Nice! This also works very well. $\endgroup$ May 19 at 12:47

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.