lib/dydx/algebra/operator/parts/num.rb in dydx-0.0.7 vs lib/dydx/algebra/operator/parts/num.rb in dydx-0.0.8
- old
+ new
@@ -1,42 +1,32 @@
module Dydx
module Algebra
module Operator
module Parts
module Num
- def +(x)
- if n == 0
- x
- elsif x.is_a?(Num)
- _(n + x.n)
- elsif x.subtrahend? && x.x.is_a?(Num)
- _(n - x.x.n)
- else
- super(x)
- end
- end
-
- def *(x)
- if n == 0
- self
- elsif n == 1
- x
- elsif x.is_a?(Num)
- _(n * x.n)
- elsif x.divisor? && x.x.is_a?(Num)
- _(n / x.x.n)
- else
- super(x)
- end
- end
-
- def ^(x)
- if (n == 0) || (n == 1)
- self
- elsif x.is_a?(Num)
- _(n ^ x.n)
- else
- super(x)
+ %w(+ * ^).map(&:to_sym).each do |operator|
+ define_method(operator) do |x|
+ if is_0?
+ case operator
+ when :+ then x
+ when :* then e0
+ when :^ then e0
+ end
+ elsif is_1?
+ case operator
+ when :+ then super(x)
+ when :* then x
+ when :^ then e1
+ end
+ elsif x.is_a?(Num)
+ _(n.send(operator, x.n))
+ elsif operator == :+ && x.inverse?(:+) && x.x.is_a?(Num)
+ _(n - x.x.n)
+ elsif operator == :* && x.inverse?(:*) && x.x.is_a?(Num)
+ _(n / x.x.n)
+ else
+ super(x)
+ end
end
end
end
end
end