# Swift program to check if a given square matrix is an Identity Matrix

In this article, we will learn how to write a swift program to check if a given square matrix is the identity matrix. The identity matrix is an MxM square matrix in which the main diagonal consists of ones and other elements are all zero. For example −

$\mathrm{M\:=\:\begin{bmatrix}1 & 0 & 0 & 0\newline0 & 1 & 0 & 0 \newline0 & 0 & 1 & 0 \newline0 & 0 & 0 & 1\end{bmatrix}}$

Whereas a square matrix is a matrix in which the number of rows is equal to number of columns and it may or may not contain zeros and ones, means apart from 0s and 1s it contain other numbers. For example −

$\mathrm{M\:=\:\begin{bmatrix}1 & 3 & 4 & 0\newline6 & 4 & 0 & 6 \newline3 & 6 & 1 & 0 \newline4 & 4 & 0 & 1\end{bmatrix}}$

## Algorithm

Step 1 − Create a function to check the given matrix is the identity matrix.

Step 2 − In this function, first we check the given matrix is the square matrix by checking the total number of rows and columns.

Step 3 − Using for loop, we check all the elements present in the main diagonal should be one.

Step 4 − Using nested for loop, we check all the elements other than diagonals elements are zero.

Step 5 − If all the conditions mentioned in steps 2, 3, and 4 are true, then this function will return true. Otherwise, return false.

Step 6 − Create three test matrices of integer type.

Step 7 − Call the function and pass the created matrices as a parameter in it.

Step 8 − Print the output.

## Example

Following Swift program to check if a given square matrix is identity matrix.

import Foundation
import Glibc
func CheckIdentityMatrix(mxt:[[Int]])->Bool
{
// Verifying the given matrix is the square matrix
if mxt.count != mxt[0].count
{
return false
}
for x in 0..<mxt.count
{
if mxt[x][x] != 1
{
return false
}
}
for m in 0..<mxt.count
{
for n in 0..<mxt[0].count
{
if m != n && mxt[m][n] != 0
{
return false
}
}
}
return true
}
var matrix1 = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
print("Is matrix 1 is an identity matrix?: ", CheckIdentityMatrix(mxt: matrix1))
var matrix2 = [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 1]]
print("Is matrix 2 is an identity matrix?: ", CheckIdentityMatrix(mxt: matrix2))
var matrix3 = [[2, 1, 4], [2, 1, 1], [4, 5, 0], [3, 4, 1]]
print("Is matrix 3 is an identity matrix?: ", CheckIdentityMatrix(mxt: matrix3))


## Output

Is matrix 1 is an identity matrix?: true
Is matrix 2 is an identity matrix?: false
Is matrix 3 is an identity matrix?: false


Here in the above code, we create a function to check whether the given matrices are the identity matrices or not. Therefore, for that, we check the given matrix for three different conditions −

• Given matrix is a square matrix or not?

• The main diagonal of the given matrix consists of ones (all the elements of the main diagonal) or not.

• Elements other than the main diagonal should be zero or not.

If any of the above conditions return false, then the given matrix is not the identity matrix. Alternatively, if all the above three conditions return true, then the given matrix is an identity matrix.

## Conclusion

So this is how we can if a given square matrix is an identity matrix or not. If you multiply two identity matrices with each other, then the resultant matrix is also the identity matrix.