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
x.
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 matrix with a zero dimension is returned unchanged rather than
handed to the factorisation. The 0x0 matrix is its own inverse — it
is the identity on the zero-dimensional space — so nothing is lost,
and LAPACK's behaviour at n = 0 is not uniform across the
implementations this runs against: the same call is harmless on one
Accelerate build and a segmentation fault on another. Guarding here
makes the answer depend on the mathematics rather than on which
machine is asking.
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]])