Problems
Loading...
1 / 1

Matrix Inverse

Linear Algebra
Medium

Compute the inverse of a square, non-singular matrix A. Return None for non-square or singular matrices.

Mathematical Definition

Matrix Inverse:

AA1=A1A=IA A^{-1} = A^{-1} A = I

where 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
Loading visualization...

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 None if singular or invalid)
  • Use NumPy functions like np.linalg.inv()
  • Result must satisfy AA1I<107\| A A^{-1} - I \| < 10^{-7}

Constraints

  • 1 ≤ n ≤ 512
  • NumPy only
  • Time limit: 300ms
Try Similar Problems