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.

__copy__()[source]

Return a shallow mutable copy of self.

__nonzero__()[source]

Return true if set is not empty.

Used to determine boolean value.

__contains__(elem)[source]

Return True if elem is in self.

_hash_()[source]

Return hash value for multiset.

Only called if immutable.

__repr__()[source]

Return a string representative of the ClonableMultiset-object.

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.
cardinality()[source]

Return sum of multiplicities.

no_uniques()[source]

Number of different elements in the multiset.

most_common(n=None)[source]

Return the n most common elements.

The == and != operators are overloaded to compare ClonableMultisets for equality. The test is based on equality of the underlying dicitonaries.

elements()[source]

Iterator returning each element as many times as its multiplicity.

keys()[source]

Return list of keys (elements).

values()[source]

Return list of values (multiplicities)

items()[source]

Return list of (key,value)-pairs.

Operations between Multisets

ClonableMultiset.sub(elem)[source]
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.inplace_multiset_sum(iterable=0, **kwds)[source]

Updates \(A\) to \(A \uplus B\).

ClonableMultiset.multiset_sum(other)[source]

Return \(A \uplus B\) as a new instance.

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.

ClonableMultiset.inplace_add(elem)[source]

“Add elem to self with multiplicity 1.

If elem is already in A its multiplicity is increased by one.

ClonableMultiset.add(elem)[source]

Same as above, except the result is returned as a new instance of ClonableMultiset.