Module: DualNumWrappable

Included in:
Math
Defined in:
lib/autodiff/math.rb

Instance Method Summary collapse

Instance Method Details

#dual_method(meth) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/autodiff/math.rb', line 4

def dual_method(meth)
  orig_meth = "predual_#{meth.to_s}".to_sym
  dual_meth = "dual_#{meth.to_s}".to_sym
  alias_method orig_meth, meth
  define_method(meth) { |*args|
    if args.any?{ |arg| arg.kind_of?(Autodiff::DualNum)}
      dual_args = args.map(&:to_dual)
      epsilon_coef = self.public_send(dual_meth, *dual_args)
      real_args = dual_args.map(&:to_float)
      r = self.public_send(orig_meth, *real_args)
      Autodiff::DualNum.new(r, epsilon_coef)
    else
      self.public_send(orig_meth, *args)
    end
  }
end