สมมติว่าเรามีเมทริกซ์สี่เหลี่ยมจัตุรัส งานของเราคือตรวจสอบว่าเมทริกซ์นั้นเป็นเมทริกซ์ของ Hankel หรือไม่ เมทริกซ์ Hankel เป็นเมทริกซ์สี่เหลี่ยมจัตุรัส ซึ่งแต่ละองค์ประกอบในแนวทแยงมุมจากซ้ายไปขวามีค่าคงที่ สมมติว่าเมทริกซ์อยู่ด้านล่าง -
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 |
ในการตรวจสอบว่าเมทริกซ์นั้นเป็น Hankel Matrix หรือไม่ เราต้องตรวจสอบว่า mat[i, j] =ai+j หรือไม่. ai+j สามารถกำหนดเป็น −
$$a_{i+j}=\begin{cases}mat[i+j,0]
ตัวอย่าง
#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"; }
ผลลัพธ์
This is Hankel Matrix