Advertisement
Huntersazzad

Untitled

Dec 30th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. package lastASSIGNMENT;
  2.  
  3. import java.io.File;
  4. import java.util.Scanner;
  5.  
  6. class Main{
  7. public static void main(String[] args) {
  8. //Scanner scan = new Scanner(System.in);
  9. int i,j;
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12.  
  13. System.out.println("Enter file location :");
  14. String file_name = scanner.nextLine();
  15.  
  16. double mat[][] = new double[3][3];
  17. try{
  18. File f = new File(file_name);
  19. Scanner inp = new Scanner(f);
  20. while(inp.hasNext()) {
  21. for( i =0;i<3;i++)
  22. {
  23. for( j=0;j<3;j++)
  24. {
  25.  
  26. mat[i][j] =inp.nextInt();
  27. }
  28. }
  29. }
  30. }catch(Exception e) {
  31.  
  32. }
  33. System.out.println("entered matrix ");
  34. for( i =0;i<3;i++)
  35. {
  36. for( j=0;j<3;j++)
  37. {
  38. System.out.print(" "+mat[i][j]);
  39. }
  40. System.out.println("");
  41. }
  42.  
  43.  
  44. System.out.println("Inverse: ");
  45. Main.inverse(mat);
  46.  
  47.  
  48.  
  49. }
  50. static void inverse(double mat[][])
  51. {
  52. double adj[][] = new double[3][3];
  53. double matDet = 0;
  54. int i,j;
  55. for(i =0;i<3;i++)
  56. {
  57. if(i%2!=0)
  58. {
  59. matDet-=mat[0][i]*Main.det( Main.adjcent(mat,0,i,3));
  60. }
  61. else
  62. {
  63. matDet+=mat[0][i]*Main.det( Main.adjcent(mat,0,i,3));
  64. }
  65. }
  66. for( i =0;i<3;i++)
  67. {
  68. for( j=0;j<3;j++)
  69. {
  70.  
  71. if((i+j)%2!=0)
  72. adj[j][i]=Main.det( Main.adjcent(mat,i,j,3))*(1/matDet)*-1;
  73. else
  74. adj[j][i]=Main.det( Main.adjcent(mat,i,j,3))*(1/matDet);
  75.  
  76.  
  77. }
  78. System.out.println("\n");
  79. }
  80. for( i =0;i<3;i++)
  81. {
  82. for( j=0;j<3;j++)
  83. {
  84.  
  85. System.out.print(adj[i][j]+" ");
  86.  
  87. }
  88. System.out.println("\n");
  89. }
  90.  
  91. }
  92. static double[][] adjcent(double arr[][],int r,int c,int n)
  93. {
  94.  
  95. double brr[][] = new double[n-1][n-1];
  96. int x =0,y=0;
  97. for(int i=0;i<n;i++)
  98. {
  99. for(int j =0;j<n;j++)
  100. {
  101. if(i!=r && j!=c)
  102. {
  103. brr[x][y]=arr[i][j];
  104. if(y!=n-2)
  105. {
  106. y++;
  107. }
  108. else
  109. {
  110. x++;
  111. y=0;
  112.  
  113. }
  114.  
  115. }
  116. }
  117. }
  118. return brr;
  119. }
  120. static double det(double mat[][])
  121. {
  122. double det = (mat[0][0] * mat[1][1]) - (mat[0][1] * mat[1][0]);
  123. return det;
  124.  
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement