Added each_row and each_column to Matrix

This commit is contained in:
Chris Watson 2019-06-18 00:13:49 -07:00
parent 442bf3cde3
commit feafd6df36
No known key found for this signature in database
GPG Key ID: 37DAEF5F446370A4
1 changed files with 19 additions and 1 deletions

View File

@ -313,6 +313,20 @@ module Apatite::LinearAlgebra
# Matrix.combine(self, matrices, &block) # Matrix.combine(self, matrices, &block)
# end # 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 # # Hadamard product
# def hadamard_product(m) # def hadamard_product(m)
# combine(m){|a, b| a * b} # combine(m){|a, b| a * b}
@ -496,7 +510,7 @@ module Apatite::LinearAlgebra
end end
def row(i, &block) def row(i, &block)
@rows.fetch(i) { return self }.each(&block) rows.fetch(i) { return self }.each(&block)
self self
end end
@ -550,6 +564,10 @@ module Apatite::LinearAlgebra
end end
end end
def to_unsafe
@buffer
end
@[AlwaysInline] @[AlwaysInline]
def unsafe_fetch(index : Int) def unsafe_fetch(index : Int)
@buffer[index] @buffer[index]