awsmpshk

N-th power of matrix

Dec 5th, 2021
544
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def multiply_matrixes(first_matrix, second_matrix):
  2.     res_matrix = [[0 for row in range(len(first_matrix))] for col in range(len(second_matrix[0]))]
  3.     for i in range(len(first_matrix)):
  4.         for j in range(len(second_matrix[0])):
  5.             for k in range(len(second_matrix)):
  6.                 res_matrix[i][j] += first_matrix[i][k] * second_matrix[k][j]
  7.     return res_matrix
  8.  
  9. n = int(input())
  10. matrix = []
  11. initial_matrix = []
  12. for _ in range(n):
  13.     row = [float(x) for x in input().split()]
  14.     matrix.append(row)
  15.     initial_matrix.append(row)
  16.  
  17. for _ in range(len(matrix) - 1):
  18.     matrix = multiply_matrixes(initial_matrix, matrix)
  19.  
  20. for line in matrix:
  21.     print(*line)
RAW Paste Data