Discrete helpers/set part comp

From Wikiversity
Jump to navigation Jump to search

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]
[edit | edit source]

related_part is a derived SetPart, where complementary blocks are merged.
There are related methods like number_of_related_blocks.

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]

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

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.)