Module: TimeSeriesMath::ElemwiseOperators
- Included in:
- LinearInterpolation
- Defined in:
- lib/time_series_math/elemwise_operators.rb
Overview
ElemwiseOperators
A collection of helper functions for by-element operations:
-
addition
-
substraction
-
multiplication by scalar
Instance Method Summary collapse
-
#elemwise_add(obj1, obj2) ⇒ Object
Element-wise addition of objects.
-
#elemwise_mul_scalar(scalar, obj) ⇒ Object
Element-wise multiplication by scalar.
-
#elemwise_sub(obj1, obj2) ⇒ Object
Element-wise substraction of objects.
Instance Method Details
#elemwise_add(obj1, obj2) ⇒ Object
Element-wise addition of objects
13 14 15 16 17 18 19 20 21 22 23 24 |
# 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) ⇒ Object
Element-wise multiplication by scalar
43 44 45 46 47 48 49 50 51 52 53 54 |
# 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) ⇒ Object
Element-wise substraction of objects
28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |