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
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 27

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:


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:


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

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

#==(other) ⇒ Object

:nocov:


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

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

#[](*index) ⇒ Object


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

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

#[]=(index, value) ⇒ Object

Raises:

  • (ArgumentError)

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

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:


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

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


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

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


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

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


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

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

#meanObject

:nocov:


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

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

#minObject


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

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

#productObject


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

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

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

Raises:

  • (ArgumentError)

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

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


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

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

#to_aObject

:nocov:


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

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