Module: Linalg::Iterators

Included in:
DMatrix
Defined in:
lib/linalg/iterators.rb

Overview

Enumerables 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

Instance Method Details

#columnsObject

Returns an Enumerable of the columns



97
98
99
# File 'lib/linalg/iterators.rb', line 97

def columns
   ColumnEnum.new(self)
end

#diagsObject

Returns an Enumerable of the diagonal elements



111
112
113
# File 'lib/linalg/iterators.rb', line 111

def diags
   DiagEnum.new(self)
end

#each_indexObject

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_indexObject

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_indexObject

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_indexObject

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

#elemsObject

Returns an Enumerable of the matrix elements



104
105
106
# File 'lib/linalg/iterators.rb', line 104

def elems
   ElemEnum.new(self)
end

#mapObject

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_indexObject

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

#rowsObject

Returns an Enumerable of the rows



90
91
92
# File 'lib/linalg/iterators.rb', line 90

def rows
   RowEnum.new(self)
end