[Java] Determinant of a Matrix - Muhammad Faruq Nuruddinsyah
[Java] Determinant of a Matrix

Algoritma untuk menghitung nilai determinan dari suatu matriks n x n dengan menggunakan teknik divide and conquer.

Contoh input:
1 2
3 4

Output:
-2

Contoh input:
1 2 3 4
2 3 4 1
3 4 2 1
4 3 1 2

Output:
60

Source Code:
// Algoritma untuk menghitung nilai determinan dari suatu matriks n x n
// dengan menggunakan teknik divide and conquer
// 
// Faruq @ June 3, 2013
 
 
import java.util.Scanner;
 
public class Determinan{
    public static void main(String[] args){
        new Determinan();
    }
 
    public Determinan(){
        Scanner scan = new Scanner(System.in);
 
        int n = scan.nextInt();
        double[][] matriks = new double[n][n];
 
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                matriks[i][j] = scan.nextDouble();
            }
        }
 
        double determinan = hitungDeterminan(matriks);
 
        System.out.println(determinan);
    }
 
    private double hitungDeterminan(double[][] matriks){
        if(matriks.length == 0 || matriks.length != matriks[0].length){
            System.out.println("Error: Matriks harus berukuran n x n, dan n > 0");
            return 0;
        }
 
        if(matriks.length == 1){
            return matriks[0][0];
        }else if(matriks.length == 2){
            return (matriks[0][0] * matriks[1][1]) - (matriks[1][0] * matriks[0][1]);
        }else{
            int row = indeksBaris(matriks);
 
            if(row == -1){
                return 0;
            }else{
                double r = matriks[row][0];
                double[][] tmp = sederhanakanMatriks(row, matriks);
                double[][] m = new double[tmp.length - 1][tmp.length - 1];
 
                int x = 0;
 
                for(int i = 0; i < tmp.length; i++){
                    if(i != row){
                        for(int j = 0; j < m.length; j++){
                            m[x][j] = tmp[i][j + 1];
                        }
 
                        x++;
                    }
                }
 
                return r * hitungDeterminan(m);
            }
        }
    }
 
    private double[][] sederhanakanMatriks(int row, double[][] matriks){
        double[][] result = new double[matriks.length][matriks.length];
 
        double r = matriks[row][0];
 
        for(int i = 0; i < matriks.length; i++){
            double d = matriks[i][0];
 
            for(int j = 0; j < matriks.length; j++){
                if(i == row){
                    result[i][j] = matriks[i][j];
                }else{
                    result[i][j] = matriks[i][j] - (d / r * matriks[row][j]);
                }
            }
        }
 
        return result;
    }
 
    private int indeksBaris(double[][] matriks){
        int result = -1;
 
        for(int i = 0; i < matriks.length; i++){
            if(matriks[i][0] != 0){
                return i;
            }
        }
 
        return result;
    }
}

Informasi:
Lihat semua daftar ACS - Download: Determinan.java - Tanggal: 3 Juni 2013 - Kategori: Java