#this is a project which calculate two cbue's multiply
class Cube (object):
def __init__(self):
self.cube = []
self.line = int(raw_input('please input the number of lines:'))
self.row = int(raw_input('please input the number of row:'))
def value(self):
tmp = []
ltmp =0
rtmp =0
for r in range (0,self.row):
for l in range (0,self.line):
tmp.append(int(raw_input('input %d row %d line value: '% (rtmp,ltmp))))
ltmp = ltmp+1
self.cube.append(tmp)
rtmp =rtmp+1
tmp = []
ltmp =0
def show(self):
print '%d row %d line: '% (self.row,self.line)
print 'cube is :'
print self.cube
def multiply (self,other):
if self.line != other.row :
print 'can\'t do multiply!'
else :
cub = []
result = []
tmp = []
for a in range (0,self.row):
for b in range (0,other.line):
for x in range (0,self.line):
result.append(self.cube[a][x]*other.cube[x][b])
tmp.append(sum(result))
result =[]
cub.append(tmp)
tmp=[]
print 'the result of multiply is :'
print cub
cube1 = Cube()
cube1.value()
cube1.show()
cube2 = Cube()
cube2.value()
cube2.show()
cube1.multiply(cube2)
首先我定义了一个Cube矩阵类:
Cube类里的第一个方法:__init__()
初始化矩阵大小
Cube类里的第二个方法:value()
对矩阵按先行后列方法进行赋值
Cube类里的第三个方法:multiply()
对两个矩阵进行相乘求结果
参数为乘数矩阵