Class: Daru::Accessors::NMatrixWrapper
- Inherits:
-
Object
- Object
- Daru::Accessors::NMatrixWrapper
- Includes:
- Statistics, Enumerable
- Defined in:
- lib/daru/accessors/nmatrix_wrapper.rb
Overview
Internal class for wrapping NMatrix
Defined Under Namespace
Modules: Statistics
Instance Attribute Summary collapse
-
#missing_data ⇒ Object
readonly
Returns the value of attribute missing_data.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#vector ⇒ Object
readonly
Returns the value of attribute vector.
Instance Method Summary collapse
- #<<(element) ⇒ Object
- #==(other) ⇒ Object
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #coerce(dtype) ⇒ Object
- #delete_at(index) ⇒ Object
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #index(key) ⇒ Object
-
#initialize(vector, caller) ⇒ NMatrixWrapper
constructor
A new instance of NMatrixWrapper.
- #resize(size = @size*2) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(vector, caller) ⇒ NMatrixWrapper
Returns a new instance of NMatrixWrapper.
186 187 188 189 190 191 192 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 186 def initialize vector, caller @size = vector.size @vector = NMatrix.new [@size*2], vector.to_a @missing_data = false @caller = caller # init with twice the storage for reducing the need to resize end |
Instance Attribute Details
#missing_data ⇒ Object (readonly)
Returns the value of attribute missing_data.
184 185 186 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 184 def missing_data @missing_data end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
184 185 186 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 184 def size @size end |
#vector ⇒ Object (readonly)
Returns the value of attribute vector.
184 185 186 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 184 def vector @vector end |
Instance Method Details
#<<(element) ⇒ Object
223 224 225 226 227 228 229 230 231 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 223 def << element if @size >= @vector.size resize end self[@size] = element @size += 1 end |
#==(other) ⇒ Object
208 209 210 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 208 def == other @vector == other and @size == other.size end |
#[](index) ⇒ Object
194 195 196 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 194 def [] index @vector[index] end |
#[]=(index, value) ⇒ Object
198 199 200 201 202 203 204 205 206 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 198 def []= index, value resize if index >= @size if value.nil? @missing_data = true @vector = @vector.cast(dtype: :object) end @vector[index] = value end |
#coerce(dtype) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 241 def coerce dtype case when dtype == Array Daru::Accessors::ArrayWrapper.new @vector[0..(@size-1)].to_a, @caller when dtype == NMatrix self when dtype == MDArray raise NotImplementedError else raise ArgumentError, "Cant coerce to dtype #{dtype}" end end |
#delete_at(index) ⇒ Object
212 213 214 215 216 217 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 212 def delete_at index arry = @vector.to_a arry.delete_at index @vector = NMatrix.new [@size-1], arry @size -= 1 end |
#dup ⇒ Object
237 238 239 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 237 def dup NMatrixWrapper.new @vector.to_a end |
#each(&block) ⇒ Object
180 181 182 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 180 def each(&block) @vector.each(&block) end |
#index(key) ⇒ Object
219 220 221 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 219 def index key @vector.to_a.index key end |
#resize(size = @size*2) ⇒ Object
254 255 256 257 258 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 254 def resize size = @size*2 raise "Size must be greater than current size" if size < @size @vector = NMatrix.new [size], @vector.to_a end |
#to_a ⇒ Object
233 234 235 |
# File 'lib/daru/accessors/nmatrix_wrapper.rb', line 233 def to_a @vector.to_a end |