Module: TensorStream::OpPatch

Included in:
Array, Float, Integer
Defined in:
lib/tensor_stream/monkey_patches/op_patch.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/tensor_stream/monkey_patches/op_patch.rb', line 3

def self.included(klass)
  ops = if klass == Array
    {:+ => "add", :- => "sub", :* => "mul"}
  else
    {:+ => "add", :- => "sub", :/ => "div", :% => "mod", :* => "mul", :** => "pow"}
  end

  ops.each do |m, name|
    klass.send(:alias_method, :"_tensor_stream_#{name}_orig", m)
    klass.send(:remove_method, m)
  end
end

Instance Method Details

#%(other) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/tensor_stream/monkey_patches/op_patch.rb', line 48

def %(other)
  if other.is_a?(TensorStream::Tensor)
    TensorStream.convert_to_tensor(self, dtype: other.data_type) % other
  else
    _tensor_stream_mod_orig(other)
  end
end

#*(other) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/tensor_stream/monkey_patches/op_patch.rb', line 32

def *(other)
  if other.is_a?(TensorStream::Tensor)
    TensorStream.convert_to_tensor(self, dtype: other.data_type) * other
  else
    _tensor_stream_mul_orig(other)
  end
end

#**(other) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/tensor_stream/monkey_patches/op_patch.rb', line 56

def **(other)
  if other.is_a?(TensorStream::Tensor)
    TensorStream.convert_to_tensor(self, dtype: other.data_type)**other
  else
    _tensor_stream_pow_orig(other)
  end
end

#+(other) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/tensor_stream/monkey_patches/op_patch.rb', line 16

def +(other)
  if other.is_a?(TensorStream::Tensor)
    TensorStream.convert_to_tensor(self, dtype: other.data_type) + other
  else
    _tensor_stream_add_orig(other)
  end
end

#-(other) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/tensor_stream/monkey_patches/op_patch.rb', line 24

def -(other)
  if other.is_a?(TensorStream::Tensor)
    TensorStream.convert_to_tensor(self, dtype: other.data_type) - other
  else
    _tensor_stream_sub_orig(other)
  end
end

#/(other) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/tensor_stream/monkey_patches/op_patch.rb', line 40

def /(other)
  if other.is_a?(TensorStream::Tensor)
    TensorStream.convert_to_tensor(self, dtype: other.data_type) * other
  else
    _tensor_stream_div_orig(other)
  end
end