Advertisement
Kushtrim

DeterminantaDheMatrica

Apr 6th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.37 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.text.*;
  3. /**
  4.  * Veprime me matrica dhe determinanta
  5.  *
  6.  * @author Kushtrim
  7.  * @version 1.01
  8.  */
  9. public class DeterminantaDheMatrica
  10. //  d.length -rreshtat
  11. // d[0].length - kolonat
  12.  
  13. {
  14.     public static void main(String args[])
  15.     {
  16.         // double[][] det = { {2,2,3,7,5}, {4,6,8,6,8 } ,{7,4,6,4,8},{6,8,4,2,3},{1,5,9,5,6}};
  17.         DeterminantaDheMatrica llogarit = new DeterminantaDheMatrica();
  18.         double[][] A = { {1,2,3}, {2,3,3 } };  //2*3
  19.         double[][] B = { {2,3}, {3,4 } ,{4,4}};   //3*2
  20.      
  21.          //per me e gjet inversin
  22.         //  llogarit.printoMatricen(A);
  23.         //  double[][] B = llogarit.gjejInversin(A);
  24.         //   llogarit.printoMatricen(B);
  25.  
  26.        
  27.         // per me e llogarit determinanten
  28.         /*    if ( det[0].length == det.length )
  29.         {
  30.         System.out.println( llogarit.llogaritDeterminanten(det));
  31.         } else { System.out.println("Matrica nuk eshte katrore, nuk mund te llogaritet determinanta e saj");
  32.  
  33.         } */
  34.        
  35.         //per me i shumezu
  36.         if ( A[0].length == B.length)
  37.         {
  38.             llogarit.printoMatricen(llogarit.shumezoMatricat(A,B));
  39.         }
  40.     }
  41.  
  42.     /**
  43.      * Method shumezoMatricat
  44.      *
  45.      * @param A Matrica e pare
  46.      * @param B Matrica e dyte
  47.      * @return Prodhimin A*B te matricave A, B
  48.      */
  49.     public double[][] shumezoMatricat(double[][] A , double[][] B )
  50.     {  
  51.         // x.length -rreshtat
  52.         // x[0].length - kolonat
  53.        
  54.         double[][] C = new double[A.length][B[0].length]; // matrica C ka rreshta sa ka rreshta A, e kolona sa ka kolona B
  55.  
  56.         int numriIKolonave_A =  A[0].length;
  57.         for ( int i = 0 ; i < C.length ; i++)
  58.         {
  59.             for ( int j = 0 ; j < C[0].length ; j++)
  60.             {
  61.                 // tash formula Cij =  Ai1*B1j + Ai2*B2j + ...+ Aik*Bkj :P
  62.                 double shuma = 0;
  63.                 for( int k =0; k< numriIKolonave_A ; k++)
  64.                 {
  65.                     shuma+= A[i][k] * B[k][j];
  66.                 }
  67.  
  68.                 C[i][j] = shuma;
  69.             }
  70.         }
  71.         return C;
  72.     }
  73.  
  74.     /**
  75.      * Metoda gjejInversin, e llogarit matricen inverse te matrices qe e merr si parameter
  76.      *
  77.      * @param matrica Matrica A
  78.      * @return Matrica inverse e A
  79.      */
  80.     public double[][] gjejInversin(double[][] A)
  81.     {
  82.         double[][] answer = new double[A[0].length][A.length];  // krijojme nje varg2D - matrice me gjatesi te njejte si A
  83.         double koeficienti_perpara =1.0/ this.llogaritDeterminanten(A); // llogaritim koeficientin sipas formules
  84.        // System.out.println(koeficienti_perpara);
  85.        // llogaritim matricen e adjuguar te A
  86.         for ( int i = 0 ; i< A[0].length ; i++)
  87.         {
  88.             for(int j = 0 ; j < A.length; j++)
  89.             {
  90.                 answer[i][j] =Math.pow(-1, i+j+2)*koeficienti_perpara * this.llogaritDeterminanten(this.gjejNenMatricen(A,i,j));
  91.             }
  92.         }
  93.         // e kthejme matricen answer, por te transponuar
  94.         return transponoMatricen(answer);
  95.     }
  96.  
  97.     /**
  98.      *  gjejNenMatricen e llogarit dhe e kthen matricen e cila mbetet nese e fshijme rreshtin e index_i, dhe kolonen_j
  99.      *
  100.      * @param A Nje matrice
  101.      * @param index_i Rreshti i cili eliminohet
  102.      * @param index_j Kolona e cila eliminohet
  103.      * @return "Nen-matricen"
  104.      */
  105.     public double[][] gjejNenMatricen( double[][] A , int index_i, int index_j )
  106.     {
  107.         double[][] answer= new double[A[0].length-1][A.length-1]; // krijojme njne matrice per 1 me te vogel se A
  108.  
  109.         int kalo_i=0;
  110.         int kalo_j=0;
  111.         for ( int i=0; i< A.length -1 ; i++)
  112.         {
  113.             if ( i == index_i )
  114.             {   // nese jemi te rreshti qe duhet kaluar, kalo_i =1
  115.                 kalo_i=1;
  116.             }
  117.  
  118.             for ( int j = 0 ; j<  A.length -1; j++)
  119.             {
  120.                 if (j == index_j )
  121.                 {   // nese jemi te kolona qe duhet kaluar, kalo_j =1
  122.                     kalo_j=1;
  123.                 }
  124.                 //  nese jemi te rreshti qe duhet kaluar, kalo_i eshte  nje, pra fillojme te kopjojme nga rreshti i ardhshem , j+kalo_i
  125.                 // kopjojme nga shtylla j+kalo_j, nese jemi te kolona qe kalohet , kalo_j =1
  126.                 answer[i][j] = A[i+kalo_i][j+kalo_j];
  127.  
  128.             }
  129.             kalo_j=0; // e resetojme, pasi per rreshtin e rradhes, perseri duhet t'ia nisim nga zero
  130.         }
  131.         return answer;
  132.     }
  133.  
  134.     /**
  135.      * Metoda merrMatricen e merr nje matrice ne menyre interaktive nga perdoruesi
  136.      *
  137.      * @return Matrica e dhene nga perdoruesi
  138.      */
  139.     private double[][] merrMatricen()
  140.     {
  141.         int shkalla1 = Integer.parseInt(JOptionPane.showInputDialog("Sa rreshta i ka matrica"));
  142.         int shkalla2 = Integer.parseInt(JOptionPane.showInputDialog("Sa kolona i ka matrica"));
  143.         double[][] matrica = new double[shkalla1][shkalla2];
  144.  
  145.         for (int i = 0 ; i<shkalla1;i++ )
  146.         {
  147.             for (int j = 0; j< shkalla2; j++)
  148.             {
  149.                 //e mbushim matricen
  150.                 matrica[i][j]= Double.parseDouble(JOptionPane.showInputDialog("Shkruani elementin e rreshtit " + (i+1) + " dhe shtylles " + (j+1)));
  151.             }
  152.         }
  153.         return matrica;
  154.     }
  155.  
  156.     /**
  157.      * Metoda llogaritDeterminanten e llogarit determinanten e Matrices A me ane te minorëve
  158.      *
  159.      * @param A Matricë - double
  160.      * @return vlerën e determinantës të A
  161.      */
  162.     public double llogaritDeterminanten( double[][] A )
  163.     {
  164.         double answer=0;
  165.         // nese eshte e shkalles 2, e llogaritim lehte
  166.         if( A.length == 2 )
  167.         {
  168.             answer = A[0][0] * A[1][1]  - A[1][0] * A[0][1];
  169.         }
  170.         else
  171.  
  172.         {   //perndryshe vazhdo me zberthimin sipas minorëve
  173.             for ( int i = 0 ; i < A.length;i++)
  174.             {
  175.                 // e krijojme nje matrice te re katrore, per 1 me te vogel
  176.                 double[][] w = new double[A.length-1][A.length-1];
  177.  
  178.                 // llogaritjen po e bejme sipas kolones 1
  179.                 int kalo=0;
  180.                 for ( int j=0; j< A.length -1 ; j++)
  181.                 {
  182.                     if ( j == i )
  183.                     {   // nese jemi te rreshti qe duhet kaluar, kalo =1
  184.                         kalo=1;
  185.                     }
  186.  
  187.                     for ( int k = 0 ; k<  A.length -1; k++)
  188.                     {
  189.                         //  nese jemi te rreshti qe duhet kaluar, k eshte  nje, pra fillojme te kopjojme nga rreshti i ardhshem , j+kalo
  190.                         //  kopjojme nga shtylla k+1, pasi kolonen 1 gjithmone e kalojme, se po bejme llogaritjen me minore sipas asaj kolone
  191.                         w[j][k] = A[j+kalo][k+1];
  192.  
  193.                     }
  194.  
  195.                 }
  196.  
  197.                 //  Math.pow e perdorim per ta llogaritur shenjen
  198.                 // pergjigjes ia shtojme elementin A[i][0], here determinanta perkatese e shkalles per nje me te vogel
  199.                 answer += Math.pow( -1 , i+2 ) * A[i][0] * llogaritDeterminanten(w);
  200.  
  201.             }
  202.         }
  203.  
  204.         return answer;
  205.     }
  206.  
  207.     /**
  208.      * Metoda transponoMatricen e kthen matricen e transponuar te A, dmth kolonat dhe rreshtat i nderrojne vendet
  209.      *
  210.      * @param A Matricë
  211.      * @return Matricën e transponuar të A
  212.      */
  213.     public double[][] transponoMatricen(double[][]  A)
  214.     {
  215.         double[][] B = new double[A[0].length][A.length];
  216.         for (int i =  0; i< A[0].length ; i++)
  217.         {
  218.             for (int j = 0; j< A.length; j++)
  219.             {
  220.                 B[i][j] = A[j][i];  // rreshtat edhe kolonat i nderrojne vendet
  221.             }
  222.         }
  223.         return B;
  224.     }
  225.  
  226.     /**
  227.      * Metoda printoMatricen, i printon elementet e nje matrice me ane te System.out.* , poashtu ben edhe formatizimin ne 2 decimale ( kajhere 1 :P )
  228.      *
  229.      * @param A Matrica e cila printohet
  230.      */
  231.     private void printoMatricen(double[][] A)
  232.     {
  233.         DecimalFormat f = new DecimalFormat("0.0");
  234.  
  235.         for (int i =  0; i< A[0].length ; i++)
  236.         {
  237.             for (int j = 0; j< A.length; j++)
  238.             {
  239.                 System.out.print(f.format(A[i][j]) + " "); // e printon secilin element te formatizuar
  240.             }
  241.             System.out.println(); // ne fund te rreshtit, kalojme ne rresht te ri
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement