Class: NumRu::MDStorage
- Inherits:
-
Object
- Object
- NumRu::MDStorage
- Defined in:
- lib/numru/gphys/mdstorage.rb
Instance Attribute Summary collapse
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
Instance Method Summary collapse
-
#[](*args) ⇒ Object
read from a position specified with integers (only one element).
-
#[]=(*args) ⇒ Object
substituion at a position specified with integers (only one element).
-
#add_dim ⇒ Object
add a new dimension to the last.
-
#count_non_nil ⇒ Object
count the number of non-nil objects.
-
#extend(dim) ⇒ Object
increase the length of a dimension by one.
-
#initialize(rank = 1) ⇒ MDStorage
constructor
A new instance of MDStorage.
-
#length ⇒ Object
count the total length (including nil).
- #shape ⇒ Object
- #to_na ⇒ Object
Constructor Details
#initialize(rank = 1) ⇒ MDStorage
Returns a new instance of MDStorage.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/numru/gphys/mdstorage.rb', line 5 def initialize(rank=1) raise(ArgumentError,"rank must be a positive integer") if !(rank>0) @rank = rank @shape = [] rank.times{@shape.push(1)} @data = [nil] (rank-1).times{ @data = [@data] } end |
Instance Attribute Details
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
16 17 18 |
# File 'lib/numru/gphys/mdstorage.rb', line 16 def rank @rank end |
Instance Method Details
#[](*args) ⇒ Object
read from a position specified with integers (only one element)
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/numru/gphys/mdstorage.rb', line 73 def [](*args) raise(ArgumentError,"# of args != rank (#{rank})") if args.length != rank x = @data args.reverse_each do |idx| raise(ArgumentError,"all args must be integers") if !idx.is_a?(Integer) x = x[idx] return(x) if x.nil? end x end |
#[]=(*args) ⇒ Object
substituion at a position specified with integers (only one element)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/numru/gphys/mdstorage.rb', line 42 def []=(*args) val = args.pop raise(ArgumentError,"# of args != rank (#{rank})") if args.length != rank x = @data (rank-1).downto(1) do |d| idx = args[d] raise(ArgumentError,"all args must be integers") if !idx.is_a?(Integer) len = shape[d] idx += len if idx<0 raise("Too big negative index for dim #{d}: #{idx-len}") if idx<0 if 0<=idx and idx<len x = x[idx] elsif idx >= len (idx-len+1).times{extend(d)} x = x[idx] else raise(ArgumentError,"invalid specification") end end idx = args[0] len = shape[0] idx += len if idx<0 raise("Too big negative index for dim #{0}: #{idx-len}") if idx<0 if idx >= len (idx-len+1).times{extend(0)} end x[idx] = val end |
#add_dim ⇒ Object
add a new dimension to the last
23 24 25 26 27 |
# File 'lib/numru/gphys/mdstorage.rb', line 23 def add_dim @rank += 1 @shape.push(1) @data = [@data] end |
#count_non_nil ⇒ Object
count the number of non-nil objects
96 97 98 99 100 |
# File 'lib/numru/gphys/mdstorage.rb', line 96 def count_non_nil a = @data.flatten a.delete(nil) a.length end |
#extend(dim) ⇒ Object
increase the length of a dimension by one
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/numru/gphys/mdstorage.rb', line 30 def extend(dim) if dim<0 or dim>=rank raise(ArgumentError,"invalid dim (#{dim}): not in #{0..rank-1}") end yield_at_depth(@data,rank-1-dim){|a| a.push( dim==0 ? nil : make_nested_array(shape[0..dim-1]) ) } @shape[dim] += 1 self end |
#length ⇒ Object
count the total length (including nil)
89 90 91 92 93 |
# File 'lib/numru/gphys/mdstorage.rb', line 89 def length len = 1 @shape.each{|s| len *= s} len end |
#shape ⇒ Object
18 19 20 |
# File 'lib/numru/gphys/mdstorage.rb', line 18 def shape @shape.dup end |
#to_na ⇒ Object
84 85 86 |
# File 'lib/numru/gphys/mdstorage.rb', line 84 def to_na NArray.to_na(@data) end |