Implementing kernel
C++ engine symbols that back this Python API.Compute the multiplicative inverse of a square matrix.
Returns the unique matrix such that
Inversion is performed via LU decomposition with partial pivoting.
When the goal is to apply to a known right-hand side,
prefer solve — explicit inversion is both slower and less
numerically stable than back-substitution.
Parameters
(*, n, n) (batch dims allowed). Must
be non-singular; raises a runtime error on singular input.Returns
TensorInverse matrix of shape (*, n, n) with the same dtype as
A.
Raises
DtypeMismatchA is a bool or integer tensor, an empty one included.Notes
Internally computes and then solves LU X = P for
X via two triangular sweeps. Cost is 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]])