Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Matrix
  • Practice Matrix
  • MCQs on Matrix
  • Tutorial on Matrix
  • Matrix Traversal
  • Sorting in Matrix
  • Matrix Rotation
  • Transpose of Matrix
  • Inverse of Matrix
  • Determinant of Matrix
  • Matrix Application
  • Adjoint & Inverse Matrix
  • Sparse Matrix
  • Matrix Exponentiation
Open In App
Next Article:
Program to check if matrix is singular or not
Next article icon

Program to check if matrix is singular or not

Last Updated : 06 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A matrix is said to be singular if the determinant of the matrix is 0 otherwise it is non-singular .

Examples: 

Input :  0 0 0
4 5 6
1 2 3
Output : Yes
Determinant value of the matrix is
0 (Note first row is 0)

Input : 1 0 0
4 5 6
1 2 3
Output : No
Determinant value of the matrix is 3
(which is non-zero).

First find the determinant of the matrix and the check the condition if the determinant id 0 or not, if it is 0 then matrix is a singular matrix otherwise it is a non-singular matrix .

Implementation:

C++
// C++ program check if a matrix is // singular or not. #include <bits/stdc++.h> using namespace std; #define N 4  // Function to get cofactor of mat[p][q] in temp[][]. // n is current dimension of mat[][] void getCofactor(int mat[N][N], int temp[N][N], int p,                                           int q, int n) {     int i = 0, j = 0;      // Looping for each element of the matrix     for (int row = 0; row < n; row++) {         for (int col = 0; col < n; col++) {              // Copying into temporary matrix only              // those element which are not in given              // row and column             if (row != p && col != q) {                 temp[i][j++] = mat[row][col];                  // Row is filled, so increase row                 // index and reset col index                 if (j == n - 1) {                     j = 0;                     i++;                 }             }         }     } }  /* Recursive function to check if mat[][] is    singular or not. */ bool isSingular(int mat[N][N], int n) {     int D = 0; // Initialize result      // Base case : if matrix contains single element     if (n == 1)         return mat[0][0];      int temp[N][N]; // To store cofactors      int sign = 1; // To store sign multiplier      // Iterate for each element of first row     for (int f = 0; f < n; f++) {          // Getting Cofactor of mat[0][f]         getCofactor(mat, temp, 0, f, n);         D += sign * mat[0][f] * isSingular(temp, n - 1);          // terms are to be added with alternate sign         sign = -sign;     }      return D; }  // Driver program to test above functions int main() {     int mat[N][N] = { { 4, 10, 1 },                       { 0, 0, 0 },                       { 1, 4, -3 } };     if (isSingular(mat, N))         cout << "Matrix is Singular" << endl;     else         cout << "Matrix is non-singular" << endl;     return 0; } 
Java
// Java program check if a matrix is  // singular or not.  class GFG  {      static final int N = 3;      // Function to get cofactor of mat[p][q] in temp[][].      // n is current dimension of mat[][]      static void getCofactor(int mat[][], int temp[][], int p,                                               int q, int n)     {         int i = 0, j = 0;          // Looping for each element of the matrix          for (int row = 0; row < n; row++)          {             for (int col = 0; col < n; col++)              {                  // Copying into temporary matrix only                  // those element which are not in given                  // row and column                  if (row != p && col != q)                  {                     temp[i][j++] = mat[row][col];                      // Row is filled, so increase row                      // index and reset col index                      if (j == n - 1)                     {                         j = 0;                         i++;                     }                 }             }         }     }      /* Recursive function to check if mat[][] is      singular or not. */     static int isSingular(int mat[][], int n)      {         int D = 0; // Initialize result           // Base case : if matrix contains single element          if (n == 1)         {             return mat[0][0];         }          int temp[][] = new int[N][N]; // To store cofactors           int sign = 1; // To store sign multiplier           // Iterate for each element of first row          for (int f = 0; f < n; f++)         {              // Getting Cofactor of mat[0][f]              getCofactor(mat, temp, 0, f, n);             D += sign * mat[0][f] * isSingular(temp, n - 1);              // terms are to be added with alternate sign              sign = -sign;         }          return D;     }      // Driver code      public static void main(String[] args)      {         int mat[][] = {{4, 10, 1},                         {0, 0, 0},                         {1, 4, -3}};         if (isSingular(mat, N) == 1)          {             System.out.println("Matrix is Singular");         }          else          {             System.out.println("Matrix is non-singular");         }     } }  /* This code contributed by PrinciRaj1992 */ 
Python3
# python 3 program check if a matrix is # singular or not. global N N = 3  # Function to get cofactor of mat[p][q] in temp[][]. # n is current dimension of mat[][] def getCofactor(mat,temp,p,q,n):     i = 0     j = 0          # Looping for each element of the matrix     for row in range(n):         for col in range(n):                          # Copying into temporary matrix only              # those element which are not in given              # row and column             if (row != p and col != q):                 temp[i][j] = mat[row][col]                 j += 1                                  # Row is filled, so increase row                 # index and reset col index                 if (j == n - 1):                     j = 0                     i += 1  # Recursive function to check if mat[][] is # singular or not. */ def isSingular(mat,n):     D = 0 # Initialize result          # Base case : if matrix contains single element     if (n == 1):         return mat[0][0]              temp = [[0 for i in range(N + 1)] for i in range(N + 1)]# To store cofactors          sign = 1 # To store sign multiplier      # Iterate for each element of first row     for f in range(n):                  # Getting Cofactor of mat[0][f]         getCofactor(mat, temp, 0, f, n)         D += sign * mat[0][f] * isSingular(temp, n - 1)                  # terms are to be added with alternate sign         sign = -sign     return D  # Driver program to test above functions if __name__ == '__main__':     mat = [[4, 10, 1],[0, 0, 0],[1, 4, -3]]     if (isSingular(mat, N)):         print("Matrix is Singular")     else:         print("Matrix is non-singular")  # This code is contributed by # Surendra_Gangwar 
C#
// C# program check if a matrix is  // singular or not. using System;  class GFG  {      static readonly int N = 3;      // Function to get cofactor of mat[p,q] in temp[,].      // n is current dimension of mat[,]      static void getCofactor(int [,]mat, int [,]temp, int p,                                             int q, int n)     {         int i = 0, j = 0;          // Looping for each element of the matrix          for (int row = 0; row < n; row++)          {             for (int col = 0; col < n; col++)              {                  // Copying into temporary matrix only                  // those element which are not in given                  // row and column                  if (row != p && col != q)                  {                     temp[i, j++] = mat[row, col];                      // Row is filled, so increase row                      // index and reset col index                      if (j == n - 1)                     {                         j = 0;                         i++;                     }                 }             }         }     }      /* Recursive function to check if mat[,] is      singular or not. */     static int isSingular(int [,]mat, int n)      {         int D = 0; // Initialize result           // Base case : if matrix contains single element          if (n == 1)         {             return mat[0, 0];         }          int [,]temp = new int[N, N]; // To store cofactors           int sign = 1; // To store sign multiplier           // Iterate for each element of first row          for (int f = 0; f < n; f++)         {              // Getting Cofactor of mat[0,f]              getCofactor(mat, temp, 0, f, n);             D += sign * mat[0, f] * isSingular(temp, n - 1);              // terms are to be added with alternate sign              sign = -sign;         }          return D;     }      // Driver code      public static void Main(String[] args)      {         int [,]mat = {{4, 10, 1},                         {0, 0, 0},                         {1, 4, -3}};         if (isSingular(mat, N) == 1)          {             Console.WriteLine("Matrix is Singular");         }          else         {             Console.WriteLine("Matrix is non-singular");         }     } }  // This code contributed by Rajput-Ji 
JavaScript
<script>   // Javascript program check if a matrix is  // singular or not. var N = 3; // Function to get cofactor of mat[p,q] in temp[,].  // n is current dimension of mat[,]  function getCofactor(mat, temp, p, q, n) {     var i = 0, j = 0;     // Looping for each element of the matrix      for (var row = 0; row < n; row++)      {         for (var col = 0; col < n; col++)          {             // Copying into temporary matrix only              // those element which are not in given              // row and column              if (row != p && col != q)              {                 temp[i][j++] = mat[row][col];                 // Row is filled, so increase row                  // index and reset col index                  if (j == n - 1)                 {                     j = 0;                     i++;                 }             }         }     } } /* Recursive function to check if mat[,] is  singular or not. */ function isSingular(mat, n)  {     var D = 0; // Initialize result      // Base case : if matrix contains single element      if (n == 1)     {         return mat[0][0];     }     var temp = Array.from(Array(N), ()=>Array(N));// To store cofactors      var sign = 1; // To store sign multiplier      // Iterate for each element of first row      for(var f = 0; f < n; f++)     {         // Getting Cofactor of mat[0,f]          getCofactor(mat, temp, 0, f, n);         D += sign * mat[0][f] * isSingular(temp, n - 1);         // terms are to be added with alternate sign          sign = -sign;     }     return D; } // Driver code  var mat = [[4, 10, 1],                 [0, 0, 0],                 [1, 4, -3]]; if (isSingular(mat, N) == 1)  {     document.write("Matrix is Singular"); }  else {     document.write("Matrix is non-singular"); }  // This code is contributed by noob2000. </script> 

Output
Matrix is non-singular

Time complexity: O(n3)
Auxiliary space: O(n2), for temp array to store co-factors


Next Article
Program to check if matrix is singular or not

M

Manish_100
Improve
Article Tags :
  • Matrix
  • DSA
  • Algebra
Practice Tags :
  • Matrix

Similar Reads

    Program to check if matrix is upper triangular
    Given a square matrix mat[][], the task is to determine whether it is in upper triangular form. A matrix is considered upper triangular if all elements below the main diagonal are zero, while the diagonal and elements above it can be any value.Examples: Input: mat[][] = [[1, 2, 3] [0, 5, 6] [0, 0, 9
    4 min read
    Javascript Program to Check if a given matrix is sparse or not
    A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem
    2 min read
    Program to check if matrix is lower triangular
    Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}}; Output : Matrix is in
    4 min read
    Program to check if a matrix is Binary matrix or not
    Given a matrix, the task is to check if that matrix is a Binary Matrix. A Binary Matrix is a matrix in which all the elements are either 0 or 1. It is also called Logical Matrix, Boolean Matrix, Relation Matrix. Examples: Input: {{1, 0, 1, 1}, {0, 1, 0, 1} {1, 1, 1, 0}} Output: Yes Input: {{1, 0, 1,
    5 min read
    Program to check if a matrix is symmetric
    A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row. Examples: Input : 1 2 3 2 1 4 3 4 3 Output : Yes Input : 3 5 8 3 4 7 8 5 3 Output : No A Simple solution is to do fol
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences