class CAS::BinaryOp

Binary operator

Attributes

x[R]

First element of the operation

y[R]

Second element of the operation

Public Class Methods

new(x, y) click to toggle source

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 50
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

==(op) click to toggle source

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 181
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
args() click to toggle source

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 166
def args
  (@x.args + @y.args).uniq
end
call(_fd) click to toggle source

Same `CAS::Op#call`

* **argument**: `Hash` of values
* **returns**: `Numeric` for result
# File lib/operators/bary-op.rb, line 145
def call(_fd)
  raise CAS::CASError, "Not Implemented. This is a virtual method"
end
depend?(v) click to toggle source

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 69
def depend?(v)
  CAS::Help.assert(v, CAS::Op)

  @x.depend? v or @y.depend? v
end
diff(v) click to toggle source

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 81
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
dot_graph() click to toggle source

Return the local Graphviz node of the tree

* **returns**: `String` of local Graphiz node
# File lib/Mr.CAS/graphviz.rb, line 52
def dot_graph
  cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}"
  "#{cls} -> #{@x.dot_graph}\n  #{cls} -> #{@y.dot_graph}"
end
inspect() click to toggle source

Inspector

* **returns**: `String`
# File lib/operators/bary-op.rb, line 173
def inspect
  "#{self.class}(#{@x.inspect}, #{@y.inspect})"
end
simplify() click to toggle source

Executes simplifications of the two branches of the graph

* **returns**: `CAS::BinaryOp` as `self`
# File lib/operators/bary-op.rb, line 193
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
subs(dt) click to toggle source

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 95
def subs(dt)
  return self.subs_lhs(dt).subs_rhs(dt)
end
subs_lhs(dt) click to toggle source

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 103
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
subs_rhs(dt) click to toggle source

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 124
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
to_code() click to toggle source

Code to be used in `CAS::BinaryOp#to_proc`

* **returns**: `String`
# File lib/operators/bary-op.rb, line 159
def to_code
  raise CAS::CASError, "Not implemented. This is a virtual method"
end
to_s() click to toggle source

String representation of the tree

* **returns**: `String`
# File lib/operators/bary-op.rb, line 152
def to_s
  raise CAS::CASError, "Not Implemented. This is a virtual method"
end