torch.linalg.tensorinv
function tensorinv<S extends Shape, D extends DType, Dev extends DeviceType>(A: Tensor<S, D, Dev>, options?: TensorinvOptions): Tensor<DynamicShape, D, Dev>function tensorinv<S extends Shape, D extends DType, Dev extends DeviceType>(A: Tensor<S, D, Dev>, ind: number, options?: TensorinvOptions): Tensor<DynamicShape, D, Dev>Computes the multiplicative inverse of a tensor with respect to tensordot operation.
Computes the inverse of a tensor in the sense that if X = tensorinv(A, ind), then
tensordot(A, X, dims=[[i for i in range(ind, A.ndim)], [0, ind)]) equals a reshaped identity matrix.
This is useful for solving tensor equations and generalizes matrix inversion to higher-order tensors.
Essential for:
- Solving multi-linear systems and tensor equations
- Inverting multi-linear transformations
- Tensor decomposition and analysis
- Higher-order generalizations of linear algebra
Core idea: The ind parameter splits the tensor shape into two parts: the first ind dimensions and the remaining dimensions. The tensor is reshaped into a matrix, inverted, and reshaped back with dimensions permuted. Requires the first ind dimensions to have the same product as the remaining dimensions for inversion to be possible.
How it works:
- Split tensor shape at index
ind: shape = shape[:ind] + shape[ind:] - Compute prod1 = product of first ind dimensions
- Compute prod2 = product of remaining dimensions
- Reshape to [prod1, prod2] matrix
- Compute matrix inverse
- Reshape result back to [shape[ind:] + shape[:ind]]
Requires: The two products must be equal (prod1 == prod2) for inversion to exist. The reshaped matrix must be invertible (full rank, non-singular).
- Dimension permutation: Output shape has dimensions from shape[ind:] followed by shape[:ind]
- Invertibility requirement: The reshaped matrix must be invertible (full rank, non-zero determinant)
- Dimension product equality: prod(A.shape[:ind]) must equal prod(A.shape[ind:])
- Index range: ind must satisfy 1 = ind A.ndim
- Numerical stability: Uses standard matrix inversion (can be ill-conditioned for singular matrices)
- Computational cost: O((prod1)³) where prod1 = product of first ind dimensions
- Default ind: Common choice is ind=2 for 4D tensors representing bilinear forms
- Dimension compatibility: Will throw if prod(shape[:ind]) ≠ prod(shape[ind:])
- Matrix singularity: Will throw if reshaped matrix is singular (determinant = 0)
- Numerical precision: Ill-conditioned matrices can produce inaccurate results
- Memory: Creates temporary reshaped tensors; can be memory-intensive for large tensors
Parameters
ATensor<S, D, Dev>- The input tensor of shape [...]. Must have ndim = 2. The dimension products around the split point must be equal: prod(shape[:ind]) == prod(shape[ind:])
optionsTensorinvOptionsoptional
Returns
Tensor<DynamicShape, D, Dev>– The inverse tensor with shape [...]. Dimensions are permuted: original shape[:ind] becomes shape[ind:], and original shape[ind:] becomes shape[:ind]Examples
// Simple 4D tensor: shape [2, 3, 2, 3]
// ind=2: first 2 dims multiply to 2*3=6, last 2 dims multiply to 2*3=6
const A = torch.randn([2, 3, 2, 3]);
const X = torch.linalg.tensorinv(A, 2);
console.log(X.shape); // [2, 3, 2, 3] (same as input, dimensions rearranged internally)
// Verify inverse property (approximately, due to numerical precision)
// tensordot(A, X, [[2, 3], [0, 1]]) should be close to identity-like structure// Solving a tensor equation: tensordot(A, X) = B
// If tensordot(A, X, dims) = B, then X = tensordot(B, tensorinv(A))
const A = torch.randn([3, 4, 3, 4]); // Multi-linear operator
const B = torch.randn([3, 4, 5]); // Right-hand side
// Compute inverse with ind=2 (first 2 dims vs last 2 dims)
const Ainv = torch.linalg.tensorinv(A, 2);
console.log(Ainv.shape); // [3, 4, 3, 4] (dimensions rearranged)// Different ind values split dimensions differently
const A = torch.randn([2, 3, 4, 5, 6]); // ndim=5
// ind=1: split at 1 → [2] vs [3,4,5,6]
// prod1 = 2, prod2 = 3*4*5*6 = 360 (not equal, would fail)
// ind=2: split at 2 → [2,3] vs [4,5,6]
// prod1 = 6, prod2 = 120 (not equal, would fail)
// ind=3: split at 3 → [2,3,4] vs [5,6]
// prod1 = 24, prod2 = 30 (not equal, would fail)
// For this to work, need compatible dimensions
const A_compat = torch.randn([2, 3, 4, 2, 3, 4]); // prod1=24, prod2=24
const X = torch.linalg.tensorinv(A_compat, 3);
console.log(X.shape); // [2, 3, 4, 2, 3, 4] (same shape, dimensions rearranged)// Shape transformation: dimensions are permuted
const A = torch.randn([2, 3, 2, 3]);
const X = torch.linalg.tensorinv(A, 2);
// Input shape: [2, 3, 2, 3]
// Split: [2, 3] vs [2, 3]
// Output shape: [2, 3, 2, 3] (rearranged, but same in this case)
// For A with shape [3, 4, 3, 4], output would be [3, 4, 3, 4]See Also
- PyTorch torch.linalg.tensorinv
- torch.linalg.inv - Standard matrix inversion for 2D tensors
- torch.linalg.tensorsolve - Solve tensor equations (uses tensorinv internally)
- torch.tensordot - Tensor contraction operation (inverse defined w.r.t. this)
- torch.linalg.matrix_power - Compute integer powers of matrices