Discrete helpers/set part comp
![]() |
set_part_comp |
This class extends set partitions (SetPart) with a complement relationship between the blocks of equivalent elements.
This can be used to save equality and complement relationships between arguments of Boolean functions (Boolf).
The initial object in the following example has three blocks. The first two of them are complements.
from discretehelpers.set_part_comp import SetPartComp
x = SetPartComp([[0, 1], [2, 3], [4, 5]], {(0, 2)})
assert x.are_equal(0, 1)
assert x.are_equal(2, 3)
assert x.are_equal(4, 5)
assert x.are_comp(0, 2)
assert x.are_comp(0, 3)
assert x.are_comp(1, 2)
assert x.are_comp(1, 3)
assert x.get_comp(0) == x.get_comp(1) == 2 # smallest element of the complementary block
assert x.get_comp(2) == x.get_comp(3) == 0
assert x.get_comp(4) is x.get_comp(5) is None # no complement for this block
assert x.get_block(2) == [2, 3] # block that contains the argument
assert x.get_repr(3) == 2 # smallest element of the block that contains the argument
x.set_equal(5, 6)
assert x == SetPartComp([[0, 1], [2, 3], [4, 5, 6]], {(0, 2)})
x.set_comp(6, 9)
assert x == SetPartComp([[0, 1], [2, 3], [4, 5, 6]], {(0, 2), (4, 9)})
assert x.are_equal(4, 6)
assert x.are_comp(5, 9)
assert not x.are_equal(3, 4)
assert not x.are_comp(3, 4)
domain
[edit | edit source]The domain is the set of integers, including the negative ones. (This is different from SetPart, where the domain can be changed, and is N by default.)
This class was developed to deal with the blight of a Boolean function.
That includes blotted Boolean functions, where the arguments can be equal or complementary to the universe,
which is represented by −1.
methods
[edit | edit source]related set partition
[edit | edit source]related_part
is a derived SetPart, where complementary blocks are merged.
There are related methods like number_of_related_blocks
.
example |
---|
from discretehelpers.set_part import SetPart
x = SetPartComp([[0, 1], [2, 3], [4, 5, 6]], {(0, 2), (4, 9)}) # same as above
assert x.related_part() == SetPart([[0, 1, 2, 3], [4, 5, 6, 9]], 'Z') # domain Z rather than implicit N
assert x.number_of_related_blocks() == 2
assert x.length() == 10
assert x.affected_elements() == [0, 1, 2, 3, 4, 5, 6, 9]
assert x.number_of_affected_elements() == 8
|
set equal and complement
[edit | edit source]set_equal(a, b)
merges the blocks containing the elements a and b.
set_comp(a, b)
makes them complementary.
all complement pairs
[edit | edit source]example |
---|
x = SetPartComp([[0, 1], [2, 3], [4, 5]], {(0, 2)})
assert x.all_complement_pairs() == {(0, 2), (1, 2), (0, 3), (1, 3)}
x.set_comp(6, 9)
assert x == SetPartComp([[0, 1], [2, 3], [4, 5]], {(0, 2), (6, 9)})
assert x.all_complement_pairs() == {
(0, 2), (1, 2), (0, 3), (1, 3),
(6, 9),
}
x.set_equal(5, 6)
assert x == SetPartComp([[0, 1], [2, 3], [4, 5, 6]], {(4, 9), (0, 2)})
assert x.all_complement_pairs() == {
(0, 2), (1, 2), (0, 3), (1, 3),
(4, 9), (5, 9), (6, 9)
}
|
Boolean function
[edit | edit source]The method boolf
returns a Boolean function (Boolf), which contains the same information.
It is only blight. (Its blightless_boolf
is the tautology.)
If there are no gaps between the affected elements x.boolf().blight
is equal to x
.
from discretehelpers.boolf import Boolf
x = SetPartComp([[0, 1], [2, 3], [4, 5]], {(0, 2)})
assert x.boolf() == Boolf(fullspots=[3, 12, 51, 60], arity=6)
assert x.affected_elements() == x.boolf().atomvals == [0, 1, 2, 3, 4, 5]
assert x == x.boolf().blight
with gaps |
---|
There is a gap between 3 and 10. But the blight of a x = SetPartComp([[0, 1], [2, 3], [10, 11]], {(0, 2)})
assert x.boolf() == Boolf(fullspots=[3, 12, 51, 60], atomvals=[0, 1, 2, 3, 10, 11])
assert x.affected_elements() == x.boolf().atomvals == [0, 1, 2, 3, 10, 11]
assert x != x.boolf().blight
assert x.boolf().blight == SetPartComp([[0, 1], [2, 3], [4, 5]], {(0, 2)})
|
integer matches
[edit | edit source]x.int_matches(n)
checks if the binary digits of n are equal or different in the way described by x.
(This is used for the method bloatless_spotint of Boolf.)
example |
---|
equal_list = [0, 3, 4, 7, 8, 11, 12, 15] # The two least-significant binary digits are equal.
diff_list = [1, 2, 5, 6, 9, 10, 13, 14] # The two least-significant binary digits are different.
equal_spc = SetPartComp([[0, 1]])
diff_spc = SetPartComp([], {(0, 1)})
for n in equal_list:
assert equal_spc.int_matches(n)
assert not diff_spc.int_matches(n)
for n in diff_list:
assert diff_spc.int_matches(n)
assert not equal_spc.int_matches(n)
|