Class: CAS::AutoDiff::DualNumber
- Inherits:
-
Object
- Object
- CAS::AutoDiff::DualNumber
- Includes:
- Math
- Defined in:
- lib/Mr.CAS/auto-diff.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #*(v) ⇒ Object
- #**(v) ⇒ Object
- #+(v) ⇒ Object
- #-(v) ⇒ Object
- #-@ ⇒ Object
- #/(v) ⇒ Object
- #abs ⇒ Object
- #diff ⇒ Object
-
#initialize(x, y) ⇒ DualNumber
constructor
A new instance of DualNumber.
- #inspect ⇒ Object
- #real ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(x, y) ⇒ DualNumber
Returns a new instance of DualNumber.
9 10 11 |
# File 'lib/Mr.CAS/auto-diff.rb', line 9 def initialize(x, y) @x, @y = x, y end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
7 8 9 |
# File 'lib/Mr.CAS/auto-diff.rb', line 7 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
7 8 9 |
# File 'lib/Mr.CAS/auto-diff.rb', line 7 def y @y end |
Instance Method Details
#*(v) ⇒ Object
21 22 23 |
# File 'lib/Mr.CAS/auto-diff.rb', line 21 def *(v) DualNumber.new @x * v.x, @y * v.x + @x * v.y end |
#**(v) ⇒ Object
33 34 35 36 |
# File 'lib/Mr.CAS/auto-diff.rb', line 33 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
13 14 15 |
# File 'lib/Mr.CAS/auto-diff.rb', line 13 def +(v) DualNumber.new @x + v.x, @y + v.y end |
#-(v) ⇒ Object
17 18 19 |
# File 'lib/Mr.CAS/auto-diff.rb', line 17 def -(v) DualNumber.new @x - v.x, @y - v.y end |
#-@ ⇒ Object
29 30 31 |
# File 'lib/Mr.CAS/auto-diff.rb', line 29 def -@ DualNumber.new -@x, -@y end |
#/(v) ⇒ Object
25 26 27 |
# File 'lib/Mr.CAS/auto-diff.rb', line 25 def /(v) DualNumber.new @x / v.x, (@y * v.x - @x * v.y) / (v.x ** 2) end |
#abs ⇒ Object
38 39 40 41 |
# File 'lib/Mr.CAS/auto-diff.rb', line 38 def abs return DualNumber.new(0, 0) if @x == 0 DualNumber.new @x.abs, @y * (@x <=> 0) end |
#diff ⇒ Object
46 |
# File 'lib/Mr.CAS/auto-diff.rb', line 46 def diff; @y; end |
#inspect ⇒ Object
44 |
# File 'lib/Mr.CAS/auto-diff.rb', line 44 def inspect; "<#{@x},#{@y}>"; end |
#real ⇒ Object
45 |
# File 'lib/Mr.CAS/auto-diff.rb', line 45 def real; @x; end |
#to_s ⇒ Object
43 |
# File 'lib/Mr.CAS/auto-diff.rb', line 43 def to_s; self.inspect; end |