Class: Daru::Accessors::NMatrixWrapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daru/accessors/nmatrix_wrapper.rb

Overview

Internal class for wrapping NMatrix

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vector, context, nm_dtype = :int32) ⇒ NMatrixWrapper

Returns a new instance of NMatrixWrapper.



26
27
28
29
30
31
32
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 26

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

#dataObject (readonly)

:nocov:



24
25
26
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 24

def data
  @data
end

#nm_dtypeObject (readonly)

:nocov:



24
25
26
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 24

def nm_dtype
  @nm_dtype
end

#sizeObject (readonly)

:nocov:



24
25
26
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 24

def size
  @size
end

Instance Method Details

#<<(element) ⇒ Object

:nocov:



67
68
69
70
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 67

def << element
  resize if @size >= @data.size
  self[@size] = element
end

#==(other) ⇒ Object

:nocov:



50
51
52
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 50

def == other
  @data[0...@size] == other[0...@size] and @size == other.size
end

#[](*index) ⇒ Object



34
35
36
37
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 34

def [] *index
  return @data[*index] if index[0] < @size
  nil
end

#[]=(index, value) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 39

def []= index, value
  raise ArgumentError, "Index #{index} does not exist" if
    index > @size && index < @data.size
  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

:nocov:



55
56
57
58
59
60
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 55

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

#dupObject



77
78
79
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 77

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



62
63
64
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 62

def index key
  @data.to_a.index key
end

#inject(*args, &block) ⇒ Object

:nocov: FIXME: not sure, why this kind of wrapper have such a pure coverage



19
20
21
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 19

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

#maxObject



100
101
102
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 100

def max
  @data[0...@size].max
end

#meanObject

:nocov:



88
89
90
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 88

def mean
  @data[0...@size].mean.first
end

#minObject



104
105
106
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 104

def min
  @data[0...@size].min
end

#productObject



92
93
94
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 92

def product
  @data[0...@size].inject(1) { |m,e| m*e }
end

#resize(size = @size*2) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 81

def resize size=@size*2
  raise ArgumentError, 'Size must be greater than current size' if size < @size

  @data = NMatrix.new [size], @data.to_a, dtype: @nm_dtype
end

#sumObject



96
97
98
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 96

def sum
  @data[0...@size].inject(:+)
end

#to_aObject

:nocov:



73
74
75
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 73

def to_a
  @data[0...@size].to_a
end