Skip to content
fn

inv

→Tensor
inv(A: Tensor)

Implementing kernel

C++ engine symbols that back this Python API.

Compute the multiplicative inverse of a square matrix.

Returns the unique matrix A−1A^{-1} such that

AA−1=A−1A=IA A^{-1} = A^{-1} A = I

Inversion is performed via LU decomposition with partial pivoting. When the goal is to apply A−1A^{-1} to a known right-hand side, prefer solve — explicit inversion is both slower and less numerically stable than back-substitution.

Parameters

Square matrix of shape (*, n, n) (batch dims allowed). Must be non-singular; raises a runtime error on singular input.

Returns

Tensor

Inverse matrix of shape (*, n, n) with the same dtype as A.

Raises

DtypeMismatch
If A is a bool or integer tensor, an empty one included.

Notes

Internally computes PA=LUPA = LU and then solves LU X = P for X via two triangular sweeps. Cost is O(n3)O(n^3) per matrix. For ill-conditioned A consider pinv (SVD-based) for a more robust pseudo-inverse.

A floating matrix with a zero dimension is returned unchanged rather than handed to the factorisation: the 0x0 matrix is its own inverse, and LAPACK's behaviour at n = 0 is not uniform across the implementations this runs against.

Examples

>>> import lucid
>>> from lucid.linalg import inv
>>> A = lucid.tensor([[4.0, 7.0], [2.0, 6.0]])
>>> inv(A)
tensor([[0.6, -0.7], [-0.2, 0.4]])

Used by 3