Thursday, 2 April 2015

Skew Symmetric Matrix in C#

Introduction:

A square matrix  A = [aij] is said to be skew symmetric matrix if A' = -A , that is aji = -aij for all possible values of i an dj. Now, if we put i=j, we have
aii = -aii. Therefore 2aii = 0 or aii = 0 for all i's. This means that all the diagonal element of a skew symmetric matrix are zero.

For example:
Example of Skew Symmetric matrix
Fig:  Skew Symmetric matrix
Code snippet:

  1. using System;  
  2. public class SkewSymmetry  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int[,] matA = new int[10,10];  
  7.         int[,] matB = new int[10,10];  
  8.         int[,] matC = new int[10,10];  
  9.   
  10.         bool isSkew = true;  
  11.   
  12.         int i, j, ord;  
  13.   
  14.         Console.Write("Enter the order of Matrix A: ");  
  15.         ord = Convert.ToInt32(Console.ReadLine());  
  16.   
  17.         Console.WriteLine("\nEnter the Element of Matrix: ");  
  18.         for(i = 0; i<ord; i++)  
  19.         {  
  20.             Console.WriteLine();  
  21.             for(j = 0; j<ord; j++)  
  22.             {  
  23.                 Console.Write("Matrix[{0},{1}] : ", i, j);  
  24.                 matA[i,j]=Convert.ToInt32(Console.ReadLine());  
  25.             }  
  26.         }  
  27.   
  28.         Console.ReadLine();  
  29.           
  30.         Console.WriteLine("Matrix A: ");  
  31.         for(i = 0; i<ord; i++)  
  32.         {  
  33.             Console.WriteLine();  
  34.             for(j = 0; j<ord; j++)  
  35.             {  
  36.                 Console.Write(" {0}", matA[i,j]);  
  37.             }  
  38.         }  
  39.   
  40.         for(i = 0; i<ord; i++)  
  41.         {  
  42.             Console.WriteLine();  
  43.             for(j = 0; j<ord; j++)  
  44.             {  
  45.                 matB[i,j] = matA[i,j] * (-1);  
  46.             }  
  47.         }  
  48.   
  49.         Console.WriteLine("Matrix A' : ");  
  50.         for(i = 0; i<ord; i++)  
  51.         {  
  52.             Console.WriteLine();  
  53.             for(j = 0; j<ord; j++)  
  54.             {  
  55.                 Console.Write(" {0}", matA[j,i]);  
  56.             }  
  57.         }  
  58.   
  59.         Console.ReadLine();  
  60.   
  61.         for(i = 0; i<ord; i++)  
  62.         {  
  63.             Console.WriteLine();  
  64.             for(j = 0; j<ord; j++)  
  65.             {  
  66.                 matC[i,j] = matA[j,i];  
  67.             }  
  68.         }  
  69.   
  70.         for(i = 0; i<ord; i++)  
  71.         {  
  72.             Console.WriteLine();  
  73.             for(j = 0; j<ord; j++)  
  74.             {  
  75.                 if(matC[i,j] != matB[i,j])  
  76.                 {  
  77.                     isSkew = false;  
  78.                     break;  
  79.                 }  
  80.             }  
  81.         }  
  82.   
  83.         if(isSkew == false)  
  84.         {  
  85.             Console.WriteLine("Not Skew Symmetric");  
  86.         }  
  87.         else  
  88.         {  
  89.             Console.WriteLine("The matrix is Skew Symmetric");  
  90.         }  
  91.   
  92.         Console.ReadLine();  
  93.     }  
** 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 main 2-dimensional array on which we apply the transpose operation. To do this firstly we copy all the element of matA to matC now multiply the matC[] to -1 after matA[] is going to the transpose operation.

  1. Console.WriteLine("Matrix A' : ");  
  2.         for(i = 0; i<ord; i++)  
  3.         {  
  4.             Console.WriteLine();  
  5.             for(j = 0; j<ord; j++)  
  6.             {  
  7.                 Console.Write(" {0}", matA[j,i]);  
  8.             }  
  9.         }  
  10.   
  11.         Console.ReadLine();  
  12.   
  13.         for(i = 0; i<ord; i++)  
  14.         {  
  15.             Console.WriteLine();  
  16.             for(j = 0; j<ord; j++)  
  17.             {  
  18.                 matC[i,j] = matA[j,i];  
  19.             }  
  20.         }  
  21.   
  22.         for(i = 0; i<ord; i++)  
  23.         {  
  24.             Console.WriteLine();  
  25.             for(j = 0; j<ord; j++)  
  26.             {  
  27.                 if(matC[i,j] != matB[i,j])  
  28.                 {  
  29.                     isSkew = false;  
  30.                     break;  
  31.                 }  
  32.             }  
  33.         }  
  34.   
  35.         if(isSkew == false)  
  36.         {  
  37.             Console.WriteLine("Not Skew Symmetric");  
  38.         }  
  39.         else  
  40.         {  
  41.             Console.WriteLine("The matrix is Skew Symmetric");  
  42.         }  
  43.   

In this code:

the transpose of matrix is copied in matC[] aafter that we compare matB[] and matC[] and according result answer print in console.

Logic:

Step 1: matA[i,j];

Step 2: matB[i,j] = matA[i,j];

Step 3: matB[i,j] = matB[i,j] * (-1); 

Step 3: matA'[i,j];

Step 4: matC[i,j] = matA'[i,j];

Step 5: compare (matB[i,j] == matC[i,j]);

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

Screenshots: 

Output of Skew symmetric matrix
Screenshot of Output


Read Also: