【问题标题】:Why isn't my Python code for finding determinants working?为什么我的用于查找行列式的 Python 代码不起作用?
【发布时间】:2013-11-25 05:44:28
【问题描述】:

Python 3 中的以下代码旨在返回任何阶矩阵的行列式。它接受一个文本文件,格式为:

3 6 8 9
9 -7 5 -7
2 0 8 0
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))  

【问题讨论】:

  • 你可以使用 numpy 来完成这个任务,除非这是一个家庭作业:stackoverflow.com/questions/462500/…
  • 手工计算,看看你的函数的计算从哪里开始不同。
  • 这是一个家庭作业,但我们被允许编写我们想要的代码。不过我不能用那个。

标签: python matrix determinants


【解决方案1】:

一些事情

您的打开文件命令没有正确处理数据前后的空格

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 模块的示例http://docs.python.org/2/library/csv.html

return det

似乎提前退出函数,因为 det 在循环范围内

最后,还有一种更简单的方法来解决和编码行列式

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

http://en.wikipedia.org/wiki/Determinant

【讨论】:

    【解决方案2】:
    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)
    

    【讨论】:

    • 你应该提供一些关于这段代码是什么的信息,以及它如何帮助回答这个问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2015-01-03
    • 2017-09-19
    • 2014-01-23
    • 2013-08-06
    相关资源
    最近更新 更多