Sha256: bc45f31cd91c3ab8afcced7618ea9bead9366985658516e1dbe1164372252170

Contents?: true

Size: 937 Bytes

Versions: 3

Compression:

Stored size: 937 Bytes

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.d(sym)
        when :*
          (f.d(sym) * g) + (f * g.d(sym))
        when :/
          ((f.d(sym) * g) - (f * g.d(sym)))/(g ^ _(2))
        when :^
          # TODO:
          if f == sym
            g * (f ^ (g - 1))
          else
            self * (g * log(f)).d(sym)
          end
        end
      end
      alias_method :d, :differentiate

      def to_s
        if (subtraction? && f.is_0?) ||
          (multiplication? && (f.is_minus1? || g.is_minus1?)  )
          "( - #{g.to_s} )"
        else
          "( #{f.to_s} #{@operator} #{g.to_s} )"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dydx-0.0.3 lib/dydx/algebra/formula.rb
dydx-0.0.2 lib/dydx/algebra/formula.rb
dydx-0.0.1 lib/dydx/algebra/formula.rb