เราจะมาดูวิธีการตรวจสอบว่าเมทริกซ์สามารถกลับด้านได้หรือไม่ หากเมทริกซ์หนึ่งเป็น M ดังนั้นเมทริกซ์กลับด้าน M-1 จะเป็น -
$$M^-1=\frac{adj(M)}{|M\lvert}$$
ดังนั้นหากดีเทอร์มีแนนต์ของ M ไม่ใช่ศูนย์ เราก็จะได้อินเวอร์สเท่านั้น ไม่เช่นนั้น เราจะไม่ได้อินเวอร์สของมัน ดังนั้นที่นี่เราต้องตรวจสอบว่าดีเทอร์มีแนนต์ไม่เป็นศูนย์หรือไม่ การหาดีเทอร์มีแนนต์เป็นกระบวนการแบบเรียกซ้ำ เราต้องหาเมทริกซ์ย่อย แล้วหาดีเทอร์มีแนนต์ของมัน จากนั้นใช้ผลลัพธ์นั้นในการคำนวณขั้นสุดท้าย ให้เราดูโค้ดเพื่อความเข้าใจที่ดีขึ้น
ตัวอย่าง
#include <iostream>
#define N 4
using namespace std;
void findCoFactor(int mat[N][N], int mat2[N][N], int p, int q, int n) {
int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
mat2[i][j++] = mat[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
int getDeterminant(int mat[N][N], int n) {
int determinant = 0;
if (n == 1)
return mat[0][0];
int temp[N][N];
int sign = 1;
for (int f = 0; f < n; f++) {
findCoFactor(mat, temp, 0, f, n);
determinant += sign * mat[0][f] * getDeterminant(temp, n - 1);
sign = -sign;
}
return determinant;
}
bool isMatrixInvertible(int mat[N][N], int n) {
if (getDeterminant(mat, N) != 0)
return true;
else
return false;
}
int main() {
int matrix[N][N] = {
{ 1, 0, 2, -1 },
{ 3, 0, 0, 5 },
{ 2, 1, 4, -3 },
{ 1, 0, 5, 0 }
};
if (isMatrixInvertible(matrix, N))
cout << "The matrix is invetible";
else
cout << "The matrix is not invetible";
} ผลลัพธ์
The matrix is invetible