1
1
package com .thealgorithms .misc ;
2
2
3
- import java .util .Scanner ;
4
-
5
- /*
6
- * Wikipedia link : https://en.wikipedia.org/wiki/Invertible_matrix
7
- *
8
- * Here we use gauss elimination method to find the inverse of a given matrix.
9
- * To understand gauss elimination method to find inverse of a matrix:
10
- * https://www.sangakoo.com/en/unit/inverse-matrix-method-of-gaussian-elimination
11
- *
12
- * We can also find the inverse of a matrix
3
+ /**
4
+ * This class provides methods to compute the inverse of a square matrix
5
+ * using Gaussian elimination. For more details, refer to:
6
+ * https://en.wikipedia.org/wiki/Invertible_matrix
13
7
*/
14
8
public final class InverseOfMatrix {
15
9
private InverseOfMatrix () {
16
10
}
17
11
18
- public static void main (String [] argv ) {
19
- Scanner input = new Scanner (System .in );
20
- System .out .println ("Enter the matrix size (Square matrix only): " );
21
- int n = input .nextInt ();
22
- double [][] a = new double [n ][n ];
23
- System .out .println ("Enter the elements of matrix: " );
24
- for (int i = 0 ; i < n ; i ++) {
25
- for (int j = 0 ; j < n ; j ++) {
26
- a [i ][j ] = input .nextDouble ();
27
- }
28
- }
29
-
30
- double [][] d = invert (a );
31
- System .out .println ();
32
- System .out .println ("The inverse is: " );
33
- for (int i = 0 ; i < n ; ++i ) {
34
- for (int j = 0 ; j < n ; ++j ) {
35
- System .out .print (d [i ][j ] + " " );
36
- }
37
- System .out .println ();
38
- }
39
- input .close ();
40
- }
41
-
42
12
public static double [][] invert (double [][] a ) {
43
13
int n = a .length ;
44
14
double [][] x = new double [n ][n ];
45
15
double [][] b = new double [n ][n ];
46
16
int [] index = new int [n ];
17
+
18
+ // Initialize the identity matrix
47
19
for (int i = 0 ; i < n ; ++i ) {
48
20
b [i ][i ] = 1 ;
49
21
}
50
22
51
- // Transform the matrix into an upper triangle
23
+ // Perform Gaussian elimination
52
24
gaussian (a , index );
53
25
54
- // Update the matrix b[i][j] with the ratios stored
26
+ // Update matrix b with the ratios stored during elimination
55
27
for (int i = 0 ; i < n - 1 ; ++i ) {
56
28
for (int j = i + 1 ; j < n ; ++j ) {
57
29
for (int k = 0 ; k < n ; ++k ) {
@@ -60,7 +32,7 @@ public static double[][] invert(double[][] a) {
60
32
}
61
33
}
62
34
63
- // Perform backward substitutions
35
+ // Perform backward substitution to find the inverse
64
36
for (int i = 0 ; i < n ; ++i ) {
65
37
x [n - 1 ][i ] = b [index [n - 1 ]][i ] / a [index [n - 1 ]][n - 1 ];
66
38
for (int j = n - 2 ; j >= 0 ; --j ) {
@@ -73,19 +45,20 @@ public static double[][] invert(double[][] a) {
73
45
}
74
46
return x ;
75
47
}
76
-
77
- // Method to carry out the partial-pivoting Gaussian
78
- // elimination. Here index[] stores pivoting order.
79
- public static void gaussian (double [][] a , int [] index ) {
48
+ /**
49
+ * Method to carry out the partial-pivoting Gaussian
50
+ * elimination. Here index[] stores pivoting order.
51
+ **/
52
+ private static void gaussian (double [][] a , int [] index ) {
80
53
int n = index .length ;
81
54
double [] c = new double [n ];
82
55
83
- // Initialize the index
56
+ // Initialize the index array
84
57
for (int i = 0 ; i < n ; ++i ) {
85
58
index [i ] = i ;
86
59
}
87
60
88
- // Find the rescaling factors, one from each row
61
+ // Find the rescaling factors for each row
89
62
for (int i = 0 ; i < n ; ++i ) {
90
63
double c1 = 0 ;
91
64
for (int j = 0 ; j < n ; ++j ) {
@@ -97,22 +70,23 @@ public static void gaussian(double[][] a, int[] index) {
97
70
c [i ] = c1 ;
98
71
}
99
72
100
- // Search the pivoting element from each column
101
- int k = 0 ;
73
+ // Perform pivoting
102
74
for (int j = 0 ; j < n - 1 ; ++j ) {
103
75
double pi1 = 0 ;
76
+ int k = j ;
104
77
for (int i = j ; i < n ; ++i ) {
105
- double pi0 = Math .abs (a [index [i ]][j ]);
106
- pi0 /= c [index [i ]];
78
+ double pi0 = Math .abs (a [index [i ]][j ]) / c [index [i ]];
107
79
if (pi0 > pi1 ) {
108
80
pi1 = pi0 ;
109
81
k = i ;
110
82
}
111
83
}
112
- // Interchange rows according to the pivoting order
113
- int itmp = index [j ];
84
+
85
+ // Swap rows
86
+ int temp = index [j ];
114
87
index [j ] = index [k ];
115
- index [k ] = itmp ;
88
+ index [k ] = temp ;
89
+
116
90
for (int i = j + 1 ; i < n ; ++i ) {
117
91
double pj = a [index [i ]][j ] / a [index [j ]][j ];
118
92
0 commit comments