Java Program to Perform LU Decomposition of Any Matrix

This is a java program to LU Decomposition of a given matrix. LU decomposition is the process of reducing single matrix into 2 matrices such that, upon multiplication we get the original matrix, having property that one of them is lower trinagular matrix and other one is upper trinagular matrix.

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

  1. //This is a sample program to calulate the LU decomposition of the given matrix
  2. import java.util.Scanner;
  3.  
  4. public class LUDecomposition
  5. {
  6.     public static void main(String args[])
  7.     {
  8.         System.out.println("Enter the dimension of the matrix:");
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         double [][]mat = new double[n][n];
  12.         for(int i=0; i<n; i++)
  13.             for(int j=0; j<n; j++)
  14.                 mat[i][j] = sc.nextDouble();
  15.  
  16.         if(n==2)
  17.         {
  18.             double [][]l = new double[n][n];
  19.             l[0][0] = l[1][1] = 1;
  20.             l[0][1] = 0;
  21.  
  22.             double [][]u = new double[n][n];
  23.             u[1][0] = 0;
  24.  
  25.             u[0][0] = mat[0][0];
  26.             u[0][1] = mat[0][1];
  27.  
  28.             l[1][0] = mat[1][0]/mat[0][0];
  29.             u[1][1] = mat[1][1] - (l[1][0]*u[0][1]);   //mat[2][2]-(mat[2][1]*mat[1][2]/mat[1][1]);
  30.  
  31.             System.out.println("The L Component is:");
  32.             for(int i=0; i<n; i++)
  33.             {
  34.                 for(int j=0; j<n; j++)
  35.                     System.out.print(" "+l[i][j]);
  36.                 System.out.println();
  37.             }
  38.             System.out.println("The U Component is:");
  39.             for(int i=0; i<n; i++)
  40.             {
  41.                 for(int j=0; j<n; j++)
  42.                     System.out.print(" "+u[i][j]);
  43.                 System.out.println();
  44.             }
  45.  
  46.         }
  47.         if(n==3)
  48.         {
  49.             double [][]l = new double[n][n];
  50.             l[0][0] = l[1][1] = l[2][2] = 1;
  51.             l[0][1] = l[0][2] = l[1][2] = 0;
  52.  
  53.             double [][]u = new double[n][n];
  54.             u[1][0] = u[2][0] = u[2][1] = 0;
  55.  
  56.             u[0][0] = mat[0][0];
  57.             u[0][1] = mat[0][1];
  58.             u[0][2] = mat[0][2];
  59.  
  60.             l[1][0] = mat[1][0]/mat[0][0];
  61.             u[1][1] = mat[1][1] - (l[1][0]*u[0][1]);   //mat[2][2]-(mat[2][1]*mat[1][2]/mat[1][1]);
  62.             u[1][2] = mat[1][2] - (l[1][0]*u[0][2]);
  63.  
  64.             l[2][0] = mat[2][0]/u[0][0];
  65.             l[2][1] = (mat[2][1] - l[2][1]*u[0][1])/u[1][1];
  66.             u[2][2] = mat[2][2] - (l[2][0]*u[0][2]) - (l[2][1]*u[1][2]);
  67.  
  68.             System.out.println("The L Component is:");
  69.             for(int i=0; i<n; i++)
  70.             {
  71.                 for(int j=0; j<n; j++)
  72.                     System.out.print(" "+l[i][j]);
  73.                 System.out.println();
  74.             }
  75.             System.out.println("The U Component is:");
  76.             for(int i=0; i<n; i++)
  77.             {
  78.                 for(int j=0; j<n; j++)
  79.                     System.out.print(" "+u[i][j]);
  80.                 System.out.println();
  81.             }
  82.         }
  83.         sc.close();
  84.     }
  85. }

Output:

$ javac LUDecomposition.java
$ java LUDecomposition.java
Enter the dimension of the matrix:
3
2 3 1
4 5 1
1 1 1
The L Component is:
 1.0 0.0 0.0
 2.0 1.0 0.0
 0.5 -1.0 1.0
The U Component is:
 2.0 3.0 1.0
 0.0 -1.0 -1.0
 0.0 0.0 -0.5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java 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.