Using Python numpy to realize magic square

Source: Internet
Author: User

#-*-Coding:utf-8-*-
#利用numpy模块构造幻方
Import NumPyAs NP

#列表循环向左移offset位
DefShift_left(LST, offset):
return [lst[(I+offset)%len (LST)]For IIn range (len (LST))]

#列表循环向右移offset位
DefShift_right(LST, offset):
return [Lst[i-offset]For IIn range (len (LST))]

#构造奇数阶幻方函数
DefMagic_of_odd_order(n):
p = (int) (N-1)/2)

#创建矩阵1
Initial_lst1 = List (range (p+1,n)) +list (range (p+1))
INITIAL_MAT1 = []
For IIn 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 IIn range (n): Initial_mat2.append (Shift_right (Initial_lst2, i)) Mat2 = Np.array (INITIAL_MAT2)#创建矩阵3, which is a matrix of all 1 elements
Mat3= Np.ones ((n,n), Dtype=np.int)

#构造幻方
Magic = N*MAT2+MAT1+MAT3
Return Magic

#构造4n阶幻方函数
DefMagic_of_4n_order(n):
Mat = Np.array (Range (1,n*n+1). Reshape (N,n)
For IIn range ((int) (n/4)):
For JIn range ((int) (n/4)):
For KIn range (4):#将每个4 the diagonal of a small square into complementary elements
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-Order magic square function
DefMagic_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 IIn range (p):
If I! = row:
For KIn range ((int) ((n2)/4)):
MATA[I][K],MATC[I][K] = Matc[i][k],mata[i][k]
Else
For KIn range ((int) ((n2)/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 JIn range (col+(int) ((n2)/4), col+1):
For IIn range (p):
MATB[I][J],MATD[I][J] = Matd[i][j],matb[i][j]

#合并矩阵块A, b,c,d form Magic Square
Magic = Np.row_stack ((Np.column_stack ((MATA,MATB)), Np.column_stack ((MATC,MATD)))
Return Magic

DefMain():
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 rowIn Magic:
For ColIn row:
Print (col, end=' \ t ')
Print ()

#验证生成的magic是否为幻方
val = input ("Does want to validate?" [y| N] "))
If val = =' Y ' or val = = ' y ':
Print (' per-line and: ', Np.sum (Magic, axis=0))
Print (' per column and: ', Np.sum (Magic, axis=1))
Print (' main diagonal sum: ', sum ([magic[i][i] for i in range (order)]))
Print (' diagonal sum: ', sum ([magic[i][order-1-i] for i in range (order)]))
Print (' it\ ' s done! ')

Main ()

Using Python numpy to realize magic square

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.