Class: C4::Model::Matrix
- Inherits:
-
Object
- Object
- C4::Model::Matrix
- Defined in:
- lib/c4/model/matrix.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#matrix ⇒ Object
readonly
Returns the value of attribute matrix.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
-
#diagonal ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#diagonals ⇒ Object
rubocop:enable Metrics/AbcSize, Performance/TimesMap.
-
#initialize(matrix, rows, columns) ⇒ Matrix
constructor
A new instance of Matrix.
- #partial_square(from, to) ⇒ Object
- #quadrant_diagonals ⇒ Object
- #quadrants ⇒ Object
- #reverse ⇒ Object
-
#rotate ⇒ Object
rubocop:disable Metrics/AbcSize, Performance/TimesMap.
- #size ⇒ Object
-
#square ⇒ Object
rubocop:disable Metrics/AbcSize.
- #transpose ⇒ Object
Constructor Details
#initialize(matrix, rows, columns) ⇒ Matrix
Returns a new instance of Matrix.
12 13 14 15 16 |
# File 'lib/c4/model/matrix.rb', line 12 def initialize(matrix, rows, columns) @matrix = matrix @rows = rows @columns = columns end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
10 11 12 |
# File 'lib/c4/model/matrix.rb', line 10 def columns @columns end |
#matrix ⇒ Object (readonly)
Returns the value of attribute matrix.
10 11 12 |
# File 'lib/c4/model/matrix.rb', line 10 def matrix @matrix end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
10 11 12 |
# File 'lib/c4/model/matrix.rb', line 10 def rows @rows end |
Instance Method Details
#diagonal ⇒ Object
rubocop:enable Metrics/AbcSize
48 49 50 51 52 |
# File 'lib/c4/model/matrix.rb', line 48 def diagonal Array.new(size).each_with_index.map do |_, i| matrix[i][i] end end |
#diagonals ⇒ Object
rubocop:enable Metrics/AbcSize, Performance/TimesMap
69 70 71 |
# File 'lib/c4/model/matrix.rb', line 69 def diagonals quadrants.flat_map(&:quadrant_diagonals).uniq end |
#partial_square(from, to) ⇒ Object
34 35 36 |
# File 'lib/c4/model/matrix.rb', line 34 def partial_square(from, to) self.class.new(matrix[from..to].map { |a| a[from..to] }, to - from + 1, to - from + 1) end |
#quadrant_diagonals ⇒ Object
82 83 84 85 86 |
# File 'lib/c4/model/matrix.rb', line 82 def quadrant_diagonals (0...size).map do |i| partial_square(0, i).rotate.diagonal end end |
#quadrants ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/c4/model/matrix.rb', line 73 def quadrants [ square, square.rotate, square.rotate.rotate, square.rotate.rotate.rotate ] end |
#reverse ⇒ Object
54 55 56 |
# File 'lib/c4/model/matrix.rb', line 54 def reverse self.class.new(matrix.reverse, rows, columns) end |
#rotate ⇒ Object
rubocop:disable Metrics/AbcSize, Performance/TimesMap
59 60 61 62 63 64 65 66 |
# File 'lib/c4/model/matrix.rb', line 59 def rotate result = rows.times.map do |r| columns.times.map do |c| matrix[columns - c - 1][r] end end self.class.new(result, columns, rows) end |
#size ⇒ Object
30 31 32 |
# File 'lib/c4/model/matrix.rb', line 30 def size [rows, columns].max end |
#square ⇒ Object
rubocop:disable Metrics/AbcSize
39 40 41 42 43 44 45 |
# File 'lib/c4/model/matrix.rb', line 39 def square sq = matrix.map do |col| col[0, size].fill_up_to(size, nil) end sq[0, size].fill_up_to(size, Array.new(size)) self.class.new(sq, size, size) end |
#transpose ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/c4/model/matrix.rb', line 18 def transpose self.class.new( Array.new(rows).each_with_index.map do |_, row| Array.new(columns).each_with_index.map do |_, column| matrix[column][row] end end, columns, rows ) end |