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.
32 33 34 |
# File 'lib/Mr.CAS/auto-diff.rb', line 32 def initialize(x, y) @x, @y = x, y end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
30 31 32 |
# File 'lib/Mr.CAS/auto-diff.rb', line 30 def x @x end |
#y ⇒ Object (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 |
#abs ⇒ Object
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 |
#diff ⇒ Object
69 |
# File 'lib/Mr.CAS/auto-diff.rb', line 69 def diff; @y; end |
#inspect ⇒ Object
67 |
# File 'lib/Mr.CAS/auto-diff.rb', line 67 def inspect; "<#{@x},#{@y}>"; end |
#real ⇒ Object
68 |
# File 'lib/Mr.CAS/auto-diff.rb', line 68 def real; @x; end |
#to_s ⇒ Object
66 |
# File 'lib/Mr.CAS/auto-diff.rb', line 66 def to_s; self.inspect; end |