Given a square matrix, determine if the matrix is invertible. A matrix is invertible if and only if its determinant is non-zero. Write a function that takes a square matrix as input and returns true if it is invertible and false otherwise.
An invertible matrix (also known as a non-singular or non-degenerate matrix) is a square matrix that has an inverse. Specifically, a square matrix ( A ) is said to be invertible if there exists another square matrix ( B ) of the same size such that:
$$
A \cdot B = B \cdot A = I
$$
where I is the identity matrix of the same dimension as A.
Input: matrix =[[1,2],[3,4]]Output: falseExplanation: The determinant of the matrix is calculated as(1*4-2*3)=4-6=-2. Since the determinant is not zero, the matrix is not invertible.
Example 2:
1
2
3
4
Input: matrix =[[2,3],[1,4]]Output: true Explanation: The determinant of the matrix is calculated as(2*4-3*1)=8-3=5. Since the determinant is non-zero, the matrix is invertible.
Example 3:
1
2
3
Input: matrix =[[0,1],[1,2]]Output: trueExplanation: The determinant of the matrix is calculated as(0*2-1*1)=0-1=-1. Since the determinant is non-zero, the matrix is invertible.
To determine if a matrix is invertible, we need to check if its determinant is non-zero. The steps are as follows:
Square Matrix Check: First, ensure that the input matrix is square (i.e., it has the same number of rows and columns). If not, the matrix cannot be invertible.
Determinant Calculation: Calculate the determinant of the matrix. For a 2x2 matrix [a, b], [c, d], the determinant is ad - bc. For larger matrices, we recursively calculate the determinant using the cofactor expansion.
Invertibility Check: If the determinant is non-zero, the matrix is invertible. If the determinant is zero, the matrix is not invertible.
classSolution:
defis_matrix_invertible(self, matrix):
n = len(matrix)
if n != len(matrix[0]):
raiseValueError("Matrix must be square")
# Calculate the determinant determinant = self.calculate_determinant(matrix, n)
return determinant !=0# Helper method to calculate the determinantdefcalculate_determinant(self, matrix, n):
if n ==1:
return matrix[0][0]
if n ==2:
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
determinant =0 sign =1for i in range(n):
determinant += sign * matrix[0][i] * self.calculate_determinant(self.get_minor(matrix, 0, i), n -1)
sign =-sign
return determinant
# Helper method to get the minor of a matrixdefget_minor(self, matrix, row, col):
minor = []
for i in range(len(matrix)):
if i != row:
minor.append([matrix[i][j] for j in range(len(matrix[i])) if j != col])
return minor
# Example usagesolution = Solution()
matrix1 = [
[1, 2],
[3, 4]
]
print(solution.is_matrix_invertible(matrix1)) # Output: Falsematrix2 = [
[2, 3],
[1, 4]
]
print(solution.is_matrix_invertible(matrix2)) # Output: Truematrix3 = [
[0, 1],
[1, 2]
]
print(solution.is_matrix_invertible(matrix3)) # Output: True
⏰ Time complexity: O(n!). The time complexity of calculating the determinant of an n x n matrix can be high (O(n!) using cofactor expansion), which may be impractical for large matrices.