Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不是';我找到行列式的Python代码是否有效?_Python_Matrix_Determinants - Fatal编程技术网

为什么不是';我找到行列式的Python代码是否有效?

为什么不是';我找到行列式的Python代码是否有效?,python,matrix,determinants,Python,Matrix,Determinants,Python3中的以下代码旨在返回任意阶矩阵的行列式。它采用以下格式的文本文件: 3689 9-75-7 2080 8 9-1-1 我没有错,但它给出了错误的答案。有什么线索吗?谢谢 def determinant(inputFileName): def cofactorExp(listOfRows): if len(listOfRows) <= 2: return (float(listOfRows[0][0]) * float

Python3中的以下代码旨在返回任意阶矩阵的行列式。它采用以下格式的文本文件:

3689
9-75-7
2080
8 9-1-1

我没有错,但它给出了错误的答案。有什么线索吗?谢谢

def determinant(inputFileName):  
    def cofactorExp(listOfRows):  
        if len(listOfRows) <= 2:  
            return (float(listOfRows[0][0]) * float(listOfRows[1][1])) - (float(listOfRows[0][1]) * float(listOfRows[1][0]))  
        else:  
            for i in range(len(listOfRows)):  
                tempList = listOfRows[:]  
                del tempList[i]  
                for x in range(len(tempList)):  
                    tempList[x] = tempList[x][1:]  
                det = ((-1) ** i) * float(listOfRows[i][0]) * cofactorExp(tempList)  
                return det  
    rows = []  
    for line in open(inputFileName):  
    rows append(line split(" "))  
    for item in rows:  
        if "\n" in item[len(item) - 1]:  
            item[len(item) - 1] = item[len(item) - 1][:-1]  
    return(cofactorExp(rows))  
def行列式(inputFileName):
def辅因子REXP(列表行):
如果len(listOfRows)有几件事

“打开文件”命令无法正确处理数据前后的空格

rows = []
for line in open(inputFileName):  
   rows.append(line.split(" "))  
for item in rows:  
   if "\n" in item[len(item) - 1]:  
        item[len(item) - 1] = item[len(item) - 1][:-1]
当我在生成的矩阵上运行代码时,命令返回

[['3', '6', '8', '9'], ['9', '-7', '5', '-7'], ['2', '0', '8', '0'], ['8', '9', '-1', '-1', '']]
请注意,矩阵中有一个空元素。在对象中调用命令时,不要忘记添加句点

我建议使用csv模块的示例

似乎过早退出函数,因为det在循环的范围内

最后,有一个更简单的方法来求解和编码行列式

   (aei+bfg+cdh)-(ceg+bdi+afh) 

您可以使用numpy完成此任务,除非这是一项家庭作业:手工计算,看看函数的计算结果有何差异。这是一项家庭作业,但我们可以编写符合我们要求的代码。但是我不能用它。你应该提供一些关于这段代码的信息,以及它如何帮助回答这个问题!
   (aei+bfg+cdh)-(ceg+bdi+afh) 
from numpy import *
x=input("give order of square matrix")
a=[]
for i in range(x):
    a.append([])
    for j in range(x):
        y=raw_input("input a["+str(i)+"]["+ str(j)+ "] element")
        a[i].append(y)
b=array(a)


print b

def rem(cc):
    s=cc.shape
    y=[]
    for i in range(s[0]):
        y.append([])
        for j in range(s[0]):
            if i==0:
                continue
            elif j==x:
                continue
            else:
                y[i].append(cc[i][j])
    y.pop(0)          
    return array(y)

def det(bb):
    n=0
    s=bb.shape  
    if s==(1,1):
        return bb[0][0]
    else:
        for j in range(s[0]):
            x=j
            global x  
            p=int(bb[0][j])
            pp=int(det(rem(bb)))        
            k=p*pp
            n=n+((-1)**(j))*int(k)
    return n

print "value is ",det(b)