Wednesday 1 April 2015

Symmetric Matrix

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

Symmetric matrix: 
A square matrix A = [aij] is said to be symmetric if A' = A, that is [aij]= [aji] for all possible value of i and j.  For Example 

Symmetric matrix Example
Code Snippet:

  1. using System;  
  2. public class SkewSymmetric  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int i, j, ord;  
  7.         int[,] matA = new int[10,10];  
  8.         int[,] matB = new int[10,10];  
  9.         int[,] matC = new int[10,10];  
  10.         bool isSymmetry = true;    
  11.   
  12.         Console.Write("Enter the order of square matrix. ");  
  13.         ord = Convert.ToInt32(Console.ReadLine());  
  14.   
  15.   
  16.         Console.WriteLine("Enter the element of the matrix");  
  17.         for(i = 0; i<ord; i++)  
  18.         {  
  19.             for(j = 0; j<ord; j++)  
  20.             {  
  21.                 Console.Write("Matrix A[{0},{1}] : ", i, j);  
  22.                 matA[i,j] = Convert.ToInt32(Console.ReadLine());  
  23.             }  
  24.             Console.WriteLine();  
  25.         }  
  26.         Console.ReadLine();  
  27.   
  28.         Console.WriteLine("Matrix A: ");  
  29.   
  30.         for(i = 0; i<ord; i++)  
  31.         {  
  32.             for(j = 0; j<ord; j++)  
  33.             {  
  34.                 Console.Write("{0} ", matA[i,j]);  
  35.             }  
  36.             Console.WriteLine();  
  37.         }  
  38.         Console.ReadLine();  
  39.   
  40.         for(i = 0; i<ord; i++)  
  41.         {  
  42.             for(j = 0; j<ord; j++)  
  43.             {  
  44.                 matC[i,j] = matA[i,j];  
  45.             }  
  46.             Console.WriteLine();  
  47.         }  
  48.   
  49.         Console.WriteLine("Matrix A' ");  
  50.         for(i = 0; i<ord; i++)  
  51.         {  
  52.             for(j = 0; j<ord; j++)  
  53.             {  
  54.                 Console.Write("{0} ", matA[j,i]);  
  55.             }  
  56.             Console.WriteLine();  
  57.         }  
  58.         Console.ReadLine();  
  59.           
  60.   
  61.         for(i = 0; i<ord; i++)  
  62.         {  
  63.             for(j = 0; j<ord; j++)  
  64.             {  
  65.                 matB[i,j] = matA[j,i];  
  66.             }  
  67.             Console.WriteLine();  
  68.         }  
  69.   
  70.         for(i = 0; i<ord; i++)  
  71.         {  
  72.             for(j = 0; j<ord; j++)  
  73.             {  
  74.                 if(matB[i,j] != matC[i,j] )  
  75.                 {  
  76.                     isSymmetry = false;  
  77.                     break;  
  78.                 }  
  79.             }  
  80.             Console.WriteLine();  
  81.         }  
  82.   
  83.         if(isSymmetry == false)  
  84.         {  
  85.             Console.WriteLine("Not Symmetric");  
  86.         }  
  87.         else  
  88.         {  
  89.             Console.WriteLine("The matrix is Symmetric");  
  90.         }  
  91.   
  92.         Console.ReadLine();  
  93.           
  94.     }  
  95. } 
** Complete code

Description:
In the above code first of all we ask user to input the order of matrix after that we enter the element of the matrix and we print the matrix on the console. there are three 2-dimensional array in the above code matA, matB, matC. MatA is the first 2-dimensional array by which we know whether the matrix is symmetric or not. To do this firstly we eneter all the element of matA to matC after that it is going to the transpose operation after the transpose operation done the value of MatA' is store in the matB.

  1. for(i = 0; i<ord; i++)  
  2.         {  
  3.             for(j = 0; j<ord; j++)  
  4.             {  
  5.                 if(matB[i,j] != matC[i,j] )  
  6.                 {  
  7.                     isSymmetry = false;  
  8.                     break;  
  9.                 }  
  10.             }  
  11.             Console.WriteLine();  
  12.         }  
  13.   
  14.         if(isSymmetry == false)  
  15.         {  
  16.             Console.WriteLine("Not Symmetric");  
  17.         }  
  18.         else  
  19.         {  
  20.             Console.WriteLine("The matrix is Symmetric");  
  21.         }  
  22.   

The above code is the Condition of symmetric matrix here we check matB[i,j] is equals to the matC[i,j], if each element of matB is equal to the matC than the matrix is symmetric other wise not.

Logic:

Step 1: matA[,];

Step 2: matC[,] = matA[,];

Step 3: matA'[,];

Step 4: matB[,] = matA'[,];

Step 5: matB[,] == matC[,];

Step 6: if yes 
                 matrix is symmetric;
Step 7: else 
                  not symmetric matrix  

Screenshot of Output:

Symmetric matrix in C#

Read Also: