Module: Linalg::Iterators
- Included in:
- DMatrix
- Defined in:
- lib/linalg/iterators.rb
Overview
Enumerable
s and Enumerable
-like methods for matrices.
For blocks which yield i,j
indexes, the visitation is always in column-major order – that is, i
changes faster than j
.
To visit in row-major order, one would say
m.rows.each_with_index { |row, i|
row.elems.each_with_index { |e, j|
# e == m[i,j]
}
}
Defined Under Namespace
Classes: ColumnEnum, DiagEnum, ElemEnum, MatrixEnum, RowEnum
Instance Method Summary collapse
-
#columns ⇒ Object
Returns an
Enumerable
of the columns. -
#diags ⇒ Object
Returns an
Enumerable
of the diagonal elements. -
#each_index ⇒ Object
Like
Array#each_index
, but with an index pair. -
#each_lower_with_index ⇒ Object
Visit below-diagonal elements, with index pair.
-
#each_upper_with_index ⇒ Object
Visit above-diagonal elements, with index pair.
-
#each_with_index ⇒ Object
Like
Enumerable#each_with_index
, but with an index pair. -
#elems ⇒ Object
Returns an
Enumerable
of the matrix elements. -
#map ⇒ Object
Like
Enumerable#map
, but the resultant is a another matrix. -
#map! ⇒ Object
In-place
map
. -
#map_with_index ⇒ Object
map
with an index pair. -
#map_with_index! ⇒ Object
In-place
map_with_index
. -
#rows ⇒ Object
Returns an
Enumerable
of the rows.
Instance Method Details
#columns ⇒ Object
Returns an Enumerable
of the columns
97 98 99 |
# File 'lib/linalg/iterators.rb', line 97 def columns ColumnEnum.new(self) end |
#diags ⇒ Object
Returns an Enumerable
of the diagonal elements
111 112 113 |
# File 'lib/linalg/iterators.rb', line 111 def diags DiagEnum.new(self) end |
#each_index ⇒ Object
Like Array#each_index
, but with an index pair
130 131 132 133 134 135 136 137 |
# File 'lib/linalg/iterators.rb', line 130 def each_index (0...hsize).each { |j| (0...vsize).each { |i| yield i, j } } self end |
#each_lower_with_index ⇒ Object
Visit below-diagonal elements, with index pair
184 185 186 187 188 189 190 191 |
# File 'lib/linalg/iterators.rb', line 184 def each_lower_with_index # :yields: e, i, j (0...hsize).each { |j| ((j+1)...vsize).each { |i| yield self[i,j], i, j } } self end |
#each_upper_with_index ⇒ Object
Visit above-diagonal elements, with index pair
196 197 198 199 200 201 202 203 |
# File 'lib/linalg/iterators.rb', line 196 def each_upper_with_index # :yields: e, i, j (1...hsize).each { |j| (0...(j > vsize ? vsize : j)).each { |i| yield self[i,j], i, j } } self end |
#each_with_index ⇒ Object
Like Enumerable#each_with_index
, but with an index pair
118 119 120 121 122 123 124 125 |
# File 'lib/linalg/iterators.rb', line 118 def each_with_index # :yields: e, i, j (0...hsize).each { |j| (0...vsize).each { |i| yield self[i,j], i, j } } self end |
#elems ⇒ Object
Returns an Enumerable
of the matrix elements
104 105 106 |
# File 'lib/linalg/iterators.rb', line 104 def elems ElemEnum.new(self) end |
#map ⇒ Object
Like Enumerable#map
, but the resultant is a another matrix
142 143 144 145 146 147 148 |
# File 'lib/linalg/iterators.rb', line 142 def map # :yields: e m = self.class.reserve(self.vsize, self.hsize) each_with_index { |e, i, j| m[i,j] = yield e } m end |
#map! ⇒ Object
In-place map
164 165 166 167 168 169 |
# File 'lib/linalg/iterators.rb', line 164 def map! # :yields: e each_with_index { |e, i, j| self[i,j] = yield e } self end |
#map_with_index ⇒ Object
map
with an index pair
153 154 155 156 157 158 159 |
# File 'lib/linalg/iterators.rb', line 153 def map_with_index # :yields: e, i, j m = self.class.reserve(self.vsize, self.hsize) each_with_index { |e, i, j| m[i,j] = yield e, i, j } m end |
#map_with_index! ⇒ Object
In-place map_with_index
174 175 176 177 178 179 |
# File 'lib/linalg/iterators.rb', line 174 def map_with_index! # :yields: e, i, j each_with_index { |e, i, j| self[i,j] = yield e, i, j } self end |