fn

inv

Tensor
inv(x: Tensor)
source

Compute the multiplicative inverse of a square matrix.

Returns the unique matrix A1A^{-1} such that

AA1=A1A=IA A^{-1} = A^{-1} A = I

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

Parameters

xTensor
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 x.

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.

Examples

>>> import lucid
>>> from lucid.linalg import inv
>>> A = lucid.tensor([[4.0, 7.0], [2.0, 6.0]])
>>> inv(A)
Tensor([[ 0.6000, -0.7000],
        [-0.2000,  0.4000]])