lib/dydx/algebra/operator/parts/formula.rb in dydx-0.1.314 vs lib/dydx/algebra/operator/parts/formula.rb in dydx-0.1.412

- old
+ new

@@ -1,63 +1,84 @@ module Dydx module Algebra module Operator module Parts module Formula - %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) + %w(+ *).map(&:to_sym).each do |op| + define_method(op) do |rtm| + if operator == op + if tms[0].combinable?(rtm, op) + _( + _(tms[0], op, rtm), op, tms[1] + ) + elsif tms[1].combinable?(rtm, op) + _( + _(tms[1], op, rtm), op, tms[0] + ) else - super(x) + super(rtm) end - elsif formula?(operator.sub) && openable?(operator, x) - f.send(operator, x).send(operator.sub, g.send(operator, x)) - elsif formula?(operator.super) && x.formula?(operator.super) - w1, w2 = common_factors(x) - return super(x) unless (w1 && w2) && (operator.super.commutative? || w1 == w2) + elsif formula?(op.sub) && openable?(op, rtm) + _( + _(tms[0], op, rtm), op.sub, _(tms[1], op, rtm) + ) + elsif formula?(op.super) && rtm.formula?(op.super) + cmn_fct = (tms & rtm.tms).first + return super(rtm) unless cmn_fct - case operator - when :+ - send(w1).send(operator.super, send(rest(w1)).send(operator, x.send(rest(w2)))) - when :* - case w1 - when :f - send(w1).send(operator.super, send(rest(w1)).send(operator.sub, x.send(rest(w2)))) - when :g - send(w1).send(operator.super, send(rest(w1)).send(operator, x.send(rest(w2)))).commutate! + if op.super.commutative? + _( + cmn_fct, op.super, _(delete(cmn_fct), op, rtm.delete(cmn_fct)) + ) + else + return super(rtm) if index(cmn_fct) != rtm.index(cmn_fct) + + case index(cmn_fct) + when 0 + _( + cmn_fct, op.super, _(delete(cmn_fct), op.sub, rtm.delete(cmn_fct)) + ) + when 1 + _( + _(delete(cmn_fct), op, rtm.delete(cmn_fct)), op.super, cmn_fct + ) end end - elsif formula?(operator.super) && x.inverse?(operator) && x.x.formula?(operator.super) - w1, w2 = common_factors(x.x) - return super(x) unless (w1 && w2) && (operator.super.commutative? || w1 == w2) + elsif formula?(op.super) && rtm.inverse?(op) && rtm.x.formula?(op.super) + cmn_fct = (tms & rtm.x.tms).first + return super(rtm) unless cmn_fct - case operator - when :+ - send(w1).send(operator.super, send(rest(w1)).send(inverse_ope(operator), x.x.send(rest(w2)))) - when :* - case w1 - when :f - send(w1).send(operator.super, send(rest(w1)).send(inverse_ope(operator.sub), x.x.send(rest(w2)))) - when :g - send(w1).send(operator.super, send(rest(w1)).send(inverse_ope(operator), x.x.send(rest(w2)))).commutate! + if op.super.commutative? + _( + cmn_fct, op.super, _(delete(cmn_fct), op.inv, rtm.x.delete(cmn_fct)) + ) + else + return super(rtm) if index(cmn_fct) != rtm.x.index(cmn_fct) + case index(cmn_fct) + when 0 + _( + cmn_fct, op.super, _(delete(cmn_fct), op.sub.inv, rtm.x.delete(cmn_fct)) + ) + when 1 + _( + _(delete(cmn_fct), op.inv, rtm.x.delete(cmn_fct)), op.super, cmn_fct + ) end end else - super(x) + super(rtm) end end end - %w(^).map(&:to_sym).each do |operator| - define_method(operator) do |x| - if formula?(operator.sub) && openable?(operator, x) - f.send(operator, x).send(operator.sub, g.send(operator, x)) + %w(**).map(&:to_sym).each do |op| + define_method(op) do |rtm| + if formula?(op.sub) && openable?(op, rtm) + _( + _(tms[0], op, rtm), op.sub, _(tms[1], op, rtm) + ) else - super(x) + super(rtm) end end end end end