lib/dydx/algebra/operator/parts/base.rb in dydx-0.0.4 vs lib/dydx/algebra/operator/parts/base.rb in dydx-0.0.5
- old
+ new
@@ -1,26 +1,40 @@
module Dydx
module Algebra
module Operator
module Parts
module Base
- %w(+ - * / ^).each do |operator|
+ %w(+ * ^).map(&:to_sym).each do |operator|
define_method(operator) do |x|
- if self == x && operator != '^'
+ if self == x && operator != :^
case operator
- when '+'
+ when :+
_(2) * self
- when '-'
- _(0)
- when '*'
+ when :*
self ^ _(2)
- when '/'
- _(1)
end
+ elsif %w(+ *).map(&:to_sym).include?(operator) && x.send("#{to_str(operator)}?")
+ if combinable?(x.f, operator)
+ send(operator, x.f).send(operator, x.g)
+ elsif combinable?(x.g, operator)
+ send(operator, x.g).send(operator, x.f)
+ else
+ ::Algebra::Formula.new(self, x, operator.to_sym)
+ end
+ elsif x.subtrahend? && %w(* ^).map(&:to_sym).include?(operator)
+ inverse(::Algebra::Formula.new(self, x.x, operator.to_sym), :+)
else
::Algebra::Formula.new(self, x, operator.to_sym)
end
end
+ end
+
+ def to_str(operator)
+ {
+ addition: :+,
+ multiplication: :*,
+ exponentiation: :^
+ }.key(operator)
end
end
end
end
end