Skip to contents

Letting be or , this function computes the solution of the linear system associated to , which is defined as

Usage

linalg_solve(A, B)

Arguments

A

(Tensor): tensor of shape (*, n, n) where * is zero or more batch dimensions.

B

(Tensor): right-hand side tensor of shape (*, n) or (*, n, k) or (n,) or (n, k) according to the rules described above

Details

$$ AX = B $$

This system of linear equations has one solution if and only if is invertible_. This function assumes that is invertible. Supports inputs of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if the inputs are batches of matrices then the output has the same batch dimensions.

Letting * be zero or more batch dimensions,

  • If A has shape (*, n, n) and B has shape (*, n) (a batch of vectors) or shape (*, n, k) (a batch of matrices or "multiple right-hand sides"), this function returns X of shape (*, n) or (*, n, k) respectively.

  • Otherwise, if A has shape (*, n, n) and B has shape (n,) or (n, k), B is broadcasted to have shape (*, n) or (*, n, k) respectively.

This function then returns the solution of the resulting batch of systems of linear equations.

Note

This function computes X = A$inverse() @ B in a faster and more numerically stable way than performing the computations separately.

Examples

if (torch_is_installed()) {
A <- torch_randn(3, 3)
b <- torch_randn(3)
x <- linalg_solve(A, b)
torch_allclose(torch_matmul(A, x), b)
}
#> [1] TRUE