Guest User

LU Decomposition Code

a guest
Feb 26th, 2020
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.lang.*;
  4.  
  5. public class ComputerProject {
  6.  
  7.     // If fails, remove vector p
  8.     private float[][] upperSovler(float[][] u, float[][] x, float[][] y, int[][] p, float epsilon, int n){
  9.  
  10.         // Algorithm so solve upper triangle matrices via backward substitution
  11.         for(int j = (n-1) ; j >= 0 ; j--){ // Start at the last column
  12.             x[p[j][0]][0] = y[p[j][0]][0] / u[p[j][0]][j]; // Solve for x given the previously done Gaussian Eliminations
  13.             for(int i = 0 ; i < j ; i++){ // Loop through the rows from 1 to (column number - 1)
  14.                 y[p[i][0]][0] = y[p[i][0]][0] - u[p[i][0]][j] * x[p[j][0]][0]; // Gaussian Elimination
  15.             }
  16.         }
  17.         return x; // Return the solution vector
  18.     }
  19.  
  20.  
  21.  
  22.     public static void main(String[] args) {
  23.  
  24.         // Create variables
  25.         Scanner uInput = new Scanner(System.in);
  26.         ComputerProject callMethod = new ComputerProject();
  27.         int n;
  28.         float[][] matrix_A;
  29.         float[][] vector_x;
  30.         int[][] vector_p;
  31.         float[][] vector_b;
  32.         final int precision;
  33.         float epsilon;
  34.         int pivotMax;
  35.         int temp;
  36.         float eliminate;
  37.         boolean isSingular = false;
  38.         float[][] vector_y;
  39.  
  40.  
  41.         // Intro
  42.         System.out.println("This code will compute the LU factorization of a given matrix, and if nonsingular, solve" +
  43.                 " for some vector x such that Ax=b. This code also explicitly uses square matrices.\n");
  44.  
  45.         // Get matrix size
  46.         System.out.print("Enter the dimension of the given matrix: ");
  47.         n = uInput.nextInt();
  48.         System.out.println();
  49.  
  50.         // Initialize matrices
  51.         matrix_A = new float[n][n];
  52.         vector_x = new float[n][1];
  53.         vector_p = new int[n][1];
  54.         for(int i = 0 ; i < n ; i++){ // Set the permutation vector to its base form
  55.             vector_p[i][0] = i;
  56.         }
  57.         vector_b = new float[n][1];
  58.         vector_y = new float[n][1];
  59.  
  60.         // Get matrix A
  61.         for(int i = 0 ; i < n ; i++){
  62.             for(int j = 0 ; j < n ; j++){
  63.                 System.out.print("Enter the " + (i + 1) + " x " + (j + 1) + " element of the matrix: ");
  64.                 matrix_A[i][j] = uInput.nextFloat();
  65.             }
  66.         }
  67.         System.out.println();
  68.  
  69.         // Get vector b
  70.         for(int i = 0 ; i < n ; i++){
  71.             System.out.print("Enter the " + (i + 1) + " x 1 element of vector b: ");
  72.             vector_b[i][0] = uInput.nextFloat();
  73.         }
  74.         System.out.println();
  75.  
  76.         // Get precision
  77.         System.out.print("This program will assume precision of 6.\n");
  78.         precision = 6;
  79.         epsilon = ((float) Math.pow(10 , -((double) precision)));
  80.         System.out.println();
  81.  
  82.         // Get matrix U & vector p & report matrix L
  83.         for(int k = 0 ; k < (n - 1) ; k++){ // Overarching loop
  84.  
  85.             // Find rows to permutate
  86.             pivotMax = k; // Set initial max as the pivot being observed
  87.             for(int l = (k + 1) ; l < n ; l++) { // Check the rows to see if any are larger and thus will be swapped; strictly below the pivot only
  88.                 if (Math.abs(matrix_A[(vector_p[pivotMax][0])][k]) < Math.abs(matrix_A[(vector_p[l][0])][k])) { // If there exists a larger absolute value
  89.                         pivotMax = l; // Record largest value to swap in the permutation vector
  90.                 }
  91.             }
  92.  
  93.             // Swap the rows such that the max value is in the pivot position
  94.             temp = vector_p[k][0];
  95.             vector_p[k][0] = vector_p[pivotMax][0];
  96.             vector_p[pivotMax][0] = temp;
  97.  
  98.             // Eliminate all entries below the pivot and spread across the matrix
  99.             for(int m = (k + 1) ; m < n ; m++){ // Loop through rows & find the elimination multiplicity per row
  100.                 eliminate = -(matrix_A[(vector_p[m][0])][k] / matrix_A[(vector_p[k][0])][k]);
  101.                 for(int o = k ; o < n ; o++){ // Loop through columns & eliminate
  102.                     matrix_A[(vector_p[m][0])][o] = eliminate*matrix_A[(vector_p[k][0])][o] + matrix_A[(vector_p[m][0])][o];
  103.                 }
  104.             }
  105.         }
  106.  
  107.         // Report matrix U
  108.         System.out.println("\n\nFrom the factorization, matrix U is as follows:\n");
  109.         for(int i = 0 ; i < n ; i++){
  110.             for(int j = 0 ; j < n ; j++){
  111.                 System.out.printf("%-10.6f",matrix_A[vector_p[i][0]][j]);
  112.             }
  113.             System.out.println();
  114.         }
  115.         System.out.println();
  116.  
  117.         // Report vector p
  118.         System.out.println("After the factorization, the permutation vector p is as follows:\n");
  119.         for(int i = 0 ; i < n ; i++){
  120.             System.out.println((vector_p[i][0] + 1));
  121.         }
  122.         System.out.println();
  123.  
  124.         // Check for singularity
  125.         for(int i = 0 ; i < n ; i++){
  126.             if(Math.abs(matrix_A[vector_p[i][0]][i]) < epsilon){ // If any diagonal element is 0
  127.                 isSingular = true;
  128.                 break;
  129.             }
  130.         }
  131.  
  132.         // Perform actions based on singularity
  133.         if(isSingular){
  134.             System.out.println("The matrix A is singular. Program will terminate.\n");
  135.             System.exit(0);
  136.         }else{
  137.             System.out.println("The matrix A is non-singular. Program will solve Ax=b for x.\n");
  138.  
  139.             // Occupy vector x with the solution
  140.             vector_x = callMethod.upperSovler(matrix_A , vector_x , vector_y , vector_p , epsilon , n);
  141.  
  142.             // Output x
  143.             System.out.println("The solution vector x is as follows:\n");
  144.             for(int i = 0 ; i < n ; i++){
  145.                 System.out.printf("%-10.6f" , vector_x[i][0]);
  146.                 System.out.println();
  147.             }
  148.  
  149.         }
  150.  
  151.     }
  152.  
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment