Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package determinant;
- import java.util.Scanner;
- public class Determinant {
- //======================================
- // ** calculateDeterminant of the matrix
- // ** A - left side matrix
- // ** B - right side matrix
- //======================================
- public static double calculateDeterminant(double[][] A) {
- int n = A.length;
- double[] b = {1,2,3,4,5,6,7,8,9,10};
- int numofswaps=0;
- for (int p = 0; p < n; p++) {
- //======================================
- //** Find pivot row and swap it
- //======================================
- int max = p;
- for (int i = p + 1; i < n; i++) {
- if (Math.abs(A[i][p]) > Math.abs(A[max][p])) {
- max = i;
- numofswaps++;
- }
- }
- double[] temp = A[p]; A[p] = A[max]; A[max] = temp;
- double t = b[p]; b[p] = b[max]; b[max] = t;
- //======================================
- // ** Do standard GE
- //======================================
- for (int i = p + 1; i < n; i++) {
- double factor = A[i][p] / A[p][p];
- b[i] -= factor * b[p];
- for (int j = p; j < n; j++) {
- A[i][j] -= factor * A[p][j];
- }
- }
- }
- //======================================
- // ** Do back substitution
- //======================================
- double[] x = new double[n];
- for (int i = n - 1; i >= 0; i--) {
- double sum = 0.0;
- for (int j = i + 1; j < n; j++) {
- sum += A[i][j] * x[j];
- }
- x[i] = (b[i] - sum) / A[i][i];
- }
- //======================================
- // ** After GE, calculate determinant
- //======================================
- System.out.println("Number of swaps: "+numofswaps);
- double det = 1;
- for(int i=0;i<n;i++){
- det = det*A[i][i];
- }
- if(numofswaps%2==0){
- det = -det;
- }
- return det;
- }
- //======================================
- // ** Main
- //======================================
- public static void main(String[] args) {
- //======================================
- // ** Define variables
- //======================================
- double[][] A;
- double[] b;
- //======================================
- // ** Get data from user
- //======================================
- System.out.println("Enter matrix dimension: ");
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- // Initialize vars
- A = new double[n][n];
- b = new double[n];
- System.out.println("Enter left-side matrix: ");
- for(int i=0; i<n; i++)
- for(int j=0; j<n; j++)
- A[i][j] = sc.nextDouble();
- System.out.println("Enter right-side matrix: ");
- for(int j=0;j<n;j++){
- b[j] = sc.nextDouble();
- }
- //======================================
- //** Calculate determinant
- //======================================
- double result = calculateDeterminant(A);
- //======================================
- // ** Round determinant
- //======================================
- result = Math.round(result);
- //======================================
- // ** Print result
- //======================================
- System.out.println("Determinant: "+result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement