Sha256: ce178df0986623a683537bcf5f093aa9ad2c97214bb02d2f8c0acd2419088772

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 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

      def include?(x)
        f == x || g == x
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dydx-0.0.4 lib/dydx/algebra/formula.rb