Module: NMatrix::MagicHelpers

Defined in:
lib/nmatrix/shortcuts.rb

Overview

Methods for generating magic matrix.

Class Method Summary collapse

Class Method Details

.doubly_even_magic(nm, shape) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nmatrix/shortcuts.rb', line 55

def doubly_even_magic(nm, shape)
  mini_square_num = shape / 4
  count = 1     
  inv_count = shape * shape
  shape.times do |row|
    shape.times do |col|
      if col >= mini_square_num and col < shape - mini_square_num
        if row >= mini_square_num and row < shape - mini_square_num
	        nm[row,col] = count
        else 
          nm[row,col] = inv_count
        end
      elsif row < mini_square_num or row >= shape - mini_square_num
        nm[row,col] = count
      else
        nm[row,col] = inv_count
      end
      count += 1
      inv_count -= 1  
    end
  end
end

.odd_magic(nm, shape) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nmatrix/shortcuts.rb', line 40

def odd_magic(nm, shape)
  row = shape - 1
  col = shape / 2       
  nm[row,col] = 1
  (2..shape * shape).each do |index|
    if nm[(row + 1) % shape,(col + 1) % shape] == 0
      row = (row + 1) % shape
      col = (col + 1) % shape
    else
      row = (row - 1 + shape) % shape
    end
      nm[row,col] = index
  end
end

.singly_even_magic(nm, shape) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nmatrix/shortcuts.rb', line 78

def singly_even_magic(nm, shape)
  half_shape = shape / 2
  complementary_pair = (shape - 2) / 4
  swap_col = NMatrix.new([shape])
  index = 0 
  mini_magic = NMatrix.new([half_shape,half_shape], 0, dtype: nm.dtype)
  odd_magic mini_magic, half_shape
  half_shape.times do |row|
    half_shape.times do |col|
      nm[row,col] = mini_magic[row,col]  	
      nm[row + half_shape,col + half_shape] = mini_magic[row,col] + half_shape * half_shape  
      nm[row,col + half_shape] = mini_magic[row,col] + 2 * half_shape * half_shape      
      nm[row + half_shape,col] = mini_magic[row,col] + 3 * half_shape * half_shape       
    end  
  end
  
  (1..complementary_pair).each do |complementary_entry|
    swap_col[index] = complementary_entry
    index += 1
  end

  (shape - complementary_pair + 2..shape).each do |center|
    swap_col[index] = center
    index += 1
  end 

  (1..half_shape).each do |row|
    (1..index).each do |col|
      temp = nm[row - 1,swap_col[col - 1] - 1]
      nm[row - 1,swap_col[col - 1] - 1] = nm[row + half_shape - 1,swap_col[col - 1] - 1]
      nm[row + half_shape - 1,swap_col[col - 1] - 1] = temp
    end
  end

  temp = nm[complementary_pair,0] 
  nm[complementary_pair,0] = nm[complementary_pair + half_shape,0] 
  nm[complementary_pair + half_shape,0] = temp

  temp = nm[complementary_pair + half_shape,complementary_pair]
  nm[complementary_pair + half_shape,complementary_pair] = nm[complementary_pair,complementary_pair] 
  nm[complementary_pair,complementary_pair] = temp
end