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.



9
10
11
# File 'lib/Mr.CAS/auto-diff.rb', line 9

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

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



7
8
9
# File 'lib/Mr.CAS/auto-diff.rb', line 7

def x
  @x
end

#yObject (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

#absObject



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

#diffObject



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

def diff; @y; end

#inspectObject



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

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

#realObject



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

def real; @x; end

#to_sObject



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

def to_s;    self.inspect;  end