Multisets
Introduction[edit | edit source]
For decades the several set symbols have languished on APL keyboards either unused or underused. Sometimes the vendor has assigned no dyadic function to that symbol and sometimes the assigned function isn't very useful. All vendors have implemented Set Difference (<apll>L~R</apll>), one has implemented Set Union (<apll>L∪R</apll>) and Set Intersection (<apll>L∩R</apll>), and, up to now, no APL vendor has implemented the missing fourth function.
Part of the reason the so-called set functions in APL are in an odd state is that they are defined on sets, but implemented on non-sets.
From Wikipedia, “in computer science, a set is an abstract data structure that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set”.
However, APL implementations of the set functions don't enforce the “no repeated value” requirement. Moreover, allowing repeated values is quite useful giving rise to useful idioms such as <apll>L~' '</apll> to remove all blanks from a vector.
Interestingly, sets with repeated values have a long history in mathematics and are known as Multisets. The usual set functions have identical counterparts as multiset functions with the same definitions except the multiset version takes into account the multiplicity of the unique values.
Multisets in an APL context are scalars or vectors of arbitrary items with various operations defined on them (monadically), but mostly between them (dyadically).
More formally, if <apll>L</apll> and <apll>R</apll> are multisets and the function <apll>m(x,M)</apll> returns the multiplicity of element <apll>x</apll> in the multiset <apll>M</apll>, then
- The Union of multisets is the multiset whose unique elements are the unique elements of <apll>L,R</apll> where the multiplicity of element <apll>x</apll> in the result is the larger of <apll>m(x,L)</apll> and <apll>m(x,R)</apll>,
- The Intersection of multisets is the multiset similar to Union, but with larger replaced by smaller, and
- The Difference (also called Asymmetric Difference and Relative Complement) of multisets is similar to Union and Intersection, but where the multiplicity calculation is <apll>max(0,m(x,L)-m(x,R))</apll>, that is if element <apll>x</apll> is in the result if it occurs more in <apll>L</apll> than in <apll>R</apll>, and it occurs with the multiplicity of the difference of the left and right multiplicities.
For multisets with no repeated elements, the multiset function and the corresponding set function produce the same results.
Notation[edit | edit source]
There are a dozen or so APL primitive functions we'd like to define on Multisets. One way to do this is to come up with a dozen new symbols to represent those Multiset functions; another is to define a single Multiset Operator that can be applied to the APL primitives which is the approach taken here, and that symbol is (<apll>⍦</apll>) which can be typed with Alt-'m', a keystroke previously used for the stile symbol (<apll>|</apll>) which was duplicated elsewhere on the keyboard. This operator is different from previous operators in APL in that it applies to a select set of primitive functions, but not system functions nor derived functions. It does, however, apply to user-defined functions via the System Label <apll>⎕MS</apll>. In that sense it's more like an inflection such as how J uses <apll>.</apll> and <apll>:</apll>. Nonetheless, it is a (monadic) operator in the full mathematical (and APL) sense of it taking a function as an operand and returning a (derived) function.
Examples[edit | edit source]
A common example of a multiset is the decomposition of an integer into its prime factors, each of which may occur multiple times. For example, <apll>600</apll> may be factored into the multiset <apll>L←2 2 2 3 5 5</apll>, and <apll>2100</apll> into <apll>R←2 2 3 5 5 7</apll>.
Two useful properties of a multiset are the Underlying Set of Unique Elements (obtained via the usual <apll>∪R</apll>) along with their Multiplicities (obtained via the derived function <apll>∪⍦R</apll>), where the latter may be defined as <apll>¯2-/⍸1,(2≠/R[⍋R⍳R]),1</apll>.
For the two multisets above the two properties are
<apll> (∪L),[0.5] ∪⍦L</apll> | <apll> (∪R),[0.5] ∪⍦R</apll> | |
<apll> 2 3 5</apll> | <apll> 2 3 5 7</apll> | |
<apll> 3 1 2</apll> | <apll> 2 1 2 1</apll> |
The values (but not the order) of the original multiset may be reconstructed from <apll>∊(∪⍦R)⍴¨∪R</apll>. That is, <apll>R≡⍦∊(∪⍦R)⍴¨∪R</apll>, but not necessarily <apll>R≡∊(∪⍦R)⍴¨∪R</apll>.
Two common operations performed between multisets are Union and Intersection.
Multiset Union on <apll>L</apll> and <apll>R</apll> is defined as the Multiset whose underlying set is the set union of the underlying sets of <apll>L</apll> and <apll>R</apll>, and whose multiplicities are the larger of the multiplicities of the corresponding elements of <apll>L</apll> and <apll>R</apll>.
For <apll>L←2 2 2 3 5 5</apll> and <apll>R←2 2 3 5 5 7</apll> the Multiset Union is <apll>2 2 2 3 5 5 7</apll>. Note that, for example, there are three <apll>2</apll>s in the result because that is the larger of the number of <apll>2</apll>s in <apll>L</apll> (3) and <apll>R</apll> (2).
Multiset Intersection on <apll>L</apll> and <apll>R</apll> is defined the same as for Multiset Union except that the smaller of the multiplicities is taken instead of the larger. For the two Multisets above, the Multiset Intersection is <apll>2 2 3 5 5</apll> where there are two <apll>2</apll>s in the result because that's the smaller of the number of <apll>2</apll>s in <apll>L</apll> (3) and <apll>R</apll> (2), and there are no <apll>7</apll>s in the result because that's the smaller of the number of <apll>7</apll>s in <apll>L</apll> (0) and <apll>R</apll> (1).
Interestingly, in the context of prime factorization, Multiset Union is the direct analog of Least Common Multiple and Multiset Intersection is Greatest Common Divisor. For the two Multisets above,
<apll>L∪⍦R ←→ 2 2 2 3 5 5 7</apll> and
<apll>L∩⍦R ←→ 2 2 3 5 5</apll>
The Least Common Multiple of two original numbers <apll>×/L ←→ 600</apll> and <apll>×/R ←→ 2100</apll> is
<apll>
(×/L)∧×/R 4200 ×/L∪⍦R 4200
</apll>
and the Greatest Common Divisor is
<apll>
(×/L)∨×/R 300 ×/L∩⍦R 300
</apll>
Moreover, using <apll>P←2 2 2 3 3 5 7</apll> and <apll>Q←2 2 3 5</apll> where as in this case <apll>P</apll> is a superset of <apll>Q</apll> (that is, <apll>P⊇Q</apll>) then Multiset Asymmetric Difference is the direct analog of integer division. That is,
<apll>
P~⍦Q 2 3 7 ×/P~⍦Q 42 (×/P)÷×/Q 42
</apll>
Other examples illustrate the distinction between the multiset and non-multiset functions
<apll>
'mississippi'~'miss' pp 'mississippi'~⍦'miss' issippi
</apll>
Subscripts[edit | edit source]
One way to understand Multisets and the operations performed on them is to view them with equal elements having unique subscripts. That is, for the Multisets <apll>2 2 2 3 5 5</apll> and <apll>2 2 3 5 5 7</apll>, write them as
<apll>2</apll>_{1} | <apll>2</apll>_{2} | <apll>2</apll>_{3} | <apll>3</apll>_{1} | <apll>5</apll>_{1} | <apll>5</apll>_{2} | and | ||
<apll>2</apll>_{1} | <apll>2</apll>_{2} | <apll>3</apll>_{1} | <apll>5</apll>_{1} | <apll>5</apll>_{2} | <apll>7</apll>_{1} |
Now Multiset Union reduces to simple union where only one copy of like elements is kept, and similarly for Multiset Intersection and Multiset Asymmetric Difference. That is,
<apll>L</apll> | <apll>←</apll> | <apll>2</apll>_{1} | <apll>2</apll>_{2} | <apll>2</apll>_{3} | <apll>3</apll>_{1} | <apll>5</apll>_{1} | <apll>5</apll>_{2} | |||
<apll>R</apll> | <apll>←</apll> | <apll>2</apll>_{1} | <apll>2</apll>_{2} | <apll>3</apll>_{1} | <apll>5</apll>_{1} | <apll>5</apll>_{2} | <apll>7</apll>_{1} | |||
<apll>L∪⍦R</apll> | <apll>←→</apll> | <apll>2</apll>_{1} | <apll>2</apll>_{2} | <apll>2</apll>_{3} | <apll>3</apll>_{1} | <apll>5</apll>_{1} | <apll>5</apll>_{2} | <apll>7</apll>_{1} | ||
<apll>L∩⍦R</apll> | <apll>←→</apll> | <apll>2</apll>_{1} | <apll>2</apll>_{2} | <apll>3</apll>_{1} | <apll>5</apll>_{1} | <apll>5</apll>_{2} | ||||
<apll>L~⍦R</apll> | <apll>←→</apll> | <apll>2</apll>_{3} |
Missing Function[edit | edit source]
To see if we have covered all of the possible set/multiset results, look at the usual Venn diagram for two sets – all seven results (excluding the empty set) appear as follows:
<apll>L∪R</apll> | Error creating thumbnail: File missing | Union | <apll>L,R~L</apll> | |||
<apll>L∩R</apll> | Error creating thumbnail: File missing | Intersection | <apll>L~L~R</apll> | |||
<apll>L~R</apll> | Error creating thumbnail: File missing | Asymmetric Difference Left | <apll>L~R</apll> | |||
<apll>R~L</apll> | Error creating thumbnail: File missing | Asymmetric Difference Right | <apll>R~L</apll> | |||
<apll>L</apll> | Error creating thumbnail: File missing | Left | <apll>L</apll> | |||
<apll>R</apll> | Error creating thumbnail: File missing | Right | <apll>R</apll> | |||
<apll>L∆R</apll> | Error creating thumbnail: File missing | Symmetric Difference | <apll>(L~R)∪R~L ←→ (L~R),R~L</apll> |
The last diagram shows the missing function along with its name and effect. The mathematical symbol for this function is delta (<apll>∆</apll>), however because old APL programs use this symbol as another alphabetic character, we use the Section symbol (<apll>§</apll>, Alt-'S') instead.
Multiset Derived Functions[edit | edit source]
The APL functions defined on Multisets are as follows:
<apll>L∪⍦R</apll> | Multiset Union | |
<apll>L∩⍦R</apll> | Multiset Intersection | |
<apll>L~⍦R</apll> | Multiset Asymmetric Difference | |
<apll>L§⍦R</apll> | Multiset Symmetric Difference | |
<apll>L⍳⍦R</apll> | Multiset Index Of | |
<apll>L∊⍦R</apll> | Multiset Member Of | |
<apll>L≡⍦R</apll> | Multiset Match | |
<apll>L≢⍦R</apll> | Multiset Mismatch (same as <apll>~L≡⍦R</apll>) | |
<apll>L⊂⍦R</apll> | Multiset Proper Subset Of | |
<apll>L⊆⍦R</apll> | Multiset Subset Of | |
<apll>L⊃⍦R</apll> | Multiset Proper Superset Of | |
<apll>L⊇⍦R</apll> | Multiset Superset Of | |
<apll> ∪⍦R</apll> | Multiset Multiplicities (<apll>⍴∪⍦R ←→ ⍴∪R</apll>) |
Multiset Member Of and Index Of[edit | edit source]
The key to understanding the meaning of the Multiset Operator as it is applied to the above APL primitive functions is Multiset Member Of (<apll>∊⍦</apll>), so we'll investigate that first.
Using the subscript approach above the definition is straightforward,
<apll>L</apll> | <apll>←</apll> | <apll>2</apll>_{1} | <apll>2</apll>_{2} | <apll>2</apll>_{3} | <apll>3</apll>_{1} | <apll>5</apll>_{1} | <apll>5</apll>_{2} | |||
<apll>R</apll> | <apll>←</apll> | <apll>2</apll>_{1} | <apll>2</apll>_{2} | <apll>3</apll>_{1} | <apll>5</apll>_{1} | <apll>5</apll>_{2} | <apll>7</apll>_{1} | |||
<apll>L∊⍦R</apll> | <apll>←→</apll> | <apll>1</apll> | <apll>1</apll> | <apll>0</apll> | <apll>1</apll> | <apll>1</apll> | <apll>1</apll> |
again, matching like elements with like elements between the two arguments.
We'll use this definition many times over in defining how the Multiset Operator applies to various APL primitive functions.
In a similar manner, Multiset Index Of can be understood best by writing the two arguments with subscripts. For a more detailed coverage of this see Anatomy of An Idiom.
Multiset Match[edit | edit source]
This derived function is a convenient way to determine whether or not two multisets are identical up to but not including order, as in these anagrams:
<apll>
'dynamo'≡⍦'monday' 1 'pepsicola'≡⍦'episcopal' 1 'the morse code'≡⍦'here come dots' 1
</apll>
APL Definitions[edit | edit source]
Each of the above dyadic Multiset functions has a simple analog in a non-Multiset context:
Function <apll> f</apll> |
Non-Multiset Definition <apll>L f R</apll> |
Multiset Definition <apll>L f⍦ R</apll> |
||
<apll> ∪</apll> | <apll>L,R~L</apll> | <apll>L,R~⍦L</apll> | ||
<apll> ∩</apll> | <apll>(L∊R)/L</apll> | <apll>(L∊⍦R)/L</apll> | ||
<apll> ~</apll> | <apll>(~L∊R)/L</apll> | <apll>(~L∊⍦R)/L</apll> | ||
<apll> §</apll> | <apll>(L~R),R~L</apll> | <apll>(L~⍦R),R~⍦L</apll> | ||
<apll> ⍳</apll> | <apll>L⍳R</apll> | <apll>L⍳⍦R</apll> | ||
<apll> ∊</apll> | <apll>L∊R</apll> | <apll>L∊⍦R</apll> | ||
<apll> ≡</apll> | <apll>((≢L)≡≢R)∧∧/L∊R</apll> | <apll>((≢L)≡≢R)∧∧/L∊⍦R</apll> | ||
<apll> ≢</apll> | <apll>~L≡R</apll> | <apll>~L≡⍦R</apll> | ||
<apll> ⊂</apll> | <apll>(L⊆R)∧L≢R</apll> † | <apll>(L⊆⍦R)∧L≢⍦R</apll> | ||
<apll> ⊆</apll> | <apll>∧/L∊R</apll> | <apll>∧/L∊⍦R</apll> | ||
<apll> ⊃</apll> | <apll>(R⊆L)∧R≢L</apll> † | <apll>(R⊆⍦L)∧R≢⍦L</apll> | ||
<apll> ⊇</apll> | <apll>∧/R∊L</apll> | <apll>∧/R∊⍦L</apll> |
† = This is the meaning this function would have as a set function if it didn't have another meaning in a non-Multiset context.
Note that in every case the Multiset definition may be obtained from the non-Multiset definition by introducing the Multiset Operator at the appropriate place(s). Beyond the above explanations for Multiset Member Of and Multiset Index Of, all of the other Multiset definitions can be reduced to the definition of Multiset Member Of.
Moreover, Multiset Union, Intersection, and Symmetric Difference are all symmetric up to order. That is,
<apll>(L∪⍦R)≡⍦R∪⍦L</apll> |
<apll>(L∩⍦R)≡⍦R∩⍦L</apll> |
<apll>(L~⍦R)≡⍦R~⍦L</apll> |
Acknowledgments[edit | edit source]
The idea of defining Multisets in an APL context is due to Patrick Parks of APL2000, Inc.