# Different Value for LU Decomposition [duplicate]

This question already has an answer here:

When I try to LU decompose a matrix m, I get a different result in mathematica than an online calculator. Mathematica also gives me values that is different from my own derived values for the elements of L and U matrices. Can anyone shed some light on what mathematica is doing?

m = {{2, -3, 1, 3}, {1, 4, -3, -3}, {5, 3, -1, -1}, {3, -6, -3, 1}}
{lu, piv, cond} = LUDecomposition[m]


The matrix lu needs to be decomposed to get L and U

l = LowerTriangularize[lu, -1] + IdentityMatrix[Length[lu]]
u = UpperTriangularize[lu]


The determinant of m is -160. The product of the determinant of L and U should be equal to that but it isn't. From an online calculator, I get this.

LU Decomposition Calculator

## marked as duplicate by Michael E2, Henrik Schumacher, m_goldberg, MarcoB, J. M. will be back soon♦Feb 15 at 3:27

• You have to also consider the determinant of the permutation matrix (from pivoting). (The decomposition is $PM=LU$.) The determinant of the permutation matrix corresponding to piv is -1. Thus the product of the determinants of l and u should be Det[m[[piv]] or 160. – Michael E2 Feb 11 at 3:02

m = {{2, -3, 1, 3}, {1, 4, -3, -3}, {5, 3, -1, -1}, {3, -6, -3, 1}};

{lu, p, c} = LUDecomposition[m];
u = UpperTriangularize@lu;
l = LowerTriangularize[lu, -1] + IdentityMatrix[Length@m];


the original matrix is obtained:

Permute[l.u, p]
{{2, -3, 1, 3}, {1, 4, -3, -3}, {5, 3, -1, -1}, {3, -6, -3, 1}}
m == %
True

Det@m
-160

Det[Permute[l.u, p]]
-160

Det[l.u]
160  (* is not correct*)


l.u is equal to a permutation of the rows of m:

l.u == m[[p]]
True


lookup in the docs

Alternatively, one should use Signature[] to ensure the correct sign for the determinant.

m = {{2, -3, 1, 3}, {1, 4, -3, -3}, {5, 3, -1, -1}, {3, -6, -3, 1}}
{lu, piv, cond} = LUDecomposition[m]

{Signature[piv] Tr[lu, Times], Det[m]}


as noted in this previous answer.