Class: CAS::BoxCondition

Inherits:
Condition show all
Defined in:
lib/functions/fnc-box-conditions.rb

Overview

BoxCondition class constructs a condition of the type:

“‘ L < f(x) < U “`

and this is a metaclass for different type of box conditions:

* open: `a < f(x) < b`
* lower closed: `a ≤ f(x) < b`
* upper closed: `a < f(x) ≤ b`
* closed: `a ≤ f(x) ≤ b`

Instance Attribute Summary collapse

Attributes inherited from Condition

#y

Instance Method Summary collapse

Constructor Details

#initialize(x, lower, upper) ⇒ BoxCondition

Initializes a new box condition. A function is required as central term, while the second and the third elements are lower and upper bounds as ‘CAS::Constant`

* **argument**: `CAS::Op` central term of the box condition
* **argument**: `CAS::Constant` lower bound
* **argument**: `CAS::Constant` upper bound
* **returns**: `CAS::BoxCondition` new instance


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/functions/fnc-box-conditions.rb', line 61

def initialize(x, lower, upper)
  if lower.is_a? Numeric
    lower = CAS::const lower
  end
  if upper.is_a? Numeric
    upper = CAS::const upper
  end
  CAS::Help.assert(lower, CAS::Constant)
  CAS::Help.assert(upper, CAS::Constant)

  CAS::Help.assert(x, CAS::Op)

  lower, upper = upper, lower if lower.x > upper.x

  @lower = lower
  @upper = upper
  @x = x
end

Instance Attribute Details

#lowerObject (readonly)

Upper bound as ‘CAS::Constant`



49
50
51
# File 'lib/functions/fnc-box-conditions.rb', line 49

def lower
  @lower
end

#upperObject (readonly)

Lower bound as ‘CAs::Constant`



51
52
53
# File 'lib/functions/fnc-box-conditions.rb', line 51

def upper
  @upper
end

#xObject (readonly)

Contained operation



47
48
49
# File 'lib/functions/fnc-box-conditions.rb', line 47

def x
  @x
end

Instance Method Details

#==(cond) ⇒ Object

Return true if two BoxConditions are equal, false if different

* **argument**: `CAS::Op` operator to check against for equality
* **returns**: `TrueClass` or `FalseClass`


139
140
141
142
# File 'lib/functions/fnc-box-conditions.rb', line 139

def ==(cond)
  return false if not self.class != cond.class
  return (@x == cond.x and @lower == cond.lower and @upper == cond.upper)
end

#argsObject

Returns an array of variables of the central function

* **returns**: `Array` of `CAS::Variable`


131
132
133
# File 'lib/functions/fnc-box-conditions.rb', line 131

def args
  @x.args
end

#call(_fd) ⇒ Object

Function call will evaluate left and right functions to solve the relation

* **argument**: `Hash` with feed dictionary
* **returns**: `Trueclass` or `Falseclass`

Raises:



149
150
151
# File 'lib/functions/fnc-box-conditions.rb', line 149

def call(_fd)
  raise CAS::CASError, "This is a virtual method"
end

#depend?(v) ⇒ Boolean

Returns true if one the central function depends upon the expression included

* **argument**: `CAS::Op` operator to check against for dependencies
* **returns**: `TrueClass` or `FalseClass`

Returns:

  • (Boolean)


91
92
93
# File 'lib/functions/fnc-box-conditions.rb', line 91

def depend?(v)
  @x.depend? v
end

#diff(v) ⇒ Object

Performs the derivative of the box condition. The derivative of a box condition is a ‘CAS::Equal` object (the derivative of a constant is zero):

“‘

 d
-- [a < f(x) < b] = f'(x) == 0
dx

“‘

since between the two there is a difference relation.

* **argument**: `CAS::Op` to perform the derivative


124
125
126
# File 'lib/functions/fnc-box-conditions.rb', line 124

def diff(v)
  CAS::equal(@x.diff(v).simplify, CAS::Zero)
end

#inspectObject

Inspector for the class. It is class specific

* **returns**: `String`


156
157
158
# File 'lib/functions/fnc-box-conditions.rb', line 156

def inspect
  "(#{@lower.inspect} #{@lower_cond} #{@x.inspect} #{@upper_cond} #{@upper.inspect})"
end

#representativeObject

Saves some required elements



81
82
83
84
85
# File 'lib/functions/fnc-box-conditions.rb', line 81

def representative
  @lower_cond  = @upper_cond = "<"
  @lower_str   = @upper_str  = "<"
  self
end

#simplifyObject

Simplify left and right term of the operator

* **returns**: `CAS::BoxCondition`


98
99
100
101
# File 'lib/functions/fnc-box-conditions.rb', line 98

def simplify
  @x.simplify
  return self
end

#subs(fd) ⇒ Object

Substitute in the central element using a dictionary

* **returns**: `Hash` of substitutions


106
107
108
109
# File 'lib/functions/fnc-box-conditions.rb', line 106

def subs(fd)
  @x = @x.subs(fd)
  return self
end

#to_codeObject

Return the code that performs a condition evaluation

* **returns**: `String`


170
171
172
# File 'lib/functions/fnc-box-conditions.rb', line 170

def to_code
  "((#{@lower.to_code} #{@lower_cond} (#{@x.to_code})) and ((#{@x.to_code}) #{@upper_cond} #{@upper.to_code}))"
end

#to_sObject

Returns a string that represents the object to be printed

‘String`



163
164
165
# File 'lib/functions/fnc-box-conditions.rb', line 163

def to_s
  "#{@lower} #{@lower_str} #{@x} #{@upper_str} #{@upper}"
end