# Sample Code ## This file implements common matrix operations `# Packages import numpy as np import random` ``` def NMatrix(N): """ This functions generates an N*N size matrix with randome numbers """ Parameters ---------- N dimension of the matrix to create Returns ------- 2D array of the N * N matrix """ # Elements in the matrix are normally distributed with mean 0 # and standard deviation 1 matrix = [[random.normalvariate(0, 1) for _ in range(N)] for _ in range(N)] print(matrix) return matrix ``` ``` def NMatrix(N): """ This functions uses numpy to generate an N*N size matrix with randome numbers Parameters ---------- N dimension of the matrix to create Returns ------- 2D array of the N * N matrix """ # Elements in the matrix are normally distributed with mean 0 # and standard deviation 1 matrix = np.random.normal(0, 1, size = (N, N)) matrix = matrix.tolist() print(matrix) return matrix ``` ``` def addMatricesNP(A, B): """ This function performs 2 matrices addition Passed in matrices are all valid matrices Parameters ---------- A The first matrix B The second matrix Returns ------- Sum Matrix when two matrices have compatible size -1 otherwise """ # Check whether 2 matrices have same size if len(A) != len(B): return -1 if len(A) and len(A[0]) != len(B[0]): return -1 rowNum = len(A) colNum = len(A[0]) res = [[0 for _ in range(colNum)] for _ in range(rowNum)] for i in range(rowNum): for j in range(colNum): res[i][j] = A[i][j] + B[i][j] return res ``` ``` def addMatricesNP(A, B): """ This function performs 2 matrices addition with numPy Passed in matrices are all valid matrices Parameters ---------- A The first matrix B The second matrix Returns ------- Sum Matrix when two matrices have compatible size -1 otherwise """ # Convert to numPy array A = np.array(A) B = np.array(B) # Check whether size compatibility if A.shape != B.shape: return -1 res = (A + B).tolist() return res ``` ``` def dotProduct(A, B): """ Calculate dot product of two vectors Parameters ---------- A The first vector B The second vector Returns ------- Dot product when two vectors have the same size -1 otherwise """ if len(A) != len(B): return -1 dotProduct = 0 for i in range(len(A)): dotProduct += A[i] * B[i] return dotProduct ``` ``` def dotProductNP(A, B): """ Calculate dot product of two vectors using NumPy Parameters ---------- A The first vector B The second vector Returns ------- Dot product when two vectors have the same size -1 otherwise """ A = np.array(A) B = np.array(B) if A.shape != B.shape: return -1 dotProduct = np.dot(A, B).tolist() return dotProduct ``` ``` def matrixProduct(A, B): """ Calculate matrix vector product of two matrices Parameters ---------- A The first matrix B The second matrix Returns ------- Matrix vector product when the column number of A equals row number of B otherwise -1 """ rowA, colA = len(A), len(A[0]) rowB, colB = len(B), len(B[0]) if colA != rowB: return -1 res = [[0 for _ in range(colB)] for _ in range(rowA)] for i in range(rowA): for j in range(colB): for k in range(colA): res[i][j] += A[i][k] * B[k][j] return res ``` ``` def matrixProductNP(A, B): """ Calculate matrix vector product of two matrices with NumPy Parameters ---------- A The first matrix B The second matrix Returns ------- Matrix vector product when the column number of A equals row number of B otherwise -1 """ A = np.array(A) B = np.array(B) if A.shape[1] != B.shape[0]: return -1 res = np.dot(A, B).tolist() return res ``` ``` def transpose(matrix): """ Obtain the transpose of the given matrix Parameters ---------- matrix Input matrix to be transposed Returns ------- The transposed matrix """ res = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))] return res ``` ``` def transposeNP(matrix): """ Obtain the transpose of the given matrix with NumPy Parameters ---------- matrix Input matrix to be transposed Returns ------- The transposed matrix """ res = np.array(matrix).T.tolist() return res ```