module Geode::MatrixIterators(T, M, N)

Overview

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.

Direct including types

Defined in:

geode/matrices/iterators.cr

Instance Method Summary

Instance Method Detail

def each_column : Iterator(CommonVector(T, M)) #

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)]

[View source]
def 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}]

[View source]
def each_indices : Iterator(Tuple(Int32, Int32)) #

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}]

[View source]
def each_row : Iterator(CommonVector(T, N)) #

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)]

[View source]
def 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}]

[View source]
def each_with_indices : Iterator(Tuple(T, Int32, Int32)) #

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}]

[View source]