Class: Cantor::RelativeComplement

Inherits:
AbstractSet show all
Defined in:
lib/cantor/relative_complement.rb

Overview

This data type is an infinite and non-enumerable set of values that encodes the complement of a RelativeSet

Set Operations collapse

Set Ordering collapse

Pretty Printing collapse

Instance Method Summary collapse

Methods inherited from AbstractSet

#&, #+, #-, #<, #<=, #>, #>=, #^, #contain?, #disjoint?, #exclude?, #infinite?, #member?, #present?, #subset?, #superset?, #|, #~

Constructor Details

#initialize(complement) ⇒ RelativeComplement

Returns a new instance of RelativeComplement.



11
12
13
# File 'lib/cantor/relative_complement.rb', line 11

def initialize(complement)
  @complement = complement
end

Instance Method Details

#==(other) ⇒ Object



118
119
120
121
# File 'lib/cantor/relative_complement.rb', line 118

def ==(other)
  eql?(other) or
   (other.is_a?(RelativeComplement) and complement == other.complement)
end

#complementObject



47
48
49
50
# File 'lib/cantor/relative_complement.rb', line 47

def complement
  # ¬(¬A) = A
  @complement
end

#difference(other) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/cantor/relative_complement.rb', line 94

def difference(other)
  if other.is_a?(RelativeComplement)
    # ¬A ∖ ¬B = ¬A ∩ B = B ∖ A
    other.complement.difference(complement)
  else
    # ¬A ∖ B = ¬A ∩ ¬B = ¬(A ∪ B)
    complement.union(Cantor.build(other)).complement
  end
end

#empty?Boolean

Returns false.

Returns:

  • (Boolean)

    false



34
35
36
# File 'lib/cantor/relative_complement.rb', line 34

def empty?
  false
end

#finite?Boolean

Returns false.

Returns:

  • (Boolean)

    false



28
29
30
# File 'lib/cantor/relative_complement.rb', line 28

def finite?
  false
end

#include?(object) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/cantor/relative_complement.rb', line 16

def include?(object)
  not @complement.include?(object)
end

#inspectString

Returns:

  • (String)


127
128
129
# File 'lib/cantor/relative_complement.rb', line 127

def inspect
  "RelativeComplement(#{@complement.inspect})"
end

#intersection(other) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/cantor/relative_complement.rb', line 53

def intersection(other)
  if other.is_a?(RelativeComplement)
    # ¬A ∩ ¬B = ¬(A ∪ B)
    complement.union(other.complement).complement
  else
    # ¬A ∩ B = B ∖ A
    Cantor.build(other).difference(complement)
  end
end

#proper_subset?(other) ⇒ Boolean

True if this set is a subset of the ‘other` set and there exists at least one element in the `other` set that doesn’t belong to this set

Returns:

  • (Boolean)

See Also:



108
109
110
# File 'lib/cantor/relative_complement.rb', line 108

def proper_subset?(other)
  other.is_a?(RelativeComplement) and intersection(other) == self
end

#proper_superset?(other) ⇒ Boolean

True if this set is a superset of the ‘other` set and there exists at least one element in this set that doesn’t belong to the ‘other` set

Returns:

  • (Boolean)

See Also:



113
114
115
# File 'lib/cantor/relative_complement.rb', line 113

def proper_superset?(other)
  other.is_a?(RelativeComplement) and intersection(other) == other
end

#replace(other) ⇒ Object



39
40
41
# File 'lib/cantor/relative_complement.rb', line 39

def replace(other)
  Cantor.build(other)
end

#sizeObject

Returns Infinity.

Returns:

  • Infinity



22
23
24
# File 'lib/cantor/relative_complement.rb', line 22

def size
  1.0 / 0.0
end

#symmetric_difference(other) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cantor/relative_complement.rb', line 75

def symmetric_difference(other)
  if other.is_a?(RelativeComplement)
    # ¬A ⊖ ¬B = (¬A ∖ ¬B) ∪ (¬B ∖ ¬A)
    #         =  (B ∖ A)  ∪ (A ∖ B)
    #         = A ⊖ B
    complement.symmetric_difference(other.complement)
  else
    # ¬A ⊖ B = (¬A ∖ B)  ∪ (B ∖ ¬A)
    #        = (¬A ∩ ¬B) ∪ (B ∩ A)
    #        = (¬B ∖ A)  ∪ (A ∖ ¬B)
    #        = A ⊖ ¬B
    other = Cantor.build(other)

    intersection(other.complement).
      union(other.intersection(complement))
  end
end

#union(other) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/cantor/relative_complement.rb', line 64

def union(other)
  if other.is_a?(RelativeComplement)
    # ¬A ∪ ¬B = ¬(A ∩ B)
    complement.intersection(other.complement).complement
  else
    # ¬A ∪ B = ¬(A ∖ B)
    complement.difference(Cantor.build(other)).complement
  end
end