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
|