How to Find the Determinant of a 2x2 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.
Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course .
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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++
#include <iostream>
using
namespace
std;
#define N 4
void
getCofactor(
int
mat[N][N],
int
temp[N][N],
int
p,
int
q,
int
n)
{
int
i = 0, j = 0;
for
(
int
row = 0; row < n; row++)
{
for
(
int
col = 0; col < n; col++)
{
if
(row != p && col != q)
{
temp[i][j++] = mat[row][col];
if
(j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
int
determinantOfMatrix(
int
mat[N][N],
int
n)
{
int
D = 0;
if
(n == 1)
return
mat[0][0];
int
temp[N][N];
int
sign = 1;
for
(
int
f = 0; f < n; f++)
{
getCofactor(mat, temp, 0, f, n);
D += sign * mat[0][f]
* determinantOfMatrix(temp, n - 1);
sign = -sign;
}
return
D;
}
void
display(
int
mat[N][N],
int
row,
int
col)
{
for
(
int
i = 0; i < row; i++)
{
for
(
int
j = 0; j < col; j++)
cout <<
" "
<< mat[i][j];
cout <<
"n"
;
}
}
int
main()
{
int
mat[N][N] = { { 1, 0, 2, -1 },
{ 3, 0, 0, 5 },
{ 2, 1, 4, -3 },
{ 1, 0, 5, 0 } };
cout <<
"Determinant of the matrix is : "
<< determinantOfMatrix(mat, N);
return
0;
}
C
#include <stdio.h>
#define N 4
void
getCofactor(
int
mat[N][N],
int
temp[N][N],
int
p,
int
q,
int
n)
{
int
i = 0, j = 0;
for
(
int
row = 0; row < n; row++)
{
for
(
int
col = 0; col < n; col++)
{
if
(row != p && col != q)
{
temp[i][j++] = mat[row][col];
if
(j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
int
determinantOfMatrix(
int
mat[N][N],
int
n)
{
int
D = 0;
if
(n == 1)
return
mat[0][0];
int
temp[N][N];
int
sign = 1;
for
(
int
f = 0; f < n; f++)
{
getCofactor(mat, temp, 0, f, n);
D += sign * mat[0][f]
* determinantOfMatrix(temp, n - 1);
sign = -sign;
}
return
D;
}
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"
);
}
}
int
main()
{
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
class
GFG {
static
final
int
N =
4
;
static
void
getCofactor(
int
mat[][],
int
temp[][],
int
p,
int
q,
int
n)
{
int
i =
0
, j =
0
;
for
(
int
row =
0
; row < n; row++)
{
for
(
int
col =
0
; col < n; col++)
{
if
(row != p && col != q)
{
temp[i][j++] = mat[row][col];
if
(j == n -
1
)
{
j =
0
;
i++;
}
}
}
}
}
static
int
determinantOfMatrix(
int
mat[][],
int
n)
{
int
D =
0
;
if
(n ==
1
)
return
mat[
0
][
0
];
int
temp[][] =
new
int
[N][N];
int
sign =
1
;
for
(
int
f =
0
; f < n; f++)
{
getCofactor(mat, temp,
0
, f, n);
D += sign * mat[
0
][f]
* determinantOfMatrix(temp, n -
1
);
sign = -sign;
}
return
D;
}
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"
);
}
}
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));
}
}
Python3
def
getcofactor(m, i, j):
return
[row[: j]
+
row[j
+
1
:]
for
row
in
(m[: i]
+
m[i
+
1
:])]
def
determinantOfMatrix(mat):
if
(
len
(mat)
=
=
2
):
value
=
mat[
0
][
0
]
*
mat[
1
][
1
]
-
mat[
1
][
0
]
*
mat[
0
][
1
]
return
value
Sum
=
0
for
current_column
in
range
(
len
(mat)):
sign
=
(
-
1
)
*
*
(current_column)
sub_det
=
determinantOfMatrix(getcofactor(mat,
0
, current_column))
Sum
+
=
(sign
*
mat[
0
][current_column]
*
sub_det)
return
Sum
if
__name__
=
=
'__main__'
:
mat
=
[[
1
,
0
,
2
,
-
1
],
[
3
,
0
,
0
,
5
],
[
2
,
1
,
4
,
-
3
],
[
1
,
0
,
5
,
0
]]
print
(
'Determinant of the matrix is :'
, determinantOfMatrix(mat))
C#
using
System;
class
GFG {
static
int
N = 4;
static
void
getCofactor(
int
[, ] mat,
int
[, ] temp,
int
p,
int
q,
int
n)
{
int
i = 0, j = 0;
for
(
int
row = 0; row < n; row++)
{
for
(
int
col = 0; col < n; col++)
{
if
(row != p && col != q) {
temp[i, j++] = mat[row, col];
if
(j == n - 1) {
j = 0;
i++;
}
}
}
}
}
static
int
determinantOfMatrix(
int
[, ] mat,
int
n)
{
int
D = 0;
if
(n == 1)
return
mat[0, 0];
int
[, ] temp =
new
int
[N, N];
int
sign = 1;
for
(
int
f = 0; f < n; f++)
{
getCofactor(mat, temp, 0, f, n);
D += sign * mat[0, f]
* determinantOfMatrix(temp, n - 1);
sign = -sign;
}
return
D;
}
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"
);
}
}
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));
}
}
Javascript
<script>
let N = 4;
function
getCofactor(mat,temp,p,q,n)
{
let i = 0, j = 0;
for
(let row = 0; row < n; row++)
{
for
(let col = 0; col < n; col++)
{
if
(row != p && col != q)
{
temp[i][j++] = mat[row][col];
if
(j == n - 1)
{
j = 0;
i++;
}
}
}
}
}
function
determinantOfMatrix(mat,n)
{
let D = 0;
if
(n == 1)
return
mat[0][0];
let temp =
new
Array(N);
for
(let i=0;i<N;i++)
{
temp[i]=
new
Array(N);
}
let sign = 1;
for
(let f = 0; f < n; f++)
{
getCofactor(mat, temp, 0, f, n);
D += sign * mat[0][f]
* determinantOfMatrix(temp, n - 1);
sign = -sign;
}
return
D;
}
function
display(mat,row,col)
{
for
(let i = 0; i < row; i++)
{
for
(let j = 0; j < col; j++)
document.write(mat[i][j]);
document.write(
"<br>"
);
}
}
let mat=[[ 1, 0, 2, -1 ],
[ 3, 0, 0, 5 ],
[ 2, 1, 4, -3 ],
[ 1, 0, 5, 0 ]];
document.write(
"Determinant "
+
"of the matrix is : "
+ determinantOfMatrix(mat, N));
</script>
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,
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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 properties of Determinant. In this approach we are converting the given matrix into upper triangular matrix using determinant properties The determinant of upper triangular 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
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using
namespace
std;
#define N 4
int
determinantOfMatrix(
int
mat[N][N],
int
n)
{
int
num1, num2, det = 1, index,
total = 1;
int
temp[n + 1];
for
(
int
i = 0; i < n; i++)
{
index = i;
while
(mat[index][i] == 0 && index < n)
{
index++;
}
if
(index == n)
{
continue
;
}
if
(index != i)
{
for
(
int
j = 0; j < n; j++)
{
swap(mat[index][j], mat[i][j]);
}
det = det *
pow
(-1, index - i);
}
for
(
int
j = 0; j < n; j++)
{
temp[j] = mat[i][j];
}
for
(
int
j = i + 1; j < n; j++)
{
num1 = temp[i];
num2 = mat[j][i];
for
(
int
k = 0; k < n; k++)
{
mat[j][k]
= (num1 * mat[j][k]) - (num2 * temp[k]);
}
total = total * num1;
}
}
for
(
int
i = 0; i < n; i++)
{
det = det * mat[i][i];
}
return
(det / total);
}
int
main()
{
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
class
GFG
{
static
final
int
N =
4
;
static
int
determinantOfMatrix(
int
mat[][],
int
n)
{
int
num1, num2, det =
1
, index,
total =
1
;
int
[] temp =
new
int
[n +
1
];
for
(
int
i =
0
; i < n; i++)
{
index = i;
while
(mat[index][i] ==
0
&& index < n)
{
index++;
}
if
(index == n)
{
continue
;
}
if
(index != i)
{
for
(
int
j =
0
; j < n; j++)
{
swap(mat, index, j, i, j);
}
det = (
int
)(det * Math.pow(-
1
, index - i));
}
for
(
int
j =
0
; j < n; j++)
{
temp[j] = mat[i][j];
}
for
(
int
j = i +
1
; j < n; j++)
{
num1 = temp[i];
num2 = mat[j]
[i];
for
(
int
k =
0
; k < n; k++)
{
mat[j][k] = (num1 * mat[j][k])
- (num2 * temp[k]);
}
total = total * num1;
}
}
for
(
int
i =
0
; i < n; i++)
{
det = det * mat[i][i];
}
return
(det / total);
}
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;
}
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.printf(
"Determinant of the matrix is : %d"
,
determinantOfMatrix(mat, N));
}
}
Python3
def
determinantOfMatrix(mat, n):
temp
=
[
0
]
*
n
total
=
1
det
=
1
for
i
in
range
(
0
, n):
index
=
i
while
(mat[index][i]
=
=
0
and
index < n):
index
+
=
1
if
(index
=
=
n):
continue
if
(index !
=
i):
for
j
in
range
(
0
, n):
mat[index][j], mat[i][j]
=
mat[i][j], mat[index][j]
det
=
det
*
int
(
pow
(
-
1
, index
-
i))
for
j
in
range
(
0
, n):
temp[j]
=
mat[i][j]
for
j
in
range
(i
+
1
, n):
num1
=
temp[i]
num2
=
mat[j][i]
for
k
in
range
(
0
, n):
mat[j][k]
=
(num1
*
mat[j][k])
-
(num2
*
temp[k])
total
=
total
*
num1
for
i
in
range
(
0
, n):
det
=
det
*
mat[i][i]
return
int
(det
/
total)
if
__name__
=
=
"__main__"
:
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#
using
System;
class
GFG {
static
readonly
int
N = 4;
static
int
determinantOfMatrix(
int
[, ] mat,
int
n)
{
int
num1, num2, det = 1, index,
total = 1;
int
[] temp =
new
int
[n + 1];
for
(
int
i = 0; i < n; i++)
{
index = i;
while
(mat[index, i] == 0 && index < n)
{
index++;
}
if
(index == n)
{
continue
;
}
if
(index != i)
{
for
(
int
j = 0; j < n; j++)
{
swap(mat, index, j, i, j);
}
det = (
int
)(det * Math.Pow(-1, index - i));
}
for
(
int
j = 0; j < n; j++)
{
temp[j] = mat[i, j];
}
for
(
int
j = i + 1; j < n; j++)
{
num1 = temp[i];
num2 = mat[j,
i];
for
(
int
k = 0; k < n; k++)
{
mat[j, k] = (num1 * mat[j, k])
- (num2 * temp[k]);
}
total = total * num1;
}
}
for
(
int
i = 0; i < n; i++)
{
det = det * mat[i, i];
}
return
(det / total);
}
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;
}
public
static
void
Main(String[] args)
{
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));
}
}
Javascript
Javascript<script>
var
N = 4;
function
determinantOfMatrix(mat , n)
{
var
num1, num2, det = 1, index,
total = 1;
var
temp = Array(n + 1).fill(0);
for
(i = 0; i < n; i++)
{
index = i;
while
(mat[index][i] == 0 && index < n)
{
index++;
}
if
(index == n)
{
continue
;
}
if
(index != i)
{
for
(j = 0; j < n; j++)
{
swap(mat, index, j, i, j);
}
det = parseInt((det * Math.pow(-1, index - i)));
}
for
(j = 0; j < n; j++)
{
temp[j] = mat[i][j];
}
for
(j = i + 1; j < n; j++)
{
num1 = temp[i];
num2 = mat[j]
[i];
for
(k = 0; k < n; k++)
{
mat[j][k] = (num1 * mat[j][k])
- (num2 * temp[k]);
}
total = total * num1;
}
}
for
(i = 0; i < n; i++)
{
det = det * mat[i][i];
}
return
(det / total);
}
function
swap(arr , i1 , j1 , i2,
j2)
{
var
temp = arr[i1][j1];
arr[i1][j1] = arr[i2][j2];
arr[i2][j2] = temp;
return
arr;
}
var
mat = [ [ 1, 0, 2, -1 ],
[ 3, 0, 0, 5 ],
[ 2, 1, 4, -3 ],
[ 1, 0, 5, 0 ] ];
document.write(
"Determinant of the matrix is : "
,
determinantOfMatrix(mat, N));
</script>
Output
Determinant of the matrix is : 30
Time complexity: O(n3)
Auxiliary Space: O(n)
Method 3: Using numpy package in python
There is a built-in function or method in linalg module of numpy package in python. It can be called as numpy.linalg.det(mat) which returns the determinant value of matrix mat passed in the argument.
Python3
import
numpy as np
def
determinant(mat):
det
=
np.linalg.det(mat)
return
round
(det)
mat
=
[[
1
,
0
,
2
,
-
1
],
[
3
,
0
,
0
,
5
],
[
2
,
1
,
4
,
-
3
],
[
1
,
0
,
5
,
0
]]
print
(
'Determinant of the matrix is:'
,
determinant(mat))
Output:
Determinant of the matrix is: 30.0
How to Find the Determinant of a 2x2 Matrix
Source: https://www.geeksforgeeks.org/determinant-of-a-matrix/
0 Response to "How to Find the Determinant of a 2x2 Matrix"
Post a Comment