Class: CAS::AutoDiff::DualNumber

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/Mr.CAS/auto-diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ DualNumber

Returns a new instance of DualNumber.



32
33
34
# File 'lib/Mr.CAS/auto-diff.rb', line 32

def initialize(x, y)
  @x, @y = x, y
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



30
31
32
# File 'lib/Mr.CAS/auto-diff.rb', line 30

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



30
31
32
# File 'lib/Mr.CAS/auto-diff.rb', line 30

def y
  @y
end

Instance Method Details

#*(v) ⇒ Object



44
45
46
# File 'lib/Mr.CAS/auto-diff.rb', line 44

def *(v)
  DualNumber.new @x * v.x, @y * v.x + @x * v.y
end

#**(v) ⇒ Object



56
57
58
59
# File 'lib/Mr.CAS/auto-diff.rb', line 56

def **(v)
  t = (v.y == 0 ? 0 : @x * log(@x) * v.y)
  DualNumber.new @x ** v.x, (@x ** (v.x - 1)) * (v.x * @y + t)
end

#+(v) ⇒ Object



36
37
38
# File 'lib/Mr.CAS/auto-diff.rb', line 36

def +(v)
  DualNumber.new @x + v.x, @y + v.y
end

#-(v) ⇒ Object



40
41
42
# File 'lib/Mr.CAS/auto-diff.rb', line 40

def -(v)
  DualNumber.new @x - v.x, @y - v.y
end

#-@Object



52
53
54
# File 'lib/Mr.CAS/auto-diff.rb', line 52

def -@
  DualNumber.new(-@x, -@y)
end

#/(v) ⇒ Object



48
49
50
# File 'lib/Mr.CAS/auto-diff.rb', line 48

def /(v)
  DualNumber.new @x / v.x, (@y * v.x - @x * v.y) / (v.x ** 2)
end

#absObject



61
62
63
64
# File 'lib/Mr.CAS/auto-diff.rb', line 61

def abs
  return DualNumber.new(0, 0) if @x == 0
  DualNumber.new @x.abs, @y * (@x <=> 0)
end

#diffObject



69
# File 'lib/Mr.CAS/auto-diff.rb', line 69

def diff; @y; end

#inspectObject



67
# File 'lib/Mr.CAS/auto-diff.rb', line 67

def inspect; "<#{@x},#{@y}>"; end

#realObject



68
# File 'lib/Mr.CAS/auto-diff.rb', line 68

def real; @x; end

#to_sObject



66
# File 'lib/Mr.CAS/auto-diff.rb', line 66

def to_s;    self.inspect;  end