Suppose we have a square matrix, our task is to check whether the matrix is Hankel matrix or not. The Hankel matrix is a square matrix, in which each ascending skew-diagonal elements from left to right is constant. Suppose a matrix is like below −
| 1 | 2 | 3 | 4 | 5 |
| 2 | 3 | 4 | 5 | 6 |
| 3 | 4 | 5 | 6 | 7 |
| 4 | 5 | 6 | 7 | 8 |
| 5 | 6 | 7 | 8 | 9 |
To check whether the matrix is Hankel Matrix or not, we have to check whether mat[i, j] = ai+j or not. ai+j can be defined as −
$$a_{i+j}=\begin{cases}mat[i+j,0]< n\\mat[i+j-n+1,n-1]otherwise\end{cases}$$
Example
#include <iostream>
#define N 5
using namespace std;
bool isHankelMat(int mat[N][N], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j < n) {
if (mat[i][j] != mat[i + j][0])
return false;
} else {
if (mat[i][j] != mat[i + j - n + 1][n - 1])
return false;
}
}
}
return true;
}
int main() {
int n = 5;
int mat[N][N] = {
{ 1, 2, 3, 4, 5},
{ 2, 3, 4, 5, 6},
{ 3, 4, 5, 6, 7},
{ 4, 5, 6, 7, 8},
{ 5, 6, 7, 8, 9}
};
if(isHankelMat(mat, n))
cout << "This is Hankel Matrix";
else
cout << "This is not Hankel Matrix";
}Output
This is Hankel Matrix