Sha256: dd52082d619e89de2d3e65df6d62377616c63e605623a69d50378f124e28863d

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module Daru
  module Math
    module Arithmetic
      module Vector
        def + other
          case other
          when Daru::Vector
            v2v_binary :+, other
          else
            Daru::Vector.new self.map { |e| e + other }, name: @name, index: @index
          end
        end

        def - other
          case other
          when Daru::Vector
            v2v_binary :-, other
          else
            Daru::Vector.new self.map { |e| e - other }, name: @name, index: @index
          end
        end

        def * other
          case other
          when Daru::Vector
            v2v_binary :*, other
          else
            Daru::Vector.new self.map { |e| e * other }, name: @name, index: @index
          end
        end

        def / other
          case other
          when Daru::Vector
            v2v_binary :/, other
          else
            Daru::Vector.new self.map { |e| e / other }, name: @name, index: @index
          end
        end

        def % other
          case other
          when Daru::Vector
            v2v_binary :%, other
          else
            Daru::Vector.new self.map { |e| e % other }, name: @name, index: @index
          end
        end

       private

        def v2v_binary operation, other
          common_idxs = []
          elements    = []

          @index.each do |idx|
            this = self[idx]
            that = other[idx]

            if this and that
              elements << this.send(operation ,that)
              common_idxs << idx
            end
          end

          Daru::Vector.new(elements, name: @name, index: common_idxs)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
daru-0.0.3.1 lib/daru/math/arithmetic/vector.rb