1. 程式人生 > >用python numpy實現幻方

用python numpy實現幻方

nump param eva validate d+ 矩陣 axis numpy int()

# -*- coding: utf-8 -*-
#利用numpy模塊構造幻方
import numpy as np

#列表循環向左移offset位
def shift_left(lst, offset):
return [lst[(i+offset)%len(lst)] for i in range(len(lst))]

#列表循環向右移offset位
def shift_right(lst, offset):
return [lst[i-offset] for i in range(len(lst))]

#構造奇數階幻方函數
def magic_of_odd_order(n):
p = (int)((n-1)/2)

#創建矩陣1
initial_lst1 = list(range(p+1,n))+list(range(p+1))
initial_mat1 = []
for i in range(n):
initial_mat1.append(shift_left(initial_lst1, i))
mat1 = np.array(initial_mat1)

#創建矩陣2
initial_lst2 = list(range(p,-1,-1))+list(range(2*p,p,-1))
initial_mat2 = []
for i in range(n): initial_mat2.append(shift_right(initial_lst2, i)) mat2 = np.array(initial_mat2) #創建矩陣3,即元素全為1的矩陣
mat3= np.ones((n,n),dtype=np.int)

#構造幻方
magic = n*mat2+mat1+mat3
return magic

#構造4n階幻方函數
def magic_of_4n_order(n):
mat = np.array(range(1,n*n+1)).reshape(n,n)
for i in range((int)(n/4)):
for j in range((int)(n/4)):
for k in range(4): #將每個4*4小方塊的對角線換成互補元素
mat[k+4*j][k+4*i] = n*n+1-mat[k+4*j][k+4*i]
mat[k+4*j][3-k+4*i] = n*n+1-mat[k+4*j][3-k+4*i]

return mat

#構造4n+2階幻方函數
def magic_of_4n2_order(n):
p = (int)(n/2)
matA = magic_of_odd_order(p)
matD = matA+p**2
matB = matD+p**2
matC = matB+p**2

#交換矩陣塊A與矩陣塊C中特定元素的位置
row = (int)((p-1)/2)
for i in range(p):
if i != row:
for k in range((int)((n-2)/4)):
matA[i][k],matC[i][k] = matC[i][k],matA[i][k]
else:
for k in range((int)((n-2)/4)):
matA[i][row+k],matC[i][row+k] = matC[i][row+k],matA[i][row+k]

#交換矩陣塊B與矩陣塊D中特定元素的位置
col = (int)((p-1)/2)
for j in range(col+2-(int)((n-2)/4),col+1):
for i in range(p):
matB[i][j],matD[i][j] = matD[i][j],matB[i][j]

#合並矩陣塊A,B,C,D組成幻方
magic = np.row_stack((np.column_stack((matA,matB)),np.column_stack((matC,matD))))
return magic

def main():
order = eval(input(‘Enter the order of magic square(>=3): ‘))

if order%2 ==1:
magic = magic_of_odd_order(order)
elif order%4 == 0:
magic = magic_of_4n_order(order)
else:
magic = magic_of_4n2_order(order)
print(‘Generating magic square of %d order......‘%order)

for row in magic:
for col in row:
print(col, end=‘\t‘)
print()

#驗證生成的magic是否為幻方
val = input(("Do you want to validate?[Y|N]"))
if val == ‘Y‘ or val == ‘y‘:
print(‘每行的和:‘, np.sum(magic, axis=0))
print(‘每列的和:‘, np.sum(magic, axis=1))
print(‘主對角線的和:‘, sum([magic[i][i] for i in range(order)]))
print(‘副對角線的和:‘, sum([magic[i][order-1-i] for i in range(order)]))
print(‘It\‘s Done!‘)

main()

用python numpy實現幻方