Class: Daru::Accessors::NMatrixWrapper
- Inherits:
-
Object
- Object
- Daru::Accessors::NMatrixWrapper
- Includes:
- Enumerable
- Defined in:
- lib/daru/accessors/nmatrix_wrapper.rb
Overview
Internal class for wrapping NMatrix
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#nm_dtype ⇒ Object
readonly
Returns the value of attribute nm_dtype.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #<<(element) ⇒ Object
- #==(other) ⇒ Object
- #[](*index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #delete_at(index) ⇒ Object
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #index(key) ⇒ Object
-
#initialize(vector, context, nm_dtype = :int32) ⇒ NMatrixWrapper
constructor
A new instance of NMatrixWrapper.
- #inject(*args, &block) ⇒ Object
- #map!(&block) ⇒ Object
- #max ⇒ Object
- #mean ⇒ Object
- #min ⇒ Object
- #product ⇒ Object
- #resize(size = @size*2) ⇒ Object
- #sum ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(vector, context, nm_dtype = :int32) ⇒ NMatrixWrapper
Returns a new instance of NMatrixWrapper.
23 24 25 26 27 28 29 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 23 def initialize vector, context, nm_dtype=:int32 @size = vector.size @data = NMatrix.new [@size*2], vector.to_a, dtype: nm_dtype @context = context @nm_dtype = @data.dtype # init with twice the storage for reducing the need to resize end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 21 def data @data end |
#nm_dtype ⇒ Object (readonly)
Returns the value of attribute nm_dtype.
21 22 23 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 21 def nm_dtype @nm_dtype end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
21 22 23 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 21 def size @size end |
Instance Method Details
#<<(element) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 59 def << element resize if @size >= @data.size self[@size] = element @size += 1 end |
#==(other) ⇒ Object
44 45 46 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 44 def == other @data == other and @size == other.size end |
#[](*index) ⇒ Object
31 32 33 34 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 31 def [] *index return @data[*index] if index[0] < @size nil end |
#[]=(index, value) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 36 def []= index, value resize if index >= @data.size @size += 1 if index == @size @data = @data.cast(dtype: :object) if value.nil? @data[index] = value end |
#delete_at(index) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 48 def delete_at index arry = @data.to_a arry.delete_at index @data = NMatrix.new [(2*@size-1)], arry, dtype: @nm_dtype @size -= 1 end |
#dup ⇒ Object
70 71 72 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 70 def dup NMatrixWrapper.new @data[0...@size].to_a, @context, @nm_dtype end |
#each(&block) ⇒ Object
7 8 9 10 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 7 def each(&block) @data[0...@size].each(&block) self end |
#index(key) ⇒ Object
55 56 57 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 55 def index key @data.to_a.index key end |
#inject(*args, &block) ⇒ Object
17 18 19 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 17 def inject(*args, &block) @data[0...@size].inject(*args, &block) end |
#map!(&block) ⇒ Object
12 13 14 15 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 12 def map!(&block) @data = NMatrix.new [@size*2], map(&block).to_a, dtype: nm_dtype self end |
#max ⇒ Object
92 93 94 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 92 def max @data.max end |
#mean ⇒ Object
80 81 82 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 80 def mean @data[0...@size].mean.first end |
#min ⇒ Object
96 97 98 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 96 def min @data.min end |
#product ⇒ Object
84 85 86 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 84 def product @data[0...@size].inject(1) { |m,e| m*e } end |
#resize(size = @size*2) ⇒ Object
74 75 76 77 78 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 74 def resize size = @size*2 raise ArgumentError, "Size must be greater than current size" if size < @size @data = NMatrix.new [size], @data.to_a end |
#sum ⇒ Object
88 89 90 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 88 def sum @data[0...@size].inject(:+) end |
#to_a ⇒ Object
66 67 68 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 66 def to_a @data[0...@size].to_a end |