lib/dydx/algebra/formula.rb in dydx-0.1.2 vs lib/dydx/algebra/formula.rb in dydx-0.1.3
- old
+ new
@@ -3,19 +3,18 @@
class Formula
include Helper
attr_accessor :f, :operator, :g
def initialize(f, g, operator)
+ g, f = f, g if g.is_num? && operator.commutative?
@f, @g, @operator = f, g, operator
end
def differentiate(sym=:x)
case @operator
- when :+
- f.d(sym) + g.d(sym)
- when :*
- (f.d(sym) * g) + (f * g.d(sym))
+ when :+ then f.d(sym) + g.d(sym)
+ when :* then (f.d(sym) * g) + (f * g.d(sym))
when :^
# TODO:
if g.is_num?
f.d(sym) * g * (f ^ (g - 1) )
elsif f == sym
@@ -28,21 +27,17 @@
end
end
alias_method :d, :differentiate
def to_s
- if (multiplication? && (f.is_minus1? || g.is_minus1?) )
+ if (formula?(:*) && (f.is_minus1? || g.is_minus1?) )
"( - #{g.to_s} )"
- elsif multiplication? && g.inverse?(:*)
- "( #{f.to_s} / #{g.x.to_s} )"
- elsif multiplication? && f.inverse?(:*)
- "( #{g.to_s} / #{f.x.to_s} )"
- elsif addition? && g.inverse?(:+)
- "( #{f.to_s} - #{g.x.to_s} )"
- elsif addition? && f.inverse?(:+)
- "( #{g.to_s} - #{f.x.to_s} )"
+ elsif g.inverse?(operator)
+ "( #{f.to_s} #{inverse_ope(operator)} #{g.x.to_s} )"
+ elsif f.inverse?(operator)
+ "( #{g.to_s} #{inverse_ope(operator)} #{f.x.to_s} )"
else
- "( #{f.to_s} #{@operator} #{g.to_s} )"
+ "( #{f.to_s} #{operator} #{g.to_s} )"
end
end
def include?(x)
f == x || g == x