In this article, we will discuss a C++ program to check if a matrix is orthogonal or Not with its output. But before going to the program, we must know about the orthogonal .
The orthogonal matrix is one in which the transpose of the original matrix and its inverse matrix are identical. If we have a square matrix, we may consider it an orthogonal matrix if its transpose and inversion are comparable to each other, i.e., A T = A -1 . In this case, A T represents the transpose of matrix A, and A -1 represents the inverse of matrix A. On the basis of this definition, there is one different definition of an orthogonal matrices, which will be explained as follows:
A T = A -1
- All orthogonal matrices must be invertible. The transposition has held back the determinant. In the case of any orthogonal matrix, the value of the determinant can only be +1 or -1.
- It is required that all orthogonal matrices be square, but it is not required that every square matrix be orthogonal .
Steps to check whether the given matrix is orthogonal or not:
Various steps must be taken to find out whether the matrix is orthogonal or not. We'll use a square matrix A for this. The steps involved are as follows:
Step 1: In this step, we are going to find the determinant of the provided matrix. If the determinate of matrix A is 1, the matrix is orthogonal .
Step 2: In this step, we will find the transformed version of this matrix and its inverse.
Step 3: The matrix is said to be orthogonal if the product of the transpose of matrix A and the inverse of matrices A is an identity matrix, i.e., A T *A -1 = I . It won't be an orthogonal matrix anymore. I denote the identity matrix.
Determinant of an Orthogonal matrix:
If we compute the determinant of orthogonal matrices, it remains either +1 or -1 . Now, we will explain this. We'll use an orthogonal matrix A for this. We are able to determine from its definition that.
A*A T = I
Now, we will compute the determinant on the two sides of the above equation and obtain the following results:
det(I) = det(A*A T )
We understood that if we calculate the determinant of a matrix of identity, we get 1 . As well, if A and B are matrices, det(AB) = detA*detB . So,
AA T = AA -1
As we have seen, AA -1 = I , where I is an identity matrix, which has the same rank as matrix A.
As a result, AA -1 = I .
Similarly, we can show that A T A = I .
We can get the following expression from the previous two:
A T A = AA T = I.
Example:
Let us take a C++ program to check for a matrix is Orthogonal or not.
Approach 1:
//Program to check whether the given matrix is orthogonal or not
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
bool isOrthogonalMatrix(int array[][MAX],
int r, int c)
{
if (r != c)
return false;
// Finding the product and transposing matrix
int pro[c][c];
for (int i = 0; i < c; i++)
{
for (int j = 0; j < c; j++)
{
int total= 0;
for (int k = 0; k < c; k++)
{
// Because we are multiplying by
// transposition of itself. We use
total= total + (array[i][k] * array[j][k]);
}
pro[i][j] = total;
}
}
// checking whether the pro matrix is the identity matrix
for (int i = 0; i < c; i++)
{
for (int j = 0; j < c; j++)
{
if (i != j && pro[i][j] != 0)
return false;
if (i == j && pro[i][j] != 1)
return false;
}
}
return true;
}
// Main
int main()
{
int array[][MAX] = {{0, 0, 1},
{0, 1, 0},
{1, 0, 0}};
if (isOrthogonalMatrix(array, 3, 3))
cout << "The given matrix is an Orthogonal Matrix";
else
cout << "The given matrix is not an Orthogonal Matrix";
return 0;
}
Output:
The given matrix is an Orthogonal Matrix
Approach 2:
Combining three traversals is an optimal solution. Instead of finding transposition explicitly, we make use of arrayj instead of arrayk. Also, rather than calculating the product specifically, we check identity while computing the product.
Example:
Let us take another C++ program to check for a matrix is Orthogonal or not.
//Program to check whether the given matrix is orthogonal or not
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
bool isOrthogonalMatrix(int array[][MAX],
int r, int c)
{
if (r != c)
return false;
// matrix multiplication
for (int i = 0; i < c; i++)
{
for (int j = 0; j < c; j++)
{
int total = 0;
for (int k = 0; k < c; k++)
{
//Because we are multiplying by // transposition of itself. Instead of array[k][j], we're going to use array[j][k].
total = total + (array[i][k] * array[j][k]);
}
if (i == j && total != 1)
return false;
if (i != j && total != 0)
return false;
}
}
return true;
}
// Driver Code
int main()
{
int array[][MAX] = {{0, 0, 1},
{0, 1, 0},
{1, 0, 0}};
if (isOrthogonalMatrix(array, 3, 3))
cout << "The given matrix is an Orthogonal Matrix";
else
cout << "The given matrix is not an Orthogonal Matrix";
return 0;
}
Output:
The given matrix is an Orthogonal Matrix
Orthogonal matrix applications:
The orthogonal matrix has several applications or uses, some of which are listed below:
- We can use an orthogonal matrix to perform multi-channel processing of signals.
- We can use an orthogonal matrix to perform a time series with multiple variables analysis.
- This matrix can be used in a variety of linear algebra methods .
- This matrix can be used in QR decomposition .