Neural Mastery
← Practice
Matrix Inverse via Gauss-Jordan Eliminationmedium

Matrix Inverse via Gauss-Jordan Elimination

medium Math for ML 15-20 min

NumPy allowed · plain Python earns 10 bonus points

Linear algebra step 1/4

A vector transforms

Matrix multiplication is the first computation behind every model.
Open lesson →
01vector
02matrix
03transform
04result
[2, −1] × [[1, 2], [−.5, 1]] = [0, −2]

Computed matrix × vector transformation

Invert a square matrix using row operations and partial pivoting.

Related Skills

Task

Implement matrix_inverse(A): augment A with the identity matrix [A | I] and perform Gauss-Jordan elimination with partial pivoting. If at any step the maximal pivot element is < 1e-9, return None. Otherwise return the inverse matrix with entries rounded to 4 decimals.

Function Signature

matrix_inverse(A: list[list[float]]) -> list[list[float]] | None

Examples

Example 1: 2x2 Invertible
Input: A = [[4, 7], [2, 6]]
Output: [[0.6, -0.7], [-0.2, 0.4]]
Explanation: Standard 2x2 inverse.
Example 2: 2x2 Singular
Input: A = [[1, 2], [2, 4]]
Output: None
Explanation: Singular matrix returns None.

Constraints

  • A is square n x n with 1 <= n <= 10.
Python3Saved ✓
def matrix_inverse(A: list[list[float]]) -> list[list[float]] | None:
"""Compute inverse of square matrix A using Gauss-Jordan elimination with partial pivoting.
Return None if singular (pivot < 1e-9). Round values to 4 decimals."""
# Your implementation here
pass

Case 1: 2x2 Invertible
Input: A = [[4, 7], [2, 6]]
Expected: [[0.6, -0.7], [-0.2, 0.4]]
Case 2: 2x2 Singular
Input: A = [[1, 2], [2, 4]]
Expected: None