Matrix Inverse With np.linalg.inv

Quick Overview

Write a NumPy function that inverts a square, invertible matrix with np.linalg.inv and verifies the result against the identity within floating-point tolerance. It tests how NumPy computes an inverse, when to solve a linear system instead, and how to handle singular or ill-conditioned matrices.

Matrix Inverse With np.linalg.inv

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `inverse(A)` that returns the inverse of an invertible square NumPy array `A` using `np.linalg.inv`. The result must satisfy `A @ inverse(A)` equal to `np.eye(n)` within floating-point tolerance, as checked by `np.allclose(A @ inverse(A), np.eye(n))`. For example: ```python A = np.array([[4., 7.], [2., 6.]]) inverse(A) # array([[ 0.6, -0.7], # [-0.2, 0.4]]) ``` ```hint Verifying the result An exact comparison with `np.eye(n)` fails for most inputs. Think about where the small errors come from before you decide how much to trust the output. ``` ### Constraints & Assumptions - `A` is square (n x n) and invertible. - The entries are real numbers. ### What a Strong Answer Covers - How the input is converted and what dtype the result has. - How to verify the result against the identity, and why a tolerance is needed. - How NumPy computes the inverse and what that costs for an n x n matrix. - What happens when `A` is singular or close to singular. ### Follow-up Questions - When should you prefer `np.linalg.solve` over computing an explicit inverse? - How would you handle a singular or near-singular `A`? - You need to solve systems with the same `A` for many right-hand sides that arrive one at a time. What would you compute once and reuse?

Overview: Write a NumPy function that inverts a square, invertible matrix with np.linalg.inv and verifies the result against the identity within floating-point tolerance. It tests how NumPy computes an inverse, when to solve a linear system instead, and how to handle singular or ill-conditioned matrices.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write inverse(A) that returns the inverse of an invertible square NumPy array A using np.linalg.inv. The result must satisfy A @ inverse(A) equal to np.eye(n) within floating-point tolerance, as checked by np.allclose(A @ inverse(A), np.eye(n)).

For example:

A = np.array([[4., 7.], [2., 6.]])
inverse(A)
# array([[ 0.6, -0.7],
#        [-0.2,  0.4]])

Constraints & Assumptions

  • A is square (n x n) and invertible.
  • The entries are real numbers.

What a Strong Answer Covers Guidance

  • How the input is converted and what dtype the result has.
  • How to verify the result against the identity, and why a tolerance is needed.
  • How NumPy computes the inverse and what that costs for an n x n matrix.
  • What happens when A is singular or close to singular.

Follow-up Questions Guidance

  • When should you prefer np.linalg.solve over computing an explicit inverse?
  • How would you handle a singular or near-singular A ?
  • You need to solve systems with the same A for many right-hand sides that arrive one at a time. What would you compute once and reuse?