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.



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

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#nm_dtypeObject (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

#sizeObject (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

#dupObject



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

#maxObject



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

def max
  @data.max
end

#meanObject



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

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

#minObject



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

def min
  @data.min
end

#productObject



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

Raises:

  • (ArgumentError)


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

#sumObject



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

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

#to_aObject



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

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