Java Program to Find Inverse of a Matrix

«
»
This is the java program to find the inverse of square invertible matrix. The matrix is invertible if its determinant is non zero.

Here is the source code of the Java Program to Find Inverse of a Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

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

Output:

$ javac Inverse.java
$ java Inverse
Enter the dimension of square matrix: 
2
Enter the elements of matrix: 
1 2 
3 4 
The Inverse is: 
-1.9999999999999998  1.0  
1.4999999999999998  -0.49999999999999994

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.