Class: C4::Model::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/c4/model/matrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#columnsObject (readonly)

Returns the value of attribute columns.



10
11
12
# File 'lib/c4/model/matrix.rb', line 10

def columns
  @columns
end

#matrixObject (readonly)

Returns the value of attribute matrix.



10
11
12
# File 'lib/c4/model/matrix.rb', line 10

def matrix
  @matrix
end

#rowsObject (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

#diagonalObject

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

#diagonalsObject

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_diagonalsObject



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

#quadrantsObject



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

#reverseObject



54
55
56
# File 'lib/c4/model/matrix.rb', line 54

def reverse
  self.class.new(matrix.reverse, rows, columns)
end

#rotateObject

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

#sizeObject



30
31
32
# File 'lib/c4/model/matrix.rb', line 30

def size
  [rows, columns].max
end

#squareObject

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

#transposeObject



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