LU is not same as A when I used torch.solver

Viewed 63

let A=[[3,2],[1,-3]] and B=[[3],[-10]] and solve equation AX=B by using torch.solve:

X, LU = torch.solve(B,A)

Then I got X=[[-1],[3]] and LU=[[3,2],[0.333,-3.666]].

According to definition of LU decompose, LU must be same as A, however they aren't same.

Can anyone explain this???

Thank you

1 Answers

The representation you got is a compact way of representing the lower trainagular matrix L and the upper trainagular matrix U. You can use torch.tril and torch.triu to get these matrices explicitly:

L = torch.tril(LU, -1) + torch.eye(LU.shape[-1])
U = torch.triu(LU)

verify:

In [*]: L
Out[*]:
tensor([[1.0000, 0.0000],
       [0.3333, 1.0000]])
In [*]: U
Out[*]:
tensor([[ 3.0000,  2.0000],
        [ 0.0000, -3.6667]])

And the product is indeed equal to A:

In [*]: torch.dist(L @ U , A)
Out[*]: tensor(0.)
Related