Sha256: 055a34cb7dc0d2c1777c2b4ce76f48f23a6e65bc5eda0f68158f64fe6e8cef7f

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require 'dydx/helper'
require 'dydx/algebra'
require 'dydx/delta'
require 'dydx/function'
require 'dydx/integrand'

module Dydx
  include Algebra
  %w(f g h int_f).each do |functioner|
    define_method(functioner) do |*vars|
      function = eval("$#{functioner}")
      return eval("$#{functioner} = Function.new(*vars)") unless function

      raise ArgumentError, "invalid number of values (#{vars.count} for #{function.vars.count})" unless function.vars.count == vars.count
      return function if function.vars == vars
      return function unless function.algebra

      subst_hash = Hash[*[function.vars, vars].transpose.flatten]
      begin
        function.algebra.subst(subst_hash).to_f
      rescue ArgumentError
        eval(function.algebra.subst(subst_hash).to_s)
      end
    end
  end

  def S(function, delta)
    Integrand.new(function, delta.var)
  end

  def d
    Delta.new
  end

  def reset
    $f, $g, $h = nil, nil, nil
  end

  def method_missing(method, *args, &block)
    method_name = method.to_s
    if method_name =~ /^d.$/
      Delta.new(method_name[1].to_sym, args.first)
    elsif method_name =~ /^[a-z]$/
      method_name.to_sym
    else
      super
    end
  end

  private

  def substitute(vars, function)
    string = function.algebra.to_s
    function.vars.each_with_index { |var, i| string.gsub!(var.to_s, vars[i].to_s) }
    string
  end

  def all_vars_num?(vars)
    vars.all? { |v| v.is_a?(Numeric) }
  end

  def rename_for_calc(string)
    # TODO: need more refactoring...
    string.gsub!('cos', 'Math.cos')
    string.gsub!('sin', 'Math.sin')
    string.gsub!('log', 'Math.log')
    string.gsub!(' e ', ' Math::E ')
    string.gsub!('pi', 'Math::PI')
    string
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dydx-0.1.314 lib/dydx.rb