class Autodiff::DualNum

Attributes

real[RW]

Public Class Methods

new(n, e=0) click to toggle source
# File lib/autodiff/dual_num.rb, line 19
def initialize(n, e=0)
  @real = n
  @epsilon = e
  self.freeze
end

Public Instance Methods

*(other) click to toggle source
# File lib/autodiff/dual_num.rb, line 45
def *(other)
  r = @real * other.real
  e = @real * other.epsilon + @epsilon * other.real
  self.class.new(r, e)
end
**(other) click to toggle source
# File lib/autodiff/dual_num.rb, line 59
def **(other)
  r = @real ** other.real
  e = other.real * (@real ** (other.real - 1)) * @epsilon
  self.class.new(r, e)
end
+(other) click to toggle source
# File lib/autodiff/dual_num.rb, line 33
def +(other)
  r = @real + other.real
  e = @epsilon + other.epsilon
  self.class.new(r, e)
end
-(other) click to toggle source
# File lib/autodiff/dual_num.rb, line 39
def -(other)
  r = @real - other.real
  e = @epsilon - other.epsilon
  self.class.new(r, e)
end
/(other) click to toggle source

do tmp = 1/other then do self * tmp

# File lib/autodiff/dual_num.rb, line 52
def /(other)
  r = 1.0 / other.real
  e = - other.epsilon.to_f / (other.real ** 2)
  tmp = self.class.new(r, e)
  self * tmp
end
to_dual() click to toggle source
# File lib/autodiff/dual_num.rb, line 25
def to_dual
  self
end
to_float() click to toggle source
# File lib/autodiff/dual_num.rb, line 65
def to_float
  real
end
to_one_epsilon() click to toggle source
# File lib/autodiff/dual_num.rb, line 29
def to_one_epsilon
  Autodiff::DualNum.new(self.real, 1)
end