Rabu, 16 April 2014

Gauss Matrix dengan GUI

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package matriks_algo2;

import javax.swing.JOptionPane;

/**
 *
 * @author Dyah
 */
public class Matriks_algo2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "SELAMAT DATANG DIPROGRAM MATRIKS");
      
        int baris = Integer.parseInt(JOptionPane.showInputDialog("masukkan banyak baris"));
        int kolom = Integer.parseInt(JOptionPane.showInputDialog("masukkan banyak kolom"));
       
       
      
      
      
        int C [][] = new int [baris][kolom];
        int D [][] = new int [baris][kolom];
      
      
      
     //matriks A
         int A [][] = new int [baris][kolom];
         System.out.println("Matriks A : \n");
for(int i=0;i<baris;i++){
System.out.print(" | ");
for(int j=0;j< baris;j++){
A[i][j]=Integer.parseInt(JOptionPane.showInputDialog(null,"Matriks A Baris Ke-"+(i+1)+" Kolom Ke-"+(j+1)));
System.out.print("  "+A[i][j]+ "  ");
}
System.out.println(" | ");
        }

//matriks B
 int B [][] = new int [baris][kolom];
         System.out.println("Matriks B : \n");
for(int i=0;i<kolom;i++){
System.out.print(" | ");
for(int j=0;j< kolom;j++){
B[i][j]=Integer.parseInt(JOptionPane.showInputDialog(null,"Matriks B Baris Ke-"+(i+1)+" Kolom Ke-"+(j+1)));
System.out.print("  "+B[i][j]+ "  ");
}
System.out.println(" | ");
        }
 int menu = Integer.parseInt(JOptionPane.showInputDialog("SELAMAT DATANG DI PROGRAM MATRIKS \n"
                + "Silahkan Pilih Operasi : \n"
                + "1. Penjumlahan \n"
                + "2. Perkalian \n"
                + "3. Pengurangan \n"
                + "4. Pembagian \n"
                + "5. Determinan \n"
                + "6. Kofaktor \n"
                + "7. Invers \n"
                + "8. Eliminasi Gauss \n"
                + "9. Gauss Jordan \n"
                + "10. Crammer"));

//penjumlahan
if (menu == 1){
  

    System.out.println("Hasil matrik");
for(int i=0; i<baris; i++){
System.out.print("|");
for(int j=0; j<kolom; j++){

System.out.print("  " + (A[i][j] + B[i][j]) +"  ");
}
System.out.println("|");
}

    }
if (menu == 2){
    System.out.println("Hasil matrik");
for(int i=0; i<baris; i++){
System.out.print("|");
for(int j=0; j<kolom; j++){
 for (int k=0;k<baris;k++){
C[i][j]= C[i][j]+(A[i][k]*B[k][j]);
 }
System.out.print("  " + C[i][j]+"  ");
}
System.out.println("|");
   
}

   
}
if (menu == 3){
    System.out.println("Hasil matrik");
for(int i=0; i<baris; i++){
System.out.print("|");
for(int j=0; j<kolom; j++){

System.out.print("  " + (A[i][j] - B[i][j]) +"  ");
}
System.out.println("|");
   
}
}
else if (menu == 4){
     System.out.println("Maav tidak ditemukan hasil untuk pembagian matriks karena matrik tidak bisa dibagi");

   
}


else if (menu == 5){
//    System.out.println("");
//    for (int i = 0; i<baris; i++){
//      
//        for (int j=0; j<kolom; j++){
//           int k = (A[i][j]*A[i+1][j+1]);
//           int l = A[i+1][j]*A[i][j+1];
//                  
//                    int u = (B[i][j]*B[i+1][j+1]);
//           int v = B[i+1][j]*B[i][j+1];
//           System.out.println(" Determinan Matriks A : \n"+(k-l));
//           System.out.println("Determinan Matriks B : \n "+(u-v));
//                }
//        
        //A[i+1][j]*A[i][j+1]
 float detA=(A[0][0]*A[1][1]*A[2][2])+
(A[0][1]*A[1][2]*A[2][0])+
(A[0][2]*A[1][0]*A[2][1]);
float detB=(A[0][1]*A[1][0]*A[2][2])+
(A[0][0]*A[1][2]*A[2][1])+
(A[0][2]*A[1][1]*A[2][0]);
float determinanA = detA - detB;
System.out.println("Determiunan Matriks A = "+determinanA);

float deta=(B[0][0]*B[1][1]*B[2][2])+
(B[0][1]*B[1][2]*B[2][0])+
(B[0][2]*B[1][0]*B[2][1]);
float detb=(B[0][1]*B[1][0]*B[2][2])+
(B[0][0]*B[1][2]*B[2][1])+
(B[0][2]*B[1][1]*B[2][0]);
float determinanB = deta - detb;
System.out.println("Determinan Matriks B = "+determinanB);

//System.out.println(" Determinan Matriks B : "+detB);
 //adjoin matriks
    int tempA[][]=new int[3][3];
    tempA[0][0]=A[1][1]* A[2][2]-A[2][1]*A [1][2];
    tempA[0][1]=A[1][0]* A[2][2]-A[2][0]*A [1][2];
    tempA[0][2]=A[1][0]* A[2][1]-A[1][1]*A [2][0];
    tempA[1][0]=A[0][1]* A[2][2]-A[0][2]*A [2][1];
    tempA[1][1]=A[0][0]* A[2][2]-A[0][2]*A [2][0];
    tempA[1][2]=A[0][0]* A[2][1]-A[0][1]*A [2][0];
    tempA[2][0]=A[1][1]* A[2][2]-A[1][2]*A [2][1];
    tempA[2][1]=A[0][0]* A[1][2]-A[0][2]*A [1][0];
    tempA[2][2]=A[0][0]* A[1][1]-A[0][1]*A [1][0];
        System.out.println ();
    System.out.println ("Adjoin Matriks : ");
    for (int i=0;i<3;i++)
    {
        System.out.print("| ");
        for (int j=0;j<3;j++)
        {
            System.out.print(tempA[i][j]+" ");
        }
        System.out.println("|");
       
       
   
    }
        //B
        int temp[][]=new int[3][3];
        temp[0][0]=B[1][1]* B[2][2]-B[2][1]*B [1][2];
    temp[0][1]=B[1][0]* B[2][2]-B[2][0]*B [1][2];
    temp[0][2]=B[1][0]* B[2][1]-B[1][1]*B [2][0];
    temp[1][0]=B[0][1]* B[2][2]-B[0][2]*B [2][1];
    temp[1][1]=B[0][0]* B[2][2]-B[0][2]*B [2][0];
    temp[1][2]=B[0][0]* B[2][1]-B[0][1]*B [2][0];
    temp[2][0]=B[1][1]* B[2][2]-B[1][2]*B [2][1];
    temp[2][1]=B[0][0]* B[1][2]-B[0][2]*B [1][0];
    temp[2][2]=B[0][0]* B[1][1]-B[0][1]*B [1][0];
       System.out.println ();
    //System.out.println ("Adjoin Matriks : ");
    for (int i=0;i<3;i++)
    {
        System.out.print("| ");
        for (int j=0;j<3;j++)
        {
            System.out.print(temp[i][j]+" ");
        }
        System.out.println("|");
       
       
   
    }


}
else if (menu == 6){
     double m00 = (Math.pow(-1, 2))*((A[1][1]*A[2][2])-(A[1][2]*A[2][1]));
    double m01 = (Math.pow(-1, 3))*((A[1][0]*A[2][2])- (A[1][2]*A[2][0]));
    double m02 = (Math.pow(-1, 4))*((A[1][0]*A[2][1])-(A[1][1]*A[2][0]));
    double m10 = (Math.pow(-1, 3))*((A[0][1]*A[2][2])-(A[0][2]*A[2][1]));
    double m11 = (Math.pow(-1, 4))*((A[0][0]*A[2][2])-(A[0][2]*A[2][0]));
    double m12= (Math.pow(-1, 5))*((A[0][0]*A[2][1])-(A[0][1]*A[2][0]));
    double m20 = (Math.pow(-1, 4))*((A[0][1]*A[1][2])- (A[0][2]*A[1][1]));
    double m21 = (Math.pow(-1, 5))*((A[0][0]*A[1][2])-(A[0][2]*A[1][0]));
    double m22 = (Math.pow(-1, 6))*((A[0][0]*A[1][1])- (A[0][1]*A[1][0]));
    // for (int i =0; i<3;i++){
    //for (int j = 0;j<3;j++){
    System.out.println("Kofaktor Matriks A adalah : \n"
            + "C11 :" +m00+" \n"
            + ""
            + "C12 :" +m01+"  \n"
            + ""
            + "C13 :" +m02+"  \n"
            + ""
            + "C21 :" +m10+"  \n"
            + ""
            + "C22 :" +m11+"  \n"
            + ""
            + "C23 :" +m12+" \n"
            + ""
            + "C31 :" +m20+"  \n"
            + ""
            + "C32 :" +m21+"  \n"
            + ""
            + "C33 :" +m22+"  \n"
            + "");
    //matriks B
  
     double mb00 = (Math.pow(-1, 2))*((B[1][1]*B[2][2])-(B[1][2]*B[2][1]));
    double mb01 = (Math.pow(-1, 3))*((B[1][0]*B[2][2])- (B[1][2]*B[2][0]));
    double mb02 = (Math.pow(-1, 4))*((B[1][0]*B[2][1])-(B[1][1]*B[2][0]));
    double mb10 = (Math.pow(-1, 3))*((B[0][1]*B[2][2])-(B[0][2]*B[2][1]));
    double mb11 = (Math.pow(-1, 4))*((B[0][0]*B[2][2])-(B[0][2]*B[2][0]));
    double mb12= (Math.pow(-1, 5))*((B[0][0]*B[2][1])-(B[0][1]*A[2][0]));
    double mb20 = (Math.pow(-1, 4))*((B[0][1]*B[1][2])- (B[0][2]*A[1][1]));
    double mb21 = (Math.pow(-1, 5))*((B[0][0]*B[1][2])-(B[0][2]*A[1][0]));
    double mb22 = (Math.pow(-1, 6))*((B[0][0]*B[1][1])- (B[0][1]*A[1][0]));
    // for (int i =0; i<3;i++){
    //for (int j = 0;j<3;j++){
    System.out.println("Kofaktor Matriks B adalah : \n"
            + "C11 :" +mb00+" \n"
            + ""
            + "C12 :" +mb01+"  \n"
            + ""
            + "C13 :" +mb02+"  \n"
            + ""
            + "C21 :" +mb10+"  \n"
            + ""
            + "C22 :" +mb11+"  \n"
            + ""
            + "C23 :" +mb12+" \n"
            + ""
            + "C31 :" +mb20+"  \n"
            + ""
            + "C32 :" +mb21+"  \n"
            + ""
            + "C33 :" +mb22+"  \n"
            + "");
}
else if (menu == 7) //invers
{
    int detA=(A[0][0]*A[1][1]*A[2][2])+
(A[0][1]*A[1][2]*A[2][0])+
(A[0][2]*A[1][0]*A[2][1]);
int detB=(A[0][1]*A[1][0]*A[2][2])+
(A[0][0]*A[1][2]*A[2][1])+
(A[0][2]*A[1][1]*A[2][0]);
int determinanA = detA - detB;

 //adjoin matriks
    int temp[][]=new int[3][3];
    temp[0][0]=A[1][1]* A[2][2]-A[2][1]*A [1][2];
    temp[0][1]=A[1][0]* A[2][2]-A[2][0]*A [1][2];
    temp[0][2]=A[1][0]* A[2][1]-A[1][1]*A [2][0];
    temp[1][0]=A[0][1]* A[2][2]-A[0][2]*A [2][1];
    temp[1][1]=A[0][0]* A[2][2]-A[0][2]*A [2][0];
    temp[1][2]=A[0][0]* A[2][1]-A[0][1]*A [2][0];
    temp[2][0]=A[1][1]* A[2][2]-A[1][2]*A [2][1];
    temp[2][1]=A[0][0]* A[1][2]-A[0][2]*A [1][0];
    temp[2][2]=A[0][0]* A[1][1]-A[0][1]*A [1][0];
    if (determinanA !=0)
    {
    temp[0][0]=temp[0][0]/determinanA;
    temp[0][1]=temp[0][1]/determinanA;
    temp[0][2]=temp[0][2]/determinanA;
    temp[1][0]=temp[1][0]/determinanA;
    temp[1][1]=temp[1][1]/determinanA;
    temp[1][2]=temp[1][2]/determinanA;
    temp[2][0]=temp[2][0]/determinanA;
    temp[2][1]=temp[2][1]/determinanA;
    temp[2][2]=temp[2][2]/determinanA;
   
    System.out.println ();
    System.out.println ("Invers Matriks A : ");
    for (int i=0;i<3;i++)
    {
        System.out.print("| ");
        for (int j=0;j<3;j++)
        {
            System.out.print(temp[i][j]+" ");
        }
        System.out.println("|");
    }
    }
   
    int deta=(B[0][0]*B[1][1]*B[2][2])+
(B[0][1]*B[1][2]*B[2][0])+
(B[0][2]*B[1][0]*B[2][1]);
int detb=(B[0][1]*B[1][0]*B[2][2])+
(B[0][0]*B[1][2]*B[2][1])+
(B[0][2]*B[1][1]*B[2][0]);
int determinanB = deta - detb;
    int tempp[][]=new int[3][3];
        tempp[0][0]=B[1][1]* B[2][2]-B[2][1]*B [1][2];
    tempp[0][1]=B[1][0]* B[2][2]-B[2][0]*B [1][2];
    tempp[0][2]=B[1][0]* B[2][1]-B[1][1]*B [2][0];
    tempp[1][0]=B[0][1]* B[2][2]-B[0][2]*B [2][1];
    tempp[1][1]=B[0][0]* B[2][2]-B[0][2]*B [2][0];
    tempp[1][2]=B[0][0]* B[2][1]-B[0][1]*B [2][0];
    tempp[2][0]=B[1][1]* B[2][2]-B[1][2]*B [2][1];
    tempp[2][1]=B[0][0]* B[1][2]-B[0][2]*B [1][0];
    tempp[2][2]=B[0][0]* B[1][1]-B[0][1]*B [1][0];
       
        if (determinanB!=0)
    {
    tempp[0][0]=tempp[0][1]/determinanB;
    tempp[0][2]=tempp[0][2]/determinanB;
    tempp[1][0]=tempp[1][0]/determinanB;
    tempp[1][1]=tempp[1][1]/determinanB;
    tempp[1][2]=tempp[1][2]/determinanB;
    tempp[2][0]=tempp[2][0]/determinanB;
    tempp[2][1]=tempp[2][1]/determinanB;
    tempp[2][2]=tempp[2][2]/determinanB;
       
        System.out.println ();
    System.out.println ("Invers Matriks B : ");
    for (int i=0;i<3;i++)
    {
        System.out.print("| ");
        for (int j=0;j<3;j++)
        {
            System.out.print(tempp[i][j]+" ");
        }
        System.out.println("|");
    }
    }
    }
else if (menu == 8){
   
    //eliminasi gaus
    if (baris== 2 && kolom == 2){
        System.out.println("Metode Eliminasi Gauss");
    System.out.println("Matriks A");
    System.out.println("| " + (Math.pow(A[0][0], 0))+ "  " +A[0][1]+  " | ");
    System.out.println("| " + (A[1][0]*0)+ "  " +(Math.pow(A[1][1], 0))+  " | ");

     System.out.println("Matriks B");
    System.out.println("| " + (Math.pow(B[0][0], 0))+ "  " +B[0][1]+  " | ");
    System.out.println("| " + (B[1][0]*0)+ "  " +(Math.pow(B[1][1], 0))+  " | ");

    }
    else {
    System.out.println("Metode Eliminasi Gauss");
    System.out.println("Matriks A");
    System.out.println("| " + (Math.pow(A[0][0], 0))+ "  " +A[0][1]+ "  " + A[0][2]+ " | ");
    System.out.println("| " + (A[1][0]*0)+ "  " +(Math.pow(A[1][1], 0))+ "  " + A[1][2]+ " | ");
    System.out.println("| " + (A[2][0]*0)+ "  " +A[0][1]*0+ "  " + (Math.pow(A[2][2], 0))+ " | ");

     System.out.println("Matriks B");
    System.out.println("| " + (Math.pow(B[0][0], 0))+ "  " +B[0][1]+ "  " + B[0][2]+ " | ");
    System.out.println("| " + (B[1][0]*0)+ "  " +(Math.pow(B[1][1], 0))+ "  " + B[1][2]+ " | ");
    System.out.println("| " + (B[2][0]*0)+ "  " +B[0][1]*0+ "  " + (Math.pow(B[2][2], 0))+ " | ");
}}
else if (menu == 9){
    //gauss jordan
    if (baris == 2 && kolom == 2){
        System.out.println("Metode Eliminasi Gauss Jordan");
    System.out.println("Matriks A");
    System.out.println("| " + (Math.pow(A[0][0], 0))+ "  " +A[0][1]*0+  " | ");
    System.out.println("| " + (A[1][0]*0)+ "  " +(Math.pow(A[1][1], 0))+ " | ");
   

      System.out.println("Matriks B");
    System.out.println("| " + (Math.pow(B[0][0], 0))+ "  " +B[0][1]*0+ " | ");
    System.out.println("| " + (B[1][0]*0)+ "  " +(Math.pow(B[1][1], 0))+  " | ");
  

    }
    else {
    System.out.println("Metode Eliminasi Gauss Jordan");
    System.out.println("Matriks A");
    System.out.println("| " + (Math.pow(A[0][0], 0))+ "  " +A[0][1]*0+ "  " + A[0][2]*0+ " | ");
    System.out.println("| " + (A[1][0]*0)+ "  " +(Math.pow(A[1][1], 0))+ "  " + A[1][2]*0+ " | ");
    System.out.println("| " + (A[2][0]*0)+ "  " +A[0][1]*0+ "  " + (Math.pow(A[2][2], 0))+ " | ");

      System.out.println("Matriks B");
    System.out.println("| " + (Math.pow(B[0][0], 0))+ "  " +B[0][1]*0+ "  " + B[0][2]*0+ " | ");
    System.out.println("| " + (B[1][0]*0)+ "  " +(Math.pow(B[1][1], 0))+ "  " + B[1][2]*0+ " | ");
    System.out.println("| " + (B[2][0]*0)+ "  " +B[0][1]*0+ "  " + (Math.pow(B[2][2], 0))+ " | ");

}}
else {
    //crammer
    System.out.println("Metode Crammer");
    //determinan matriks A
    int detA=(A[0][0]*A[1][1]*A[2][2])+
(A[0][1]*A[1][2]*A[2][0])+
(A[0][2]*A[1][0]*A[2][1]);
int detB=(A[0][1]*A[1][0]*A[2][2])+
(A[0][0]*A[1][2]*A[2][1])+
(A[0][2]*A[1][1]*A[2][0]);
int determinanA = detA - detB;

//determinan matriks b
 int deta=(B[0][0]*B[1][1]*B[2][2])+
(B[0][1]*B[1][2]*B[2][0])+
(B[0][2]*B[1][0]*B[2][1]);
int detb=(B[0][1]*B[1][0]*B[2][2])+
(B[0][0]*B[1][2]*B[2][1])+
(B[0][2]*B[1][1]*B[2][0]);
int determinanB = deta - detb;
    if (detA == 0){
        System.out.println("Metode crammer tidak dapat dikerjakan");
    }
    else {
        int b1 = Integer.parseInt(JOptionPane.showInputDialog("masukkan b1 :"));
        int b2 = Integer.parseInt(JOptionPane.showInputDialog("masukkan b2 :"));
        int b3 = Integer.parseInt(JOptionPane.showInputDialog("masukkan b3 :"));
     //matriks A1  
 int detA1 = (b1*A[1][1]*A[2][2])+
(A[0][1]*A[1][2]*b3)+
(A[0][2]*b2*A[2][1]);

int det=(A[0][1]*b2*A[2][2])+
(b1*A[1][2]*A[2][1])+
(A[0][2]*A[1][1]*b3);

int deterA = detA1 - det ;

// A2
int detA2 = (A[0][0]*b2*A[2][2])+
(b1*A[1][2]*A[2][0])+
(A[0][2]*A[1][0]*b3);

int det2=(b1*A[1][0]*A[2][2])+
(b3*A[1][2]*A[0][0])+
(A[0][2]*b2*A[2][0]);

int deterA2 = detA2 - det2 ;

//A3
int detA3 = (A[0][0]*b3*A[1][1])+
(b2*A[0][1]*A[2][0])+
(A[1][2]*A[1][0]*b1);

int det3=(b3*A[1][0]*A[0][1])+
(b2*A[2][1]*A[0][0])+
(A[1][1]*b1*A[2][0]);

int deterA3 = detA3 - det3 ;


  //matriks B1  
 int detB1 = (b1*A[1][1]*B[2][2])+
(B[0][1]*B[1][2]*b3)+
(B[0][2]*b2*B[2][1]);

int dettB=(B[0][1]*b2*B[2][2])+
(b1*B[1][2]*B[2][1])+
(B[0][2]*B[1][1]*b3);

int deterB = detB1 - dettB ;

// B2
int detB2 = (B[0][0]*b2*B[2][2])+
(b1*B[1][2]*B[2][0])+
(B[0][2]*B[1][0]*b3);

int deT2=(b1*B[1][0]*B[2][2])+
(b3*B[1][2]*B[0][0])+
(B[0][2]*b2*B[2][0]);

int deterB2 = detB2 - deT2 ;

//B3
int detB3 = (B[0][0]*b3*B[1][1])+
(b2*B[0][1]*B[2][0])+
(B[1][2]*B[1][0]*b1);

int deT3=(b3*B[1][0]*B[0][1])+
(b2*B[2][1]*B[0][0])+
(B[1][1]*b1*B[2][0]);

int deterB3 = detB3 - deT3 ;
float A1 = deterA/determinanA ;
float A2 = deterA2/determinanA;
float A3 = deterA3/determinanA;

float B1 = deterB/determinanB ;
float B2 = deterB2/determinanB ;
float B3 = deterB3/determinanB ;
        System.out.println("Penyelesaian MAtriks A dengan Metode Crammer didapatkan :");
        System.out.println(" x1 =" +A1 + "       x2 =" + A2 + "       x3 =" +A3 );
       
        System.out.println("Penyelesaian Matriks B dengan Metode Crammer didapatkan :");
        System.out.println(" x1 =" +B1 + "       x2 =" + B2+ "       x3 =" +B3 );
       
    }
   
   
   
}
    }
    }