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

SciPy · Function

linalg.inv

Computes the inverse of a square matrix and returns it as an ndarray. It validates finite inputs by default and reports invalid shapes or singular matrices through exceptions.

How it works

The function computes a square matrix inverse and returns it as an ndarray. By default it preserves the input, validates that values are finite, and raises an error when the matrix is singular or has an invalid shape.

Syntax

scipy.linalg.inv(a, overwrite_a=False, check_finite=True)

Parameters

NameTypeDescription
aarray_likeSquare matrix to be inverted.
overwrite_abool, optionalWhether to discard data in a; may improve performance. Default: False.
check_finitebool, optionalWhether to check that the input matrix contains only finite numbers. Default: True.

Returns

ainv ndarray — Inverse of the matrix a.

Key points

  • Use this function to obtain the matrix inverse.
  • The required argument is an array-like square matrix.
  • Set overwrite_a to permit discarding input data when a possible performance gain matters.
  • check_finite determines whether SciPy validates that every input value is finite.
  • The result is the inverse of the supplied matrix and has ndarray type.

Common mistakes

  • Turning off finite-value checking can improve speed, but NaNs or infinities may cause crashes or non-termination.
  • The input must be a two-dimensional square matrix; otherwise a ValueError is raised.

Example: Invert and verify a matrix

import numpy as np
from scipy import linalg

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

Computes the inverse and verifies that multiplying the matrix by it produces the identity matrix.

When to use it

Obtain a matrix inverse

You need the explicit matrix that reverses a square linear transformation.

Check an inverse by multiplication

You want to verify an inversion numerically.

Interview questions

QWhat does scipy.linalg.inv return?

It accepts an array-like square matrix and returns its inverse as an ndarray. The input is not overwritten by default, and finite-value checking is enabled by default.

Follow-up: What happens for a singular matrix? SciPy raises LinAlgError, so callers should handle that case or avoid explicitly forming the inverse when solving a linear system.

QWhat are the two optional parameters?

overwrite_a controls whether the input data may be discarded, potentially improving performance; its default is False. check_finite controls validation of finite values and defaults to True.

Follow-up: When might you disable check_finite? Only when inputs are known to contain finite values and the performance trade-off is justified, because invalid values can cause severe failures.

QWhich errors can the function raise?

A non-square or non-2D input raises ValueError, while a singular matrix raises LinAlgError. These checks distinguish invalid shape from a matrix without an inverse.

Follow-up: How can the result be checked? Multiply the original matrix by the returned array and compare the product with the identity matrix.

  • scipy.linalg.solve
  • scipy.linalg.det
  • scipy.linalg.pinv

Source: inv#. Checked 2026-10-15.