Matrix Inverse
Matrix Inverse
Compute the inverse of a square, non-singular matrix A. Return None for non-square or singular matrices.
Mathematical Definition
Matrix Inverse:
AA−1=A−1A=Iwhere I is the identity matrix and A−1 exists only if det(A)≠0.
Function Arguments
A: 2D NumPy array, shape (n, n)- square matrix to invert
Examples
Input: A = [[1, 2], [3, 4]]
Output: shape = (2, 2), A_inv ≈ [[-2, 1], [1.5, -0.5]]
Input: A = [[2.0]]
Output: shape = (1, 1), A_inv = [[0.5]]
Hint 1
Check for 2D and square matrix using A.ndim == 2 and A.shape[0] == A.shape[1].
Hint 2
Check if matrix is singular by computing np.linalg.det(A) and comparing with a small threshold like 1e-10.
Requirements
- Validate input is 2D and square
- Check if matrix is singular (return
Noneif singular or invalid) - Use NumPy functions like
np.linalg.inv() - Result must satisfy ∥AA−1−I∥<10−7
Constraints
- 1 ≤ n ≤ 512
- NumPy only
- Time limit: 300ms
Log in to take notes on this problem
Accepts: array
Matrix Inverse
Matrix Inverse
Compute the inverse of a square, non-singular matrix A. Return None for non-square or singular matrices.
Mathematical Definition
Matrix Inverse:
AA−1=A−1A=Iwhere I is the identity matrix and A−1 exists only if det(A)≠0.
Function Arguments
A: 2D NumPy array, shape (n, n)- square matrix to invert
Examples
Input: A = [[1, 2], [3, 4]]
Output: shape = (2, 2), A_inv ≈ [[-2, 1], [1.5, -0.5]]
Input: A = [[2.0]]
Output: shape = (1, 1), A_inv = [[0.5]]
Hint 1
Check for 2D and square matrix using A.ndim == 2 and A.shape[0] == A.shape[1].
Hint 2
Check if matrix is singular by computing np.linalg.det(A) and comparing with a small threshold like 1e-10.
Requirements
- Validate input is 2D and square
- Check if matrix is singular (return
Noneif singular or invalid) - Use NumPy functions like
np.linalg.inv() - Result must satisfy ∥AA−1−I∥<10−7
Constraints
- 1 ≤ n ≤ 512
- NumPy only
- Time limit: 300ms
Log in to take notes on this problem
Accepts: array