lib/dydx/algebra/operator/parts/formula.rb in dydx-0.1.4 vs lib/dydx/algebra/operator/parts/formula.rb in dydx-0.1.25

- old
+ new

@@ -1,85 +1,60 @@ module Dydx module Algebra module Operator module Parts module Formula - %w(+ *).map(&:to_sym).each do |op| - define_method(op) do |rtr| - if self.operator == op - if f.combinable?(rtr, op) - _( - _(trs[0], op, rtr), op, trs[1] - ) - elsif g.combinable?(rtr, op) - _( - _(trs[1], op, rtr), op, trs[0] - ) + %w(+ *).map(&:to_sym).each do |operator| + define_method(operator) do |x| + if self.operator == operator + if f.combinable?(x, operator) + f.send(operator, x).send(operator, g) + elsif g.combinable?(x, operator) + g.send(operator, x).send(operator, f) else - super(rtr) + super(x) end - elsif formula?(op.sub) && openable?(op, rtr) - _( - _(trs[0], op, rtr), op.sub, _(trs[1], op, rtr) - ) - elsif formula?(op.super) && rtr.formula?(op.super) - cmn_fct = (trs & rtr.trs).first - return super(rtr) unless cmn_fct + elsif formula?(super_ope(operator)) && x.formula?(super_ope(operator)) + w1, w2 = common_factors(x) + return super(x) unless (w1 && w2) && (super_ope(operator).commutative? || w1 == w2) - if op.super.commutative? - _( - cmn_fct, op.super, _(delete(cmn_fct), op, rtr.delete(cmn_fct)) - ) - else - return super(rtr) if index(cmn_fct) != rtr.index(cmn_fct) - - case index(cmn_fct) - when 0 - _( - cmn_fct, op.super, _(delete(cmn_fct), op.sub, rtr.delete(cmn_fct)) - ) - when 1 - _( - _(delete(cmn_fct), op, rtr.delete(cmn_fct)), op.super, cmn_fct - ) + case operator + when :+ + send(w1).send(super_ope(operator), send(rest(w1)).send(operator, x.send(rest(w2)))) + when :* + case w1 + when :f + send(w1).send(super_ope(operator), send(rest(w1)).send(sub_ope(operator), x.send(rest(w2)))) + when :g + send(w1).send(super_ope(operator), send(rest(w1)).send(operator, x.send(rest(w2)))).commutate! end end - elsif formula?(op.super) && rtr.inverse?(op) && rtr.x.formula?(op.super) - cmn_fct = (trs & rtr.x.trs).first - return super(rtr) unless cmn_fct + elsif formula?(super_ope(operator)) && x.inverse?(operator) && x.x.formula?(super_ope(operator)) + w1, w2 = common_factors(x.x) + return super(x) unless (w1 && w2) && (super_ope(operator).commutative? || w1 == w2) - if op.super.commutative? - _( - cmn_fct, op.super, _(delete(cmn_fct), op.inv, rtr.x.delete(cmn_fct)) - ) - else - return super(rtr) if index(cmn_fct) != rtr.x.index(cmn_fct) - case index(cmn_fct) - when 0 - _( - cmn_fct, op.super, _(delete(cmn_fct), op.sub.inv, rtr.x.delete(cmn_fct)) - ) - when 1 - _( - _(delete(cmn_fct), op.inv, rtr.x.delete(cmn_fct)), op.super, cmn_fct - ) + case operator + when :+ + send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(operator), x.x.send(rest(w2)))) + when :* + case w1 + when :f + send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(sub_ope(operator)), x.x.send(rest(w2)))) + when :g + send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(operator), x.x.send(rest(w2)))).commutate! end end else - super(rtr) + super(x) end end end - %w(**).map(&:to_sym).each do |op| - define_method(op) do |rtr| - if formula?(op.sub) && openable?(op, rtr) - _( - _(trs[0], op, rtr), op.sub, _(trs[1], op, rtr) - ) - else - super(rtr) - end + def ^(x) + if multiplication? && openable?(:^, x) + (f ^ x).send(self.operator, (g ^ x)) + else + super(x) end end end end end