In this tutorial, we are going to explore a C++ code to verify whether a matrix is orthogonal or not, along with the corresponding output. However, it is essential to have a clear understanding of the concept of orthogonality before delving into the program.
An orthogonal matrix is a type of matrix where the transpose of the matrix is equal to its inverse. For a square matrix to be classified as orthogonal, it must meet the condition where the transpose of the matrix is equivalent to its inverse, denoted as A^T = A^-1. Here, A^T signifies the transpose of matrix A, while A^-1 represents the inverse of matrix A. Another definition of orthogonal matrices will be elaborated upon based on this premise.
A transpose equals the inverse matrix.
All orthogonal matrices are mandated to be invertible. The determinant is affected by transposition. For any orthogonal matrix, the determinant value is limited to either +1 or -1.
It is essential for all orthogonal matrices to have square dimensions, although not every square matrix must be orthogonal.
Steps to check whether the given matrix is orthogonal or not:
To determine if a matrix is orthogonal, several procedures need to be carried out. In this case, we will be utilizing a square matrix denoted as A. The sequential actions are outlined below:
Step 1: In this initial stage, we will calculate the determinant of the given matrix. When the determinant of matrix A equals 1, the matrix is considered orthogonal.
In step 2, we will determine the converted form of this matrix as well as its reciprocal.
Step 3: A matrix is considered orthogonal when the result of multiplying the transpose of matrix A by the inverse of matrix A equals an identity matrix, denoted as I. If this condition is not met, the matrix cannot be classified as orthogonal anymore.
Determinant of an Orthogonal matrix:
If the determinant of orthogonal matrices is calculated, it will always result in either +1 or -1. This property can be elucidated by considering an orthogonal matrix A. It can be inferred from the definition of orthogonal matrices that.
A*A T = I
Now, we are going to calculate the determinant on both sides of the equation mentioned above and derive the following outcomes:
det(I) = det(A*A T )
We are aware that finding the determinant of an identity matrix results in 1. Similarly, when A and B are matrices, the determinant of their product AB is equal to the product of their determinants, det(AB) = detA * detB. So,
- .
AA T = AA -1
As demonstrated previously, AA -1 = I, where I represents an identity matrix with an equivalent rank to matrix A.
As a result, AA -1 = I .
Similarly, we can show that A T A = I .
We can derive the subsequent expression from the preceding pair:
A T A = AA T = I.
Example:
Let's consider a C++ program to determine if 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:
Merging three iterations represents the most efficient approach. Instead of directly determining transposition, we opt for arrayj over arrayk. Furthermore, instead of explicitly computing the product, we assess identity during the product computation.
Example:
Let's consider a different C++ program to determine if a matrix is orthogonal.
//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 .