The document defines functions for generating random matrices, multiplying matrices, adding matrices, subtracting matrices, and displaying matrices. It then generates two random 3x3 matrices X and Y, calculates the product matrix Z of X and Y, the sum matrix P of X and Y, and the difference matrix M of X and Y, and displays the results.
The document defines functions for generating random matrices, multiplying matrices, adding matrices, subtracting matrices, and displaying matrices. It then generates two random 3x3 matrices X and Y, calculates the product matrix Z of X and Y, the sum matrix P of X and Y, and the difference matrix M of X and Y, and displays the results.
The document defines functions for generating random matrices, multiplying matrices, adding matrices, subtracting matrices, and displaying matrices. It then generates two random 3x3 matrices X and Y, calculates the product matrix Z of X and Y, the sum matrix P of X and Y, and the difference matrix M of X and Y, and displays the results.
def rand_matrix(n,m): A = [[randint(0,9) for j in range(m)] for i in range(n)] return A def mult_matrix(A,B): n = len(A) C = [[0 for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): for k in range(n): C[i][j] += A[i][k]*B[k][j] return C def show_matrix(A): for a in A: for b in a: print(b, end=" ") print() def plus_matrix(A,B): n = len(A) C = [[0 for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): C[i][j] +=A[i][j]+B[i][j] return C def minus_matrix(A,B): n = len(A) C = [[0 for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): C[i][j] +=A[i][j]-B[i][j] return C seed(2014) X = rand_matrix(3,3); Y = rand_matrix(3,3) print("Список X: ", X); print("Первая матрица"); show_matrix(X) print("Список Y: ", Y); print("Вторая матрица"); show_matrix(Y) Z = mult_matrix(X,Y) P = plus_matrix(X,Y) M = minus_matrix(X,Y) print("Список Z: ", Z); print("Матрица X*Y"); show_matrix(Z) print("Список P: ", P); print("Матрица X+Y"); show_matrix(P) print("Список M: ", M); print("Матрица X-Y"); show_matrix(M)