lib/dydx/algebra/operator/parts/formula.rb in dydx-0.0.5 vs lib/dydx/algebra/operator/parts/formula.rb in dydx-0.0.6

- old
+ new

@@ -11,70 +11,38 @@ elsif g.combinable?(x, operator) g.send(operator, x).send(operator, f) else super(x) end - elsif operator == :+ - if multiplication? && x.multiplication? - if f == x.f - f * g.send(operator, x.g) - elsif f == x.g - f * g.send(operator, x.f) - elsif g == x.f - f.send(operator, x.g) * g - elsif g == x.g - f.send(operator, x.f) * g - else - super(x) + elsif send("#{to_str(super_ope(operator))}?") && x.send("#{to_str(super_ope(operator))}?") + return super(x) if !common_factors(x) || (operator == :* && common_factors(x)[0] != common_factors(x)[1]) + w1, w2 = common_factors(x) + case operator + when :+ + send(w1).send(super_ope(operator), send(rest(w1)).send(operator, x.send(rest(w2)))) + when :* + if w1 == :f + send(w1).send(super_ope(operator), send(rest(w1)).send(sub_ope(operator), x.send(rest(w2)))) + elsif w1 == :g + send(w1).send(super_ope(operator), send(rest(w1)).send(operator, x.send(rest(w2)))).commutate! end - # expect(((:b * :a) - (:c * :a)).to_s).to eq('( ( b - c ) * a )') - elsif multiplication? && x.subtrahend? && x.x.multiplication? - if f == x.x.f - f * g.send(operator, inverse(x.x.g, :+)) - elsif f == x.x.g - f * g.send(operator, inverse(x.x.f, :+)) - elsif g == x.x.f - f.send(operator, inverse(x.x.g, :+)) * g - elsif g == x.x.g - f.send(operator, inverse(x.x.f, :+)) * g - else - super(x) - end - else - super(x) end - elsif operator == :* - if exponentiation? && x.exponentiation? - if f == x.f - f ^ g.send(:+, x.g) - elsif g == x.g - f.send(operator, x.f) ^ g - else - super(x) + elsif send("#{to_str(super_ope(operator))}?") && x.send("#{to_str_inv(operator)}?") && x.x.send("#{to_str(super_ope(operator))}?") + return super(x) if !common_factors(x.x) || (operator == :* && common_factors(x.x)[0] != common_factors(x.x)[1]) + w1, w2 = common_factors(x.x) + case operator + when :+ + send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(operator), x.x.send(rest(w2)))) + when :* + if w1 == :f + send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(sub_ope(operator)), x.x.send(rest(w2)))) + elsif w1 == :g + send(w1).send(super_ope(operator), send(rest(w1)).send(inverse_ope(operator), x.x.send(rest(w2)))).commutate! end - elsif exponentiation? && x.divisor? && x.x.exponentiation? - if f == x.x.f - f ^ g.send(:-, x.x.g) - elsif g == x.x.g - f.send(:/, x.x.f) ^ g - else - super(x) - end - # x * inverse(:y, :+) - elsif x.subtrahend? - inverse(self * x.x, :+) - elsif multiplication? - 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(x) - end - else - super(x) end + else + super(x) end end end def ^(x) @@ -89,9 +57,20 @@ { addition: :+, multiplication: :*, exponentiation: :^ }.key(operator) + end + + def to_str_inv(operator) + { + subtrahend: :+, + divisor: :* + }.key(operator) + end + + def rest(f_or_g) + ([:f, :g] - [f_or_g]).first end end end end end