Class: Matrix
Overview
Matrix extensions
Class Method Summary collapse
-
.random(*dims) ⇒ Object
Create a matrix of the specified size full of random numbers.
Instance Method Summary collapse
-
#print(header = true, separator = " ") ⇒ Object
(also: #draw)
Print the matrix to the STDOUT.
-
#size ⇒ Object
The size of the matrix, returned as ‘[rows, columns]`.
Class Method Details
.random(*dims) ⇒ Object
Create a matrix of the specified size full of random numbers.
14 15 16 |
# File 'lib/epitools/core_ext/matrix.rb', line 14 def self.random(*dims) build(*dims) { rand } end |
Instance Method Details
#print(header = true, separator = " ") ⇒ Object Also known as: draw
Print the matrix to the STDOUT.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/epitools/core_ext/matrix.rb', line 29 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 |
#size ⇒ Object
The size of the matrix, returned as ‘[rows, columns]`.
22 23 24 |
# File 'lib/epitools/core_ext/matrix.rb', line 22 def size [row_size, column_size] end |