Sha256: fbb70d48735bc2967133ab505d6086c6e8d8c330767e9c2c2623dcaefb846014

Contents?: true

Size: 870 Bytes

Versions: 5

Compression:

Stored size: 870 Bytes

Contents

module Dydx
  module Algebra
    class Inverse
      include Helper
      attr_accessor :x, :operator

      def initialize(x, operator)
        @x, @operator = x, operator
      end

      def to_s
        case operator
        when :+ then "( - #{x} )"
        when :* then "( 1 / #{x} )"
        end
      end

      def subst(hash = {})
        case operator
        when :+ then x.subst(hash) * -1
        when :* then x.subst(hash) ** -1
        end
      end

      def to_f
        case operator
        when :+ then x.to_f * -1
        when :* then x.to_f ** -1
        end
      end

      def differentiate(sym = :x)
        case operator
        when :+
          inverse(x.differentiate(sym), :+)
        when :*
          inverse(x.differentiate(sym) * inverse(x ** 2, :*), :+)
        end
      end
      alias_method :d, :differentiate
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dydx-0.2.7000001 lib/dydx/algebra/inverse.rb
dydx-0.2.7000000 lib/dydx/algebra/inverse.rb
dydx-0.1.41421 lib/dydx/algebra/inverse.rb
dydx-0.1.412 lib/dydx/algebra/inverse.rb
dydx-0.1.4 lib/dydx/algebra/inverse.rb