#2019.1.12 liu先僧
from numpy import *
#创建矩阵类,包含矩阵的行、列和内容以及操作
class Matrix:
def __init__(self,row,column,ele):
self.row = row
self.column = column
self.ele = ele
def Multiply(m1,m2):
if judge(m1,m2) == 1:
print(m1.ele*m2.ele)
else:
print("Can not Multiply!")
def Transpose(m):
print(m.ele.T)
#判断两个矩阵是否可以相乘的函数
def Judge(m1,m2):
if m1.column == m2.row:
return 1
else:
return 0
#创建一个储存矩阵的字典,例如matrixs['2*2'] = [[1,2],[3,4]]
matrixs = {}
#初始化,输入矩阵
print("Please input 16 matrices one by one, for example [[1,2],[3,4]] is a matrix of 2 rows and 2 columns.")
for i in range(2,6):
for j in range(2,6):
print("Please enter the matrix of the {0} row {1} column".format(i,j))
row = i
column = j
ele = mat(eval(input()))
name = str(i) + '*' + str(j)
matrixs[name] = Matrix(row,column,ele)
#菜单,可以进行矩阵的打印、乘法以及转置
while(True):
print("Input:\n1:to Print\n2:to Multiply or Transpose\n3:to Quit")#菜单
op = int(input())
if op == 1:#矩阵的打印
print("Input the matrix you want to output like 2*2")
s = input()
print(matrixs[s].ele)
while(True):#输入 1 继续
print("Input 1 to continue!")
a = int(input())
if a == 1:
break
elif op == 2:#矩阵的运算
print("Input:\n1:to Multiply\n2:to Transpose\n3:to Quit")
b = int(input())
if b == 1:#矩阵的乘法
print("Input the first Matrix")
s1 = input()
print("Input the other Matrix")
s2 = input()
m1 = matrixs[s1]
m2 = matrixs[s2]
Matrix.Multiply(m1,m2)
elif b == 2:#矩阵的转置
print("Input the Matrix")
s = input()
m = matrixs[s]
Matrix.Transpose(m)
elif b == 3:#退出
print("Quit Successfully!")
continue
else:
print("INPUT ERROR!")
elif op == 3:
print("Quit Successfully!")
break
else:
print("INPUT ERROR!")