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


36
37
38
39
40
# File 'lib/functions/fnc-conditions.rb', line 36

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

Instance Attribute Details

#xObject (readonly)

Left hand side



26
27
28
# File 'lib/functions/fnc-conditions.rb', line 26

def x
  @x
end

#yObject (readonly)

Right hand side



28
29
30
# File 'lib/functions/fnc-conditions.rb', line 28

def y
  @y
end

Instance Method Details

#==(op) ⇒ Object

Return true if two functions are equal, false if different

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


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

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

  # 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`


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

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:



54
55
56
# File 'lib/functions/fnc-conditions.rb', line 54

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)


109
110
111
112
113
# File 'lib/functions/fnc-conditions.rb', line 109

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


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

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

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

#dot_graph(node) ⇒ Object

Returns the dot graphviz representation of the code

* **returns**: `String`


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

def dot_graph(node)
  cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}"
  "#{cls} -> #{@x.dot_graph node}\n  #{cls} -> #{@y.dot_graph node}"
end

#inspectObject

Inspector for the class. It is class specific

* **returns**: `String`


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

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

#representativeObject

Saves some required elements



43
44
45
46
47
# File 'lib/functions/fnc-conditions.rb', line 43

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

#simplifyObject

Simplify left and right term of the operator

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


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

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

#subs(fd) ⇒ Object

Substitute in the two elements using a dictionary

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


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

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`


75
76
77
# File 'lib/functions/fnc-conditions.rb', line 75

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

#to_sObject

Returns a string that represents the object to be printed

‘String`



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

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