Advertisement
jasonpogi1669

Gaussian Elimination using Java

Jul 3rd, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package main;
  7.  
  8. /**
  9.  *
  10.  * @author markjasongalang
  11.  */
  12.  
  13. import java.io.*;
  14. import java.util.*;
  15.  
  16. public class Main {
  17.   static Vector<Vector<Double>> GetMatrix(double[][] matrix) {
  18.     Vector<Vector<Double>> temp_2D = new Vector<Vector<Double>>();
  19.     for (int i = 0; i < (int) matrix.length; i++) {
  20.       Vector<Double> temp_1D = new Vector<Double>();
  21.       for (int j = 0; j < (int) matrix[i].length; j++) {
  22.         temp_1D.add(matrix[i][j]);
  23.       }
  24.       temp_2D.add(temp_1D);
  25.     }
  26.     return temp_2D;
  27.   }
  28.  
  29.   static void Print(double[][] matrix) {
  30.     for (int i = 0; i < (int) matrix.length; i++) {
  31.       for (int j = 0; j < (int) matrix[i].length; j++) {
  32.         if (matrix[i][j] % 1 == 0) {
  33.           System.out.print((int) (matrix[i][j]) + " ");
  34.         } else {
  35.           System.out.printf("%.2f ", matrix[i][j]);
  36.         }
  37.       }
  38.       System.out.println("");
  39.     }
  40.   }
  41.  
  42.   public static void main(String[] args) {
  43.     Scanner in = new Scanner(System.in);
  44.     int n = 0;
  45.     while (true) {
  46.       System.out.print("Enter number of equations (2, 3, 4, 5, 6): ");
  47.       n = in.nextInt();
  48.       if (n != 2 && n != 3 && n != 4 && n != 5 && n != 6) {
  49.         System.out.println("Please try again.");
  50.       } else {
  51.         break;
  52.       }
  53.     }
  54.     // Choose if input or random
  55.     double[][] matrix = new double[n][n + 1];
  56.     while (true) {
  57.       System.out.print("Randomize values or not (Y/N): ");
  58.       char ch = in.next().charAt(0);
  59.       if (ch == 'Y' || ch == 'y') {
  60.         int mn = 1;
  61.         int mx = 100;
  62.         for (int i = 0; i < n; i++) {
  63.           for (int j = 0; j < n + 1; j++) {
  64.             matrix[i][j] = (int) Math.floor(Math.random() * (mx - mn + 1) + mn);
  65.           }
  66.         }
  67.         break;
  68.       } else if (ch == 'N' || ch == 'n') {
  69.         System.out.println("\nValues for the system of linear equations");
  70.         for (int i = 0; i < n; i++) {
  71.           System.out.println("Equation #" + (i + 1) + " values: (Enter " + (n + 1) + " values)");
  72.           for (int j = 0; j < n + 1; j++) {
  73.             matrix[i][j] = in.nextDouble();
  74.           }
  75.         }
  76.         break;
  77.       }
  78.       System.out.println("Please try again.");
  79.     }
  80.     // print equation form
  81.     System.out.println("\nEquation Form:");
  82.     for (int i = 0; i < n; i++) {
  83.       int coeff = 97;
  84.       for (int j = 0; j < n + 1; j++) {
  85.         if (j == n) {
  86.           System.out.print(" = ");
  87.         } else if (j > 0) {
  88.           System.out.print(matrix[i][j] > 0 ? " + " : " - ");
  89.         }
  90.         if (matrix[i][j] % 1 == 0) {
  91.           int val = (int) (matrix[i][j]);
  92.           if (j > 0 && j < n) {
  93.             System.out.print(val > 0 ? val: -val);
  94.           } else {
  95.             System.out.print(val);
  96.           }
  97.         } else {
  98.           double val = matrix[i][j];
  99.           if (j > 0 && j < n) {
  100.             System.out.printf("%.2f", val > 0 ? val : -val);
  101.           } else {
  102.             System.out.printf("%.2f", val);
  103.           }
  104.         }
  105.         if (j < n) {
  106.           System.out.print((char) (coeff));
  107.         }
  108.         coeff++;
  109.       }
  110.       System.out.println("");
  111.     }
  112.     // print the matrix form
  113.     System.out.println("\nMatrix Form:");
  114.     Print(matrix);
  115.     // solving part (for upper triangular matrix)
  116.     Vector<Vector<Vector<Double>>> v = new Vector<Vector<Vector<Double>>>();
  117.     int step = 1;
  118.     Vector<String> operations = new Vector<String>();
  119.     for (int i = 0; i < n - 1; i++) {
  120.       for (int j = i + 1; j < n; j++) {
  121.         double quotient = matrix[j][i] / matrix[i][i];
  122.         operations.add(String.valueOf("R" + (j + 1) + " + " + -quotient + "R" + (i + 1)));
  123.         for (int k = 0; k < n + 1; k++) {
  124.           matrix[j][k] = matrix[j][k] - (matrix[i][k] * quotient);
  125.         }
  126.         // store tables in 3D vector (for simulation)
  127.         v.add(GetMatrix(matrix));
  128.       }
  129.     }
  130.     // simulation
  131.     System.out.println("\nSimulation");
  132.     for (int i = 0; i < (int) v.size(); i++) {
  133.       System.out.print("\nRow Operation: ");
  134.       System.out.println(operations.get(i));
  135.       for (int j = 0; j < (int) v.get(i).size(); j++) {
  136.         for (int k = 0; k < (int) v.get(i).get(j).size(); k++) {
  137.           double element = v.get(i).get(j).get(k);
  138.           if (element % 1 == 0) {
  139.             System.out.print((int) (element) + " ");
  140.           } else {
  141.             System.out.printf("%.2f ", element);
  142.           }
  143.         }
  144.         System.out.println("");
  145.       }
  146.     }
  147.     // upper triangular matrix
  148.     System.out.println("\nUpper Triangular Matrix");
  149.     Print(matrix);
  150.     // getting the values part    
  151.     double[] solutions = new double[n];
  152.     for (int i = n - 1; i >= 0; i--) {
  153.       double res = 0;
  154.       for (int j = i + 1; j < n; j++) {
  155.         res += (matrix[i][j] * solutions[j]);
  156.       }
  157.       solutions[i] = (matrix[i][n] - res) / matrix[i][i];
  158.     }
  159.     // final part (solutions)
  160.     boolean checker = true;
  161.     for (int i = 0; i < n; i++) {
  162.       Double d = new Double(solutions[i]);
  163.       if (d.isNaN()) {
  164.         checker = false;
  165.         break;
  166.       }
  167.     }
  168.     System.out.println("\nSolutions:");
  169.     if (checker) {
  170.       int coeff = 97;
  171.       for (int i = 0; i < (int) solutions.length; i++) {
  172.         System.out.println((char) (coeff) + " = " + solutions[i]);
  173.         coeff++;
  174.       }
  175.     } else {
  176.       System.out.println("Infinitely Many Solutions or No Solutions");
  177.     }
  178.     System.out.println("");
  179.   }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement