Skip to content

Instantly share code, notes, and snippets.

View MeherajUlMahmmud's full-sized avatar

Meheraj MeherajUlMahmmud

View GitHub Profile
def ones(rows, cols):
matrix = [[1 for i in range(cols)] for j in range(rows)]
return matrix
mat = ones(3, 3)
def zeros(rows, cols):
matrix = [[0 for i in range(cols)] for j in range(rows)]
return matrix
mat = zeros(3, 3)
def calcDeterminant(matrix):
det = 0
for c in range(len(matrix)):
det += matrix[0][c] * calcMinor(matrix, 0, c)
return det
def calcMinor(matrix, r, c):
minor = (matrix[r-2][c-2] * matrix[r-1][c-1]) - (matrix[r-1][c-2] * matrix[r-2][c-1])
return minor
def calcMinor(matrix, r, c):
minor = (matrix[r-2][c-2] * matrix[r-1][c-1]) - (matrix[r-1][c-2] * matrix[r-2][c-1])
return minor
matrix = [[1, 2, 3],
[0, 1, 4],
[5, 6, 0]]
def calcCofactorMatrix(matrix):
cofactor_matrix = [[float(calcMinor(matrix, r, c)) for c in range(3)] for r in range(3)]
return cofactor_matrix
def inverseUtil(matrix, det):
inverse_matrix = [[matrix[r][c] * (1 / det) for c in range(3)] for r in range(3)]
return inverse_matrix
def calcDeterminant(matrix):
det = 0
for c in range(len(matrix)):
det += matrix[0][c] * calcMinorMeheraj(matrix, 0, c)
return det