def calculate_determinant(matrix):
dimension = len(matrix)
if dimension == 1:
return matrix[0][0]
determinant = 0
for col in range(dimension):
sub_matrix = [[0] * (dimension - 1) for _ in range(dimension - 1)]
for i in range(1, dimension):
curr_col = 0
for j in range(dimension):
if j != col:
sub_matrix[i - 1][curr_col] = matrix[i][j]
curr_col += 1
sub_determinant = calculate_determinant(sub_matrix)
determinant += ((-1) ** col) * matrix[0][col] * sub_determinant
return determinant
def get_cofactor(matrix, row, col):
return [row[:col] + row[col + 1:] for row in (matrix[:row] + matrix[row + 1:])]
def calculate_adjoint(matrix):
dimension = len(matrix)
adj = [[0] * dimension for _ in range(dimension)]
for i in range(dimension):
for j in range(dimension):
cofactor = get_cofactor(matrix, i, j)
adj[j][i] = ((-1) ** (i + j)) * calculate_determinant(cofactor)
return adj
def calculate_inverse(matrix):
det = calculate_determinant(matrix)
if det == 0:
print("Singular matrix, can't find its inverse")
return None
dimension = len(matrix)
inv = [[0] * dimension for _ in range(dimension)]
adj = calculate_adjoint(matrix)
for i in range(dimension):
for j in range(dimension):
inv[i][j] = adj[i][j] / det
return inv
def display(matrix):
for row in matrix:
print(" ".join(str(num) for num in row))
def enter_matrix():
dimension = int(input("Enter the dimension of the matrix: "))
matrix = []
for i in range(dimension):
row = []
for j in range(dimension):
while True:
try:
element = int(input(f"Enter element at position a{i+1}{j+1}: "))
break
except ValueError:
print("Invalid input. Please enter a numeric value.")
row.append(element)
matrix.append(row)
return matrix
def gaussian_elimination(matrix):
dimension = len(matrix)
augmented_matrix = []
for i in range(dimension):
row = list(matrix[i]) + [1 if j == i else 0 for j in range(dimension)]
augmented_matrix.append(row)
for i in range(dimension):
pivot_row = i
for j in range(i + 1, dimension):
if abs(augmented_matrix[j][i]) > abs(augmented_matrix[pivot_row][i]):
pivot_row = j
augmented_matrix[i], augmented_matrix[pivot_row] = augmented_matrix[pivot_row], augmented_matrix[i]
pivot = augmented_matrix[i][i]
augmented_matrix[i] = [elem / pivot for elem in augmented_matrix[i]]
for j in range(dimension):
if j != i:
factor = augmented_matrix[j][i]
augmented_matrix[j] = [elem - factor * augmented_matrix[i][idx] for idx, elem in
enumerate(augmented_matrix[j])]
inverse = [row[dimension:] for row in augmented_matrix]
return inverse
while True:
try:
matrix = enter_matrix()
print("\nMatrix:")
display(matrix)
determinant = calculate_determinant(matrix)
print("\nDeterminant:", determinant)
print("\nAdjoint:")
adjoint_matrix = calculate_adjoint(matrix)
display(adjoint_matrix)
print("\nInverse using adjoint_matrix/determinant:")
inverse_matrix = calculate_inverse(matrix)
if inverse_matrix is not None:
display(inverse_matrix)
inverse_matrix_ge = gaussian_elimination(matrix)
for i in range(len(inverse_matrix_ge)):
for j in range(len(inverse_matrix_ge[i])):
inverse_matrix_ge[i][j] = round(inverse_matrix_ge[i][j])
print("Inverse Matrix using gaussian_elimination :")
for row in inverse_matrix_ge:
print(row)
except ValueError:
print("Invalid matrix input. Please enter numeric values only.")