Advertisement
arch239

Gauss-Jordan Elimination Method

Sep 4th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. import static java.lang.Math.abs;
  5. import static java.lang.Math.round;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         SquareMatrix m = new SquareMatrix(readMatrix());
  10.         System.out.println(round(m.getDeterminant()));
  11.     }
  12.  
  13.     private static int[][] readMatrix() {
  14.         Scanner scanner = new Scanner(System.in);
  15.         int n = scanner.nextInt();
  16.  
  17.         int[][] result = new int[n][n];
  18.  
  19.         for (int i = 0; i < n; i++)
  20.             for (int j = 0; j < n; j++)
  21.                 result[i][j] = scanner.nextInt();
  22.  
  23.         return result;
  24.     }
  25. }
  26.  
  27. class SquareMatrix {
  28.     private Vector[] x;
  29.  
  30.     private double get(int i, int j) {
  31.         return x[i].get(j);
  32.     }
  33.  
  34.     private void set(int i, int j, double value) {
  35.         x[i].set(j, value);
  36.     }
  37.  
  38.     private SquareMatrix() {
  39.     }
  40.  
  41.     private SquareMatrix(int n) {
  42.         x = new Vector[n];
  43.         for (int i = 0; i < n; i++)
  44.             x[i] = new Vector(n);
  45.     }
  46.  
  47.     SquareMatrix(int[][] a) {
  48.         this(a.length);
  49.  
  50.         if (!Arrays.stream(a).allMatch(i -> i.length == x.length))
  51.             throw new RuntimeException("Dimensions are not equal.");
  52.  
  53.         for (int i = 0; i < x.length; i++)
  54.             for (int j = 0; j < x.length; j++)
  55.                 set(i, j, a[i][j]);
  56.     }
  57.  
  58.     private SquareMatrix deepClone() {
  59.         SquareMatrix result = new SquareMatrix();
  60.  
  61.         result.x = Arrays.stream(x)
  62.                 .map(Vector::deepClone)
  63.                 .toArray(Vector[]::new);
  64.  
  65.         return result;
  66.     }
  67.  
  68.     double getDeterminant() {
  69.         double result = 1;
  70.         SquareMatrix m = deepClone();
  71.  
  72.         for (int k = 0; k < m.x.length; k++) {
  73.             if (!m.swapToMaxRow(k))
  74.                 return 0;
  75.  
  76.             for (int i = k + 1; i < m.x.length; i++) {
  77.                 double d = m.get(i, k) / m.get(k, k);
  78.  
  79.                 set(i, k, 0);
  80.  
  81.                 for (int j = k + 1; j < m.x.length; j++)
  82.                     m.x[i].x[j] -= d * m.get(k, j);
  83.             }
  84.  
  85.             result *= m.get(k, k);
  86.         }
  87.  
  88.         return result;
  89.     }
  90.  
  91.     private boolean swapToMaxRow(int k) {
  92.         int index = k;
  93.         double max = abs(get(k, k));
  94.  
  95.         for (int j = k + 1; j < x.length; j++) {
  96.             double d = abs(get(j, k));
  97.  
  98.             if (d > max) {
  99.                 max = abs(get(j, k));
  100.                 index = j;
  101.             }
  102.         }
  103.  
  104.         if (max == 0)
  105.             // the matrix is degenerate
  106.             return false;
  107.  
  108.         if (index != k) {
  109.             negateRow(k);
  110.             swapRows(k, index);
  111.         }
  112.  
  113.         return true;
  114.     }
  115.  
  116.     private void swapRows(int i, int j) {
  117.         Vector temp = x[i];
  118.         x[i] = x[j];
  119.         x[j] = temp;
  120.     }
  121.  
  122.     private void negateRow(int i) {
  123.         x[i].negate();
  124.     }
  125. }
  126.  
  127. class Vector {
  128.     double[] x;
  129.  
  130.     double get(int i) {
  131.         return x[i];
  132.     }
  133.  
  134.     void set(int i, double value) {
  135.         x[i] = value;
  136.     }
  137.  
  138.     Vector(int n) {
  139.         x = new double[n];
  140.     }
  141.  
  142.     private Vector(double... a) {
  143.         x = a;
  144.     }
  145.  
  146.     Vector deepClone() {
  147.         return new Vector(x.clone());
  148.     }
  149.  
  150.     void negate() {
  151.         x = Arrays.stream(x).map(x -> -x).toArray();
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement