Class: BinaryMatrix

Inherits:
Object
  • Object
show all
Includes:
LogAndPrint, Utilities
Defined in:
lib/binary_matrix.rb,
lib/binary_matrix/version.rb

Overview

Binary matrix class for Warfield Augumented Boolean Logic

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LogAndPrint

#square_print_with_comment, #write_log

Methods included from Utilities

#boolean_map, #compress_array, #expand_end, #id_matrix, #reachability_matrix, #subtract_id_matrix, #swap_index, #swap_row_column

Constructor Details

#initialize(size) ⇒ BinaryMatrix

Returns a new instance of BinaryMatrix.



12
13
14
15
16
17
18
# File 'lib/binary_matrix.rb', line 12

def initialize(size)
  @size = size
  @number_of_rows = size
  @number_of_columns = size
  @bm = Array.new(size) { Array.new(size, 0) }
  @bmi = MatrixIndex.new(size)
end

Instance Attribute Details

#bmObject

Returns the value of attribute bm.



10
11
12
# File 'lib/binary_matrix.rb', line 10

def bm
  @bm
end

#bmiObject

Returns the value of attribute bmi.



10
11
12
# File 'lib/binary_matrix.rb', line 10

def bmi
  @bmi
end

#number_of_columnsObject

Returns the value of attribute number_of_columns.



10
11
12
# File 'lib/binary_matrix.rb', line 10

def number_of_columns
  @number_of_columns
end

#number_of_rowsObject

Returns the value of attribute number_of_rows.



10
11
12
# File 'lib/binary_matrix.rb', line 10

def number_of_rows
  @number_of_rows
end

#sizeObject

Returns the value of attribute size.



10
11
12
# File 'lib/binary_matrix.rb', line 10

def size
  @size
end

Instance Method Details

#add(matrix = bm) ⇒ Object



20
21
22
23
24
# File 'lib/binary_matrix.rb', line 20

def add(matrix = bm)
  sum = 0
  matrix.each { |x| x.each { |y| sum += y } }
  sum
end

#boolean_add(matrix_one, matrix_two) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/binary_matrix.rb', line 34

def boolean_add(matrix_one, matrix_two)
  temp_matrix_one = matrix_one.dup
  temp_matrix_two = matrix_two.dup
  sz = (temp_matrix_two[0].length - 1)
  temp_matrix_out = Array.new(sz + 1) { Array.new(sz + 1, 0) }
  (0..sz).map do | x|
    (0..sz).map do |y|
      temp_matrix_out[x][y] = (temp_matrix_one[x][y] + temp_matrix_two[x][y])
    end
  end
  boolean_map(temp_matrix_out)
  temp_matrix_out
end

#boolean_multiply(matrix_one, matrix_two) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/binary_matrix.rb', line 62

def boolean_multiply(matrix_one, matrix_two)
  sz = (matrix_one[0].length - 1)
  id_1 = id_matrix(size).dup
  id_2 = id_matrix(size).dup
  m_1 = boolean_add(id_1, matrix_one.dup)
  m_2 = boolean_add(id_2, matrix_two.dup)
  m_2_t = m_2.transpose
  b_m_out = (0..sz).map do |j|
    (0..sz).map { |i| m_1[j].zip(m_2_t[i]).map { |x, y| x * y }.inject(:+) }
  end
  boolean_map(b_m_out)
  b_m_out
end

#boolean_subtract(matrix_one, matrix_two) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/binary_matrix.rb', line 48

def boolean_subtract(matrix_one, matrix_two)
  temp_matrix_one = matrix_one.dup
  temp_matrix_two = matrix_two.dup
  sz = (temp_matrix_two[0].length - 1)
  temp_matrix_out = Array.new(size) { Array.new(size, 0) }
  (0..sz).map do | x|
    (0..sz).map do |y|
      temp_matrix_out[x][y] = (temp_matrix_one[x][y] - temp_matrix_two[x][y])
    end
  end
  boolean_map(temp_matrix_out)
  temp_matrix_out
end

#compress(index_1, index_2, matrix = bm, matrix_index = bmi.mi) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/binary_matrix.rb', line 83

def compress(index_1, index_2, matrix = bm, matrix_index = bmi.mi)
  new_matrix = matrix.dup
  i_1 = matrix_index.index(index_1)
  i_2 = matrix_index.index(index_2)
  a_1 = new_matrix[i_1]
  a_2 = new_matrix[i_2]
  a_c_1_2 = compress_array(a_1, a_2, i_1)
  new_matrix[i_1] = a_c_1_2
  new_matrix.delete_at(i_2)
  new_matrix_t = new_matrix.transpose.dup
  a_1_t = new_matrix_t[i_1]
  a_2_t = new_matrix_t[i_2]
  a_c_1_2_t = compress_array(a_1_t, a_2_t, i_1)
  new_matrix_t[i_1] = a_c_1_2_t
  new_matrix_t.delete_at(i_2)
  new_matrix_t.transpose.dup
end

#enter_cell_content(index_1, index_2, number = 1, matrix = bm, matrix_in = bmi.mi) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/binary_matrix.rb', line 26

def enter_cell_content(index_1, index_2, number = 1, matrix = bm, matrix_in = bmi.mi)
  number = 0 if index_1 == index_2
  r_1 = matrix_in.index(index_1)
  r_2 = matrix_in.index(index_2)
  matrix[r_1][r_2] = number
  matrix
end

#expand(matrix, index_1) ⇒ Object



76
77
78
79
80
81
# File 'lib/binary_matrix.rb', line 76

def expand(matrix, index_1)
  new_matrix = expand_end(matrix)
  last_index = new_matrix[0].length - 1
  matrix_out = swap_row_column([index_1], [last_index], new_matrix)
  matrix_out
end