Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- public class ComputerProject {
- // If fails, remove vector p
- private float[][] upperSovler(float[][] u, float[][] x, float[][] y, int[][] p, float epsilon, int n){
- // Algorithm so solve upper triangle matrices via backward substitution
- for(int j = (n-1) ; j >= 0 ; j--){ // Start at the last column
- x[p[j][0]][0] = y[p[j][0]][0] / u[p[j][0]][j]; // Solve for x given the previously done Gaussian Eliminations
- for(int i = 0 ; i < j ; i++){ // Loop through the rows from 1 to (column number - 1)
- y[p[i][0]][0] = y[p[i][0]][0] - u[p[i][0]][j] * x[p[j][0]][0]; // Gaussian Elimination
- }
- }
- return x; // Return the solution vector
- }
- public static void main(String[] args) {
- // Create variables
- Scanner uInput = new Scanner(System.in);
- ComputerProject callMethod = new ComputerProject();
- int n;
- float[][] matrix_A;
- float[][] vector_x;
- int[][] vector_p;
- float[][] vector_b;
- final int precision;
- float epsilon;
- int pivotMax;
- int temp;
- float eliminate;
- boolean isSingular = false;
- float[][] vector_y;
- // Intro
- System.out.println("This code will compute the LU factorization of a given matrix, and if nonsingular, solve" +
- " for some vector x such that Ax=b. This code also explicitly uses square matrices.\n");
- // Get matrix size
- System.out.print("Enter the dimension of the given matrix: ");
- n = uInput.nextInt();
- System.out.println();
- // Initialize matrices
- matrix_A = new float[n][n];
- vector_x = new float[n][1];
- vector_p = new int[n][1];
- for(int i = 0 ; i < n ; i++){ // Set the permutation vector to its base form
- vector_p[i][0] = i;
- }
- vector_b = new float[n][1];
- vector_y = new float[n][1];
- // Get matrix A
- for(int i = 0 ; i < n ; i++){
- for(int j = 0 ; j < n ; j++){
- System.out.print("Enter the " + (i + 1) + " x " + (j + 1) + " element of the matrix: ");
- matrix_A[i][j] = uInput.nextFloat();
- }
- }
- System.out.println();
- // Get vector b
- for(int i = 0 ; i < n ; i++){
- System.out.print("Enter the " + (i + 1) + " x 1 element of vector b: ");
- vector_b[i][0] = uInput.nextFloat();
- }
- System.out.println();
- // Get precision
- System.out.print("This program will assume precision of 6.\n");
- precision = 6;
- epsilon = ((float) Math.pow(10 , -((double) precision)));
- System.out.println();
- // Get matrix U & vector p & report matrix L
- for(int k = 0 ; k < (n - 1) ; k++){ // Overarching loop
- // Find rows to permutate
- pivotMax = k; // Set initial max as the pivot being observed
- 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
- 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
- pivotMax = l; // Record largest value to swap in the permutation vector
- }
- }
- // Swap the rows such that the max value is in the pivot position
- temp = vector_p[k][0];
- vector_p[k][0] = vector_p[pivotMax][0];
- vector_p[pivotMax][0] = temp;
- // Eliminate all entries below the pivot and spread across the matrix
- for(int m = (k + 1) ; m < n ; m++){ // Loop through rows & find the elimination multiplicity per row
- eliminate = -(matrix_A[(vector_p[m][0])][k] / matrix_A[(vector_p[k][0])][k]);
- for(int o = k ; o < n ; o++){ // Loop through columns & eliminate
- matrix_A[(vector_p[m][0])][o] = eliminate*matrix_A[(vector_p[k][0])][o] + matrix_A[(vector_p[m][0])][o];
- }
- }
- }
- // Report matrix U
- System.out.println("\n\nFrom the factorization, matrix U is as follows:\n");
- for(int i = 0 ; i < n ; i++){
- for(int j = 0 ; j < n ; j++){
- System.out.printf("%-10.6f",matrix_A[vector_p[i][0]][j]);
- }
- System.out.println();
- }
- System.out.println();
- // Report vector p
- System.out.println("After the factorization, the permutation vector p is as follows:\n");
- for(int i = 0 ; i < n ; i++){
- System.out.println((vector_p[i][0] + 1));
- }
- System.out.println();
- // Check for singularity
- for(int i = 0 ; i < n ; i++){
- if(Math.abs(matrix_A[vector_p[i][0]][i]) < epsilon){ // If any diagonal element is 0
- isSingular = true;
- break;
- }
- }
- // Perform actions based on singularity
- if(isSingular){
- System.out.println("The matrix A is singular. Program will terminate.\n");
- System.exit(0);
- }else{
- System.out.println("The matrix A is non-singular. Program will solve Ax=b for x.\n");
- // Occupy vector x with the solution
- vector_x = callMethod.upperSovler(matrix_A , vector_x , vector_y , vector_p , epsilon , n);
- // Output x
- System.out.println("The solution vector x is as follows:\n");
- for(int i = 0 ; i < n ; i++){
- System.out.printf("%-10.6f" , vector_x[i][0]);
- System.out.println();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment