module

Geode::MatrixIterators(T, M, N)

Iterator methods for matrices.

Intended to be used as a mix-in on matrix types. T is the type of each element in the matrix. M and N are positive integers indicating the number of rows and columns respectively.

Instance methods

each_column

Returns an iterator that enumerates through each column of the matrix.

Yields a vector with the elements from the current column. The size of the vector is equal to the number of rows.

Matrix[[1, 2, 3], [4, 5, 6]].each_column.to_a
# => [(1, 4), (2, 5), (3, 6)]
Source
each_column_with_index(offset = 0) : Iterator(Tuple(CommonVector(T, M), Int32))

Returns an iterator that enumerates through each column of the matrix.

Yields a vector with the elements from the current column and the column index. The size of the vector is equal to the number of rows. An offset can be specified, which is added to the yielded column index. This does not affect the starting column.

Matrix[[1, 2, 3], [4, 5, 6]].each_column_with_index(1).to_a
# => [{(1, 4), 1}, {(2, 5), 2}, {(3, 6), 3}]
Source
each_indices

Returns an iterator that enumerates through each of the indices (not the elements).

Yields for each index combination in the matrix. Two arguments are supplied to the block: i and j.

Matrix[[0, 1], [2, 3]].each_indices.to_a
# => [{0, 0}, {0, 1}, {1, 0}, {1, 1}]
Source
each_row

Returns an iterator that enumerates through each row of the matrix.

Yields a vector with the elements from the current row. The size of the vector is equal to the number of columns.

Matrix[[1, 2, 3], [4, 5, 6]].each_row.to_a
# => [(1, 2, 3), (4, 5, 6)]
Source
each_row_with_index(offset = 0) : Iterator(Tuple(CommonVector(T, N), Int32))

Returns an iterator that enumerates through each row of the matrix.

Yields a vector with the elements from the current row and the row index. The size of the vector is equal to the number of columns. An offset can be specified, which is added to the yielded row index. This does not affect the starting row.

Matrix[[1, 2, 3], [4, 5, 6]].each_row_with_index(1).to_a
# => [{(1, 2, 3), 1}, {(4, 5, 6), 2}]
Source
each_with_indices

Returns an iterator that enumerates through each element and its indices.

Yields for each element in the matrix. Three arguments are supplied to the block: the element, i, and j.

Matrix[[0, 1], [2, 3]].each_with_indices.to_a
# => [{0, 0, 0}, {1, 0, 1}, {2, 1, 0}, {3, 1, 1}]
Source