Function Repository Resource:

MultisetProduct

Source Notebook

Perform the product operation on multisets

Contributed by: Robert B. Nachbar (Wolfram Solutions)

ResourceFunction["MultisetProduct"][list1,list2,]

gives a sorted list of all the elements that appear in any of the listi. The number of occurrences of each distinct element is the product of the counts in each of the listi.

ResourceFunction["MultisetProduct"][list]

effectively sorts the elements of list.

ResourceFunction["MultisetProduct"][assoc1,assoc2,]

gives an Association where the keys are the union of the keys in the associations associ and the values are the product of the merged values.

Details and Options

If the listi are considered as multisets, ResourceFunction["MultisetProduct"] gives their product.
The listi must have the same head, but it need not be List.
The values of associ must be counts—that is, non-negative Integer values.

Examples

Basic Examples (2) 

Give a sorted list of elements common to all the lists, where the number of each distinct element is the product of the individual cardinalities:

In[1]:=
ResourceFunction[
 "MultisetProduct"][{a, b, a, c}, {d, a, e, b}, {c, d, b, a, d}]
Out[1]=

Multisets as an Association of counts:

In[2]:=
ResourceFunction[
 "MultisetProduct"][<|"a" -> 3, "b" -> 1, "c" -> 2, "d" -> 1|>,
 <|"a" -> 2, "b" -> 2, "c" -> 2, "e" -> 1|>,
 <|"a" -> 1, "b" -> 2, "c" -> 2, "e" -> 2|>]
Out[2]=

Scope (1) 

MultisetProduct works with any head, not just List:

In[3]:=
ResourceFunction["MultisetProduct"][f[a, b, c, c], f[c, a, a], f[b, b, a, c, c, c]]
Out[3]=

Properties and Relations (1) 

Compare to Intersection:

In[4]:=
ResourceFunction[
 "MultisetProduct"][{a, b, c, c}, {c, a, a}, {b, b, a, c, c, c}]
Out[4]=
In[5]:=
Intersection[{a, b, c, c}, {c, a, a}, {b, b, a, c, c, c}]
Out[5]=

Publisher

Robert Nachbar

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 19 February 2019

Related Resources

License Information