Sha256: 27473c7444ca5dad9f081b30e0b8025befdb86b8aea75a6ad0069975ed25730e

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module Daru
  module Maths
    module Arithmetic
      module Vector
        def + other
          binary_op :+, other
        end

        def - other
          binary_op :-, other
        end

        def * other
          binary_op :*, other
        end

        def / other
          binary_op :/, other
        end

        def % other
          binary_op :%, other
        end

        def ** other
          binary_op :**, other
        end

        def exp
          math_unary_op :exp
        end

        def sqrt
          math_unary_op :sqrt
        end

        def abs
          self.dup.map! { |e| e.abs unless e.nil? }
        end

        def round precision=0
          self.dup.map! { |e| e.round(precision) unless e.nil? }
        end

       private

        def math_unary_op operation
          self.dup.map! { |e| Math.send(operation, e) unless e.nil? }
        end

        def binary_op operation, other
          case other
          when Daru::Vector
            v2v_binary operation, other
          else
            v2o_binary operation, other
          end
        end

        def v2o_binary operation, other
          Daru::Vector.new self.map { |e| e.nil? ? nil : e.send(operation, other) },
           name: @name, index: @index
        end

        def v2v_binary operation, other
          common_idxs = []
          elements    = []
          index = (@index.to_a + other.index.to_a).uniq.sort

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

            if this and that
              elements << this.send(operation ,that)
              common_idxs << idx
            else
              elements << nil
              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.5 lib/daru/maths/arithmetic/vector.rb