Class: Differentiation::DualNumber

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/differentiation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, diff = lambda{|_| 0}) ⇒ DualNumber

Returns a new instance of DualNumber.



53
54
55
56
# File 'lib/differentiation.rb', line 53

def initialize(n, diff=lambda{|_| 0})
  @n = n
  @diff = diff
end

Instance Attribute Details

#diffObject (readonly)

Returns the value of attribute diff.



58
59
60
# File 'lib/differentiation.rb', line 58

def diff
  @diff
end

#nObject (readonly)

Returns the value of attribute n.



58
59
60
# File 'lib/differentiation.rb', line 58

def n
  @n
end

Instance Method Details

#*(other) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/differentiation.rb', line 120

def *(other)
  if other.is_a?(DualNumber)
    n = @n * other.n
    diff = ->(key) { @n * other.diff.call(key) + @diff.call(key) * other.n }
  else
    n = @n * other
    diff = ->(key) { @diff.call(key) * other }
  end
  DualNumber.new(n, diff)
end

#**(other) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/differentiation.rb', line 142

def **(other)
  if other.is_a?(DualNumber)
    n = @n ** other.n
    diff = ->(key) { (@n ** other.n) * (other.diff.call(key) * Math.log(@n) + (other.n / @n)) }
  else
    n = @n ** other
    diff = ->(key) { ((@n ** (other-1)) * other) * @diff.call(key) }
  end
  DualNumber.new(n, diff)
end

#+(other) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/differentiation.rb', line 98

def +(other)
  if other.is_a?(DualNumber)
    n = @n + other.n
    diff = ->(key) { @diff.call(key) + other.diff.call(key) }
  else
    n = @n + other
    diff = @diff
  end
  DualNumber.new(n, diff)
end

#-(other) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/differentiation.rb', line 109

def -(other)
  if other.is_a?(DualNumber)
    n = @n - other.n
    diff = ->(key) { @diff.call(key) - other.diff.call(key) }
  else
    n = @n - other
    diff = @diff
  end
  DualNumber.new(n, diff)
end

#/(other) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/differentiation.rb', line 131

def /(other)
  if other.is_a?(DualNumber)
    n = @n / other.n
    diff = ->(key) { (@diff.call(key) / other) + (@n * other.diff.call(key)) / (other.n ** 2) }
  else
    n = @n / other
    diff = ->(key) { @diff.call(key) / other }
  end
  DualNumber.new(n, diff)
end

#<=>(other) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/differentiation.rb', line 90

def <=>(other)
  if other.is_a?(DualNumber)
    @n <=> other.n
  else
    @n <=> other
  end
end

#coerce(other) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/differentiation.rb', line 82

def coerce(other)
  if Differentiation.differentiable?(other)
    [DualNumber.new(other), self]
  else
    super
  end
end

#derivative(key) ⇒ Object



60
61
62
# File 'lib/differentiation.rb', line 60

def derivative(key)
  @diff.call(key)
end

#gradients(*keys) ⇒ Object



64
65
66
67
68
# File 'lib/differentiation.rb', line 64

def gradients(*keys)
  keys.each_with_object({}) do |k, o|
    o[k] = @diff.call(k)
  end
end

#inspectObject



153
154
155
156
157
158
159
# File 'lib/differentiation.rb', line 153

def inspect
  if $DEBUG
    "<DualNumber: #{@n} >"
  else
    @n.inspect
  end
end

#to_fObject



78
79
80
# File 'lib/differentiation.rb', line 78

def to_f
  @n.to_f
end

#to_iObject



70
71
72
# File 'lib/differentiation.rb', line 70

def to_i
  @n.to_i
end

#to_intObject



74
75
76
# File 'lib/differentiation.rb', line 74

def to_int
  @n.to_int
end