C++ Program to Perform LU Decomposition of Any Matrix

This is a C++ Program to perform LU Decomposition of any matrix. In numerical analysis, LU decomposition (where ‘LU’ stands for ‘Lower Upper’, and also called LU factorization) factors a matrix as the product of a lower triangular matrix and an upper triangular matrix. The product sometimes includes a permutation matrix as well. The LU decomposition can be viewed as the matrix form of Gaussian elimination. Computers usually solve square systems of linear equations using the LU decomposition, and it is also a key step when inverting a matrix, or computing the determinant of a matrix

Here is source code of the C++ Program to Perform LU Decomposition of any Matrix. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  2. #include<cstdio>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char **argv)
  7. {
  8.     void lu(float[][10], float[][10], float[][10], int n);
  9.     void output(float[][10], int);
  10.     float a[10][10], l[10][10], u[10][10];
  11.     int n = 0, i = 0, j = 0;
  12.     cout << "Enter size of 2d array(Square matrix) : ";
  13.     cin >> n;
  14.     for (i = 0; i < n; i++)
  15.     {
  16.         for (j = 0; j < n; j++)
  17.         {
  18.             cout << "Enter values no:" << i << ", " << j << ": ";
  19.             cin >> a[i][j];
  20.         }
  21.     }
  22.     lu(a, l, u, n);
  23.     cout << "\nL Decomposition\n\n";
  24.     output(l, n);
  25.     cout << "\nU Decomposition\n\n";
  26.     output(u, n);
  27.     return 0;
  28. }
  29. void lu(float a[][10], float l[][10], float u[][10], int n)
  30. {
  31.     int i = 0, j = 0, k = 0;
  32.     for (i = 0; i < n; i++)
  33.     {
  34.         for (j = 0; j < n; j++)
  35.         {
  36.             if (j < i)
  37.                 l[j][i] = 0;
  38.             else
  39.             {
  40.                 l[j][i] = a[j][i];
  41.                 for (k = 0; k < i; k++)
  42.                 {
  43.                     l[j][i] = l[j][i] - l[j][k] * u[k][i];
  44.                 }
  45.             }
  46.         }
  47.         for (j = 0; j < n; j++)
  48.         {
  49.             if (j < i)
  50.                 u[i][j] = 0;
  51.             else if (j == i)
  52.                 u[i][j] = 1;
  53.             else
  54.             {
  55.                 u[i][j] = a[i][j] / l[i][i];
  56.                 for (k = 0; k < i; k++)
  57.                 {
  58.                     u[i][j] = u[i][j] - ((l[i][k] * u[k][j]) / l[i][i]);
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }
  64. void output(float x[][10], int n)
  65. {
  66.     int i = 0, j = 0;
  67.     for (i = 0; i < n; i++)
  68.     {
  69.         for (j = 0; j < n; j++)
  70.         {
  71.             printf("%f ", x[i][j]);
  72.         }
  73.         cout << "\n";
  74.     }
  75. }

Output:

$ g++ LUDecomposition.cpp
$ a.out
 
Enter size of 2d array(Square matrix) : 3
Enter values no:0, 0: 1
Enter values no:0, 1: 1
Enter values no:0, 2: -1
Enter values no:1, 0: 2
Enter values no:1, 1: -1
Enter values no:1, 2: 3
Enter values no:2, 0: 3
Enter values no:2, 1: 1
Enter values no:2, 2: -1
 
L Decomposition
 
1.000000 0.000000 0.000000 
2.000000 -3.000000 0.000000 
3.000000 -2.000000 -1.333333 
 
U Decomposition
 
1.000000 1.000000 -1.000000 
0.000000 1.000000 -1.666667 
0.000000 0.000000 1.000000

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.