2
$\begingroup$

I am having a huge problem with my Mathematica notebook. I am trying to evaluate LUDecomposition and it keeps getting wrong - when I multiply l and u, I get my A matrix, but the first row is displayed as the last. I have no idea how to fix it... enter image description here

My A matrix looks like this:

A = {{3, 2, 0, 0, 0, 0, 0, 0, 0},   {2, 8, 1, 0, 0, 0, 0, 0, 0},   {0, 1, 4, 1, 0, 0, 0, 0, 0},   {0, 0, 1, 3, 2, 0, 0, 0, 0},   {0, 0, 0, 2, 4, 1, 0, 0, 0},   {0, 0, 0, 0, 1, 5, 2, 0, 0},   {0, 0, 0, 0, 0, 2, 3, 1, 0},   {0, 0, 0, 0, 0, 0, 1, 8, 2},   {0, 0, 0, 0, 0, 0, 0, 2, 4}};

What I do is:

{lu, p, c} = LUDecomposition[A];
l = LowerTriangularize[lu, -1] + IdentityMatrix[Length[p]];
u = UpperTriangularize[lu];
P = Part[IdentityMatrix[Length[p]], p];
MatrixForm /@ {P.A, P, l, u, l.u};

And I get:
enter image description here The last one should equal my A matrix, but obviously it does not. It seems like the first row is the last one. Do you have any idea why? I will be very grateful for any kind of help because it is getting me really crazy.

$\endgroup$
1
  • 1
    $\begingroup$ l.u goes by the permutation of the rows done. So you need to consider p as well. $\endgroup$
    – Nasser
    Commented Dec 3, 2017 at 19:38

1 Answer 1

3
$\begingroup$

As Nasser has already mentioned, you also have to take into account the permuation p. It is however not entirely obvious how to do that. Here an example:

{LU, p, c} = LUDecomposition[A];
L = LowerTriangularize[LU, -1] + IdentityMatrix[Length[p]];
U = UpperTriangularize[LU];
Permute[L.U, p] == A

True

Admittedly, my first idea was to use L[[p]].U == A (which returns False). So in that respect, the documentation could be a bit more specific.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.