class CAS::BinaryOp
Binary operator
Attributes
First element of the operation
Second element of the operation
Public Class Methods
The binary operator inherits from the `CAS::Op`, even if it is defined as a node with two possible branches. This is particular of the basic operations. The two basic nodes shares the same interface, so all the operations do not need to know which kind of node they are handling.
* **argument**: `CAS::Op` left argument of the node or `Numeric` to be converted in `CAS::Constant` * **argument**: `CAS::Op` right argument of the node or `Numeric` to be converted in `CAS::Constant` * **returns**: `CAS::BinaryOp` instance
# File lib/operators/bary-op.rb, line 27 def initialize(x, y) if x.is_a? Numeric x = BinaryOp.numeric_to_const x end if y.is_a? Numeric y = BinaryOp.numeric_to_const y end CAS::Help.assert(x, CAS::Op) CAS::Help.assert(y, CAS::Op) @x = x @y = y end
Public Instance Methods
Comparison with other `CAS::Op`. This is not a math operation.
* **argument**: `CAS::Op` to be compared against * **returns**: `TrueClass` if equal, `FalseClass` if different
# File lib/operators/bary-op.rb, line 158 def ==(op) CAS::Help.assert(op, CAS::Op) if op.is_a? CAS::BinaryOp return (self.class == op.class and @x == op.x and @y == op.y) else return false end end
Returns an array of all the variables that are in the graph
* **returns**: `Array` of `CAS::Variable`s
# File lib/operators/bary-op.rb, line 143 def args (@x.args + @y.args).uniq end
Same `CAS::Op#call`
* **argument**: `Hash` of values * **returns**: `Numeric` for result
# File lib/operators/bary-op.rb, line 122 def call(_fd) raise CAS::CASError, "Not Implemented. This is a virtual method" end
Return the dependencies of the operation. Requires a `CAS::Variable` and it is one of the recursve method (implicit tree resolution)
* **argument**: `CAS::Variable` instance * **returns**: `TrueClass` if depends, `FalseClass` if not
# File lib/operators/bary-op.rb, line 46 def depend?(v) CAS::Help.assert(v, CAS::Op) @x.depend? v or @y.depend? v end
This method returns an array with the derivatives of the two branches of the node. This method is usually called by child classes, and it is not intended to be used directly.
* **argument**: `CAS::Op` operation to differentiate against * **returns**: `Array` of differentiated branches ([0] for left, [1] for right)
# File lib/operators/bary-op.rb, line 58 def diff(v) CAS::Help.assert(v, CAS::Op) left, right = CAS::Zero, CAS::Zero left = @x.diff(v) if @x.depend? v right = @y.diff(v) if @y.depend? v return left, right end
Return the local Graphviz node of the tree
* **returns**: `String` of local Graphiz node
# File lib/Mr.CAS/graphviz.rb, line 29 def dot_graph cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}" "#{cls} -> #{@x.dot_graph}\n #{cls} -> #{@y.dot_graph}" end
Inspector
* **returns**: `String`
# File lib/operators/bary-op.rb, line 150 def inspect "#{self.class}(#{@x.inspect}, #{@y.inspect})" end
Executes simplifications of the two branches of the graph
* **returns**: `CAS::BinaryOp` as `self`
# File lib/operators/bary-op.rb, line 170 def simplify hash = @x.to_s @x = @x.simplify while @x.to_s != hash hash = @x.to_s @x = @x.simplify end hash = @y.to_s @y = @y.simplify while @y.to_s != hash hash = @y.to_s @y = @y.simplify end end
Substituitions for both branches of the graph, same as `CAS::Op#subs`
* **argument**: `Hash` of substitutions * **returns**: `CAS::BinaryOp`, in practice `self`
# File lib/operators/bary-op.rb, line 72 def subs(dt) return self.subs_lhs(dt).subs_rhs(dt) end
Substituitions for left branch of the graph, same as `CAS::Op#subs`
* **argument**: `Hash` of substitutions * **returns**: `CAS::BinaryOp`, in practice `self`
# File lib/operators/bary-op.rb, line 80 def subs_lhs(dt) CAS::Help.assert(dt, Hash) sub = dt.keys.select { |e| e == @x }[0] if sub if dt[sub].is_a? CAS::Op @x = dt[sub] elsif dt[sub].is_a? Numeric @x = CAS::const dt[sub] else raise CASError, "Impossible subs. Received a #{dt[sub].class} = #{dt[sub]}" end else @x.subs(dt) end return self end
Substituitions for left branch of the graph, same as `CAS::Op#subs`
* **argument**: `Hash` of substitutions * **returns**: `CAS::BinaryOp`, in practice `self`
# File lib/operators/bary-op.rb, line 101 def subs_rhs(dt) CAS::Help.assert(dt, Hash) sub = dt.keys.select { |e| e == @y }[0] if sub if dt[sub].is_a? CAS::Op @y = dt[sub] elsif dt[sub].is_a? Numeric @y = CAS::const dt[sub] else raise CASError, "Impossible subs. Received a #{dt[sub].class} = #{dt[sub]}" end else @y.subs(dt) end return self end
Code to be used in `CAS::BinaryOp#to_proc`
* **returns**: `String`
# File lib/operators/bary-op.rb, line 136 def to_code raise CAS::CASError, "Not implemented. This is a virtual method" end
String representation of the tree
* **returns**: `String`
# File lib/operators/bary-op.rb, line 129 def to_s raise CAS::CASError, "Not Implemented. This is a virtual method" end