Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lastASSIGNMENT;
- import java.io.File;
- import java.util.Scanner;
- class Main{
- public static void main(String[] args) {
- //Scanner scan = new Scanner(System.in);
- int i,j;
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter file location :");
- String file_name = scanner.nextLine();
- double mat[][] = new double[3][3];
- try{
- File f = new File(file_name);
- Scanner inp = new Scanner(f);
- while(inp.hasNext()) {
- for( i =0;i<3;i++)
- {
- for( j=0;j<3;j++)
- {
- mat[i][j] =inp.nextInt();
- }
- }
- }
- }catch(Exception e) {
- }
- System.out.println("entered matrix ");
- for( i =0;i<3;i++)
- {
- for( j=0;j<3;j++)
- {
- System.out.print(" "+mat[i][j]);
- }
- System.out.println("");
- }
- System.out.println("Inverse: ");
- Main.inverse(mat);
- }
- static void inverse(double mat[][])
- {
- double adj[][] = new double[3][3];
- double matDet = 0;
- int i,j;
- for(i =0;i<3;i++)
- {
- if(i%2!=0)
- {
- matDet-=mat[0][i]*Main.det( Main.adjcent(mat,0,i,3));
- }
- else
- {
- matDet+=mat[0][i]*Main.det( Main.adjcent(mat,0,i,3));
- }
- }
- for( i =0;i<3;i++)
- {
- for( j=0;j<3;j++)
- {
- if((i+j)%2!=0)
- adj[j][i]=Main.det( Main.adjcent(mat,i,j,3))*(1/matDet)*-1;
- else
- adj[j][i]=Main.det( Main.adjcent(mat,i,j,3))*(1/matDet);
- }
- System.out.println("\n");
- }
- for( i =0;i<3;i++)
- {
- for( j=0;j<3;j++)
- {
- System.out.print(adj[i][j]+" ");
- }
- System.out.println("\n");
- }
- }
- static double[][] adjcent(double arr[][],int r,int c,int n)
- {
- double brr[][] = new double[n-1][n-1];
- int x =0,y=0;
- for(int i=0;i<n;i++)
- {
- for(int j =0;j<n;j++)
- {
- if(i!=r && j!=c)
- {
- brr[x][y]=arr[i][j];
- if(y!=n-2)
- {
- y++;
- }
- else
- {
- x++;
- y=0;
- }
- }
- }
- }
- return brr;
- }
- static double det(double mat[][])
- {
- double det = (mat[0][0] * mat[1][1]) - (mat[0][1] * mat[1][0]);
- return det;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement