torch_solve: Solve

Description Usage Arguments solve(input, A) -> (Tensor, Tensor) Note Examples

View source: R/gen-namespace.R

Description

Solve

Usage

1
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.

Note

1
2
3
4
Irrespective of the original strides, the returned matrices
`solution` and `LU` will be transposed, i.e. with strides like
`B$contiguous()$transpose(-1, -2)$stride()` and
`A$contiguous()$transpose(-1, -2)$stride()` respectively.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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))
}

Example output



torch documentation built on Oct. 7, 2021, 9:22 a.m.