Tuesday 24 March 2015

Identity matrix in C#

Question: Program to check whether the matrix is Identity or not.

A matrix is Said to be identity matrix if it satisfied following criteria:

  • Matrix is Square matrix
  • Element of diagonal are all 1.
  • Rest element of matrix excluding diagonal are all zero.


 Identity Matrix : 
Conditions of Identity matrix
We denote the identity matrix of order n. When order is clear by context, we simply write it as I.

For example: 
Identity Matrix Image
In the above A, B, C are Identity matrix of order 1, 2, 3 respectively.
Observe that Scalar Matrix is an identity matrix when  k = 1. But every Identity matrix is clearly a scalar matrix.

Code Snippet:

  1. using System;  
  2.   
  3. public class IdtMat  
  4. {  
  5.    public static void Main()  
  6.    {  
  7.       int i, row, col;  
  8.        
  9.       Console.Write("\nEnter the no. of rows and cols : ");  
  10.       i = Convert.ToInt32(Console.ReadLine());  
  11.       int[,] matA = new int[i, i];       
  12.       Console.WriteLine("\nEnter the Elements in Matrix:\n");  
  13.   
  14.       for(row = 0; row < i; row++)  
  15.       {  
  16.          for(col = 0; col < i; col++)  
  17.          {  
  18.             Console.Write("A[{0}, {1}] = ", row, col);   
  19.             matA[row,col]= Convert.ToInt32(Console.ReadLine());  
  20.          }  
  21.          Console.WriteLine();  
  22.       }  
  23.   
  24.       Console.WriteLine("Matrix A:\n");  
  25.   
  26.       for(row = 0; row < i; row++)  
  27.       {  
  28.          for(col = 0; col < i; col++)  
  29.            Console.Write("{0, 2} ", matA[row,col]);  
  30.          Console.WriteLine();  
  31.       }  
  32.   
  33.       bool isIdentity = true;  
  34.   
  35.       for(row = 0; row < i; row++)  
  36.       {  
  37.          for(col = 0; col < i; col++)  
  38.          {  
  39.             if ((row == col && matA[row, col] != 1) ||  
  40.                 (row != col && matA[row, col] != 0))  
  41.             {  
  42.                isIdentity = false;  
  43.                break;  
  44.             }     
  45.          }  
  46.       }  
  47.         
  48.       if(isIdentity)  
  49.       {   
  50.          Console.WriteLine("\nIdentity Matrix");  
  51.       }  
  52.       else  
  53.       {  
  54.          Console.WriteLine("\nNot an Identity Matrix");  
  55.       }  
  56.   
  57.       Console.ReadKey(true);  
  58.    }  
** Complete code

Description:

In the above code first of all we declare some variable row, col, i to read and write the matrix we take a 2-dimensional array of matA and a bool variable which is initialize to true.
First of all we enter the no. of row and column in Line 9,10,11 . After that we enter the element of matrix and print the matrix.

  1.     for(row = 0; row < i; row++)  
  2.       {  
  3.          for(col = 0; col < i; col++)  
  4.          {  
  5.             if ((row == col && matA[row, col] != 1) ||  
  6.                 (row != col && matA[row, col] != 0))  
  7.             {  
  8.                isIdentity = false;  
  9.                break;  
  10.             }     
  11.          }  
  12.       }  

In the above piece of code we design the logic of Identity matrix if i and j equal to each other i.e (1,1) (2,2) (3,3) then "1" is print or if i and j not equals to each other and if elements of matrix not equals to 0 than ths IsIdentity bool value changed to false.

Screenshot of output:

Output of Identity Matrix


Read Also:
Types of matrix in C#