Doolittle Algorithm : LU Decomposition | Javascript Program to decompose a matrix into lower and upper triangular matrix function MAX = 100 ; ; Decomposing matrix into Upper and Lower triangular matrix ; Upper Triangular ; Summation of L ( i , j ) * U ( j , k ) ; Evaluating U ( i , k ) ; Lower Triangular ; Diagonal as 1 ; Summation of L ( k , j ) * U ( j , i ) ; Evaluating L ( k , i ) ; Setw is for displaying nicely ; Displaying the result : ; Lower ; Upper ; Driver code
var s = " " ; function luDecomposition ( mat , n ) { var lower = Array ( n ) . fill ( 0 ) . map ( x => Array ( n ) . fill ( 0 ) ) ; var upper = Array ( n ) . fill ( 0 ) . map ( x => Array ( n ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var k = i ; k < n ; k ++ ) { var sum = 0 ; for ( var j = 0 ; j < i ; j ++ ) sum += ( lower [ i ] [ j ] * upper [ j ] [ k ] ) ; upper [ i ] [ k ] = mat [ i ] [ k ] - sum ; } for ( var k = i ; k < n ; k ++ ) { if ( i == k ) lower [ i ] [ i ] = 1 ; else { var sum = 0 ; for ( var j = 0 ; j < i ; j ++ ) sum += ( lower [ k ] [ j ] * upper [ j ] [ i ] ) ; lower [ k ] [ i ] = parseInt ( ( mat [ k ] [ i ] - sum ) / upper [ i ] [ i ] ) ; } } } document . write ( setw ( 2 ) + " " + setw ( 10 ) + " " ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) document . write ( setw ( 4 ) + lower [ i ] [ j ] + " " ) ; document . write ( setw ( 10 ) ) ; for ( var j = 0 ; j < n ; j ++ ) document . write ( setw ( 4 ) + upper [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } function setw ( noOfSpace ) { var s = " " ; for ( i = 0 ; i < noOfSpace ; i ++ ) s += " " ; return s ; } var mat = [ [ 2 , - 1 , - 2 ] , [ - 4 , 6 , 3 ] , [ - 4 , - 2 , 8 ] ] ; luDecomposition ( mat , 3 ) ;