ให้เมทริกซ์ M[r][c], 'r' หมายถึงจำนวนแถวและ 'c' หมายถึงจำนวนคอลัมน์ที่ r =c สร้างเมทริกซ์สี่เหลี่ยมจัตุรัส เราต้องตรวจสอบว่าเมทริกซ์จตุรัสที่กำหนดเป็น เมทริกซ์ที่ไม่เกี่ยวข้อง หรือเปล่า
เมทริกซ์ที่ไม่เป็นระเบียบ
เมทริกซ์เรียกว่า Involutory เมทริกซ์ก็ต่อเมื่อเมทริกซ์ถูกคูณด้วยตัวมันเอง และผลลัพธ์ของมันคือเมทริกซ์เอกลักษณ์ เมทริกซ์ I คือเมทริกซ์เอกลักษณ์ก็ต่อเมื่อเส้นทแยงมุมหลักของมันคือหนึ่งและองค์ประกอบอื่นที่ไม่ใช่เส้นทแยงหลักเป็นศูนย์ ดังนั้น เราสามารถพูดได้ว่าเมทริกซ์คือ เมทริกซ์ที่ไม่เกี่ยวข้อง ก็ต่อเมื่อ M*M=I โดยที่ ม เป็นเมทริกซ์บางตัว และ I คือเมทริกซ์เอกลักษณ์
เช่นเดียวกับในตัวอย่างด้านล่าง −

เมื่อเราคูณเมทริกซ์ด้วยตัวมันเอง ผลลัพธ์ก็คือเมทริกซ์เอกลักษณ์ ดังนั้นเมทริกซ์ที่กำหนดคือ Involutory Matrix
ตัวอย่าง
Input: { {1, 0, 0},
{0, -1, 0},
{0, 0, -1}}
Output: yes
Input: { {3, 0, 0},
{0, 2, 0},
{0, 0, 3} }
Output: no อัลกอริทึม
Start
Step 1 -> define macro as #define size 3
Step 2 -> declare function for matrix multiplication.
void multiply(int arr[][size], int res[][size])
Loop For int i = 0 and i < size and i++
Loop For int j = 0 and j < size and j++
Set res[i][j] = 0
Loop For int k = 0 and k < size and k++
Set res[i][j] += arr[i][k] * arr[k][j]
End
End
End
Step 3 -> declare function to check involutory matrix or not
bool check(int arr[size][size])
declare int res[size][size]
Call multiply(arr, res)
Loop For int i = 0 and i < size and i++
Loop For int j = 0 and j < size and j++
IF (i == j && res[i][j] != 1)
return false
End
If (i != j && res[i][j] != 0)
return false
End
End
End
Return true
Step 4 -> In main()
Declare int arr[size][size] = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } }
If (check(arr))
Print its an involutory matrix
Else
Print its not an involutory matrix
Stop ตัวอย่าง
#include <bits/stdc++.h>
#define size 3
using namespace std;
// matrix multiplication.
void multiply(int arr[][size], int res[][size]){
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
res[i][j] = 0;
for (int k = 0; k < size; k++)
res[i][j] += arr[i][k] * arr[k][j];
}
}
}
// check involutory matrix or not.
bool check(int arr[size][size]){
int res[size][size];
multiply(arr, res);
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
if (i == j && res[i][j] != 1)
return false;
if (i != j && res[i][j] != 0)
return false;
}
}
return true;
}
int main(){
int arr[size][size] = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } };
if (check(arr))
cout << "its an involutory matrix";
else
cout << "its not an involutory matrix";
return 0;
} ผลลัพธ์
its an involutory matrix