简体   繁体   English

我该如何解决旨在解决行列式的功能?

[英]How can I fix my function which is designed to solve determinants?

The function can accurately compute 1x1, 2x2, and 3x3 determinants, but it gives wrong values for higher order determinants. 该函数可以准确计算1x1、2x2和3x3行列式,但是对于高阶行列式给出错误的值。 I'm thinking there's a problem with the part in the function where I call the function (recursion) to solve lower order determinants as part of the solution. 我在想函数的一部分存在问题,我称之为函数(递归)来解决低阶行列式作为解决方案的一部分。

I suspect it has something to do with variable locality, but I'm not sure why that would be an issue because I thought locality was automatic. 我怀疑这与变量的局部性有关,但是我不确定为什么这会成为问题,因为我认为局部性是自动的。

This is Python 2 by the way. 顺便说一下,这是Python 2。

Code: 码:

def is_Det(matrix):
    isdet = True
    if not isinstance(matrix,list): # Is this actually a list?
        isdet = False
    for i in matrix:
        if isinstance(i,list): # Things in matrix are lists?
            if len(i) == len(matrix): # Square shape?
                for j in i: # All numbers?
                    try:
                        j = float(j)
                    except ValueError:
                        isdet = False
            else:
                isdet = False
        else:
            isdet = False
    return isdet

def det_Disp(matrix):
    if is_Det(matrix):
        pass
    else:
        print "Error: Input not a determinant, cannot display"

def det_Eval(matrix):
    print matrix
    except StandardError:
        print "No such value"
    if is_Det(matrix):
        if len(matrix) == 1: # 1x1 determinant
            print matrix[0][0]
            return matrix[0][0]
        elif len(matrix) == 2: # 2x2 determinant
            print matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
            return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
        else:
            longdet = matrix # Expands higher-order determinant
            for row1 in longdet:
                for col1 in range(0,len(matrix)-1):
                    row1.append(row1[col1])
            value = 0
            for col2 in range(0,len(matrix)): # Recurse determinant value
                subdet = []
                for row2 in range(1,len(matrix)):
                    subsubdet = []
                    for col3 in range(col2+1,col2+len(matrix)):
                        subsubdet.append(longdet[row2][col3])
                    '''print "Subsubdet:",
                    print subsubdet #***************************'''
                    subdet.append(subsubdet)
                    '''# print "Subdet",
                    print "Subdet:",
                    print subdet #***************************'''
                value += longdet[0][col2] * det_Eval(subdet)
                ##########print value
                '''#print longdet[1][col2],
                #print det_Eval(subdet),
                #print value
            # print longdet'''
            return value
    else:
        print "Error: Input not a determinant, cannot evaluate"

#print det_Eval([[2,3],[4,9]])
#print det_Eval([[6,1,8],[9,9,9],[9,7,4]])
print det_Eval([[8,9,2,3],[4,6,1,8],[9,9,9,9],[1,9,7,4]])

In this line dealing with minors: 在处理未成年人方面:

 value += longdet[0][col2] * det_Eval(subdet)

you don't take sign into account - sign depends on parity of index of the first line element (col2) 您无需考虑符号 - 符号取决于第一行元素 (col2) 的索引奇偶性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM