Sha256: 7f210291d2de503fda626c79b32239b8fc0cbfe8d54fe4ee38e58561916e89f9
Contents?: true
Size: 1.79 KB
Versions: 2
Compression:
Stored size: 1.79 KB
Contents
module Dydx module Algebra class Formula include Helper attr_accessor :f, :g, :operator def initialize(f, g, operator) @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 :^ # TODO: if f == sym g * (f ^ (g - 1)) elsif f == e g.d(sym) * self else self * (g * log(f)).d(sym) end end end alias_method :d, :differentiate def to_s if (multiplication? && (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} )" else "( #{f.to_s} #{@operator} #{g.to_s} )" end end def include?(x) f == x || g == x end def openable?(operator, x) distributive?(self.operator, operator) && (f.combinable?(x, operator) || g.combinable?(x, operator)) end # TODO: interchangeable def ==(x) to_s == x.to_s end def common_factors(formula) nil unless formula.is_a?(Formula) if f == formula.f [:f, :f] elsif g == formula.g [:g, :g] elsif f == formula.g [:f, :g] elsif g == formula.f [:g, :f] end end def commutate! @f, @g = @g, @f self end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dydx-0.0.9 | lib/dydx/algebra/formula.rb |
dydx-0.0.8 | lib/dydx/algebra/formula.rb |