Doolittle Algorithm : LU Decomposition | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void luDecomposition ( int mat [ ] [ MAX ] , int n ) { int lower [ n ] [ n ] , upper [ n ] [ n ] ; memset ( lower , 0 , sizeof ( lower ) ) ; memset ( upper , 0 , sizeof ( upper ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = i ; k < n ; k ++ ) { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ( lower [ i ] [ j ] * upper [ j ] [ k ] ) ; upper [ i ] [ k ] = mat [ i ] [ k ] - sum ; } for ( int k = i ; k < n ; k ++ ) { if ( i == k ) else { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ( lower [ k ] [ j ] * upper [ j ] [ i ] ) ; lower [ k ] [ i ] = ( mat [ k ] [ i ] - sum ) / upper [ i ] [ i ] ; } } } cout << setw ( 6 ) << " TABSYMBOL Lower ▁ Triangular " << setw ( 32 ) << " Upper ▁ Triangular " << endl ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << setw ( 6 ) << lower [ i ] [ j ] << " TABSYMBOL " ; cout << " TABSYMBOL " ; for ( int j = 0 ; j < n ; j ++ ) cout << setw ( 6 ) << upper [ i ] [ j ] << " TABSYMBOL " ; cout << endl ; } } int main ( ) { int mat [ ] [ MAX ] = { { 2 , -1 , -2 } , { -4 , 6 , 3 } , { -4 , -2 , 8 } } ; luDecomposition ( mat , 3 ) ; return 0 ; }