sidrs

FDS (KR) - Ass 2 (Matrix Operations)

Aug 7th, 2024 (edited)
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. ##################
  2. ###  MATRICES  ###
  3. ##################
  4.  
  5. # Function for Initializing a Matrix
  6. def initMatrix():
  7.     rows = int(input("Enter the number of Rows: "))
  8.     cols = int(input("Enter the number of Columns: "))
  9.     matrix = []
  10.     for i in range(0, rows):
  11.         row = []
  12.         for j in range(0, cols):
  13.             element = int(input("Enter the element at position (" + str(i) + ", " + str(j) + "): "))
  14.             row.append(element)
  15.         matrix.append(row)
  16.     return matrix
  17.  
  18.  
  19. # Function for Displaying a Matrix
  20. def displayMatrix(matrix):
  21.         for i in range(0,len(matrix)):
  22.             print()
  23.             for j in range(0, len(matrix[0])):
  24.                 print(str(matrix[i][j]).rjust(2), end = " ")
  25.         print()
  26.         print()
  27.  
  28.  
  29. # Function for Adding two Matrices
  30. def add(matrixA, matrixB):
  31.     sum_matrix = []
  32.     if ( (len(matrixA) == len(matrixB)) and (len(matrixA[0]) == len(matrixB[0])) ):
  33.         for i in range(0, len(matrixA)):
  34.             row_sums = []
  35.             for j in range(0, len(matrixA[0])):
  36.                 row_sums.append((matrixA[i][j] + matrixB[i][j]))
  37.             sum_matrix.append(row_sums)
  38.         return sum_matrix
  39.     else:
  40.         print("ERROR! The Matrix Dimensions DO NOT MATCH")
  41.         return -1
  42.  
  43.    
  44. # Function for Subtracting two Matrices            
  45. def subtract(matrixA, matrixB):
  46.     sum_matrix = []
  47.     if ( (len(matrixA) == len(matrixB)) and (len(matrixA[0]) == len(matrixB[0])) ):
  48.         for i in range(0, len(matrixA)):
  49.             row_sums = []
  50.             for j in range(0, len(matrixA[0])):
  51.                 row_sums.append((matrixA[i][j] - matrixB[i][j]))
  52.             sum_matrix.append(row_sums)
  53.         return sum_matrix
  54.     else:
  55.         print("ERROR! The Matrix Dimensions DO NOT MATCH")
  56.         return -1
  57.  
  58.    
  59. # Function for Initializing a Null Matrix with Given Dimensions            
  60. def initNull(rows, cols):
  61.     null_matrix = []
  62.     for i in range(rows):
  63.         null_row = []
  64.         for j in range(cols):
  65.             null_row.append(0)
  66.         null_matrix.append(null_row)    
  67.     return null_matrix
  68.  
  69.  
  70. # Function for Multiplying two Matrices            
  71. def multiply(mA, mB):
  72.     if (len(mA[0]) == len(mB)):
  73.         result = initNull(len(mA[0]), len(mB))
  74.         prod_matrix = []
  75.         for i in range(0, len(mA)):
  76.             row_prods = []
  77.             for j in range(0, len(mB[0])):
  78.                 term = 0
  79.                 for k in range(0, len(mA[0])):
  80.                     term += mA[i][k] * mB[k][j]
  81.                 result[i][j] = term
  82.         return result
  83. #         ???
  84. #         for i in range(0, range(len(mA))):
  85. #             row_prods = []
  86. #             for j in range(0, range(len(mA[0]))):
  87. #                 element = (mA[i][j] * mB[j][i])
  88.     else:
  89.         print("ERROR! Invalid Dimensions")
  90.         return -1
  91.    
  92.  
  93. # Function of obtaining Transpose of a Matrix
  94. def transpose(mA):
  95.     ok = mA
  96.     null_matrix = initNull(len(mA), len(mA[0]))
  97.     for i in range(len(mA)):
  98.         for j in range (len(mA[0])):
  99.              null_matrix[j][i] = mA[i][j]
  100.     return null_matrix
  101.  
  102.  
  103.  
  104. print("Initializing Matrices")
  105. A = initMatrix()
  106. B = initMatrix()
  107. # displayMatrix(A)
  108. # displayMatrix(B)
  109.  
  110. print("Addition: ")
  111. Sum = add(A, B)
  112. displayMatrix(Sum)
  113.  
  114. print("Subtraction: ")
  115. Difference = subtract(A, B)
  116. displayMatrix(Difference)
  117.  
  118. print("Multiplication: ")
  119. Multiplication = multiply(A, B)
  120. displayMatrix(Multiplication)
  121.  
  122. print("Transpose: ")
  123. Transposed = transpose(A)
  124. displayMatrix(Transposed)
Advertisement
Add Comment
Please, Sign In to add comment