ClavinJune

matrix production

Apr 24th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. #predefined var
  2. first = None
  3. second = None
  4. result = None
  5. col_ = 0
  6. row = 0
  7. #class
  8. class Matrix:
  9.     def __init__(self, row, col):
  10.         self.row=row
  11.         self.col=col
  12.         self.matrix = [[[]for i in range(col)] for i in range(row)]
  13.    
  14.     def create(self):
  15.         for i in range(0, self.row):
  16.             for j in range(0, self.col):
  17.                 self.matrix[i][j] = input("Input row %d & col %d : " %(i+1, j+1))
  18.  
  19.     def view(self):
  20.         for i in range(0, self.row):
  21.             for j in range(0, self.col):
  22.                 print "%d" %(self.matrix[i][j]),
  23.             print ""
  24.  
  25. def firstMatrix():
  26.     print "First Matrix"
  27.     row_ = input("Row ? ")
  28.     global col_
  29.     col_ = input("Col ? ")
  30.     global first
  31.     first = Matrix(row_,col_)
  32.     first.create()
  33.  
  34. def secondMatrix():
  35.     print "Second Matrix"
  36.     global row
  37.     while row != col_:
  38.         row = input("Row ? ")
  39.     col = input("Col ? ")
  40.     global second
  41.     second = Matrix(row,col)
  42.     second.create()
  43.  
  44. def doMultiply():
  45.     global result
  46.     result = Matrix(first.row, second.col)
  47.     for i in range(0, result.row):
  48.         for j in range(0, result.col):
  49.             result.matrix[i][j] = 0
  50.     for i in range(0,first.row):
  51.         for j in range(0,second.col):
  52.             for k in range(0,second.row):
  53.                 result.matrix[i][j] += first.matrix[i][k] * second.matrix[k][j]
  54.  
  55. print "\n"*25
  56. firstMatrix()
  57. print "_"*80,
  58. secondMatrix()
  59. doMultiply()
  60. print "_"*80
  61. result.view()
Add Comment
Please, Sign In to add comment