2.69 Obtain the LU decomposition of a matrix

Problem: Given a matrix \(A\), find matrix \(L\) and \(U\) such that \(LU=A\). The matrix \(L\) will have \(1\) in all the diagonal elements and zeros in the upper triangle. The matrix \(U\) will have \(0\) in the lower triangle as shown in this diagram.

pict

Mathematica

Remove["Global`*"] 
mat = {{1, -3, 5}, 
       {2, -4, 7}, 
       {-1, -2, 1}}; 
 
{lu,pivotVector,conditionNumber}= 
     LUDecomposition[mat]
 

{{{1,-3,5}, 
 {2,2,-3}, 
 {-1,-(5/2),-(3/2)}}, 
{1,2,3},1}
 

lower=lu*SparseArray[{i_,j_}/;j<i->1,{3,3}]+ 
         IdentityMatrix[3]
 

{{1,0,0}, 
 {2,1,0}, 
{-1,-(5/2),1}}
 

upper=lu SparseArray[{i_,j_}/;j>=i->1,{3,3}]
 

{{1,-3,5}, 
 {0,2,-3}, 
 {0,0,-(3/2)}}
 

lower.upper
 

{{1,-3,5}, 
 {2,-4,7}, 
{-1,-2,1}}
 

 

Matlab

clear all; 
 
A=[1 -3 5; 
   2 -4 7; 
   -1 -2 1]; 
 
[L,U,p]=lu(A)
 

L = 
    1.0000         0         0 
   -0.5000    1.0000         0 
    0.5000    0.2500    1.0000 
 
U = 
    2.0000   -4.0000    7.0000 
         0   -4.0000    4.5000 
         0         0    0.3750 
p = 
     0     1     0 
     0     0     1 
     1     0     0
 

L*U
 

ans = 
     2    -4     7 
    -1    -2     1 
     1    -3     5
 

 

Maple

A:=Matrix([[1,-3,5],[2,-4,7],[-1,-2,1]]); 
p,l,u:=LinearAlgebra:-LUDecomposition(A);
 

\[ \left [ \left [\begin {array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end {array}\right ] , \left [\begin {array}{ccc} 1 & 0 & 0 \\ 2 & 1 & 0 \\ -1 & -\frac {5}{2} & 1 \end {array}\right ] , \left [\begin {array}{ccc} 1 & -3 & 5 \\ 0 & 2 & -3 \\ 0 & 0 & -\frac {3}{2} \end {array}\right ] \right ] \]