Doolittle Algorithm : LU Decomposition | PHP Program to decompose a matrix into lower and upper triangular matrix ; Decomposing matrix into Upper and Lower triangular matrix ; Upper Triangular ; Summation of L ( i , j ) * U ( j , k ) ; Evaluating U ( i , k ) ; Lower Triangular ; $lower [ $i ] [ $i ] = 1 ; 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
< ? php $ MAX = 100 ; function luDecomposition ( $ mat , $ n ) { $ lower ; $ upper ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ lower [ $ i ] [ $ j ] = 0 ; $ upper [ $ i ] [ $ j ] = 0 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ k = $ i ; $ k < $ n ; $ k ++ ) { $ sum = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ sum += ( $ lower [ $ i ] [ $ j ] * $ upper [ $ j ] [ $ k ] ) ; $ upper [ $ i ] [ $ k ] = $ mat [ $ i ] [ $ k ] - $ sum ; } for ( $ k = $ i ; $ k < $ n ; $ k ++ ) { if ( $ i == $ k ) else { $ sum = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ sum += ( $ lower [ $ k ] [ $ j ] * $ upper [ $ j ] [ $ i ] ) ; $ lower [ $ k ] [ $ i ] = ( int ) ( ( $ mat [ $ k ] [ $ i ] - $ sum ) / $ upper [ $ i ] [ $ i ] ) ; } } } echo " TABSYMBOL TABSYMBOL Lower ▁ Triangular " ; echo " TABSYMBOL TABSYMBOL TABSYMBOL Upper ▁ Triangular STRNEWLINE " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo " TABSYMBOL " . $ lower [ $ i ] [ $ j ] . " TABSYMBOL " ; echo " TABSYMBOL " ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo $ upper [ $ i ] [ $ j ] . " TABSYMBOL " ; echo " STRNEWLINE " ; } } $ mat = array ( array ( 2 , -1 , -2 ) , array ( -4 , 6 , 3 ) , array ( -4 , -2 , 8 ) ) ; luDecomposition ( $ mat , 3 ) ; ? >