7

I am in a Discrete Math course and am trying to replicate De Morgan's law of

Complement(B union C) = Complement(B) intersect Complement(C).

I tried searching for Python's ability to perform a complement on a set, but I didn't see much if anything.

So I just tried this in IDLE. Does this appear correct?

U = {'pink', 'purple', 'red', 'blue', 'gray', 'orange', 'green', 'yellow', 'indigo', 'violet'}
A = {'purple', 'red', 'orange', 'yellow', 'violet'}
B = {'blue', 'gray', 'orange', 'green'}
C = {'pink', 'red','blue','violet'}
Comp_B = U - B
Comp_C = U - C
Comp_Union_BC = Comp_B.intersection(Comp_C)
print(Comp_Union_BC)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
TimmyTooTough
  • 101
  • 1
  • 1
  • 6
  • The complement of a set is *everything else*. What is *everything else* here? – Martijn Pieters May 15 '18 at 20:08
  • "Without a definition of the universal set, you can't really give a definition of the complement of a set". That answered my question. It would be neat if Python had a function built in that took a defined set as a parameter then you could work with it. Perhaps I need to make this for the Python standard library. – TimmyTooTough May 15 '18 at 22:28
  • but as shown, `complement(a)` is really just an alias for `U.difference(a)`. You could just use `complement = U.difference`. – Martijn Pieters May 15 '18 at 23:14

2 Answers2

12

The complement of a set is everything not in the set, but part of the 'universal set'. Without a definition of the universal set, you can't really give a standard-library definition of the complement of a set.

Moreover, the Python set type deals in sets of discrete objects, not a mathematical construct that could be infinitely large, such as all natural numbers. So Python doesn't support the general, nebulous and infinite idea of a single universal set.

For specific domains, if you can define the universal set in discrete terms, simply define your own complement() callable. For example, given a global definition of the universal set U, you can define the complement of a set a as the difference between U and that set:

U = {'pink', 'purple', 'red', 'blue', 'gray', 'orange', 'green', 'yellow', 'indigo', 'violet'}

def complement(a):
    # difference between a global universal set U and the given set a
    return U - a

or, simpler still:

complement = U.difference

Then just pass a set to complement() to test the hypothesis:

>>> # set | set == set union, set & set == set intersection
...
>>> complement(B | C) == complement(B) & complement(C)
True

So yes, your interpretation is correct, U - B produces the complement of B.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

What if you extend the set class, I mean creating a subclass of set. This class will have a property called complement that will facilitate to corroborate De Morgan's law. Make sure to override the union method.

class Set(set):
    def __init__(self, s=(), U=None):
        super().__init__(s)
        self.U = U

    def union(self, A):
        return Set(super().union(A), self.U)

    @property
    def complement(self):
        if U is None:
            raise Exception('Universal set not defined')
        return Set(self.U - self, self.U)

U = {'pink', 'purple', 'red', 'blue', 'gray', 'orange', 'green', 'yellow', 'indigo', 'violet'}
A = Set({'purple', 'red', 'orange', 'yellow', 'violet'}, U)
B = Set({'blue', 'gray', 'orange', 'green'}, U)
C = Set({'pink', 'red','blue','violet'}, U)

Now, you just have to run the following:

>>> B.union(C).complement == B.complement.intersection(C.complement)
True
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228