Skip to content

Commit b6dc961

Browse files
ojasvasiriak
andauthored
Inverse Of a Square Matrix [Hacktoberfest] (TheAlgorithms#2582)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent eaf5395 commit b6dc961

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

Misc/InverseOfMatrix.java

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

0 commit comments

Comments
 (0)