Class: CAS::Diff

Inherits:
BinaryOp show all
Defined in:
lib/functions/fnc-base.rb

Overview

**Difference basic operation**. It's a binary operation. This cannot be implemented as a n-ary op, thus will not be changed

Instance Attribute Summary

Attributes inherited from BinaryOp

#x, #y

Attributes inherited from Op

#x

Instance Method Summary collapse

Methods inherited from BinaryOp

#==, #args, #depend?, #dot_graph, #initialize, #inspect, #subs, #subs_lhs, #subs_rhs

Methods inherited from Op

#!=, #*, #**, #+, #-, #[email protected], #/, #==, #args, #as_proc, #depend?, #dot_graph, #equal, #greater, #greater_equal, init_simplify_dict, #initialize, #inspect, #limit, numeric_to_const, simplify_dict, #simplify_dictionary, #smaller, #smaller_equal, #subs, #to_c_lib

Constructor Details

This class inherits a constructor from CAS::BinaryOp

Instance Method Details

#call(f) ⇒ Object

Same as `CAS::Op`


54
55
56
57
58
# File 'lib/functions/fnc-base.rb', line 54

def call(f)
  CAS::Help.assert(f, Hash)

  return @x.call(f).overloaded_minus(@y.call(f))
end

#diff(v) ⇒ Object

Performs the difference between two `CAS::Op`s

“`

d

—- (f(x) - g(x)) = f'(x) - g'(x)

dx

“`

* **argument**: `CAS::Op` argument of derivative
* **returns**: `CAS::Op` derivative

46
47
48
49
50
51
# File 'lib/functions/fnc-base.rb', line 46

def diff(v)
  left, right = super v
  return left if right == CAS::Zero
  return CAS::Invert.new(right) if left == CAS::Zero
  left - right
end

#simplifyObject

Same as `CAS::Op`

Simplifcation engine supports:

* 0 - y = -y
* x - 0 = x
* a - b = c (constants reduction)
* x - x = 0
* x - (-y) = x + y

* **returns**: `CAS::Op` simplified version

76
77
78
79
80
81
82
83
84
85
# File 'lib/functions/fnc-base.rb', line 76

def simplify
  super
  return CAS.invert(@y) if @x == CAS::Zero
  return @x if @y == CAS::Zero
  return CAS::Zero if @x == @y
  return CAS.const(self.call({})) if (@x.is_a? CAS::Constant and @y.is_a? CAS::Constant)
  return @x + @y.x if @y.is_a? CAS::Invert
  return -(@x.x + @y) if @x.is_a? CAS::Invert
  return self
end

#to_codeObject

Convert expression to code (internal, for `CAS::Op#to_proc` method)

* **returns**: `String` that represent Ruby code to be parsed in `CAS::Op#to_proc`

90
91
92
# File 'lib/functions/fnc-base.rb', line 90

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

#to_sObject

Same as `CAS::Op`


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

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