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

NumPy · Function

np.linalg.det

Computes determinants for square arrays, including stacks of matrices. Each trailing M×M matrix yields one determinant.

How it works

Pass a square array to compute its determinant. For arrays with leading dimensions, NumPy treats each trailing M×M slice as a separate matrix and returns the corresponding determinants; internally, the calculation uses LU factorization.

Syntax

numpy.linalg.det(a)

Parameters

NameTypeDescription
a(…, M, M) array_likeInput array to compute determinants for.

Returns

det(…) array_like — determinant of a.

Key points

  • Use numpy.linalg.det to calculate the determinant of an array.
  • The input may have leading dimensions followed by two equal matrix dimensions.
  • The function returns the determinant corresponding to each input matrix.
  • A multidimensional input can represent a stack, with one result per square matrix.
  • For a single two-dimensional matrix, the function computes one determinant value.

Common mistakes

  • The final two dimensions must describe square matrices; each leading slice is processed independently.
  • Determinants are computed numerically through LU factorization, so results can be affected by floating-point arithmetic.

Example: Determinants for a matrix stack

import numpy as np

matrices = np.array([
    [[1, 2], [3, 4]],
    [[1, 2], [2, 1]],
])

print(np.linalg.det(matrices))  # [-2. -3.]

A stack of two 2×2 matrices produces one determinant for each matrix.

When to use it

Matrix invertibility checks

Assess whether a square matrix is singular or nearly singular by examining whether its determinant is near zero.

Batched matrix processing

Compute one determinant per matrix when processing batched linear-algebra inputs.

Transformation analysis

Measure signed scaling or orientation effects represented by a transformation matrix.

Interview questions

QWhat shapes does numpy.linalg.det accept, and what shape does it return?

The input is an array whose last two dimensions are M×M. Any preceding dimensions represent a stack of matrices, and the output contains one determinant per matrix.

Follow-up: How would you avoid overflow or underflow for very large or small determinants? Use numpy.linalg.slogdet, which returns the sign and natural logarithm of the determinant.

QHow does det behave on a stack of matrices?

For a two-dimensional matrix, the result is a scalar-like determinant value. For a stack, the result has the same leading dimensions as the input, because each M×M matrix contributes one value.

Follow-up: What related function provides logarithmic scaling information? numpy.linalg.slogdet returns both the determinant sign and its natural logarithm.

QHow is the determinant computed internally?

NumPy computes the determinant using LU factorization backed by the LAPACK z/dgetrf routine. This supports both real and complex matrix inputs.

Follow-up: Which external-library function is documented as similar? scipy.linalg.det is listed as a similar function.

  • numpy.linalg.slogdet
  • scipy.linalg.det

Source: numpy.linalg.det. Checked 2026-10-15.