Sha256: 97ff171c55339f2631d7c89fe1a80dba8e75383edd9363f9b4db8e45dc9f5f65

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 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
          recode { |e| e.abs unless e.nil? }
        end

        def round precision=0
          recode { |e| e.round(precision) unless e.nil? }
        end

        private

        def math_unary_op operation
          recode { |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 map { |e| e.nil? ? nil : e.send(operation, other) },
            name: @name, index: @index
        end

        def v2v_binary operation, other
          # FIXME: why the sorting?.. - zverok, 2016-05-18
          index = (@index.to_a | other.index.to_a).sort

          elements = index.map do |idx|
            this = self.index.include?(idx) ? self[idx] : nil
            that = other.index.include?(idx) ? other[idx] : nil

            this && that ? this.send(operation, that) : nil
          end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
daru-0.1.6 lib/daru/maths/arithmetic/vector.rb
daru-0.1.5 lib/daru/maths/arithmetic/vector.rb
daru-0.1.4.1 lib/daru/maths/arithmetic/vector.rb
daru-0.1.4 lib/daru/maths/arithmetic/vector.rb