module Autodiff

Constants

VERSION

Public Instance Methods

gradient(at, &fun) click to toggle source

TODO: add ability to raise when a not supported math func is called

# File lib/autodiff.rb, line 21
def gradient(at, &fun)
  if at_ary = Array.try_convert(at)
    dual_at_mat = real_ary_to_dual_gradient_mat(at_ary)
    gradient_ary = dual_at_mat.map do |row|
      fun.(*row)
    end
    gradient_ary.map(&:epsilon)
  else
    gradient([at], &fun)[0]
  end
end
real_ary_to_dual_gradient_mat(ary) click to toggle source
# File lib/autodiff.rb, line 10
def real_ary_to_dual_gradient_mat(ary)
  dual_ary = ary.map{ |v|  v.to_dual }
  dual_ary.size.times.map do |i|
    dual_ary.map.each_with_index do |v, j|
      i==j ? v.to_one_epsilon : v
    end
  end
end