ในพีชคณิตเชิงเส้น เมทริกซ์ M[][] จะเป็น Symmetric Matrix ก็ต่อเมื่อ transpose ของเมทริกซ์เท่ากับเมทริกซ์เอง Transpose คือการพลิกเมทริกซ์ตามแนวทแยง ทำให้แถวและคอลัมน์สลับกัน
ตัวอย่าง Symmetric Matrix −
$$\begin{bmatrix} 1 & 4 & 7 \\ 4 & 5 & 6 \\ 7 & 6 & 9 \\ \end {bmatrix} \Rightarrow \begin{bmatrix} 1 & 4 & 7 \\ 4 & 5 & 6 \\ 7 & 6 & 9 \\ \end{bmatrix}$$
เมทริกซ์ด้านซ้าย transpose แล้วได้เมทริกซ์เดียวกัน จึงเป็น Symmetric Matrix
ตัวอย่าง
Input: arr1[][n] = { { 1, 2, 3 }, { 2, 2, 4 }, { 3, 4, 1 } }; Output: its a symmetric matrix Input: arr1[][n] = { { 1, 7, 3 }, { 2, 9, 5 }, { 4, 6, 8 } }; Output: its not a symmetric matrixวิธีแก้
ทำตามขั้นตอน −
- 1. หา transpose ของเมทริกซ์เก็บไว้อีกตัว
- 2. ตรวจสอบว่าเมทริกซ์ transpose เท่ากับเมทริกซ์ต้นฉบับหรือไม่
อัลกอริทึม
Start Step 1 -> define macro as #define n 10 Step 2 -> declare function to find transporse of a matrix void transpose(int arr1[][n], int arr2[][n], int a) Loop For int i = 0 and i < a and i++ Loop For int j = 0 and j < a and j++ Set arr2[i][j] = arr1[j][i] End End Step 3 -> declare function to check symmetric or not bool check(int arr1[][n], int a) declare variable as int arr2[a][n] Call transpose(arr1, arr2, a) Loop For int i = 0 and i < a and i++ Loop For int j = 0 and j < a and j++ IF (arr1[i][j] != arr2[i][j]) return false End End End Return true Step 4 -> In main() Declare variable as int arr1[][n] = { { 1, 2, 3 }, { 2, 2, 4 }, { 3, 4, 1 } } IF (check(arr1, 3)) Print its a symmetric matrix Else Print its not a symmetric matrix Stopตัวอย่าง
#include <iostream> #define n 10 using namespace std; //find transporse of a matrix void transpose(int arr1[][n], int arr2[][n], int a){ for (int i = 0; i < a; i++) for (int j = 0; j < a; j++) arr2[i][j] = arr1[j][i]; } //check symmetric or not bool check(int arr1[][n], int a){ int arr2[a][n]; transpose(arr1, arr2, a); for (int i = 0; i < a; i++) for (int j = 0; j < a; j++) if (arr1[i][j] != arr2[i][j]) return false; return true; } int main(){ int arr1[][n] = { { 1, 2, 3 }, { 2, 2, 4 }, { 3, 4, 1 } }; if (check(arr1, 3)) cout << "its a symmetric matrix"; else cout << "its not a symmetric matrix"; return 0; }ผลลัพธ์
its a symmetric matrix