module TimeSeriesMath::ElemwiseOperators

ElemwiseOperators

A collection of helper functions for by-element operations:

Public Instance Methods

elemwise_add(obj1, obj2) click to toggle source

Element-wise addition of objects

# File lib/time_series_math/elemwise_operators.rb, line 13
def elemwise_add(obj1, obj2)
  case obj1
  when Array
    obj1.clone.zip(obj2).map { |d| d[0] + d[1] }
  when Hash
    out = {}
    obj1.each { |k, v| out[k] = v + obj2[k] }
    out
  else
    obj1 + obj2
  end
end
elemwise_mul_scalar(scalar, obj) click to toggle source

Element-wise multiplication by scalar

# File lib/time_series_math/elemwise_operators.rb, line 43
def elemwise_mul_scalar(scalar, obj)
  case obj
  when Array
    obj.clone.map { |d| d * scalar }
  when Hash
    out = {}
    obj.each { |k, v| out[k] = v * scalar }
    out
  else
    obj * scalar
  end
end
elemwise_sub(obj1, obj2) click to toggle source

Element-wise substraction of objects

# File lib/time_series_math/elemwise_operators.rb, line 28
def elemwise_sub(obj1, obj2)
  case obj1
  when Array
    obj1.clone.zip(obj2).map { |d| d[0] - d[1] }
  when Hash
    out = {}
    obj1.each { |k, v| out[k] = v - obj2[k] }
    out
  else
    obj1 - obj2
  end
end