Sha256: b03414862a14043f96c96922b68864c32dae0211b79fcbb41f2b5c7bdd18c4a4

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Dydx
  module Algebra
    module Operator
      module Parts
        module Base
          %w(+ * ^).map(&:to_sym).each do |operator|
            define_method(operator) do |x|
              if self == x && operator != :^
                case operator
                when :+
                  _(2) * self
                when :*
                  self ^ _(2)
                end
              elsif %w(+ *).map(&:to_sym).include?(operator) && x.send("#{to_str(operator)}?")
                if combinable?(x.f, operator)
                  send(operator, x.f).send(operator, x.g)
                elsif combinable?(x.g, operator)
                  send(operator, x.g).send(operator, x.f)
                else
                  ::Algebra::Formula.new(self, x, operator.to_sym)
                end
              elsif x.subtrahend? && %w(* ^).map(&:to_sym).include?(operator)
                inverse(::Algebra::Formula.new(self, x.x, operator.to_sym), :+)
              else
                ::Algebra::Formula.new(self, x, operator.to_sym)
              end
            end
          end

          def to_str(operator)
            {
              addition:       :+,
              multiplication: :*,
              exponentiation: :^
            }.key(operator)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dydx-0.0.5 lib/dydx/algebra/operator/parts/base.rb