lib/dydx/helper.rb in dydx-0.1.314 vs lib/dydx/helper.rb in dydx-0.1.412
- old
+ new
@@ -1,114 +1,88 @@
module Dydx
module Helper
- OP_SYM_STR = {
- addition: :+,
- multiplication: :*,
- exponentiation: :^
- }
-
SUPER_OPE_RELATION = {
:+ => :*,
:- => :/,
- :* => :^,
+ :* => :**,
:/ => :|
}
INVERSE_OPE_RELATION = {
:+ => :-,
:- => :+,
:* => :/,
:/ => :*,
- :^ => :|,
- :| => :^
+ :** => :|,
+ :| => :**
}
- def inverse_ope(operator)
- INVERSE_OPE_RELATION[operator]
+ def num?
+ is_a?(Num) || is_a?(Numeric)
end
- def inverse_super_ope(operator)
- inverse_ope(operator.super)
+ def to_numeric
+ is_a?(Num) ? n : self
end
- def is_num?
- (is_a?(Num) || is_a?(Fixnum) || is_a?(Float)) || (is_a?(Inverse) && x.is_num?)
+ def zero?
+ [0, 0.0].include?(self) || (is_a?(Num) && n.zero?)
end
- def is_0?
- [0, 0.0].include?(self) || (is_a?(Num) && n.is_0?)
+ def one?
+ [1, 1.0].include?(self) || (is_a?(Num) && n.one?)
end
- def is_1?
- [1, 1.0].include?(self) || (is_a?(Num) && n.is_1?)
+ def minus1?
+ [-1, -1.0].include?(self) || (is_a?(Num) && n.minus1?)
end
- def is_minus1?
- [1, -1.0].include?(self)|| (is_a?(Num) && n.is_minus1?)
- end
-
def distributive?(ope1, ope2)
- [ope1.super, ope1.inverse_super].include?(ope2)
+ [ope1.super, ope1.inv_super].include?(ope2)
end
+ # TODO: Cyclomatic complexity for combinable? is too high. [17/6]
def combinable?(x, operator)
case operator
when :+
- (is_num? && x.is_num?) ||
- (formula?(:*) && (f.is_num? || g.is_num?)) && x.is_num? ||
+ (num? && x.num?) ||
+ (formula?(:*) && (f.num? || g.num?)) && x.num? ||
like_term?(x) ||
inverse?(:+, x)
when :*
self == x ||
- (is_num? && x.is_num?) ||
+ (num? && x.num?) ||
inverse?(:*, x)
- when :^
- (is_num? && x.is_num?) || is_0? || is_1?
+ when :**
+ (num? && x.num?) || zero? || one?
end
end
+ # TODO: Cyclomatic complexity for combinable? is too high. [9/6]
def like_term?(x)
- boolean = if self == x
- elsif formula?(:*) && include?(x)
- elsif x.formula?(:*) && x.include?(self)
- elsif ((formula?(:*) && formula?(:*)) && (([f, g] & [x.f, x.g]).any?{|x| x.is_a?(Symbol)}))
- else
- true
- end
-
- !boolean
+ self == x ||
+ formula?(:*) && include?(x) ||
+ x.formula?(:*) && x.include?(self)||
+ (formula?(:*) && formula?(:*) && !([f, g] & [x.f, x.g]).empty?)
end
- def is_multiple_of(x)
- if is_0?
- e0
- elsif self == x
- e1
- # elsif is_num? && x.is_num? && (self % x == 0)
- # _(n / x.n)
- elsif multiplication? && (f == x || g == x)
- f == x ? g : f
- else
- false
- end
+ # TODO: Cyclomatic complexity for combinable? is too high. [7/6]
+ def multiple_of?(x)
+ zero? ||
+ self == x ||
+ (num? && x.num? && self % x == 0) ||
+ (formula?(:*) && (f == x || g == x))
end
- OP_SYM_STR.each do |operator_name, operator|
- define_method("#{operator_name}?") do
- is_a?(Formula) && (@operator == operator)
- # is_a?(Inverse) && self.operator == operator
- end
- end
-
def rest(f_or_g)
([:f, :g] - [f_or_g]).first
end
- def distributable?(operator)
+ def distributable?(_operator)
end
- def inverse?(operator, x=nil)
+ def inverse?(operator, x = nil)
if is_a?(Algebra::Inverse)
self.operator == operator && (self.x == x || x.nil?)
elsif x.is_a?(Algebra::Inverse)
self == x.x
else
@@ -123,28 +97,31 @@
Symbol.class_eval do
def commutative?
[:+, :*].include?(self)
end
+ def associative?
+ [:+, :*].include?(self)
+ end
+
def super
SUPER_OPE_RELATION[self] || self
end
def sub
SUPER_OPE_RELATION.invert[self] || self
end
- def inverse_super
- inverse_super_ope(self) || self
+ def inv
+ INVERSE_OPE_RELATION[self] || self
end
+
+ def inv_super
+ self.super.inv
+ end
end
def ==(x)
to_s == x.to_s
- end
-
- # Refactor
- def **(x)
- self ^ (x)
end
end
end