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.
//This is a sample program to calulate the LU decomposition of the given matrix
import java.util.Scanner;
public class LUDecomposition
{
public static void main(String args[])
{
System.out.println("Enter the dimension of the matrix:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double [][]mat = new double[n][n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
mat[i][j] = sc.nextDouble();
if(n==2)
{
double [][]l = new double[n][n];
l[0][0] = l[1][1] = 1;
l[0][1] = 0;
double [][]u = new double[n][n];
u[1][0] = 0;
u[0][0] = mat[0][0];
u[0][1] = mat[0][1];
l[1][0] = mat[1][0]/mat[0][0];
u[1][1] = mat[1][1] - (l[1][0]*u[0][1]); //mat[2][2]-(mat[2][1]*mat[1][2]/mat[1][1]);
System.out.println("The L Component is:");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
System.out.print(" "+l[i][j]);
System.out.println();
}
System.out.println("The U Component is:");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
System.out.print(" "+u[i][j]);
System.out.println();
}
}
if(n==3)
{
double [][]l = new double[n][n];
l[0][0] = l[1][1] = l[2][2] = 1;
l[0][1] = l[0][2] = l[1][2] = 0;
double [][]u = new double[n][n];
u[1][0] = u[2][0] = u[2][1] = 0;
u[0][0] = mat[0][0];
u[0][1] = mat[0][1];
u[0][2] = mat[0][2];
l[1][0] = mat[1][0]/mat[0][0];
u[1][1] = mat[1][1] - (l[1][0]*u[0][1]); //mat[2][2]-(mat[2][1]*mat[1][2]/mat[1][1]);
u[1][2] = mat[1][2] - (l[1][0]*u[0][2]);
l[2][0] = mat[2][0]/u[0][0];
l[2][1] = (mat[2][1] - l[2][1]*u[0][1])/u[1][1];
u[2][2] = mat[2][2] - (l[2][0]*u[0][2]) - (l[2][1]*u[1][2]);
System.out.println("The L Component is:");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
System.out.print(" "+l[i][j]);
System.out.println();
}
System.out.println("The U Component is:");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
System.out.print(" "+u[i][j]);
System.out.println();
}
}
sc.close();
}
}
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
Related posts:
Java Program to implement Bit Matrix
Java Program to Perform Insertion in a 2 Dimension K-D Tree
An Example of Load Balancing with Zuul and Eureka
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Converting a Stack Trace to a String in Java
Merging Two Maps with Java 8
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Hướng dẫn Java Design Pattern – Transfer Object
Java Program to Implement CopyOnWriteArrayList API
Java – Reader to Byte Array
Guide to BufferedReader
Java Program to Implement a Binary Search Tree using Linked Lists
Get the workstation name or IP
Java Program to Implement Insertion Sort
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Java Program to Find the Longest Path in a DAG
Hướng dẫn Java Design Pattern – Dependency Injection
Vertical decomposition
Exception Handling in Java
Java Program to Implement Ternary Search Algorithm
Custom Cascading in Spring Data MongoDB
Spring Cloud AWS – EC2
Introduction to Spring Method Security
Request a Delivery / Read Receipt in Javamail
Java Program to Create the Prufer Code for a Tree
Spring MVC Setup with Kotlin
Java Program to Implement LinkedBlockingDeque API
Hamcrest Collections Cookbook
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Create a Custom Auto-Configuration with Spring Boot
Reactive WebSockets with Spring 5
Introduction to Java 8 Streams