Class: Cassowary::SymbolicWeight

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/symbolic_weight.rb

Constant Summary collapse

StrengthLevels =
3
Zero =
new([0.0] * StrengthLevels)

Instance Method Summary collapse

Constructor Details

#initialize(levels = {}) ⇒ SymbolicWeight

Returns a new instance of SymbolicWeight.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/symbolic_weight.rb', line 10

def initialize(levels = {})
  @levels = [0.0] * StrengthLevels
  case levels
  when Hash
    levels.each_pair do |k, v|
      self[k] = v
    end
  when Array
    levels.each_with_index do |e, idx|
      self[idx] = e
    end
  else
    raise InternalError
  end
end

Instance Method Details

#*(n) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
# File 'lib/symbolic_weight.rb', line 38

def *(n)
  raise InternalError unless n.is_a? Numeric
  result = SymbolicWeight.new
  each_with_index do |e, idx|
    result[idx] = e * n
  end
  result
end

#+(n) ⇒ Object

Raises:



56
57
58
59
60
61
62
63
# File 'lib/symbolic_weight.rb', line 56

def +(n)
  raise InternalError unless n.is_a? SymbolicWeight
  result = SymbolicWeight.new
  each_with_index do |e, idx|
    result[idx] = e + n[idx]
  end
  result
end

#-(n) ⇒ Object

Raises:



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

def -(n)
  raise InternalError unless n.is_a? SymbolicWeight
  result = SymbolicWeight.new
  each_with_index do |e, idx|
    result[idx] = e - n[idx]
  end
  result
end

#/(n) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
# File 'lib/symbolic_weight.rb', line 47

def /(n)
  raise InternalError unless n.is_a? Numeric
  result = SymbolicWeight.new
  each_with_index do |e, idx|
    result[idx] = e / n
  end
  result
end

#<=>(other) ⇒ Object

Raises:



74
75
76
77
78
79
80
81
# File 'lib/symbolic_weight.rb', line 74

def <=>(other)
  raise InternalError unless other.is_a? SymbolicWeight
  each_with_index do |e, idx|
    return -1 if e < other[idx]
    return 1 if e > other[idx]
  end
  0
end

#[](idx) ⇒ Object



30
31
32
# File 'lib/symbolic_weight.rb', line 30

def [](idx)
  @levels[idx]
end

#[]=(idx, value) ⇒ Object



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

def []=(idx, value)
  @levels[idx] = value
end

#cl_approx(s) ⇒ Object

Raises:



83
84
85
86
87
88
89
# File 'lib/symbolic_weight.rb', line 83

def cl_approx(s)
  raise InternalError unless s.is_a? SymbolicWeight
  each_with_index do |e, idx|
    return false unless e.cl_approx(s[idx])
  end
  true
end

#cl_approx_zeroObject



91
92
93
# File 'lib/symbolic_weight.rb', line 91

def cl_approx_zero
  cl_approx Zero
end

#definitely_negativeObject



95
96
97
98
99
100
101
102
103
# File 'lib/symbolic_weight.rb', line 95

def definitely_negative
  epsilon = SimplexSolver::Epsilon
  nepsilon = 0.0 - epsilon
  each do |e|
    return true if e < nepsilon
    return false if e > epsilon
  end
  false
end

#each(*args, &block) ⇒ Object



26
27
28
# File 'lib/symbolic_weight.rb', line 26

def each(*args, &block)
  @levels.each(*args, &block)
end

#inspectObject



109
110
111
# File 'lib/symbolic_weight.rb', line 109

def inspect
  "[" + @levels.join(",") + "]"
end

#symbolic_weight?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/symbolic_weight.rb', line 105

def symbolic_weight?
  true
end