ดีเทอร์มีแนนต์ของเมทริกซ์สามารถใช้เพื่อค้นหาว่าอินเวอร์ทิเบิลหรือไม่ เมทริกซ์จะกลับด้านได้หากดีเทอร์มีแนนต์ไม่ใช่ศูนย์ ดังนั้นหากดีเทอร์มีแนนต์ออกมาเป็นศูนย์ เมทริกซ์จะไม่สามารถกลับด้านได้ ตัวอย่างเช่น −
The given matrix is: 4 2 1 2 1 1 9 3 2 The determinant of the above matrix is: 3 So the matrix is invertible.
โปรแกรมที่ตรวจสอบว่าเมทริกซ์สามารถกลับด้านได้หรือไม่มีดังต่อไปนี้
ตัวอย่าง
#include<iostream>
#include<math.h>
using namespace std;
int determinant( int matrix[10][10], int n) {
int det = 0;
int submatrix[10][10];
if (n == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
else {
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 ));
}
}
return det;
}
int main() {
int n, d, i, j;
int matrix[10][10];
cout << "Enter the size of the matrix:\n";
cin >> n;
cout << "Enter the elements of the matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> matrix[i][j];
cout<<"The entered matrix is:"<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << matrix[i][j] <<" ";
cout<<endl;
}
d = determinant(matrix, n);
cout<<"Determinant of the matrix is "<< d <<endl;
if( d == 0 )
cout<<"This matrix is not invertible as the determinant is zero";
else
cout<<"This matrix is invertible as the determinant is not zero";
return 0;
} ผลลัพธ์
Enter the size of the matrix: 3 Enter the elements of the matrix: 1 2 3 2 1 2 1 1 4 The entered matrix is: 1 2 3 2 1 2 1 1 4 Determinant of the matrix is -7 This matrix is invertible as the determinant is not zero
ในโปรแกรมข้างต้น ขนาดและองค์ประกอบของเมทริกซ์มีอยู่ในฟังก์ชัน main() จากนั้นฟังก์ชันดีเทอร์มิแนนต์ () จะถูกเรียก ส่งคืนดีเทอร์มีแนนต์ของเมทริกซ์ซึ่งถูกเก็บไว้ใน d หากดีเทอร์มีแนนต์เป็น 0 เมทริกซ์จะไม่สามารถกลับด้านได้ และหากดีเทอร์มีแนนต์ไม่ใช่ 0 เมทริกซ์จะกลับด้านได้ ซึ่งแสดงให้เห็นด้วยข้อมูลโค้ดต่อไปนี้
cout << "Enter the size of the matrix:\n";
cin >> n;
cout << "Enter the elements of the matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> matrix[i][j];
cout<<"The entered matrix is:"<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << matrix[i][j] <<" ";
cout<<endl;
}
d = determinant(matrix, n);
cout<<"Determinant of the matrix is "<< d <<endl;
if( d == 0 )
cout<<"This matrix is not invertible as the determinant is zero";
else
cout<<"This matrix is invertible as the determinant is not zero"; ในฟังก์ชันดีเทอร์มิแนนต์ () ถ้าขนาดของเมทริกซ์เท่ากับ 2 ดีเทอร์มีแนนต์จะถูกคำนวณโดยตรงและส่งกลับค่า ดังแสดงไว้ดังนี้
if (n == 2) return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
ถ้าขนาดของเมทริกซ์ไม่ใช่ 2 ดีเทอร์มีแนนต์จะถูกคำนวณซ้ำ มี 3 ซ้อนสำหรับลูปที่ใช้กับตัวแปรลูป x, i และ j ลูปเหล่านี้ใช้ในการคำนวณดีเทอร์มิแนนต์และฟังก์ชันดีเทอร์มิแนนต์ () ถูกเรียกซ้ำเพื่อคำนวณดีเทอร์มีแนนต์ภายในแล้วคูณด้วยค่าภายนอก สิ่งนี้แสดงให้เห็นโดยข้อมูลโค้ดต่อไปนี้
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 ))
}