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



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

def Interval.[](*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



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

def Interval.union(*array)
  l = []
  array.map(&:components).flatten.sort_by(&:inf).each{|x|
    if x.sup < x.inf
      # skip it
    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
  }
  if l.size == 1
    l.first
  else
    Multiple.new(l)
  end
end

Instance Method Details

#+(other) ⇒ Object



94
95
96
# File 'lib/borel/interval.rb', line 94

def +(other)
  self | other
end

#==(other) ⇒ Object



58
59
60
# File 'lib/borel/interval.rb', line 58

def ==(other)
  self.components == other.components
end

#^(other) ⇒ Object



98
99
100
# File 'lib/borel/interval.rb', line 98

def ^(other)
  self & other
end

#coerce(other) ⇒ Object



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

def coerce(other)
  [other.to_interval, self]
end

#constructionObject



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

def construction
  map{|x| x.extrema.uniq}
end

#degenerate?Boolean

Returns:

  • (Boolean)


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

def degenerate?
  all? {|x| x.degenerate?}
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  components.empty?
end

#hullObject



82
83
84
85
86
87
88
# File 'lib/borel/interval.rb', line 82

def hull
  if empty?
    Interval[]
  else
    Interval[components.first.inf, components.last.sup]
  end
end

#include?(x) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/borel/interval.rb', line 62

def include?(x)
  any?{|i| i.include? x}
end

#inspectObject



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

def inspect
  "Interval" + construction.inspect
end

#simple?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/borel/interval.rb', line 46

def simple?
  false
end

#to_intervalObject



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

def to_interval
  self
end

#to_sObject



54
55
56
# File 'lib/borel/interval.rb', line 54

def to_s
  inspect
end

#union(other) ⇒ Object



90
91
92
# File 'lib/borel/interval.rb', line 90

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

#|(other) ⇒ Object



102
103
104
# File 'lib/borel/interval.rb', line 102

def |(other)
  self.union other.to_interval
end