Sha256: 27b0f42ac78f7fd07c82a27ba3de5a87e582674cde9a1dea129dbe3509596665

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

require 'pry-byebug'
module TensorStream
  # various monkey patches to FixNum types
  module MonkeyPatch
    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

    def shape
      TensorStream.shape_eval(self)
    end

    def t(name = nil)
      TensorStream.convert_to_tensor(self, name: name)
    end

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

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

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

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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tensor_stream-0.9.8 lib/tensor_stream/monkey_patches/patch.rb
tensor_stream-0.9.7 lib/tensor_stream/monkey_patches/patch.rb
tensor_stream-0.9.6 lib/tensor_stream/monkey_patches/patch.rb