Advertisement
Narzew

Determinant.java

Jan 11th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1.  
  2. package determinant;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Determinant {
  7.    
  8.     //======================================
  9.     // ** calculateDeterminant of the matrix
  10.     // ** A - left side matrix
  11.     // ** B - right side matrix
  12.     //======================================
  13.    
  14.     public static double calculateDeterminant(double[][] A) {
  15.         int n = A.length;
  16.         double[] b = {1,2,3,4,5,6,7,8,9,10};
  17.         int numofswaps=0;
  18.  
  19.        
  20.         for (int p = 0; p < n; p++) {
  21.  
  22.             //======================================
  23.             //** Find pivot row and swap it
  24.             //======================================
  25.            
  26.             int max = p;
  27.             for (int i = p + 1; i < n; i++) {
  28.                 if (Math.abs(A[i][p]) > Math.abs(A[max][p])) {
  29.                     max = i;
  30.                     numofswaps++;
  31.                 }
  32.             }
  33.             double[] temp = A[p]; A[p] = A[max]; A[max] = temp;
  34.             double   t    = b[p]; b[p] = b[max]; b[max] = t;
  35.  
  36.             //======================================
  37.             // ** Do standard GE
  38.             //======================================
  39.            
  40.             for (int i = p + 1; i < n; i++) {
  41.                 double factor = A[i][p] / A[p][p];
  42.                 b[i] -= factor * b[p];
  43.                 for (int j = p; j < n; j++) {
  44.                     A[i][j] -= factor * A[p][j];
  45.                 }
  46.             }
  47.         }
  48.  
  49.         //======================================
  50.         // ** Do back substitution
  51.         //======================================
  52.        
  53.         double[] x = new double[n];
  54.         for (int i = n - 1; i >= 0; i--) {
  55.             double sum = 0.0;
  56.             for (int j = i + 1; j < n; j++) {
  57.                 sum += A[i][j] * x[j];
  58.             }
  59.             x[i] = (b[i] - sum) / A[i][i];
  60.         }
  61.        
  62.         //======================================
  63.         // ** After GE, calculate determinant
  64.         //======================================
  65.        
  66.         System.out.println("Number of swaps: "+numofswaps);
  67.         double det = 1;
  68.         for(int i=0;i<n;i++){
  69.             det = det*A[i][i];
  70.         }
  71.         if(numofswaps%2==0){
  72.             det = -det;
  73.         }
  74.        
  75.         return det;
  76.     }
  77.  
  78.     //======================================
  79.     // ** Main
  80.     //======================================
  81.    
  82.     public static void main(String[] args) {
  83.        
  84.         //======================================
  85.         // ** Define variables
  86.         //======================================
  87.        
  88.         double[][] A;
  89.         double[] b;
  90.        
  91.         //======================================
  92.         // ** Get data from user
  93.         //======================================
  94.        
  95.         System.out.println("Enter matrix dimension: ");
  96.         Scanner sc = new Scanner(System.in);
  97.         int n = sc.nextInt();
  98.         // Initialize vars
  99.         A = new double[n][n];
  100.         b = new double[n];
  101.        
  102.         System.out.println("Enter left-side matrix: ");
  103.         for(int i=0; i<n; i++)
  104.             for(int j=0; j<n; j++)
  105.                 A[i][j] = sc.nextDouble();
  106.         System.out.println("Enter right-side matrix: ");
  107.         for(int j=0;j<n;j++){
  108.             b[j] = sc.nextDouble();
  109.         }
  110.  
  111.         //======================================
  112.         //** Calculate determinant
  113.         //======================================
  114.        
  115.         double result = calculateDeterminant(A);
  116.        
  117.         //======================================
  118.         // ** Round determinant
  119.         //======================================
  120.  
  121.         result = Math.round(result);
  122.        
  123.         //======================================
  124.         // ** Print result
  125.         //======================================
  126.        
  127.         System.out.println("Determinant: "+result);
  128.  
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement