class Matrix
Matrix
extensions
Public Class Methods
ones(*dims)
click to toggle source
Build an array of 1's
# File lib/epitools/core_ext/matrix.rb, line 25 def self.ones(*dims) build(*dims) { 1 } end
random(*dims)
click to toggle source
Create a matrix of the specified size full of random numbers.
# File lib/epitools/core_ext/matrix.rb, line 32 def self.random(*dims) build(*dims) { rand } end
Public Instance Methods
blit!(submatrix, top, left)
click to toggle source
Overlay one matrix onto another
# File lib/epitools/core_ext/matrix.rb, line 83 def blit!(submatrix, top, left) submatrix.each_row.with_index do |row, y| row.each.with_index do |elem, x| self[top+y,left+x] = elem end end end
Also aliased as: overlay!
each_row() { |row(num)| ... }
click to toggle source
Iterate over rows (takes a block or returns an Enumerator
)
# File lib/epitools/core_ext/matrix.rb, line 46 def each_row return to_enum(:each_row) unless block_given? (0...row_count).each do |num| yield row(num) end end
print(header=true, separator=" ")
click to toggle source
Print the matrix to the STDOUT.
# File lib/epitools/core_ext/matrix.rb, line 57 def print(header=true, separator=" ") max_width = map {|num| num.to_s.size }.max case first when Integer justify = :rjust when Float justify = :ljust else raise "Unknown matrix element type: #{first.class}" end # print it! puts "#{size.join("x")} matrix:" if header rows.each do |row| puts " " + row.map { |n| n.to_s.send(justify, max_width) }.join(separator) end puts end
Also aliased as: draw
size()
click to toggle source
The size of the matrix, returned as `[rows, columns]`.
# File lib/epitools/core_ext/matrix.rb, line 39 def size [row_size, column_size] end