Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ตรวจสอบเมทริกซ์ Hankel ใน C++

Hankel Matrix คืออะไร?

เมทริกซ์สี่เหลี่ยมจัตุรัสที่สมาชิกบน skew-diagonal (แนวทแยงมุมจากซ้ายล่างไปขวาบน) ทุกเส้นมีค่าเท่ากัน

เงื่อนไข: M[i][j] = M[i+1][j-1] สำหรับทุก i, j ที่ถูกต้อง

หรืออีกนัย: M[i][j] ขึ้นอยู่กับ i+j เท่านั้น (ค่าคงที่ตาม i+j)

ตัวอย่าง Hankel Matrix 5×5

12345
23456
34567
45678
56789

skew-diagonals: (1), (2,2), (3,3,3), (4,4,4,4), (5,5,5,5,5), (6,6,6,6), (7,7,7), (8,8), (9)

เงื่อนไขทางคณิตศาสตร์

ให้ a[k] = ค่าบน skew-diagonal ที่ i+j = k

M[i][j] = a[i+j] โดย:

  • ถ้า i+j < n: a[i+j] = M[i+j][0]
  • ถ้า i+j ≥ n: a[i+j] = M[i+j-n+1][n-1]

ตัวอย่างโค้ด C++

#include <iostream>
#include <vector>
using namespace std;

bool isHankelMatrix(const vector<vector<int>>& mat) {
    int n = mat.size();
    if (n == 0) return true;
    
    for (int i = 0; i < n; i++) {
        if (mat[i].size() != n) return false; // ไม่ใช่สี่เหลี่ยมจัตุรัส
        for (int j = 0; j < n; j++) {
            int expected;
            if (i + j < n)
                expected = mat[i + j][0];
            else
                expected = mat[i + j - n + 1][n - 1];
            
            if (mat[i][j] != expected)
                return false;
        }
    }
    return true;
}

int main() {
    vector<vector<int>> mat = {
        {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}
    };
    
    cout << (isHankelMatrix(mat) ? "This is a Hankel Matrix" : "This is NOT a Hankel Matrix") << endl;
    
    // ทดสอบเมทริกซ์ที่ไม่ใช่
    vector<vector<int>> mat2 = {
        {1, 2, 3},
        {2, 3, 5}, // ควรเป็น 4
        {3, 5, 6}
    };
    cout << "Matrix 2: " << (isHankelMatrix(mat2) ? "Hankel" : "Not Hankel") << endl;
    
    return 0;
}

ผลลัพธ์

This is a Hankel Matrix
Matrix 2: Not Hankel

ความสัมพันธ์กับ Toeplitz Matrix

ประเภทเงื่อนไขค่าคงที่ตาม
HankelM[i][j] = M[i+1][j-1]i+j (skew-diagonal)
ToeplitzM[i][j] = M[i+1][j+1]i-j (main diagonal)

การประยุกต์ใช้

  • Signal Processing: Hankel matrices ใช้ใน system identification
  • Control Theory: Hankel singular values
  • Numerical Analysis: Hankel operators