Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.text.*;
- /**
- * Veprime me matrica dhe determinanta
- *
- * @author Kushtrim
- * @version 1.01
- */
- public class DeterminantaDheMatrica
- // d.length -rreshtat
- // d[0].length - kolonat
- {
- public static void main(String args[])
- {
- // 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}};
- DeterminantaDheMatrica llogarit = new DeterminantaDheMatrica();
- double[][] A = { {1,2,3}, {2,3,3 } }; //2*3
- double[][] B = { {2,3}, {3,4 } ,{4,4}}; //3*2
- //per me e gjet inversin
- // llogarit.printoMatricen(A);
- // double[][] B = llogarit.gjejInversin(A);
- // llogarit.printoMatricen(B);
- // per me e llogarit determinanten
- /* if ( det[0].length == det.length )
- {
- System.out.println( llogarit.llogaritDeterminanten(det));
- } else { System.out.println("Matrica nuk eshte katrore, nuk mund te llogaritet determinanta e saj");
- } */
- //per me i shumezu
- if ( A[0].length == B.length)
- {
- llogarit.printoMatricen(llogarit.shumezoMatricat(A,B));
- }
- }
- /**
- * Method shumezoMatricat
- *
- * @param A Matrica e pare
- * @param B Matrica e dyte
- * @return Prodhimin A*B te matricave A, B
- */
- public double[][] shumezoMatricat(double[][] A , double[][] B )
- {
- // x.length -rreshtat
- // x[0].length - kolonat
- double[][] C = new double[A.length][B[0].length]; // matrica C ka rreshta sa ka rreshta A, e kolona sa ka kolona B
- int numriIKolonave_A = A[0].length;
- for ( int i = 0 ; i < C.length ; i++)
- {
- for ( int j = 0 ; j < C[0].length ; j++)
- {
- // tash formula Cij = Ai1*B1j + Ai2*B2j + ...+ Aik*Bkj :P
- double shuma = 0;
- for( int k =0; k< numriIKolonave_A ; k++)
- {
- shuma+= A[i][k] * B[k][j];
- }
- C[i][j] = shuma;
- }
- }
- return C;
- }
- /**
- * Metoda gjejInversin, e llogarit matricen inverse te matrices qe e merr si parameter
- *
- * @param matrica Matrica A
- * @return Matrica inverse e A
- */
- public double[][] gjejInversin(double[][] A)
- {
- double[][] answer = new double[A[0].length][A.length]; // krijojme nje varg2D - matrice me gjatesi te njejte si A
- double koeficienti_perpara =1.0/ this.llogaritDeterminanten(A); // llogaritim koeficientin sipas formules
- // System.out.println(koeficienti_perpara);
- // llogaritim matricen e adjuguar te A
- for ( int i = 0 ; i< A[0].length ; i++)
- {
- for(int j = 0 ; j < A.length; j++)
- {
- answer[i][j] =Math.pow(-1, i+j+2)*koeficienti_perpara * this.llogaritDeterminanten(this.gjejNenMatricen(A,i,j));
- }
- }
- // e kthejme matricen answer, por te transponuar
- return transponoMatricen(answer);
- }
- /**
- * gjejNenMatricen e llogarit dhe e kthen matricen e cila mbetet nese e fshijme rreshtin e index_i, dhe kolonen_j
- *
- * @param A Nje matrice
- * @param index_i Rreshti i cili eliminohet
- * @param index_j Kolona e cila eliminohet
- * @return "Nen-matricen"
- */
- public double[][] gjejNenMatricen( double[][] A , int index_i, int index_j )
- {
- double[][] answer= new double[A[0].length-1][A.length-1]; // krijojme njne matrice per 1 me te vogel se A
- int kalo_i=0;
- int kalo_j=0;
- for ( int i=0; i< A.length -1 ; i++)
- {
- if ( i == index_i )
- { // nese jemi te rreshti qe duhet kaluar, kalo_i =1
- kalo_i=1;
- }
- for ( int j = 0 ; j< A.length -1; j++)
- {
- if (j == index_j )
- { // nese jemi te kolona qe duhet kaluar, kalo_j =1
- kalo_j=1;
- }
- // nese jemi te rreshti qe duhet kaluar, kalo_i eshte nje, pra fillojme te kopjojme nga rreshti i ardhshem , j+kalo_i
- // kopjojme nga shtylla j+kalo_j, nese jemi te kolona qe kalohet , kalo_j =1
- answer[i][j] = A[i+kalo_i][j+kalo_j];
- }
- kalo_j=0; // e resetojme, pasi per rreshtin e rradhes, perseri duhet t'ia nisim nga zero
- }
- return answer;
- }
- /**
- * Metoda merrMatricen e merr nje matrice ne menyre interaktive nga perdoruesi
- *
- * @return Matrica e dhene nga perdoruesi
- */
- private double[][] merrMatricen()
- {
- int shkalla1 = Integer.parseInt(JOptionPane.showInputDialog("Sa rreshta i ka matrica"));
- int shkalla2 = Integer.parseInt(JOptionPane.showInputDialog("Sa kolona i ka matrica"));
- double[][] matrica = new double[shkalla1][shkalla2];
- for (int i = 0 ; i<shkalla1;i++ )
- {
- for (int j = 0; j< shkalla2; j++)
- {
- //e mbushim matricen
- matrica[i][j]= Double.parseDouble(JOptionPane.showInputDialog("Shkruani elementin e rreshtit " + (i+1) + " dhe shtylles " + (j+1)));
- }
- }
- return matrica;
- }
- /**
- * Metoda llogaritDeterminanten e llogarit determinanten e Matrices A me ane te minorëve
- *
- * @param A Matricë - double
- * @return vlerën e determinantës të A
- */
- public double llogaritDeterminanten( double[][] A )
- {
- double answer=0;
- // nese eshte e shkalles 2, e llogaritim lehte
- if( A.length == 2 )
- {
- answer = A[0][0] * A[1][1] - A[1][0] * A[0][1];
- }
- else
- { //perndryshe vazhdo me zberthimin sipas minorëve
- for ( int i = 0 ; i < A.length;i++)
- {
- // e krijojme nje matrice te re katrore, per 1 me te vogel
- double[][] w = new double[A.length-1][A.length-1];
- // llogaritjen po e bejme sipas kolones 1
- int kalo=0;
- for ( int j=0; j< A.length -1 ; j++)
- {
- if ( j == i )
- { // nese jemi te rreshti qe duhet kaluar, kalo =1
- kalo=1;
- }
- for ( int k = 0 ; k< A.length -1; k++)
- {
- // nese jemi te rreshti qe duhet kaluar, k eshte nje, pra fillojme te kopjojme nga rreshti i ardhshem , j+kalo
- // kopjojme nga shtylla k+1, pasi kolonen 1 gjithmone e kalojme, se po bejme llogaritjen me minore sipas asaj kolone
- w[j][k] = A[j+kalo][k+1];
- }
- }
- // Math.pow e perdorim per ta llogaritur shenjen
- // pergjigjes ia shtojme elementin A[i][0], here determinanta perkatese e shkalles per nje me te vogel
- answer += Math.pow( -1 , i+2 ) * A[i][0] * llogaritDeterminanten(w);
- }
- }
- return answer;
- }
- /**
- * Metoda transponoMatricen e kthen matricen e transponuar te A, dmth kolonat dhe rreshtat i nderrojne vendet
- *
- * @param A Matricë
- * @return Matricën e transponuar të A
- */
- public double[][] transponoMatricen(double[][] A)
- {
- double[][] B = new double[A[0].length][A.length];
- for (int i = 0; i< A[0].length ; i++)
- {
- for (int j = 0; j< A.length; j++)
- {
- B[i][j] = A[j][i]; // rreshtat edhe kolonat i nderrojne vendet
- }
- }
- return B;
- }
- /**
- * Metoda printoMatricen, i printon elementet e nje matrice me ane te System.out.* , poashtu ben edhe formatizimin ne 2 decimale ( kajhere 1 :P )
- *
- * @param A Matrica e cila printohet
- */
- private void printoMatricen(double[][] A)
- {
- DecimalFormat f = new DecimalFormat("0.0");
- for (int i = 0; i< A[0].length ; i++)
- {
- for (int j = 0; j< A.length; j++)
- {
- System.out.print(f.format(A[i][j]) + " "); // e printon secilin element te formatizuar
- }
- System.out.println(); // ne fund te rreshtit, kalojme ne rresht te ri
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement