Section9.53

]>
Book Contents Previous Section 9.52 Matrix Next Section 9.54 MultivariatePolynomial Book Index

## 9.53 MultiSet

The domain Multiset(R) is similar to Set(R) except that multiplicities (counts of duplications) are maintained and displayed. Use the operation multisetmultisetMultiset to create multisets from lists. All the standard operations from sets are available for multisets. An element with multiplicity greater than one has the multiplicity displayed first, then a colon, and then the element.

Create a multiset of integers.

s := multiset [1,2,3,4,5,4,3,2,3,4,5,6,7,4,10]

{7, 2: 5, 3: 3, 1, 10, 6, 4: 4, 2: 2}

Type: Multiset PositiveInteger

The operation insert! adds an element to a multiset.

insert!(3,s)

{7, 2: 5, 4: 3, 1, 10, 6, 4: 4, 2: 2}

Type: Multiset PositiveInteger

Use remove! to remove an element. If a third argument is present, it specifies how many instances to remove. Otherwise all instances of the element are removed. Display the resulting multiset.

remove!(3,s,1); s

{7, 2: 5, 3: 3, 1, 10, 6, 4: 4, 2: 2}

Type: Multiset PositiveInteger
remove!(5,s); s

{7, 3: 3, 1, 10, 6, 4: 4, 2: 2}

Type: Multiset PositiveInteger

The operation count returns the number of copies of a given value.

count(5,s)

0

Type: NonNegativeInteger

A second multiset.

t := multiset [2,2,2,-9]

{-9, 3: 2}

Type: Multiset Integer

The union of two multisets is additive.

U := union(s,t)

{7, 3: 3, 1,-9, 10, 6, 4: 4, 5: 2}

Type: Multiset Integer

The intersect operation gives the elements that are in common, with additive multiplicity.

I := intersect(s,t)

{5: 2}

Type: Multiset Integer

The difference of s and t consists of the elements that s has but t does not. Elements are regarded as indistinguishable, so that if s and t have any element in common, the difference does not contain that element.

difference(s,t)

{7, 3: 3, 1, 10, 6, 4: 4}

Type: Multiset Integer

The symmetricDifference is the union of difference(s, t) and difference(t, s).

S := symmetricDifference(s,t)

{7, 3: 3, 1,-9, 10, 6, 4: 4}

Type: Multiset Integer

Check that the union of the symmetricDifference and the intersect equals the union of the elements.

(U = union(S,I))@Boolean

true

Type: Boolean

Check some inclusion relations.

t1 := multiset [1,2,2,3]; [t1 < t, t1 < s, t < s, t1 <= s]

[false, true, false, true]

Type: List Boolean
Book Contents Previous Section 9.52 Matrix Next Section 9.54 MultivariatePolynomial Book Index