xxxxxxxxxx
def determinant_3x3(matrix):
a, b, c = matrix[0]
d, e, f = matrix[1]
g, h, i = matrix[2]
determinant = a * (e*i - f*h) - b * (d*i - f*g) + c * (d*h - e*g)
return determinant
# Example matrix
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
determinant = determinant_3x3(matrix)
print("Determinant of the matrix is:", determinant)
xxxxxxxxxx
import numpy as np
arr=np.array([[1,2,3],[4,5,6],[7,8,9]])
arr
round(np.linalg.det(arr))
xxxxxxxxxx
The determinant of a nxn matrix can be found using Laplace's method.
Select a column or row (j) to traverse, then calculate the determinant.
The formula is the same, it's just swapping i and j as variables
If you selected a row, then it is:
sum(i=1, n, (-1)^i * a_ij * det(C_ij))
If you chose a column, then:
sum(j=1, n, (-1)^i * a_ij * det(C_ij))
In which:
a_ij means the element on the i-th row and j-th column of the matrix A
C_ij is the cofactor matrix, a matrix equal to A without the i-th row and j-th column
xxxxxxxxxx
def determinant_3x3(matrix):
if len(matrix) != 3 or len(matrix[0]) != 3:
raise ValueError("Input matrix should be 3x3")
determinant = matrix[0][0] * (matrix[1][1]*matrix[2][2] - matrix[1][2]*matrix[2][1])
determinant -= matrix[0][1] * (matrix[1][0]*matrix[2][2] - matrix[1][2]*matrix[2][0])
determinant += matrix[0][2] * (matrix[1][0]*matrix[2][1] - matrix[1][1]*matrix[2][0])
return determinant
# Example usage:
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(determinant_3x3(matrix))