Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class EigenvalueCalculator{
- public static void main(String[] args) {
- double[][] matrix;
- double det;
- int n;
- Scanner scanner = new Scanner(System.in);
- System.out.println("Dimension of the matrix: ");
- n = scanner.nextInt();
- matrix = new double[n][n];
- // insert values
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.printf("Values: " + i + " - " + j);
- System.out.printf("\n");
- matrix[i][j] = scanner.nextDouble();
- }
- }
- // calculate determinant
- det = findDeterminant(matrix);
- System.out.println("Determinant of matrix: " + det);
- System.out.println("Program Finished.");
- }
- public static double findDeterminant(double[][] matrix){
- if(matrix.length == 2 && isSquareMatrix(matrix)){
- return (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]);
- }else{
- int sum = 0;
- for(int i=0;i<matrix.length;i++){
- double[][] cofactor = findCofactor(matrix,0, i);
- sum += (Math.pow(-1,i) * matrix[0][i] * findDeterminant(cofactor));
- }
- return sum;
- }
- }
- public static boolean isSquareMatrix(double[][] matrix){
- return (matrix.length == matrix[0].length);
- }
- public static double[][] findCofactor(double[][] matrix,int i,int j){
- double[][] coMatrix = new double[matrix.length-1][matrix.length-1];
- int coI = 0;
- int coJ = 0;
- for(int x=0;x<matrix.length;x++){
- if(x == i){
- continue;
- }
- for(int y=0;y<matrix.length;y++){
- if(y == j){
- continue;
- }
- System.out.println("X: " + x);
- System.out.println("Y: " + y);
- coMatrix[coI][coJ] = matrix[x][y];
- coJ++;
- if(coJ == coMatrix.length){
- coI++;
- coJ = 0;
- }
- }
- }
- return coMatrix;
- }
- public static String
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement