Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. public static int[][] inverse(int[][] mat) {
  2. int detRecip = 1 / determinate(mat);
  3. int[][] adj = adjugate(mat);
  4. return matrixMultiply(adj, detRecip));
  5. }
  6.  
  7. public static int[][] matrixMultiply(int[][] mat, int mult) {
  8. int[][] multMatrix = clone(mat);
  9. for (int[] i : multMatrix) {
  10. for (int j : i) {
  11. j *= mult;
  12. }
  13. }
  14. return multMatrix;
  15. }
  16.  
  17. public static int[][] adjugate(int[][] mat) {
  18. return transpose(cofactorMatrix(mat));
  19. }
  20.  
  21. public static int[][] cofactorMatrix(int[][] mat) {
  22. int[][] cofM = new int[mat.length][mat[0].length];
  23. for (int i = 0; i < mat.length; i++) {
  24. for (int j = 0; j < mat[i].length; j++) {
  25. cofM[i][j] = cofactorSign(i, j) * determinant(cofactor(mat, i, j));
  26. }
  27. }
  28. return cofM;
  29. }
  30.  
  31. public static int cofactorSign(int i, int j) {
  32. return (int)Math.pow(-1, i + j);
  33. }
  34.  
  35. public static int determinant(int[][] mat) {
  36. return determinant(mat, mat.length);
  37. }
  38.  
  39. public static int determinant(int[][] mat, int n) {
  40. int det = 0;
  41.  
  42. if (n == 1) {
  43. return mat[0][0];
  44. }
  45.  
  46. int sign = 1;
  47. int[][] cof = new int[n][n];
  48. for (int i = 0; i < n; i++) {
  49. cof = cofactor(mat, 0, i);
  50. det += sign * mat[0][i] * determinant(cof, n - 1);
  51. sign = -sign;
  52. }
  53.  
  54. return det;
  55. }
  56.  
  57. public static int[][] cofactor(int[][] mat, int i, int j) {
  58. int[][] cofactor = new int[mat.length - 1][mat[0].length - 1];
  59. int currCofX = 0;
  60. int currCofY = 0;
  61. for (int a = 0; a < mat.length; a++) {
  62. for (int b = 0; b < mat[a].length; b++) {
  63. if (a != i && b != j) {
  64. if (currCofX >= cofactor.length) {
  65. currCofX = 0;
  66. currCofY++;
  67. }
  68. cofactor[currCofX][currCofY] = mat[a][b];
  69. currCofX++;
  70. }
  71. }
  72. }
  73. return cofactor;
  74. }
  75.  
  76. public static int[][] transpose(int[][] mat) {
  77. int[][] transpose = clone(mat);
  78. if (transpose.length == 1 && transpose[0].length == 1) {
  79. return transpose;
  80. }
  81.  
  82. for (int i = 0; i < transpose.length; i++) {
  83. for (int j = i + 1; j < transpose[i].length; j++) {
  84. swap(transpose, i, j, j, i);
  85. }
  86. }
  87. return transpose;
  88. }
  89.  
  90. public static void swap(int[][] mat, int x1, int y1, int x2, int y2) {
  91. int temp = mat[x1][y1];
  92. mat[x1][y1] = mat[x2][y2];
  93. mat[x2][y2] = temp;
  94. }
  95.  
  96. public static int[][] clone(int[][] mat) {
  97. int[][] clone = new int[mat.length][mat[0].length];
  98. for (int i = 0; i < mat.length; i++) {
  99. for (int j = 0; j < mat[i].length; j++) {
  100. clone[i][j] = mat[i][j];
  101. }
  102. }
  103. return clone;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement