lib/dydx.rb in dydx-0.1.4 vs lib/dydx.rb in dydx-0.1.25
- old
+ new
@@ -4,23 +4,36 @@
require 'dydx/function'
require 'dydx/integrand'
module Dydx
include Algebra
- %w(f g h temp_cal_f).each do |functioner|
+ # TODO: Refactor
+ %w(f g h).each do |functioner|
define_method(functioner) do |*vars|
- function = eval("$#{functioner}")
- return eval("$#{functioner} = Function.new(*vars)") unless function
-
- fail ArgumentError, "invalid number of values (#{vars.count} for #{function.vars.count})" unless function.vars.count == vars.count
- return function if function.vars == vars || !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)
+ if function = eval("$#{functioner}")
+ raise ArgumentError, "invalid number of values (#{vars.count} for #{function.vars.count})" unless function.vars.count == vars.count
+ return function if function.vars == vars
+ if function.algebra
+ if vars.all?{|v| v.is_a?(Numeric)}
+ string = function.algebra.to_s
+ .gsub('cos', 'Math.cos')
+ .gsub('sin', 'Math.sin')
+ .gsub('log', 'Math.log')
+ .gsub('e', 'Math::E')
+ .gsub('pi', 'Math::PI')
+ else
+ string = function.algebra.to_s
+ end
+ function.vars.each_with_index do |var, i|
+ string.gsub!(var.to_s, vars[i].to_s)
+ end
+ eval(string)
+ else
+ function
+ end
+ else
+ eval("$#{functioner} = Function.new(*vars)")
end
end
end
def S(function, delta)
@@ -31,20 +44,9 @@
Delta.new
end
def reset
$f, $g, $h = nil, nil, nil
- end
-
- def _(*args)
- case args.count
- when 1
- num = args.first
- Num.new(num)
- when 3
- ltr, op, rtr = args
- ltr.send(op, rtr)
- end
end
def method_missing(method, *args, &block)
method_name = method.to_s
if method_name =~ /^d.$/