Learn R Programming

torch (version 0.8.1)

torch_solve: Solve

Description

Solve

Usage

torch_solve(self, A)

Arguments

self

(Tensor) input matrix \(B\) of size \((*, m, k)\) , where \(*\) is zero or more batch dimensions.

A

(Tensor) input square matrix of size \((*, m, m)\), where \(*\) is zero or more batch dimensions.

solve(input, A) -> (Tensor, Tensor)

This function returns the solution to the system of linear equations represented by \(AX = B\) and the LU factorization of A, in order as a namedtuple solution, LU.

LU contains L and U factors for LU factorization of A.

torch_solve(B, A) can take in 2D inputs B, A or inputs that are batches of 2D matrices. If the inputs are batches, then returns batched outputs solution, LU.

Examples

Run this code
if (torch_is_installed()) {

A = torch_tensor(rbind(c(6.80, -2.11,  5.66,  5.97,  8.23),
                      c(-6.05, -3.30,  5.36, -4.44,  1.08),
                      c(-0.45,  2.58, -2.70,  0.27,  9.04),
                      c(8.32,  2.71,  4.35,  -7.17,  2.14),
                      c(-9.67, -5.14, -7.26,  6.08, -6.87)))$t()
B = torch_tensor(rbind(c(4.02,  6.19, -8.22, -7.57, -3.03),
                      c(-1.56,  4.00, -8.67,  1.75,  2.86),
                      c(9.81, -4.09, -4.57, -8.61,  8.99)))$t()
out = torch_solve(B, A)
X = out[[1]]
LU = out[[2]]
torch_dist(B, torch_mm(A, X))
# Batched solver example
A = torch_randn(c(2, 3, 1, 4, 4))
B = torch_randn(c(2, 3, 1, 4, 6))
out = torch_solve(B, A)
X = out[[1]]
LU = out[[2]]
torch_dist(B, A$matmul(X))
}

Run the code above in your browser using DataLab