Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.linalg.inv

Computes the inverse of a square matrix, returning a result that multiplies with the input to form the identity. It raises LinAlgError when the input is nonsquare or inversion fails.

How it works

The function accepts a square matrix, including batches whose final two dimensions are square, and returns an inverse with matching matrix dimensions. The inverse is the array that produces the identity when multiplied with the input in either order; failure to invert raises LinAlgError.

Syntax

numpy.linalg.inv(a)

Parameters

NameTypeDescription
a(..., M, M) array_likeMatrix to be inverted; expected shape (..., M, M).

Returns

ainv (..., M, M) ndarray or matrix: inverse of the matrix a.

Key points

  • For a square matrix, it produces the multiplicative inverse.
  • The result is defined by identity-matrix multiplication on both sides.
  • The input can be any array-like object whose final two dimensions form an M×M matrix.
  • The output preserves the matrix dimensions and is returned as an ndarray or matrix.
  • Singularity is explicitly checked as a condition that prevents inversion.

Common mistakes

  • Singular matrices cannot be inverted; handle numpy.linalg.LinAlgError when the input has no inverse.
  • The operation also raises LinAlgError for nonsquare input or another inversion failure.

Example: Invert a square matrix

import numpy as np

A = np.array([[1.0, 2.0], [3.0, 4.0]])
A_inv = np.linalg.inv(A)

print(np.allclose(A @ A_inv, np.eye(2)))
print(np.allclose(A_inv @ A, np.eye(2)))

Computes the inverse and verifies both multiplication orders produce the 2×2 identity matrix.

When to use it

Matrix inversion

Recover an inverse when a square matrix is known to be nonsingular.

Batched inversion

Process several matrices together when each item occupies the final two axes.

Numerical validation

Verify an inversion numerically by checking products against an identity matrix.

Interview questions

QWhat does numpy.linalg.inv compute?

The function returns an array ainv such that A @ ainv and ainv @ A equal the identity matrix. Its input must have square final dimensions.

Follow-up: How can you validate the result? Check both multiplication orders against np.eye using np.allclose, while also handling LinAlgError.

QWhat shapes does inv accept and return?

The parameter has shape (..., M, M), so the input can represent one square matrix or a stack of square matrices. The returned value has shape (..., M, M).

Follow-up: What shape constraint applies to each item in a batch? Every matrix represented by the final two axes must be M by M.

QWhich errors can inv raise?

It raises numpy.linalg.LinAlgError when the input is not square or inversion fails. In particular, a detected singular matrix causes this exception.

Follow-up: What should code do for a singular matrix? Use a try/except around inv or choose a method suited to the problem, such as a solve operation when solving a system.

  • numpy.linalg.solve
  • numpy.linalg.pinv
  • numpy.linalg.det
  • numpy.linalg.slogdet
  • numpy.linalg.matrix_power

Source: numpy.linalg.inv — NumPy v2.4 Manual. Checked 2026-10-15.