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.



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

def initialize vector, context, nm_dtype=:int32
  # To avoid arrays with nils throwing TypeError for nil nm_dtype
  nm_dtype = :object if nm_dtype.nil? && vector.any?(&:nil?)
  @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:



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

def data
  @data
end

#nm_dtypeObject (readonly)

:nocov:



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

def nm_dtype
  @nm_dtype
end

#sizeObject (readonly)

:nocov:



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

def size
  @size
end

Instance Method Details

#<<(element) ⇒ Object

:nocov:



70
71
72
73
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 70

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

#==(other) ⇒ Object

:nocov:



53
54
55
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 53

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

#[](*index) ⇒ Object



37
38
39
40
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 37

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

#[]=(index, value) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 42

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:



58
59
60
61
62
63
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 58

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



80
81
82
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 80

def dup
  NMatrixWrapper.new @data[0...@size].to_a, @context, @nm_dtype
end

#each(&block) ⇒ Object



8
9
10
11
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 8

def each(&block)
  @data[0...@size].each(&block)
  self
end

#index(key) ⇒ Object



65
66
67
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 65

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



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

def inject(*args, &block)
  @data[0...@size].inject(*args, &block)
end

#map!(&block) ⇒ Object



13
14
15
16
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 13

def map!(&block)
  @data = NMatrix.new [@size*2], map(&block).to_a, dtype: nm_dtype
  self
end

#maxObject



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

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

#meanObject

:nocov:



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

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

#minObject



107
108
109
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 107

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

#productObject



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

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

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

Raises:

  • (ArgumentError)


84
85
86
87
88
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 84

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



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

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

#to_aObject

:nocov:



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

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