Minggu, 23 Januari 2011

Program Mencari Determinan Menggunakan Kofaktor


/*
* Matriks.java
*
* Created on January 23, 2011, 4:28 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author Mr. Miscall
*/
public class Matriks {
protected int baris;
protected int kolom;
protected double[][] nilai;

/*constructor -- Nilai dari matriks diset nol*/
public Matriks(int baris, int koloms) {
if (baris <= 0 || koloms <= 0) throw new IndexOutOfBoundsException();
this.baris = baris;
this.kolom = koloms;
this.nilai = new double[baris][koloms];
this.zero();
}

/*Full constructor -- Nilai dari matriks diset sesuai dengan nilai yang diberikan*/
public Matriks(int baris, int koloms, double[][] nilai) {
this(baris, koloms);
this.set(nilai);
}

/*Prosedure Membuat Matriks Nol*/
public void zero() {
for (int i = 0; i < this.baris; i++) {
for (int j = 0; j < this.kolom; j++) {
this.nilai[i][j] = 0;
}
}
}

/*Prosedure Menset Nilai Matriks*/
public void set(double[][] nilai) {
if (nilai == null) return;
int baris = nilai.length;
if (baris == 0) return;
for (int i = 0; i < baris && i < this.baris; i++) {
double[] row = nilai[i];
if (row != null) {
int kolom = row.length;
if (kolom != 0) {
kolom = kolom > this.kolom ? this.kolom : kolom;
System.arraycopy(row, 0, this.nilai[i], 0, kolom);
}
}
}
}

/*Fungsi Mengambil SubMatriks*/
public Matriks getSubMatriks(int row, int col) throws IndexOutOfBoundsException {
if (this.baris == 1 && row == 0) throw new IndexOutOfBoundsException();
if (this.kolom == 1 && col >= 0) throw new IndexOutOfBoundsException();

int nRows = (row >= this.baris || row < 0) ? this.baris : this.baris - 1;
int nCols = (col >= this.kolom || col < 0) ? this.kolom : this.kolom - 1;

int a = 0;
int b = 0;

Matriks m = new Matriks(nRows, nCols);

for (int i = 0; i < baris; i++) {
if (i != row) {
for (int j = 0; j < kolom; j++) {
if (j != col) {
m.nilai[a][b] = this.nilai[i][j];
b++;
}
}
a++;
b = 0;
}
}
return m;
}

/*Fungsi Rekursif Mencari Determinan Metode Cofactor*/
public double determinant(){
//untuk matriks bukan persegi
if (this.baris != this.kolom) {System.out.print("Tidak bisa!! Jumlah baris harus sama dengan jumlah kolom."); return 0;}

//untuk matriks 1x1
if (this.baris == 1) return this.nilai[0][0];

//untuk matriks 2x2
if (this.baris == 2) return this.nilai[0][0] * this.nilai[1][1] - this.nilai[0][1] * this.nilai[1][0];

//untuk matriks 3x3 ke atas
double det = 0;
for (int i = 0; i < baris; i++) {
det += (i % 2 == 0 ? 1 : -1) * this.nilai[i][0] * this.getSubMatriks(i, 0).determinant();
}
return det;
}

public void cetakIsiMatriks() {
System.out.println("Matriks =");
for(int i=0; i < this.baris; i++){
for(int j=0; j < this.kolom; j++){
System.out.print(this.nilai[i][j]+" ");
}
System.out.println();
}
}

public static void main(String[] args){
Matriks a = new Matriks(1, 1, new double[][] {{1}});
a.cetakIsiMatriks();
System.out.println("Determinan = "+a.determinant());

Matriks b = new Matriks(2, 2, new double[][] {{1, 3}, {1, 1}});
b.cetakIsiMatriks();
System.out.println("Determinan = "+b.determinant());

Matriks c = new Matriks(2, 1, new double[][] {{1}, {2}});
c.cetakIsiMatriks();
System.out.println("Determinan = "+c.determinant());

Matriks d = new Matriks(3, 3, new double[][] {{1, 3, 1}, {1, 1, 2}, {2, 3, 4}});
d.cetakIsiMatriks();
System.out.println("Determinan = "+d.determinant());

Matriks e = new Matriks(5, 5, new double[][] {{1, 1, 1, 1, 1}, {1, 2, 1, 2, 1}, {2, 3, 4, 3, 1}, {1, 1, 7, 5, 1},{1, 1, 1, 5, 1}});
e.cetakIsiMatriks();
System.out.println("Determinan = "+e.determinant());
}
}

Tidak ada komentar: