Module: NumRu::Misc::MD_Iterators
- Included in:
- NArray
- Defined in:
- lib/numru/misc/md_iterators.rb
Instance Method Summary collapse
Instance Method Details
#each_subary_at_dims(*dims) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/numru/misc/md_iterators.rb', line 88 def ( *dims ) if dims.min<0 || dims.max>=rank raise ArguemntError,"Invalid dims #{dims.inspect} for #{rank}D array" end loopdims = Array.new sh = Array.new len = 1 (0...rank).each{|i| if !dims.include?(i) loopdims.push(i) sh.push(shape[i]) len *= shape[i] end } if loopdims.length == 0 yield(self) return self end cs = [1] (1...sh.length).each{|i| cs[i] = sh[i-1]*cs[i-1]} idx = Array.new all = 0..-1 for i in 0...len do loopdims.each_with_index{|d,j| idx[d] = ( (i/cs[j])%sh[j] )} dims.each{|d| idx[d] = all} sub = self[ *idx ] yield(sub) end self end |
#each_subary_at_dims_with_index(*dims) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/numru/misc/md_iterators.rb', line 120 def ( *dims ) if dims.min<0 || dims.max>=rank raise ArguemntError,"Invalid dims #{dims.inspect} for #{rank}D array" end loopdims = Array.new sh = Array.new len = 1 (0...rank).each{|i| if !dims.include?(i) loopdims.push(i) sh.push(shape[i]) len *= shape[i] end } if loopdims.length == 0 yield(self, false) return self end cs = [1] (1...sh.length).each{|i| cs[i] = sh[i-1]*cs[i-1]} idx = Array.new all = 0..-1 for i in 0...len do loopdims.each_with_index{|d,j| idx[d] = ( (i/cs[j])%sh[j] )} dims.each{|d| idx[d] = all} sub = self[ *idx ] yield(sub, idx) end self end |