Class: Matrix

Inherits:
Object show all
Defined in:
lib/epitools/core_ext/matrix.rb

Overview

Matrix extensions

Class Method Summary collapse

Instance Method Summary collapse

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

#sizeObject

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