Multisets¶
-
class
pybs.utils.clonable_multiset.
ClonableMultiset
(Clonable)[source]¶ -
__init__
(iterable=0, *args, **kwargs)[source]¶ Make a new ClonableMultiset.
If an argument is given, it is added to the new instance using
inplace_multiset_sum
. The result is immutable.
Setting and getting values is done as for a dictionary, with the following exeptions:
- Only natural numbers are allowed as values.
- If a value is set to 0, the key is deleted.
- Deleting a nonexistent key does noting.
- Retriveing a nonexistent key returns 0.
The
==
and!=
operators are overloaded to compare ClonableMultisets for equality. The test is based on equality of the underlying dicitonaries.-
Operations between Multisets¶
ClonableMultiset.
__and__
(other)[source]¶“Return the multiset intersection \(A \cap B\) as a new instance.
This is the same syntax as set intersection in Python (
&
).
ClonableMultiset.
__or__
(other)[source]¶Return the multiset union \(A \cup B\) as a new instance.
This is the same syntax as set union in Python (
|
).
ClonableMultiset.
scalar_mul
(n)[source]¶Returns \(n \bigotimes A\) as a new instance.
That is a multiset where the multiplicities are scaled by n.
ClonableMultiset.
inplace_multiset_difference
(iterable=None, **kwds)[source]¶Update \(A\) to \(A \setminus B\).
Raises an exception if the multiplicity of an element in B is larger than the multiplicity of the same element in A. This is non-standard behavior for mathematical multisets, but a sound check when they are used for trees.
ClonableMultiset.
multiset_difference
(other)[source]¶Return \(A \setminus B\) as a new instance.
Note
Does allow a multiplicity in B to be larger than the corresponding multiplicity in A, the multiplicity of the element in question is 0.