From feafd6df36a96d41cb2ad7f9a2aa6c367f044a01 Mon Sep 17 00:00:00 2001 From: Chris Watson Date: Tue, 18 Jun 2019 00:13:49 -0700 Subject: [PATCH] Added each_row and each_column to Matrix --- src/apatite/linear_algebra/matrix.cr | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/apatite/linear_algebra/matrix.cr b/src/apatite/linear_algebra/matrix.cr index 9cc4bd8..52d8bc5 100644 --- a/src/apatite/linear_algebra/matrix.cr +++ b/src/apatite/linear_algebra/matrix.cr @@ -313,6 +313,20 @@ module Apatite::LinearAlgebra # Matrix.combine(self, matrices, &block) # end + # Iterates over each column, yielding the column + def each_column(&block) + vectors = column_vectors.map { |vec| yield(vec) } + @buffer = Matrix.columns(vectors).to_unsafe + vectors + end + + # Iterates over each row, yielding the row + def each_row(&block) + vectors = rows.map { |vec| yield(vec) } + @buffer = Matrix.rows(vectors).to_unsafe + vectors + end + # # Hadamard product # def hadamard_product(m) # combine(m){|a, b| a * b} @@ -496,7 +510,7 @@ module Apatite::LinearAlgebra end def row(i, &block) - @rows.fetch(i) { return self }.each(&block) + rows.fetch(i) { return self }.each(&block) self end @@ -550,6 +564,10 @@ module Apatite::LinearAlgebra end end + def to_unsafe + @buffer + end + @[AlwaysInline] def unsafe_fetch(index : Int) @buffer[index]