• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest May 23rd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import java.util.*;
2. import java.io.*;
3.
4. public class EigenvalueCalculator{
5.   public static void main(String[] args) {
6.       double[][] matrix;
7.       double det;
8.       int n;
9.
10.       Scanner scanner = new Scanner(System.in);
11.
12.       System.out.println("Dimension of the matrix: ");
13.       n = scanner.nextInt();
14.
15.       matrix = new double[n][n];
16.
17.       // insert values
18.       for (int i = 0; i < n; i++) {
19.          for (int j = 0; j < n; j++) {
20.
21.             System.out.printf("Values: " + i + " - " + j);
22.             System.out.printf("\n");
23.             matrix[i][j] = scanner.nextDouble();
24.
25.          }
26.       }
27.
28.       // calculate determinant
29.       det = findDeterminant(matrix);
30.       System.out.println("Determinant of matrix: " + det);
31.       System.out.println("Program Finished.");
32.    }
33.
34.   public static double findDeterminant(double[][] matrix){
35.     if(matrix.length == 2 && isSquareMatrix(matrix)){
36.       return (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]);
37.     }else{
38.       int sum = 0;
39.       for(int i=0;i<matrix.length;i++){
40.         double[][] cofactor = findCofactor(matrix,0, i);
41.         sum += (Math.pow(-1,i) * matrix[0][i] * findDeterminant(cofactor));
42.       }
43.       return sum;
44.     }
45.   }
46.
47.   public static boolean isSquareMatrix(double[][] matrix){
48.     return (matrix.length == matrix[0].length);
49.   }
50.
51.   public static double[][] findCofactor(double[][] matrix,int i,int j){
52.     double[][] coMatrix = new double[matrix.length-1][matrix.length-1];
53.     int coI = 0;
54.     int coJ = 0;
55.     for(int x=0;x<matrix.length;x++){
56.       if(x == i){
57.         continue;
58.       }
59.       for(int y=0;y<matrix.length;y++){
60.         if(y == j){
61.           continue;
62.         }
63.         System.out.println("X: " + x);
64.         System.out.println("Y: " + y);
65.         coMatrix[coI][coJ] = matrix[x][y];
66.         coJ++;
67.         if(coJ == coMatrix.length){
68.           coI++;
69.           coJ = 0;
70.         }
71.       }
72.     }
73.     return coMatrix;
74.   }
75.
76.   public static String
77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?