You are here

C program to check orthogonal matrix

C program to check if a matrix is orthogonal or not. For an orthogonal matrix AAT = I. Example of an orthogonal matrix:

1 0
0 1

C programming code

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int m, n, p, c, d, k, sum = 0;
  6.   int matrix[10][10], transpose[10][10], product[10][10];
  7.  
  8.   printf("Enter the number of rows and columns of matrix\n");
  9.   scanf("%d%d", &m, &n);
  10.   printf("Enter the elements of matrix\n");
  11.  
  12.   for (c = 0; c < m; c++)
  13.     for (d = 0; d < n; d++)
  14.       scanf("%d", &matrix[c][d]);
  15.  
  16.   for (c = 0; c < m; c++)
  17.     for (d = 0; d < n; d++)
  18.       transpose[d][c] = matrix[c][d];
  19.  
  20.   for (c = 0; c < m; c++)
  21.   {
  22.     for (d = 0; d < n; d++)
  23.     {
  24.       for (k = 0; k < n; k++)
  25.       {
  26.         sum = sum + matrix[c][k]*transpose[k][d];
  27.       }
  28.  
  29.       product[c][d] = sum;
  30.       sum = 0;
  31.     }
  32.   }
  33.  
  34.   for (c = 0; c < m; c++)
  35.   {
  36.     for (d = 0; d < m; d++)
  37.     {
  38.       if (c == d)
  39.       {
  40.         if (product[c][d] != 1)
  41.           break;
  42.       }
  43.       else
  44.       {
  45.         if (product[c][d] != 0)
  46.           break;
  47.       }
  48.     }
  49.     if (d != m)
  50.       break;
  51.   }
  52.   if (c != m)
  53.     printf("The matrix isn't orthogonal.\n");
  54.   else
  55.     printf("The matrix is orthogonal.\n");
  56.  
  57.   return 0;
  58. }