Java Program to Check if a Matrix is Invertible

«
»
This is the java program to check whether the matrix is invertible or not. The square matrix is invertible if and only if its determinant is non zero.

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

  1. //This is a simple program to check whether the matrix is invertible or not.
  2. //The complexity of the algorithm is O(n^3)
  3.  
  4. import java.util.*;
  5.  
  6. public class Invertible_Matrix 
  7. {
  8.     public double determinant(double A[][],int N)
  9.     {
  10.         double det=0;
  11.         if(N == 1)
  12.         {
  13.             det = A[0][0];
  14.         }
  15.         else if (N == 2)
  16.         {
  17.             det = A[0][0]*A[1][1] - A[1][0]*A[0][1];
  18.         }
  19.         else
  20.         {
  21.             det=0;
  22.             for(int j1=0;j1<N;j1++)
  23.             {
  24.                 double[][] m = new double[N-1][];
  25.                 for(int k=0;k<(N-1);k++)
  26.                 {
  27.                     m[k] = new double[N-1];
  28.                 }
  29.                 for(int i=1;i<N;i++)
  30.                 {
  31.                     int j2=0;
  32.                     for(int j=0;j<N;j++)
  33.                     {
  34.                         if(j == j1)
  35.                             continue;
  36.                         m[i-1][j2] = A[i][j];
  37.                         j2++;
  38.                     }
  39.                 }
  40.                 det += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);
  41.             }
  42.         }
  43.         return det;
  44.     }
  45.  
  46.     public static void main(String args[])
  47.     {
  48.         Scanner input = new Scanner(System.in);
  49.         System.out.println("Enter the order of the square matrix");
  50.         int n = input.nextInt();
  51.  
  52.         System.out.println("Enter the elements of the square matrix");
  53.         double[][] mat = new double[n][n];
  54.         for(int i=0; i<n; i++)
  55.         {
  56.             for(int j=0; j<n; j++)
  57.             {
  58.                 mat[i][j] = input.nextDouble();
  59.             }
  60.         }
  61.  
  62.         Invertible_Matrix I = new Invertible_Matrix();
  63.  
  64.         if(I.determinant(mat, n) == 0)
  65.         {
  66.             System.out.println("Matrix is not Invertible, as the determinant is : "+I.determinant(mat, n));
  67.         }
  68.         else
  69.         {
  70.             System.out.println("Matrix is Invertible, as the determinant is : "+I.determinant(mat, n));
  71.         }
  72.  
  73.         input.close();
  74. 	}
  75. }

Output:

advertisement
$ javac Invertible_Matrix.java
$ java Invertible_matrix
Enter the order of the square matrix: 
3
Enter the elements of the square matrix: 
1 2 3
4 5 6
7 8 9
Matrix is not Invertible, as the determinant is : 0.0

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
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.