Class: Interval

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/borel/interval.rb

Direct Known Subclasses

Multiple, Simple

Defined Under Namespace

Classes: Multiple, Simple

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*array) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/borel/interval.rb', line 8

def self.[](*array)
  union(*
    if array.empty?
      []
    elsif array.first.kind_of?(Array)
      array.select{|x| !x.empty?}.map{|x| Simple.new(*x)}
    else
      [Simple.new(*array)]
    end
  )
rescue
  unless array.size <= 2 || array.all?{|x| Array === x && x.size <= 2}
    raise Exception::Construction, array
  end
  raise
end

.union(*array) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/borel/interval.rb', line 25

def self.union(*array)
  l = []
  array.map(&:components).flatten.sort_by(&:inf).each do |x|
    if x.sup < x.inf
      next
    elsif l.empty? || x.inf > l.last.sup
      l <<= x
    elsif x.sup > l.last.sup
      l[-1] = Simple.new(l.last.inf, x.sup)
    end
  end
  if l.size == 1 then l.first else Multiple.new(l) end
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
# File 'lib/borel/interval.rb', line 67

def ==(other)
  construction == other.construction
end

#complementObject Also known as: ~



48
49
50
51
# File 'lib/borel/interval.rb', line 48

def complement
  map{|x| x.to_interval.map(&:complement).reduce(:intersect)}.
    flatten.reduce(:union)
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/borel/interval.rb', line 71

def empty?
  components.empty?
end

#hullObject



87
88
89
90
91
92
93
# File 'lib/borel/interval.rb', line 87

def hull
  if empty?
    Interval[]
  else
    Interval[inf, sup]
  end
end

#inspectObject



79
80
81
# File 'lib/borel/interval.rb', line 79

def inspect
  "Interval" + construction.inspect
end

#intersect(other) ⇒ Object Also known as: &, ^



43
44
45
46
# File 'lib/borel/interval.rb', line 43

def intersect(other)
  other.to_interval.map{|y| map{|x| x.intersect(y)}}.
    flatten.reduce(:union) || Interval[]
end

#minus(other) ⇒ Object Also known as: -



58
59
60
61
62
63
64
65
# File 'lib/borel/interval.rb', line 58

def minus(other)
  if other.empty?
    self
  else
    map{|x| other.to_interval.map{|y| x.minus(y)}.reduce(:intersect)}.
      flatten.reduce(:union) || Interval[]
  end
end

#to_intervalObject



75
76
77
# File 'lib/borel/interval.rb', line 75

def to_interval
  self
end

#to_sObject



83
84
85
# File 'lib/borel/interval.rb', line 83

def to_s
  inspect
end

#union(other) ⇒ Object Also known as: +, |



39
40
41
# File 'lib/borel/interval.rb', line 39

def union(other)
  Interval.union(other.to_interval, self)
end