class Dydx::Algebra::Formula
Attributes
operator[RW]
terms[RW]
Public Class Methods
new(operator, *terms)
click to toggle source
# File lib/dydx/algebra/formula.rb, line 7 def initialize(operator, *terms) @operator, @terms = operator, terms commutate! if (terms[1].num? && operator.commutative?) end
Public Instance Methods
==(x)
click to toggle source
# File lib/dydx/algebra/formula.rb, line 96 def ==(x) if to_s == x.to_s true else result = commutate!.to_s == x.to_s commutate! result end end
commutate!()
click to toggle source
# File lib/dydx/algebra/formula.rb, line 106 def commutate! @terms.reverse! self end
delete(tm)
click to toggle source
# File lib/dydx/algebra/formula.rb, line 115 def delete(tm) tms.delete(tm) tms.count.one? ? tms.first : self end
differentiate(sym = :x)
click to toggle source
TODO: Cylomatic complexity for differentiate is too high. [7/6]
# File lib/dydx/algebra/formula.rb, line 33 def differentiate(sym = :x) case @operator when :+ then f.d(sym) + g.d(sym) when :* then (f.d(sym) * g) + (f * g.d(sym)) when :** # TODO: if g.num? f.d(sym) * g * (f ** (g - 1)) elsif (f == sym) && ( g.num? || ( g.is_a?(Symbol) && g != sym ) ) g * f ** (g - 1) elsif f == e g.d(sym) * self else self * (g * log(f)).d(sym) end end end
Also aliased as: d
f()
click to toggle source
# File lib/dydx/algebra/formula.rb, line 12 def f @terms[0] end
f=(x)
click to toggle source
# File lib/dydx/algebra/formula.rb, line 20 def f=(x) @terms[0] = x end
g()
click to toggle source
# File lib/dydx/algebra/formula.rb, line 16 def g @terms[1] end
g=(x)
click to toggle source
# File lib/dydx/algebra/formula.rb, line 24 def g=(x) @terms[1] = x end
include?(x)
click to toggle source
# File lib/dydx/algebra/formula.rb, line 83 def include?(x) f == x || g == x end
index(tm)
click to toggle source
# File lib/dydx/algebra/formula.rb, line 111 def index(tm) tms.index(tm) end
openable?(operator, x)
click to toggle source
# File lib/dydx/algebra/formula.rb, line 87 def openable?(operator, x) distributive?(self.operator, operator) && (f.combinable?(x, operator) || g.combinable?(x, operator)) end
rationals()
click to toggle source
# File lib/dydx/algebra/formula.rb, line 92 def rationals [f, g].select{ |term| term.num? && term.n.is_a?(Rational) } end
subst(hash = {})
click to toggle source
# File lib/dydx/algebra/formula.rb, line 75 def subst(hash = {}) f.subst(hash).send(operator, g.subst(hash)) end
tms()
click to toggle source
# File lib/dydx/algebra/formula.rb, line 28 def tms terms end
to_f()
click to toggle source
# File lib/dydx/algebra/formula.rb, line 79 def to_f f.to_f.send(operator, g.to_f) end
to_s()
click to toggle source
# File lib/dydx/algebra/formula.rb, line 57 def to_s str = if formula?(:*) && (f.minus1? || g.minus1?) "( - #{g} )" elsif g.inverse?(operator) "( #{f} #{operator.inv} #{g.x} )" elsif f.inverse?(operator) "( #{g} #{operator.inv} #{f.x} )" elsif f.negative? "( #{g} - #{f.n.abs} )" elsif formula?(:*) && !rationals.empty? terms = [f, g] terms.delete(rationals.first) "( #{(terms.first * rationals.first.n.numerator)} / #{rationals.first.n.denominator} )" else "( #{f} #{operator} #{g} )" end end