Sha256: 2b3fb80bbbe0b57023391ded3cf34b10ae2307726220bf87702c7cd24b0d5465

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

require 'dydx/algebra/formula'
require 'dydx/algebra/inverse'

require 'dydx/algebra/set'

require 'dydx/algebra/operator/inverse'
require 'dydx/algebra/operator/formula'
require 'dydx/algebra/operator/symbol'
require 'dydx/algebra/operator/num'
require 'dydx/algebra/operator/general'

module Dydx
  module Algebra
    include Set
    module Set
      Symbol.class_eval{ include Operator::Symbol }
      Fixnum.class_eval do
        %w(+ - * / ^).each do |operator|
          define_method(operator) do |g|
            if g.is_a?(Symbol) ||
              g.is_a?(Formula) ||
              g.is_a?(Base)

              Num.new(self).send(operator.to_sym, g)
            elsif operator == '^' && g.is_a?(Fixnum)
              result = 1
              g.times{ result *= self }
              result
            else
              (to_f.send(operator.to_sym, g)).to_i
            end
          end
        end
      end
      class Num;    include Operator::Num; end
      class E;      include Operator::General; end
      class Pi;     include Operator::General; end
      class Log;    include Operator::General; end
      class Sin;    include Operator::General; end
      class Cos;    include Operator::General; end
      class Tan;    include Operator::General; end
    end
    class Formula;  include Operator::Formula; end
    class Inverse;  include Operator::Inverse; end

    def inverse(x, operator)
      if operator == :+ && x.is_0?
        e0
      elsif operator == :* && x.is_1?
        e1
      elsif x.is_a?(Inverse) && x.operator == operator
        x.x
      else
        Inverse.new(x, operator)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dydx-0.0.9 lib/dydx/algebra.rb
dydx-0.0.8 lib/dydx/algebra.rb
dydx-0.0.7 lib/dydx/algebra.rb
dydx-0.0.6 lib/dydx/algebra.rb