📜  对角占优矩阵的Python程序

📅  最后修改于: 2022-05-13 01:55:09.776000             🧑  作者: Mango

对角占优矩阵的Python程序

在数学中,如果对于矩阵的每一行,一行中对角线项的大小大于或等于所有其他(非对角线)项的大小之和,则称方阵为对角线占优在那一排。更准确地说,矩阵A是对角占优的,如果

例如,矩阵

对角占优,因为
|一个11 | ≥ |a 12 | + |一个13 |因为|+3| ≥ |-2| + |+1|
|a 22 | ≥ |a 21 | + |a 23 |因为|-3| ≥ |+1| + |+2|
|a 33 | ≥ |a 31 | + |a 32 |因为|+4| ≥ |-1| + |+2|
给定一个nn列的矩阵A。任务是检查矩阵 A 是否对角占优。
例子 :

Input : A = { { 3, -2, 1 },
              { 1, -3, 2 },
              { -1, 2, 4 } };
Output : YES
Given matrix is diagonally dominant
because absolute value of every diagonal
element is more than sum of absolute values
of corresponding row.

Input : A = { { -2, 2, 1 },
              { 1, 3, 2 },
              { 1, -2, 0 } };
Output : NO

这个想法是针对行数从 i = 0 到 n-1 运行一个循环,对于每一行,运行一个循环 j = 0 到 n-1 找到非对角元素的总和,即 i != j。并检查对角元素是否大于或等于总和。如果对于任何一行,它是假的,则返回假或打印“否”。否则打印“是”。

Python3
# Python Program to check
# whether given matrix is
# Diagonally Dominant Matrix.
 
# check the given given
# matrix is Diagonally
# Dominant Matrix or not.
def isDDM(m, n) :
 
    # for each row
    for i in range(0, n) :        
     
        # for each column, finding
        # sum of each row.
        sum = 0
        for j in range(0, n) :
            sum = sum + abs(m[i][j])    
 
        # removing the
        # diagonal element.
        sum = sum - abs(m[i][i])
 
        # checking if diagonal
        # element is less than
        # sum of non-diagonal
        # element.
        if (abs(m[i][i]) < sum) :
            return False
 
    return True
 
# Driver Code
n = 3
m = [[ 3, -2, 1 ],
    [ 1, -3, 2 ],
    [ -1, 2, 4 ]]
 
if((isDDM(m, n))) :
    print ("YES")
else :
    print ("NO")
 
# This code is contributed by
# Manish Shaw(manishshaw1)


输出 :

YES

时间复杂度: O(N 2 )

辅助空间: O(1)

有关详细信息,请参阅有关对角支配矩阵的完整文章!