Class: CAS::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/functions/fnc-conditions.rb

Overview

Condition class is a pseudo-class for all the other kind of conditions:

* Equal
* Greater
* GreaterEqual
* Smaller
* SmallerEqual

When derivated, the two functions (that can be considered as the difference of two elements) are derived autonoumouosly. A condition is composed by:

* left function (x)
* right function (y)
* type of condition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Condition

Initializer for a new condition. The condition is implicit in the class, thus a pure ‘CAS::Condition` cannot be used.

* **argument**: `CAS::Op` left argument
* **argument**: `CAS::Op` right argument
* **returns**: `CAS::Condition` new instance


59
60
61
62
63
# File 'lib/functions/fnc-conditions.rb', line 59

def initialize(x, y)
  @x = x
  @y = y
  self.representative
end

Instance Attribute Details

#xObject (readonly)

Left hand side



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

def x
  @x
end

#yObject (readonly)

Right hand side



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

def y
  @y
end

Instance Method Details

#==(op) ⇒ Object

Return true if two functions are equal, false if different

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


142
143
144
145
146
147
# File 'lib/functions/fnc-conditions.rb', line 142

def ==(op)
  CAS::Help.assert(op, CAS::Condition)

  # condB = (@x == op.y) and (@y == op.x)
  return ((@x == op.x) and (@y == op.y) and (self.class == op.class))
end

#argsObject

Returns an array of variables of the two functions in the condition

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


105
106
107
# File 'lib/functions/fnc-conditions.rb', line 105

def args
  (@x.args + @y.args).uniq
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:



77
78
79
# File 'lib/functions/fnc-conditions.rb', line 77

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

#depend?(v) ⇒ Boolean

Returns true if one of the two functions depends upon the expression included

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

Returns:

  • (Boolean)


132
133
134
135
136
# File 'lib/functions/fnc-conditions.rb', line 132

def depend?(v)
  CAS::Help.assert v, CAS::Op

  @x.depend?(v) or @y.depend?(v)
end

#diff(v) ⇒ Object

Performs the derivative of the two elements:

“‘

d

– [f(x) > g(y)] = f’(x) > g’(x) dx “‘

since between the two there is a difference relation.

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


120
121
122
123
124
125
126
# File 'lib/functions/fnc-conditions.rb', line 120

def diff(v)
  CAS::Help.assert v, CAS::Op

  @x.diff(v)
  @y.diff(v)
  self.simplify
end

#inspectObject

Inspector for the class. It is class specific

* **returns**: `String`


84
85
86
# File 'lib/functions/fnc-conditions.rb', line 84

def inspect
  "#{self.class}(#{@x.inspect}, #{@y.inspect})"
end

#representativeObject

Saves some required elements



66
67
68
69
70
# File 'lib/functions/fnc-conditions.rb', line 66

def representative
  @cond_type  = "??"
  @cond_repr  = "??"
  self
end

#simplifyObject

Simplify left and right term of the operator

* **returns**: `CAS::Condition`


152
153
154
155
156
# File 'lib/functions/fnc-conditions.rb', line 152

def simplify
  @x = @x.simplify
  @x = @y.simplify
  return self
end

#subs(fd) ⇒ Object

Substitute in the two elements using a dictionary

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


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

def subs(fd)
  CAS::Help.assert(fd, Hash)
  @x.subs(fd)
  @y.subs(fd)
  return self
end

#to_codeObject

Return the code that performs a condition evaluation

* **returns**: `String`


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

def to_code
  "(#{@x} #{@cond_type} #{@y})"
end

#to_sObject

Returns a string that represents the object to be printed

‘String`



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

def to_s
  "(#{@x} #{@cond_repr} #{@y})"
end