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

หาเมทริกซ์ผกผันด้วยวิธี Gauss-Jordan ใน C++


ในปัญหานี้ เราได้รับเมทริกซ์ 2 มิติ mat[][] ภารกิจของเราคือ หาเมทริกซ์ผกผันด้วยวิธี Gauss-Jordan

ตอนนี้ มาเข้าใจพื้นฐานของปัญหา

เมทริกซ์ (MATRIX) คืออาร์เรย์สองมิติของตัวเลข

ตัวอย่าง

$\begin{bmatrix}2&5&4 \\1&6&7 \\9&3&8\end{bmatrix}$

เมทริกซ์ผกผัน [A-1] -

เป็นการดำเนินการบนเมทริกซ์สี่เหลี่ยมจัตุรัส คุณสมบัติต่อไปนี้จำเป็นสำหรับเมทริกซ์ที่จะมีผกผัน -

  • เมทริกซ์เริ่มต้นควรเป็นเมทริกซ์สี่เหลี่ยมจัตุรัส

  • ต้องเป็นเมทริกซ์ non-singular

  • มีเมทริกซ์เอกลักษณ์ I อยู่สำหรับเมทริกซ์ A โดยที่

$$AA^{-1} = A^{-1}.A = I$$

มีสูตรที่สามารถใช้หาผกผันของเมทริกซ์ที่กำหนดได้ คือ

$A^{-1}\:=\:\left(\frac{adj(A)}{\det(A)}\right)$

adj(A) คือ adjoint ของเมทริกซ์ A

det(A) คือ determinant ของเมทริกซ์ A

มีหลายวิธีที่เราสามารถใช้หาผกผันของเมทริกซ์ได้ ในบทความนี้ เราจะเรียนรู้เกี่ยวกับ วิธี Gauss-Jordan ซึ่ง cònเรียกว่า Elementary Row Operation

เป็นวิธีขั้นตอนต่อขั้นตอนในการหาผกผันของเมทริกซ์ ขั้นตอนที่เกี่ยวข้องมีดังนี้ -

  • หา augmented matrix โดยใช้ identity matrix

  • หา echelon form ของเมทริกซ์โดยดำเนินการ row reduction บน augmented matrix ที่หาได้ในขั้นตอน 1

  • การดำเนินการบางอย่างที่สามารถทำได้บน augmented matrix ในกระบวนการคือ

    • Row interchange (คุณสามารถสลับแถวใดๆ สองแถวได้)

    • Multiplication (แต่ละองค์ประกอบของแถวสามารถคูณด้วยค่าคงที่ที่ไม่ใช่ 0)

    • Row Interchange (แทนที่แถวด้วยผลรวมของแถวและค่าคงที่คูณอีกแถวหนึ่งของเมทริกซ์)

ตัวอย่าง

โปรแกรมสาธิตการทำงานของวิธีแก้ปัญหาของเรา

#include <iostream> #include <vector> using namespace std; void printMatrixValues(float** arr, int n, int m){ for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout<<arr[i][j]<<"\t"; } cout<<endl; } return; } void printInverseMatrix(float** arr, int n, int m){ for (int i = 0; i < n; i++) { for (int j = n; j < m; j++) { printf("%.3f\t", arr[i][j]); } cout<<endl; } return; } void findInvMatGaussJordan(float** mat, int order){ float temp; printf("The inverse of matrix : A = \n"); printMatrixValues(mat, order, order); for (int i = 0; i < order; i++) { for (int j = 0; j < 2 * order; j++) { if (j == (i + order)) mat[i][j] = 1; } } for (int i = order - 1; i > 0; i--) { if (mat[i - 1][0] < mat[i][0]) { float* temp = mat[i]; mat[i] = mat[i - 1]; mat[i - 1] = temp; } } for (int i = 0; i < order; i++) { for (int j = 0; j < order; j++) { if (j != i) { temp = mat[j][i] / mat[i][i]; for (int k = 0; k < 2 * order; k++) { mat[j][k] -= mat[i][k] * temp; } } } } for (int i = 0; i < order; i++) { temp = mat[i][i]; for (int j = 0; j < 2 * order; j++) { mat[i][j] = mat[i][j] / temp; } } cout<<"A' =\n"; printInverseMatrix(mat, order, 2 * order); return; } int main(){ int order = 3; float** mat = new float*[20]; for (int i = 0; i < 20; i++) mat[i] = new float[20]; mat[0][0] = 6; mat[0][1] = 9; mat[0][2] = 5; mat[1][0] = 8; mat[1][1] = 3; mat[1][2] = 2; mat[2][0] = 1; mat[2][1] = 4; mat[2][2] = 7; findInvMatGaussJordan(mat, order); return 0; }

เอาต์พุต

The inverse of matrix : A = 6 9 5 8 3 2 1 4 7 A' = -0.049 0.163 -0.011 0.205 -0.141 -0.106 -0.110 0.057 0.205