# Determinant of a Matrix

What is Determinant of a Matrix?
Determinant of a Matrix is a special number that is defined only for square matrices (matrices which have same number of rows and columns). Determinant is used at many places in calculus and other matrix related algebra, it actually represents the matrix in term of a real number which can be used in solving system of linear equation and finding the inverse of a matrix.

How to calculate?
The value of determinant of a matrix can be calculated by following procedure –
For each element of first row or first column get cofactor of those elements and then multiply the element with the determinant of the corresponding cofactor, and finally add them with alternate signs. As a base case the value of determinant of a 1*1 matrix is the single value itself.
Cofactor of an element, is a matrix which we can get by removing row and column of that element from that matrix.

Determinant of 2 x 2 Matrix:  Determinant of 3 x 3 Matrix:  ## C++

 // C++ program to find Deteminant of a matrix  #include  using namespace std;     // Dimension of input square matrix  #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 for finding determinant of matrix.     n is current dimension of mat[][]. */ int determinantOfMatrix(int mat[N][N], int n)  {      int D = 0; // Initialize result         //  Base case : if matrix contains single element      if (n == 1)          return mat;         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[f]          getCofactor(mat, temp, 0, f, n);          D += sign * mat[f] * determinantOfMatrix(temp, n - 1);             // terms are to be added with alternate sign          sign = -sign;      }         return D;  }     /* function for displaying the matrix */ void display(int mat[N][N], int row, int col)  {      for (int i = 0; i < row; i++)      {          for (int j = 0; j < col; j++)              printf("  %d", mat[i][j]);          printf("n");      }  }     // Driver program to test above functions  int main()  {      /* int mat[N][N] = {{6, 1, 1},                       {4, -2, 5},                       {2, 8, 7}}; */        int mat[N][N] = {{1, 0, 2, -1},                       {3, 0, 0, 5},                       {2, 1, 4, -3},                       {1, 0, 5, 0}                      };         printf("Determinant of the matrix is : %d",              determinantOfMatrix(mat, N));      return 0;  }

## Java

 // Java program to find Deteminant of   // a matrix  class GFG {             // Dimension of input square matrix      static final int N = 4;             // 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 for finding determinant      of matrix. n is current dimension of mat[][]. */     static int determinantOfMatrix(int mat[][], int n)      {          int D = 0; // Initialize result                 // Base case : if matrix contains single          // element          if (n == 1)              return mat;                     // To store cofactors          int temp[][] = new int[N][N];                      // To store sign multiplier          int sign = 1;                  // Iterate for each element of first row          for (int f = 0; f < n; f++)          {              // Getting Cofactor of mat[f]              getCofactor(mat, temp, 0, f, n);              D += sign * mat[f]                  * determinantOfMatrix(temp, n - 1);                     // terms are to be added with               // alternate sign              sign = -sign;          }                 return D;      }             /* function for displaying the matrix */     static void display(int mat[][], int row, int col)      {          for (int i = 0; i < row; i++)          {              for (int j = 0; j < col; j++)                  System.out.print(mat[i][j]);                                 System.out.print("\n");          }      }             // Driver code      public static void main (String[] args)      {                     int mat[][] = {{1, 0, 2, -1},                          {3, 0, 0, 5},                          {2, 1, 4, -3},                          {1, 0, 5, 0}};                 System.out.print("Determinant " +                       "of the matrix is : "+               determinantOfMatrix(mat, N));      }  }     // This code is contributed by Anant Agarwal.

## C#

 // C# program to find Deteminant of   // a matrix  using System;  class GFG {             // Dimension of input square matrix      static int N = 4;             // 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 for         finding determinant         of matrix. n is current          dimension of mat[][]. */     static int determinantOfMatrix(int [,]mat,                                      int n)      {          int D = 0; // Initialize result                 // Base case : if matrix           // contains single          // element          if (n == 1)              return mat[0, 0];                     // To store cofactors          int [,]temp = new int[N, N];                      // To store sign multiplier          int sign = 1;                  // Iterate for each element          // of first row          for (int f = 0; f < n; f++)          {                             // Getting Cofactor of mat[f]              getCofactor(mat, temp, 0, f, n);              D += sign * mat[0, f]               * determinantOfMatrix(temp, n - 1);                     // terms are to be added with               // alternate sign              sign = -sign;          }                 return D;      }             /* function for displaying         the matrix */     static void display(int [,]mat,                           int row,                           int col)      {          for (int i = 0; i < row; i++)          {              for (int j = 0; j < col; j++)                  Console.Write(mat[i,j]);                                 Console.Write("\n");          }      }             // Driver code      public static void Main ()      {                     int [,]mat = {{1, 0, 2, -1},                        {3, 0, 0, 5},                        {2, 1, 4, -3},                        {1, 0, 5, 0}};             Console.Write("Determinant " +                       "of the matrix is : "+                       determinantOfMatrix(mat, N));      }  }     // This code is contributed by nitin mittal.

Output:

Determinant of the matrix is : 30

Adjoint and Inverse of a Matrix
There are various properties of the Determinant which can be helpful for solving problems related with matrices,

In Above Method Recursive Approach is discussed.When the size of matrix is large it consumes more stack size
In this Method We are using the properities of Determinant.In this approach we are converting the given matrix into upper triangular matrix using determinant properties The determinant of upper traingular matrix is the product of all diagonal elements For properties on determinant go through this website https://cran.r-project.org/web/packages/matlib/vignettes/det-ex1.html
In this approach we are iterating every diagonal element and making all the elements down the diagonal as zero using determinant properties
If the diagonal element is zero then we will search next non zero element in the same column
There exist two cases
Case 1:
If there is no non zero element.In this case the determinant of matrix is zero
Case 2:
If there exists non zero element there exist two cases
Case a:
if index is with respective diagonal row element.Using the determinant properties we make all the column elements down to it as zero
Case b:
Here we need to swap the row with respective to diagonal element column and continue the case ‘a; operation
Time complexity : O(n^3)
Auxiliary Space : O(n)

## C++

 // C++ program to find Deteminant of a matrix   #include    using namespace std;         // Dimension of input square matrix    #define N 4    // Function to get determinant of matrix    int determinantOfMatrix(int mat[N][N], int n)    {        int num1,num2,det = 1,index,total = 1; // Initialize result                 // temporary array for storing row        int temp[n + 1];                 //loop for traversing the diagonal elements       for(int i = 0; i < n; i++)         {           index = i; // initialize the index                         //finding the index which has non zero value            while(mat[index][i] == 0 && index < n) {                index++;                                 }            if(index == n) // if there is non zero element            {                // the determinat of matrix as zero                continue;                             }            if(index != i)            {                //loop for swaping the diagonal element row and index row                for(int j = 0; j < n; j++)                {                    swap(mat[index][j],mat[i][j]);                   }                    //determinant sign changes when we shift rows                    //go through determinant properties                    det = det*pow(-1,index-i);             }                       //storing the values of diagonal row elements           for(int j = 0; j < n; j++)           {               temp[j] = mat[i][j];                           }           //traversing every row below the diagonal element           for(int j = i+1; j < n; j++)           {               num1 = temp[i]; //value of diagonal element               num2 = mat[j][i]; //value of next row element                               //traversing every column of row               // and multiplying to every row               for(int k = 0; k < n; k++)               {                   //multiplying to make the diagonal                   // element and next row element equal                   mat[j][k] = (num1 * mat[j][k]) - (num2 * temp[k]);                                   }               total = total * num1; // Det(kA)=kDet(A);               }                     }             //mulitplying the diagonal elements to get determinant        for(int i = 0; i < n; i++)        {            det = det * mat[i][i];                     }       return (det/total); //Det(kA)/k=Det(A);        }         // Driver code   int main()    {    /*int mat[N][N] = {{6, 1, 1},                        {4, -2, 5},                        {2, 8, 7}}; */          int mat[N][N] = {{1, 0, 2, -1},                        {3, 0, 0, 5},                        {2, 1, 4, -3},                        {1, 0, 5, 0}                        };                 printf("Determinant of the matrix is : %d",                determinantOfMatrix(mat, N));        return 0;             }

## Java

 // Java program to find Deteminant of a matrix   class GFG  {          // Dimension of input square matrix   static final int N = 4;      // Function to get determinant of matrix   static int determinantOfMatrix(int mat[][], int n)   {       int num1, num2, det = 1, index, total = 1; // Initialize result                  // temporary array for storing row       int []temp = new int[n + 1];                  // loop for traversing the diagonal elements       for(int i = 0; i < n; i++)       {           index = i; // initialize the index                          // finding the index which has non zero value           while(mat[index][i] == 0 && index < n)          {               index++;                                  }           if(index == n) // if there is non zero element           {               // the determinat of matrix as zero               continue;                              }           if(index != i)           {               // loop for swaping the diagonal element row and index row               for(int j = 0; j < n; j++)               {                   swap(mat, index, j, i, j);               }                   // determinant sign changes when we shift rows                   // go through determinant properties                   det = (int) (det*Math.pow(-1, index - i));           }                      // storing the values of diagonal row elements           for(int j = 0; j < n; j++)           {               temp[j] = mat[i][j];                              }                  // traversing every row below the diagonal element           for(int j = i + 1; j < n; j++)           {               num1 = temp[i]; // value of diagonal element               num2 = mat[j][i]; // value of next row element                                  // traversing every column of row               // and multiplying to every row               for(int k = 0; k < n; k++)               {                                      // multiplying to make the diagonal                   // element and next row element equal                   mat[j][k] = (num1 * mat[j][k]) - (num2 * temp[k]);                                      }               total = total * num1; // Det(kA)=kDet(A);           }                      }              // mulitplying the diagonal elements to get determinant       for(int i = 0; i < n; i++)       {           det = det * mat[i][i];                      }       return (det/total); // Det(kA)/k=Det(A);   }      static int[][] swap(int [][]arr, int i1, int j1, int i2, int j2)  {      int temp = arr[i1][j1];      arr[i1][j1] = arr[i2][j2];      arr[i2][j2] = temp;      return arr;  }      // Driver code   public static void main(String[] args)   {       /*int mat[N][N] = {{6, 1, 1},                       {4, -2, 5},                       {2, 8, 7}}; */            int mat[][] = {{1, 0, 2, -1},                       {3, 0, 0, 5},                       {2, 1, 4, -3},                       {1, 0, 5, 0}                       };                  System.out.printf("Determinant of the matrix is : %d",               determinantOfMatrix(mat, N));   }   }      // This code is contributed by Rajput-Ji

## Python3

 #Python program to find Determinant of a matrix     def determinantOfMatrix(mat,n):         temp = *n # temporary array for storing row        total=1      det=1  # initialize result         # loop for traversing the diagonal elements       for i in range(0,n):          index=i  # initialize the index             # finding the index which has non zero value            while(mat[index][i] == 0 and index < n):              index+=1               if(index == n): # if there is non zero element              # the determinat of matrix as zero              continue            if(index != i):              # loop for swaping the diagonal element row and index row              for j in range(0,n):                  mat[index][j],mat[i][j] = mat[i][j],mat[index][j]                                 # determinant sign changes when we shift rows                # go through determinant properties                det = det*int(pow(-1,index-i))               # storing the values of diagonal row elements           for j in range(0,n):              temp[j] = mat[i][j]                      #traversing every row below the diagonal element          for j in range(i+1,n):              num1 = temp[i]     # value of diagonal element              num2 = mat[j][i]   # value of next row element                 # traversing every column of row                # and multiplying to every row                for k in range(0,n):                  # multiplying to make the diagonal                    # element and next row element equal                     mat[j][k] = (num1*mat[j][k]) - (num2*temp[k])                 total = total * num1 # Det(kA)=kDet(A);         #mulitplying the diagonal elements to get determinant      for i in range(0,n):           det = det*mat[i][i]            return int(det/total) #Det(kA)/k=Det(A);       #Drivers code  if __name__ == "__main__":      # mat=[[6 1 1][4 -2 5][2 8 7]]         mat=[[1, 0, 2, -1],[3, 0, 0, 5],[2, 1, 4, -3],[1, 0, 5, 0]]      N=len(mat)      print("Determinant of the matrix is : ",determinantOfMatrix(mat,N))

## C#

 // C# program to find Deteminant of a matrix   using System;     class GFG  {          // Dimension of input square matrix   static readonly int N = 4;      // Function to get determinant of matrix   static int determinantOfMatrix(int [,]mat, int n)   {       int num1, num2, det = 1, index, total = 1; // Initialize result                  // temporary array for storing row       int []temp = new int[n + 1];                  // loop for traversing the diagonal elements       for(int i = 0; i < n; i++)       {           index = i; // initialize the index                          // finding the index which has non zero value           while(mat[index, i] == 0 && index < n)          {               index++;                                  }           if(index == n) // if there is non zero element           {               // the determinat of matrix as zero               continue;                              }           if(index != i)           {               // loop for swaping the diagonal element row and index row               for(int j = 0; j < n; j++)               {                   swap(mat, index, j, i, j);               }                   // determinant sign changes when we shift rows                   // go through determinant properties                   det = (int) (det*Math.Pow(-1, index - i));           }                      // storing the values of diagonal row elements           for(int j = 0; j < n; j++)           {               temp[j] = mat[i, j];                              }                  // traversing every row below the diagonal element           for(int j = i + 1; j < n; j++)           {               num1 = temp[i]; // value of diagonal element               num2 = mat[j, i]; // value of next row element                                  // traversing every column of row               // and multiplying to every row               for(int k = 0; k < n; k++)               {                                      // multiplying to make the diagonal                   // element and next row element equal                   mat[j,k] = (num1 * mat[j, k]) - (num2 * temp[k]);                                      }               total = total * num1; // Det(kA)=kDet(A);           }                      }              // mulitplying the diagonal elements to get determinant       for(int i = 0; i < n; i++)       {           det = det * mat[i, i];                      }       return (det/total); // Det(kA)/k=Det(A);   }      static int[,] swap(int [,]arr, int i1, int j1, int i2, int j2)  {      int temp = arr[i1, j1];      arr[i1, j1] = arr[i2, j2];      arr[i2, j2] = temp;      return arr;  }      // Driver code   public static void Main(String[] args)   {       /*int mat[N,N] = {{6, 1, 1},                       {4, -2, 5},                       {2, 8, 7}}; */            int [,]mat = {{1, 0, 2, -1},                       {3, 0, 0, 5},                       {2, 1, 4, -3},                       {1, 0, 5, 0}                       };                  Console.Write("Determinant of the matrix is : {0}",               determinantOfMatrix(mat, N));   }   }      // This code is contributed by 29AjayKumar

Output:

Determinant of the matrix is : 30

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

7

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.