如何在Python中檢查冪等矩陣?
如果一個矩陣與自身相乘後得到相同的矩陣,則稱其為冪等矩陣。當且僅當M * M = M時,矩陣M被認為是冪等矩陣。其中M是冪等矩陣中的方陣。
矩陣M
1 0 0 0 1 0 0 0 0
執行M*M
1 0 0 * 1 0 0 = 1 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0
由於結果是M,所以它是冪等矩陣
假設我們在Python中已經得到了一個M*M的輸入矩陣。在這篇文章中,我們將學習如何使用巢狀迴圈方法檢查輸入矩陣是否為冪等矩陣。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
使用import關鍵字匯入math模組。(此處math模組似乎多餘,原文可能錯誤)
定義函式multiplyMatrix(inputMatrix, output)來執行矩陣乘法。它接受輸入矩陣和一個空輸出矩陣作為引數。
變數rows賦值為輸入矩陣的長度,表示行數。
兩個巢狀的for迴圈迭代輸入矩陣的行和列。
輸出矩陣中[p][q]位置的元素初始化為0。
另一個巢狀的for迴圈迭代行範圍,透過對輸入矩陣中對應元素的乘積求和來執行矩陣乘法。
定義函式isIdempotentMat(inputMatrix)來檢查輸入矩陣是否為冪等矩陣。它接受輸入矩陣作為引數。
建立一個rows x rows維的零矩陣output。
呼叫multiplyMatrix()函式,傳入輸入矩陣和輸出矩陣作為引數,執行矩陣乘法並將結果儲存在輸出矩陣中。
兩個巢狀的for迴圈迭代輸入矩陣的行和列。
將輸入矩陣的當前元素與其在輸出矩陣中對應的元素進行比較。如果不相等,則返回False,表示矩陣不是冪等矩陣。
如果所有元素都相等,則返回True,表示矩陣是冪等矩陣。
定義輸入矩陣inputMatrix。
呼叫isIdempotentMat()函式,傳入inputMatrix作為引數,以檢查它是否為冪等矩陣。
如果函式返回True,則列印“輸入矩陣是冪等矩陣”。否則,列印“輸入矩陣不是冪等矩陣”。
示例
下面的例子包含兩個函式,用於矩陣乘法和檢查冪等矩陣屬性。multiplyMatrix()函式迭代輸入矩陣的每個元素,將輸出矩陣中對應的元素初始化為零,並透過對輸入矩陣中對應元素的乘積求和來執行矩陣乘法。isIdempotentMat()函式透過呼叫multiplyMatrix()建立輸出矩陣,比較輸入矩陣的每個元素與輸出矩陣中對應的元素,如果任何元素不同則返回False。如果所有元素都匹配,則返回True。透過使用這些函式,程式碼允許使用者執行矩陣乘法並確定給定矩陣是否滿足冪等矩陣的條件。
# importing math module with an alias name
import math
# creating a function to perform matrix multiplication
def multiplyMatrix(inputMatrix, output):
# getting len of matrix i.e, rows
rows = len(inputMatrix)
# traversing through each row of a matrix
for p in range(0, rows):
# traversing through all the columns of a current row
for q in range(0, rows):
# assuming the current element as 0
output[p][q] = 0
# performing matrix multiplication
for x in range(0, rows):
output[p][q] += inputMatrix[p][x] * inputMatrix[x][q]
# creating a function to check whether the input matrix
# is Idempotent Matrix by accepting the input matrix as an argument
def isIdempotentMat(inputMatrix):
# getting len of matrix i.e, rows
rows = len(inputMatrix)
# Creating a zero matrix of given length
output = [[0] * rows for p in range(0, rows)]
# calling the above multiplyMatrix() function
multiplyMatrix(inputMatrix, output)
# traversing through each row of a matrix
for p in range(0, rows):
# traversing through all the columns of a current row
for q in range(0, rows):
# Checking whether the current element is not equal to ouput matrix element
if inputMatrix[p][q] != output[p][q]:
# retutning false if the condition is satisfied so it is not idempotent
return False
# else returning true
return True
# input matrix
inputMatrix = [[2, -2, -4], [-1, 3, 4], [1, -2, -3]]
# calling the above defined isIdempotentMat() function by passing
# input matrix to it and checking whether the function returns true
if isIdempotentMat(inputMatrix):
# printing Idempotent Matrix if the function returns true
print("The input matrix is an Idempotent Matrix")
else:
# else printing Not Idempotent Matrix
print("The input matrix is NOT an Idempotent Matrix")
輸出
The input matrix is an Idempotent Matrix
結論
總而言之,本文指導瞭如何使用Python檢查冪等矩陣。分步說明和程式碼示例提供了系統的方法來處理矩陣操作和驗證Python中的冪等矩陣屬性。本文對於那些有興趣實現矩陣運算和探索Python中矩陣屬性的人來說是一個寶貴的資源。透過應用本文中獲得的知識,開發人員可以自信地處理矩陣,進行矩陣運算,並評估給定矩陣是否滿足冪等矩陣的條件,從而加強他們對Python中線性代數概念的理解。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP